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  * @author Hemanth Puttaswamy
  31  *
  32  * StatisticsMonitoredAttribute is provided as a convenience to collect the
  33  * Statistics of any entity. The getValue() call will be delegated to the
  34  * StatisticsAccumulator set by the user.
  35  */
  36 public class StatisticMonitoredAttribute extends MonitoredAttributeBase {
  37 
  38 
  39     // Every StatisticMonitoredAttribute will have a StatisticAccumulator. User
  40     // will use Statisticsaccumulator to accumulate the samples associated with
  41     // this Monitored Attribute
  42     private StatisticsAccumulator statisticsAccumulator;
  43 
  44     // Mutex is passed from the user class which is providing the sample values.
  45     // getValue() and clearState() is synchronized on this user provided mutex
  46     private Object  mutex;
  47 
  48 
  49   ///////////////////////////////////////
  50   // operations
  51 
  52 
  53 /**
  54  * Constructs the StaisticMonitoredAttribute, builds the required
  55  * MonitoredAttributeInfo with Long as the class type and is always
  56  * readonly attribute.
  57  *
  58  * @param name Of this attribute
  59  * @param desc should provide a good description on the kind of statistics
  60  * collected, a good example is "Connection Response Time Stats will Provide the
  61  * detailed stats based on the samples provided from every request completion
  62  * time"
  63  * @param s is the StatisticsAcumulator that user will use to accumulate the
  64  * samples and this Attribute Object will get the computed statistics values
  65  * from.
  66  * @param mutex using which clearState() and getValue() calls need to be locked.
  67  */
  68     public  StatisticMonitoredAttribute(String name, String desc,
  69         StatisticsAccumulator s, Object mutex)
  70     {
  71         super( name );
  72         MonitoredAttributeInfoFactory f =
  73             MonitoringFactories.getMonitoredAttributeInfoFactory();
  74         MonitoredAttributeInfo maInfo = f.createMonitoredAttributeInfo(
  75                 desc, String.class, false, true );
  76 
  77         this.setMonitoredAttributeInfo( maInfo );
  78         this.statisticsAccumulator = s;
  79         this.mutex = mutex;
  80     } // end StatisticMonitoredAttribute
  81 
  82 
  83 
  84 /**
  85  *  Gets the value from the StatisticsAccumulator, the value will be a formatted
  86  *  String with the computed statistics based on the samples accumulated in the
  87  *  Statistics Accumulator.
  88  */
  89     public Object getValue( ) {
  90         synchronized( mutex ) {
  91             return statisticsAccumulator.getValue( );
  92         }
  93     }
  94 
  95 /**
  96  *  Clears the state on Statistics Accumulator, After this call all samples are
  97  *  treated fresh and the old sample computations are disregarded.
  98  */
  99     public void clearState( ) {
 100         synchronized( mutex ) {
 101             statisticsAccumulator.clearState( );
 102         }
 103     }
 104 
 105 /**
 106  *  Gets the statistics accumulator associated with StatisticMonitoredAttribute.
 107  *  Usually, the user don't need to use this method as they can keep the handle
 108  *  to Accumulator to collect the samples.
 109  */
 110     public StatisticsAccumulator getStatisticsAccumulator( ) {
 111         return statisticsAccumulator;
 112     }
 113 } // end StatisticMonitoredAttribute