KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* EjbContainerChangeHandler.java
25  * $Id: EjbContainerChangeHandler.java,v 1.3 2005/12/25 03:43:39 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:43:39 $
28  * Indentation Information:
29  * 0. Please (try to) preserve these settings.
30  * 1. Tabs are preferred over spaces.
31  * 2. In vi/vim -
32  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
33  * 3. In S1 Studio -
34  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
35  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = False.
36  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
37  */

38
39 package com.sun.enterprise.admin.monitor.registry.spi.reconfig;
40
41 import java.util.Iterator JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
45 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
46 import com.sun.enterprise.admin.monitor.registry.StatsHolder;
47 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener;
48
49 import com.sun.enterprise.admin.monitor.registry.spi.ValueListMap;
50 import com.sun.enterprise.admin.monitor.registry.spi.MonitoringRegistrationHelper;
51 import com.sun.enterprise.admin.common.constant.AdminConstants;
52
53 /**
54  * Provides for dynamic reconfiguration of ejb container and related components like
55  * cache, pool, methods. This class decides the actions to take when there are
56  * changes to the monitoring level through administrative interfaces.
57  * @author <a HREF="mailto:Kedar.Mhaswade@sun.com">Kedar Mhaswade</a>
58  * @since S1AS8.0
59  * @version $Revision: 1.3 $
60  */

61 class EjbContainerChangeHandler implements ChangeHandler {
62     
63     private final ChangeHandler successor;
64     private final ValueListMap listeners;
65     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
66
67     EjbContainerChangeHandler(ChangeHandler successor, ValueListMap listeners) {
68         this.successor = successor;
69         this.listeners = listeners;
70     }
71     
72     public void handleChange(MonitoredObjectType t, MonitoringLevel from, MonitoringLevel to) {
73         if (isEjbContainerType(t)) {
74             handleChange(from, to, t);
75         }
76         else {
77             successor.handleChange(t, from, to);
78         }
79     }
80     
81     private boolean isEjbContainerType(MonitoredObjectType t) {
82         return (t == MonitoredObjectType.EJB ||
83                 t == MonitoredObjectType.BEAN_CACHE ||
84                 t == MonitoredObjectType.BEAN_METHOD ||
85                 t == MonitoredObjectType.BEAN_POOL ||
86                 t == MonitoredObjectType.ENTITY_BEAN ||
87                 t == MonitoredObjectType.STATEFUL_BEAN ||
88                 t == MonitoredObjectType.STATELESS_BEAN ||
89                 t == MonitoredObjectType.MESSAGE_DRIVEN_BEAN );
90     }
91     
92     private void handleChange(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType t) {
93         if (off2Low(from, to) || off2High(from, to)) {
94             boolean includeMethods = false;
95             notifyListeners(from, to, t);
96             // register the ejb method mbeans only if the "to" level is HIGH
97
if(to == MonitoringLevel.HIGH)
98                 includeMethods = true;
99             registerMBeans(includeMethods);
100         }
101         if (low2Off(from, to) || high2Off(from, to)) {
102             boolean includeMethods = false;
103             // need to include the ejb method mbeans, if the "from" level is HIGH
104
if(from == MonitoringLevel.HIGH)
105                 includeMethods = true;
106             unregisterMBeans(includeMethods);
107             notifyListeners(from, to, t);
108         }
109         if (low2High(from, to)) {
110             // register the ejb method mbeans
111
notifyListeners(from, to, t);
112             registerMethodMBeans();
113         }
114         if (high2Low(from, to)) {
115             // unregister the ejb method mbeans
116
unregisterMethodMBeans();
117             notifyListeners(from, to, t);
118         }
119         
120     }
121     
122     private void notifyListeners(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType t) {
123         logger.finer("DynamicReconfigurator: Now notifying the listeners for ejb stats --- from = " + from.toString() + " to = " + to.toString());
124         final Map JavaDoc l = (Map JavaDoc)listeners.get(t); // map of listeners;
125
if (l == null)
126             return; //do nothing
127
final Iterator JavaDoc it = l.keySet().iterator();
128         while (it.hasNext()) {
129             final MonitoringLevelListener ml = (MonitoringLevelListener)it.next();
130             ml.changeLevel(from, to, t);
131         }
132     }
133     
134     
135     private void registerMBeans(boolean includeMethods) {
136         final MonitoringRegistrationHelper registryImpl =
137             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
138         //note that the above refers to the actual implementation rather than interface.
139

140         //registers MBeans pertaining to ejbs, pools, caches, methods
141
final Iterator JavaDoc iter = registryImpl.getEjbContainerNodes(includeMethods).iterator();
142         while (iter.hasNext()) {
143             final StatsHolder c = (StatsHolder) iter.next();
144             c.registerMBean();
145             logger.finer("DynamicReconfigurator: Now Registering MBean for --- " + c.getName());
146         }
147     }
148     
149     private void unregisterMBeans(boolean includeMethods) {
150         final MonitoringRegistrationHelper registryImpl =
151             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
152         //note that the above refers to the actual implementation rather than interface.
153

154         final Iterator JavaDoc iter = registryImpl.getEjbContainerNodes(includeMethods).iterator();
155         while (iter.hasNext()) {
156             final StatsHolder c = (StatsHolder) iter.next();
157             c.unregisterMBean();
158             logger.finer("DynamicReconfigurator: Now UnRegistering MBean for --- " + c.getName());
159         }
160     }
161     
162     
163     private void registerMethodMBeans() {
164         final MonitoringRegistrationHelper registryImpl =
165             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
166
167         final Iterator JavaDoc iter = registryImpl.getEjbMethodNodes().iterator();
168         while (iter.hasNext()) {
169             final StatsHolder c = (StatsHolder) iter.next();
170             c.registerMBean();
171             logger.finer("DynamicReconfigurator: Now Registering MBean for --- " + c.getName());
172         }
173     }
174     
175
176     private void unregisterMethodMBeans() {
177         final MonitoringRegistrationHelper registryImpl =
178             (MonitoringRegistrationHelper) MonitoringRegistrationHelper.getInstance();
179
180         final Iterator JavaDoc iter = registryImpl.getEjbMethodNodes().iterator();
181         while (iter.hasNext()) {
182             final StatsHolder c = (StatsHolder) iter.next();
183             c.unregisterMBean();
184             logger.finer("DynamicReconfigurator: Now UnRegistering MBean for --- " + c.getName());
185         }
186     }
187     
188     private boolean off2Low(MonitoringLevel from, MonitoringLevel to) {
189         return ( from == MonitoringLevel.OFF && to == MonitoringLevel.LOW );
190     }
191     private boolean off2High(MonitoringLevel from, MonitoringLevel to) {
192         return ( from == MonitoringLevel.OFF && to == MonitoringLevel.HIGH );
193     }
194     private boolean low2Off(MonitoringLevel from, MonitoringLevel to) {
195         return ( from == MonitoringLevel.LOW && to == MonitoringLevel.OFF);
196     }
197     private boolean high2Off(MonitoringLevel from, MonitoringLevel to) {
198         return ( from == MonitoringLevel.HIGH && to == MonitoringLevel.OFF );
199     }
200     private boolean low2High(MonitoringLevel from, MonitoringLevel to) {
201         return ( from == MonitoringLevel.LOW && to == MonitoringLevel.HIGH);
202     }
203     private boolean high2Low(MonitoringLevel from, MonitoringLevel to) {
204         return ( from == MonitoringLevel.HIGH && to == MonitoringLevel.LOW );
205     }
206 }
207
Popular Tags