KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.admin.monitor.registry.spi.reconfig;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
30 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
31 import com.sun.enterprise.admin.monitor.registry.StatsHolder;
32 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener;
33
34 import com.sun.enterprise.admin.monitor.registry.spi.ValueListMap;
35 import com.sun.enterprise.admin.monitor.registry.spi.MonitoringRegistrationHelper;
36 import com.sun.enterprise.admin.common.constant.AdminConstants;
37
38 /**
39  * Provides for dynamic reconfiguration of jvm. This class decides the
40  * actions to take when there are changes to the monitoring level through
41  * administrative interfaces.
42  * @since S1AS8.1
43  */

44 class JVMChangeHandler implements ChangeHandler {
45     
46     private final ChangeHandler successor;
47     private final ValueListMap listeners;
48     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
49
50     JVMChangeHandler(ChangeHandler successor, ValueListMap listeners) {
51         this.successor = successor;
52         this.listeners = listeners;
53     }
54     
55     public void handleChange(MonitoredObjectType t, MonitoringLevel from, MonitoringLevel to) {
56         if (isJvmType(t)) {
57             handleChange(from, to, t);
58         }
59         else {
60             successor.handleChange(t, from, to);
61         }
62     }
63     
64     private boolean isJvmType(MonitoredObjectType t) {
65         return (t == MonitoredObjectType.JVM);
66     }
67     
68     private void handleChange(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType t) {
69         if (off2Low(from, to) || off2High(from, to)) {
70             boolean threadInfo = false;
71             notifyListeners(from, to, t);
72             // register the threadinfo mbeans only if the "to" level is HIGH
73
if(to == MonitoringLevel.HIGH)
74                 threadInfo = true;
75             registerMBeans(threadInfo);
76         }
77         if (low2Off(from, to) || high2Off(from, to)) {
78             boolean threadInfo = false;
79             // need to include the threadinfo mbeans, if the "from" level is HIGH
80
if(from == MonitoringLevel.HIGH)
81                 threadInfo = true;
82             unregisterMBeans(threadInfo);
83             notifyListeners(from, to, t);
84         }
85         if (low2High(from, to)) {
86             // register the threadinfo mbeans
87
notifyListeners(from, to, t);
88             registerThreadInfoMBeans();
89         }
90         if (high2Low(from, to)) {
91             // unregister the threadinfo mbeans
92
unregisterThreadInfoMBeans();
93             notifyListeners(from, to, t);
94         }
95         
96     }
97     
98     private void notifyListeners(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType t) {
99         logger.finer("DynamicReconfigurator: Now notifying the listeners for jvm stats --- from = " + from.toString() + " to = " + to.toString());
100         final Map JavaDoc l = (Map JavaDoc)listeners.get(t); // map of listeners;
101
if (l == null)
102             return; //do nothing
103
final Iterator JavaDoc it = l.keySet().iterator();
104         while (it.hasNext()) {
105             final MonitoringLevelListener ml = (MonitoringLevelListener)it.next();
106             ml.changeLevel(from, to, t);
107         }
108     }
109     
110     
111     private void registerMBeans(boolean threadInfo) {
112         final MonitoringRegistrationHelper registryImpl =
113             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
114         
115         final Iterator JavaDoc iter = registryImpl.getJvmNodes(threadInfo).iterator();
116         while (iter.hasNext()) {
117             final StatsHolder c = (StatsHolder) iter.next();
118             c.registerMBean();
119             logger.finer("DynamicReconfigurator: Now Registering MBean for --- " + c.getName());
120         }
121     }
122     
123     private void unregisterMBeans(boolean threadInfo) {
124         final MonitoringRegistrationHelper registryImpl =
125             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
126
127         final Iterator JavaDoc iter = registryImpl.getJvmNodes(threadInfo).iterator();
128         while (iter.hasNext()) {
129             final StatsHolder c = (StatsHolder) iter.next();
130             c.unregisterMBean();
131             logger.finer("DynamicReconfigurator: Now UnRegistering MBean for --- " + c.getName());
132         }
133     }
134     
135     
136     private void registerThreadInfoMBeans() {
137         final MonitoringRegistrationHelper registryImpl =
138             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
139
140         final Iterator JavaDoc iter = registryImpl.getJvmThreadInfoNodes().iterator();
141         while (iter.hasNext()) {
142             final StatsHolder c = (StatsHolder) iter.next();
143             c.registerMBean();
144             logger.finer("DynamicReconfigurator: Now Registering MBean for --- " + c.getName());
145         }
146     }
147     
148
149     private void unregisterThreadInfoMBeans() {
150         final MonitoringRegistrationHelper registryImpl =
151             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
152
153         final Iterator JavaDoc iter = registryImpl.getJvmThreadInfoNodes().iterator();
154         while (iter.hasNext()) {
155             final StatsHolder c = (StatsHolder) iter.next();
156             c.unregisterMBean();
157             logger.finer("DynamicReconfigurator: Now UnRegistering MBean for --- " + c.getName());
158         }
159     }
160     
161     private boolean off2Low(MonitoringLevel from, MonitoringLevel to) {
162         return ( from == MonitoringLevel.OFF && to == MonitoringLevel.LOW );
163     }
164     private boolean off2High(MonitoringLevel from, MonitoringLevel to) {
165         return ( from == MonitoringLevel.OFF && to == MonitoringLevel.HIGH );
166     }
167     private boolean low2Off(MonitoringLevel from, MonitoringLevel to) {
168         return ( from == MonitoringLevel.LOW && to == MonitoringLevel.OFF);
169     }
170     private boolean high2Off(MonitoringLevel from, MonitoringLevel to) {
171         return ( from == MonitoringLevel.HIGH && to == MonitoringLevel.OFF );
172     }
173     private boolean low2High(MonitoringLevel from, MonitoringLevel to) {
174         return ( from == MonitoringLevel.LOW && to == MonitoringLevel.HIGH);
175     }
176     private boolean high2Low(MonitoringLevel from, MonitoringLevel to) {
177         return ( from == MonitoringLevel.HIGH && to == MonitoringLevel.LOW );
178     }
179 }
180
Popular Tags