001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 019 package org.activemq.capacity; 020 import java.util.Iterator; 021 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList; 022 023 /** 024 * BasicCapacityMonitor implementation 025 * @version $Revision: 1.1.1.1 $ 026 */ 027 public class DelegateCapacityMonitor implements CapacityMonitor { 028 String name; 029 CapacityMonitor monitor; 030 CopyOnWriteArrayList listeners = new CopyOnWriteArrayList(); 031 032 /** 033 * Construct a DelegateCapacityMonitor 034 * 035 */ 036 public DelegateCapacityMonitor() { 037 } 038 039 /** 040 * Construct a DelegateCapacityMonitor 041 * @param name 042 * @param cm 043 */ 044 public DelegateCapacityMonitor(String name,CapacityMonitor cm){ 045 this.name = name; 046 this.monitor = cm; 047 } 048 049 /** 050 * Set the delegated one 051 * @param cm 052 */ 053 public void setDelegate(CapacityMonitor cm){ 054 this.monitor = cm; 055 if (cm != null){ 056 for (Iterator i = listeners.iterator(); i.hasNext();){ 057 CapacityMonitorEventListener listener = (CapacityMonitorEventListener)i.next(); 058 cm.addCapacityEventListener(listener); 059 } 060 } 061 } 062 063 /** 064 * Get the name of the CapacityMonitor 065 * 066 * @return 067 */ 068 public String getName() { 069 return name; 070 } 071 072 /** 073 * @param newName 074 */ 075 public void setName(String newName){ 076 name = newName; 077 } 078 079 /** 080 * Get the rounding factor - default is 10 081 * @return the rounding factor 082 */ 083 public int getRoundingFactor(){ 084 return monitor == null ? 0 : monitor.getRoundingFactor(); 085 } 086 087 /** 088 * Set the rounding factor (between 1-100) 089 * @param newRoundingFactor 090 */ 091 public void setRoundingFactor(int newRoundingFactor){ 092 if(monitor != null){ 093 monitor.setRoundingFactor(newRoundingFactor); 094 } 095 } 096 097 /** 098 * Add a CapacityEventListener 099 * 100 * @param l 101 */ 102 public void addCapacityEventListener(CapacityMonitorEventListener l) { 103 listeners.add(l); 104 if (monitor != null){ 105 monitor.addCapacityEventListener(l); 106 } 107 } 108 109 /** 110 * Remove a CapacityEventListener 111 * 112 * @param l 113 */ 114 public void removeCapacityEventListener(CapacityMonitorEventListener l) { 115 listeners.remove(l); 116 if (monitor != null){ 117 monitor.removeCapacityEventListener(l); 118 } 119 } 120 121 /** 122 * Get the current capscity of the service as a percentage 123 * 124 * @return 125 */ 126 public int getCurrentCapacity() { 127 return monitor == null ? 100 : monitor.getCurrentCapacity(); 128 } 129 130 /** 131 * Calculates the capacity rounded down to the rounding factor 132 * @return 133 */ 134 public int getRoundedCapacity(){ 135 return monitor == null ? 100 : monitor.getRoundedCapacity(); 136 } 137 138 /** 139 * Get the current value of the CapacityMonitor 140 * 141 * @return 142 */ 143 public long getCurrentValue() { 144 return monitor == null ? 100 : monitor.getCurrentValue(); 145 } 146 147 /** 148 * set the current value of the capacity 149 * 150 * @param newCurrentValue 151 */ 152 public void setCurrentValue(long newCurrentValue) { 153 if (monitor != null){ 154 monitor.setCurrentValue(newCurrentValue); 155 } 156 } 157 158 /** 159 * @return The upper limit of the value of the CapacityMonitor 160 */ 161 public long getValueLimit() { 162 return monitor == null ? 100 : monitor.getValueLimit(); 163 } 164 165 /** 166 * set a new value limit for the CapacityMonitor 167 * 168 * @param newValueLimit 169 */ 170 public void setValueLimit(long newValueLimit) { 171 if(monitor != null){ 172 monitor.setValueLimit(newValueLimit); 173 } 174 175 } 176 177 /** 178 * @return a CapacityMontorEvent for the currentCapacity 179 */ 180 public CapacityMonitorEvent generateCapacityMonitorEvent(){ 181 return monitor != null ? monitor.generateCapacityMonitorEvent() : null; 182 } 183 }