View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18
    19  package org.codehaus.activemq.capacity;
    20  import java.util.Iterator;
    21  import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
    22
    23  /***
    24   * BasicCapacityMonitor implementation
    25   * @version $Revision: 1.1 $
    26   */
    27  public class DelegateCapacityMonitor implements CapacityMonitor {
    28      String name;
    29      CapacityMonitor monitor;
    30      CopyOnWriteArrayList listeners = new CopyOnWriteArrayList();
    31
    32      /***
    33       * Construct a DelegateCapacityMonitor
    34       *
    35       */
    36      public DelegateCapacityMonitor() {
    37      }
    38
    39      /***
    40       * Construct a DelegateCapacityMonitor
    41       * @param name
    42       * @param cm
    43       */
    44      public DelegateCapacityMonitor(String name,CapacityMonitor cm){
    45          this.name = name;
    46          this.monitor = cm;
    47      }
    48
    49      /***
    50       * Set the delegated one
    51       * @param cm
    52       */
    53      public void setDelegate(CapacityMonitor cm){
    54          this.monitor = cm;
    55          if (cm != null){
    56              for (Iterator i = listeners.iterator(); i.hasNext();){
    57                  CapacityMonitorEventListener listener = (CapacityMonitorEventListener)i.next();
    58                  cm.addCapacityEventListener(listener);
    59              }
    60          }
    61      }
    62
    63      /***
    64       * Get the name of the CapacityMonitor
    65       *
    66       * @return
    67       */
    68      public String getName() {
    69          return name;
    70      }
    71
    72      /***
    73       * @param newName
    74       */
    75      public void setName(String newName){
    76          name = newName;
    77      }
    78
    79      /***
    80       * Get the rounding factor - default is 10
    81       * @return the rounding factor
    82       */
    83      public int getRoundingFactor(){
    84          return monitor == null ? 0 : monitor.getRoundingFactor();
    85      }
    86
    87      /***
    88       * Set the rounding factor (between 1-100)
    89       * @param newRoundingFactor
    90       */
    91      public void setRoundingFactor(int newRoundingFactor){
    92          if(monitor != null){
    93              monitor.setRoundingFactor(newRoundingFactor);
    94          }
    95      }
    96
    97      /***
    98       * Add a CapacityEventListener
    99       *
    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 }