KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > util > 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.util;
36 import javax.management.j2ee.statistics.*;
37 import java.lang.reflect.*;
38 import java.util.logging.*;
39 /**
40  * Mock class for using JVMStats as a component that will register and
41  * unregister with a MonitoringRegistration object which in turn created a
42  * dynamic MBean that will be registered with the MBeanServer using an ObjectName
43  * that has a unique identifier.
44  * @author sg112326
45  */

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