KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > registry > spi > S1ASJVMStatsImplMock


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /*
30  * S1ASJVMStatsImpl.java
31  *
32  * Created on August 1, 2003, 10:44 AM
33  */

34
35 package com.sun.enterprise.admin.monitor.registry.spi;
36 import com.sun.enterprise.admin.monitor.registry.*;
37 import javax.management.j2ee.statistics.*;
38 import java.lang.reflect.*;
39 import java.util.logging.*;
40 /**
41  * Mock class for using JVMStats as a component that will register and
42  * unregister with a MonitoringRegistration object which in turn created a
43  * dynamic MBean that will be registered with the MBeanServer using an ObjectName
44  * that has a unique identifier.
45  * @author sg112326
46  */

47 public class S1ASJVMStatsImplMock implements S1ASJVMStatsMock {
48     private long initTime;
49     public static final String JavaDoc LOGGER_NAME="this.is.console";
50     final Logger logger;
51
52     /** Creates a new instance of JVMStatsImpl */
53     public S1ASJVMStatsImplMock() {
54         initTime = System.currentTimeMillis();
55         logger = Logger.getLogger(LOGGER_NAME);
56     }
57     
58     public javax.management.j2ee.statistics.Statistic JavaDoc getStatistic(String JavaDoc statisticName) {
59         if(statisticName != null){
60             try{
61                 return (Statistic)(this.getClass().getMethod("get"+statisticName, null)).invoke(this,null);
62             }
63             catch(Exception JavaDoc e){
64                e.printStackTrace();
65             }
66         }
67         throw new NullPointerException JavaDoc("Cannot find Statistic as Statistic Name is not provided");
68     }
69     
70     
71     public String JavaDoc[] getStatisticNames() {
72         return new String JavaDoc[]{"HeapSize","MaxMemory","UpTime", "AvailableProcessors"};
73     }
74     
75     public javax.management.j2ee.statistics.Statistic JavaDoc[] getStatistics() {
76         Statistic[] stats = new Statistic[]{};
77         for(int i=0;i<getStatisticNames().length;i++){
78             stats[i]=getStatistic(getStatisticNames()[i]);
79         }
80         return stats;
81     }
82     
83     public CountStatistic getAvailableProcessors(){
84         int availProcs = Runtime.getRuntime().availableProcessors();
85         return StatisticFactory.getCountStatistic(availProcs, "AvailableProcessors",
86                                                    "Numbers", "Processors Available to the JVM",
87                                                    System.currentTimeMillis(), initTime);
88     }
89     
90     public javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc getHeapSize() {
91         long heapSize = Runtime.getRuntime().totalMemory();
92         long upper = Runtime.getRuntime().maxMemory();
93         return StatisticFactory.getBoundedRangeStatistic(heapSize, heapSize, heapSize,
94                                                          upper, 0, "HeapSize", "bytes",
95                                                          "JVM Heap Size", initTime,
96                                                          System.currentTimeMillis());
97     }
98     
99     public CountStatistic getMaxMemory() {
100         long maxMem = Runtime.getRuntime().maxMemory();
101         return StatisticFactory.getCountStatistic(maxMem, "MaxMemory",
102                                                    "bytes", "Memory Available to the JVM",
103                                                    System.currentTimeMillis(), initTime);
104     }
105     
106     public CountStatistic getUpTime() {
107         long curTime = System.currentTimeMillis();
108         long upTime = curTime - initTime;
109         return StatisticFactory.getCountStatistic(upTime, "UpTime", "milliseconds",
110                                                   "Amount of time the JVM has been running",
111                                                    curTime, initTime);
112     }
113 }
114
Popular Tags