1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.corba.se.spi.monitoring;
  26 
  27 import java.util.*;
  28 
  29 /**
  30  * <p>
  31  *
  32  * @author Hemanth Puttaswamy
  33  * </p>
  34  * <p>
  35  * StatisticsMonitoredAttribute is provided as a convenience to collect the
  36  * Statistics of any entity. The getValue() call will be delegated to the
  37  * StatisticsAccumulator set by the user.
  38  * </p>
  39  */
  40 public class StatisticMonitoredAttribute extends MonitoredAttributeBase {
  41 
  42 
  43     // Every StatisticMonitoredAttribute will have a StatisticAccumulator. User
  44     // will use Statisticsaccumulator to accumulate the samples associated with
  45     // this Monitored Attribute
  46     private StatisticsAccumulator statisticsAccumulator;
  47 
  48     // Mutex is passed from the user class which is providing the sample values.
  49     // getValue() and clearState() is synchronized on this user provided mutex
  50     private Object  mutex;
  51 
  52 
  53   ///////////////////////////////////////
  54   // operations
  55 
  56 
  57 /**
  58  * <p>
  59  * Constructs the StaisticMonitoredAttribute, builds the required
  60  * MonitoredAttributeInfo with Long as the class type and is always
  61  * readonly attribute.
  62  * </p>
  63  * <p>
  64  *
  65  * @param name Of this attribute
  66  * </p>
  67  * <p>
  68  * @return a StatisticMonitoredAttribute
  69  * </p>
  70  * <p>
  71  * @param desc should provide a good description on the kind of statistics
  72  * collected, a good example is "Connection Response Time Stats will Provide the
  73  * detailed stats based on the samples provided from every request completion
  74  * time"
  75  * </p>
  76  * <p>
  77  * @param s is the StatisticsAcumulator that user will use to accumulate the
  78  * samples and this Attribute Object will get the computed statistics values
  79  * from.
  80  * </p>
  81  * <p>
  82  * @param mutex using which clearState() and getValue() calls need to be locked.
  83  * </p>
  84  */
  85     public  StatisticMonitoredAttribute(String name, String desc,
  86         StatisticsAccumulator s, Object mutex)
  87     {
  88         super( name );
  89         MonitoredAttributeInfoFactory f =
  90             MonitoringFactories.getMonitoredAttributeInfoFactory();
  91         MonitoredAttributeInfo maInfo = f.createMonitoredAttributeInfo(
  92                 desc, String.class, false, true );
  93 
  94         this.setMonitoredAttributeInfo( maInfo );
  95         this.statisticsAccumulator = s;
  96         this.mutex = mutex;
  97     } // end StatisticMonitoredAttribute
  98 
  99 
 100 
 101 /**
 102  *  Gets the value from the StatisticsAccumulator, the value will be a formatted
 103  *  String with the computed statistics based on the samples accumulated in the
 104  *  Statistics Accumulator.
 105  */
 106     public Object getValue( ) {
 107         synchronized( mutex ) {
 108             return statisticsAccumulator.getValue( );
 109         }
 110     }
 111 
 112 /**
 113  *  Clears the state on Statistics Accumulator, After this call all samples are
 114  *  treated fresh and the old sample computations are disregarded.
 115  */
 116     public void clearState( ) {
 117         synchronized( mutex ) {
 118             statisticsAccumulator.clearState( );
 119         }
 120     }
 121 
 122 /**
 123  *  Gets the statistics accumulator associated with StatisticMonitoredAttribute.
 124  *  Usually, the user don't need to use this method as they can keep the handle
 125  *  to Accumulator to collect the samples.
 126  */
 127     public StatisticsAccumulator getStatisticsAccumulator( ) {
 128         return statisticsAccumulator;
 129     }
 130 } // end StatisticMonitoredAttribute