KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > monitor > JDBCPoolMonitoringLevelListener


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 package com.sun.enterprise.resource.monitor;
24
25 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
26 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
27 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener;
28 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistry;
29 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException;
30 import com.sun.enterprise.admin.monitor.stats.JDBCConnectionPoolStats;
31 import com.sun.enterprise.config.serverbeans.JdbcConnectionPool;
32 import com.sun.enterprise.PoolManager;
33 import com.sun.enterprise.resource.MonitorableResourcePool;
34 import com.sun.enterprise.server.ApplicationServer;
35 import com.sun.enterprise.server.ResourcesUtil;
36 import com.sun.enterprise.server.ServerContext;
37 import com.sun.enterprise.Switch;
38 import java.util.HashMap JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.concurrent.ConcurrentHashMap JavaDoc;
41 import javax.management.j2ee.statistics.Stats JavaDoc;
42 import com.sun.logging.LogDomains;
43 import java.util.logging.Logger JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.security.AccessController JavaDoc;
46 import java.security.PrivilegedAction JavaDoc;
47
48 import com.sun.enterprise.resource.MonitorableResourcePool;
49 import com.sun.enterprise.resource.ResourcePool;
50
51 /**
52  * Provides an implementation of the MonitoringLevelListener interface to
53  * receive callbacks from admin regarding change in the monitoring level.
54  * Though there are 3 monitoring levels defined by JSR77, we support
55  * only 2 levels - OFF and ON (HIGH/LOW). So essentially, HIGH and LOW
56  * for us is only ON
57  *
58  * @author Aditya Gore
59  * @since s1aspe 8.0
60  */

61 public class JDBCPoolMonitoringLevelListener implements MonitoringLevelListener {
62     private PoolManager poolManager_;
63     private MonitoringRegistry registry_;
64
65     private static final MonitoringLevel OFF = MonitoringLevel.OFF;
66     private static final MonitoringLevel HIGH = MonitoringLevel.HIGH;
67     private static final MonitoringLevel LOW = MonitoringLevel.LOW;
68     
69     private static Logger JavaDoc _logger = LogDomains.getLogger(
70         LogDomains.RSR_LOGGER );
71
72     public JDBCPoolMonitoringLevelListener() {
73
74     }
75     /**
76      * This is the callback for change in monitoring levels.
77      * This shall go off once the admin team removes this method
78      */

79     public void setLevel( MonitoringLevel level ) {}
80     
81     /**
82      * This is the callback method called when the monitoring level
83      * changes from HIGH/LOW <-> OFF
84      * <p>Here since the MonitoringLevel is a "type-safe enum", we
85      * can directly check equality using ==
86      * @param from - the old level
87      * @param to - the new level
88      * @param handback - the stats implementation object which was
89      * registered with the MonitoringRegistry
90      */

91     public void changeLevel( MonitoringLevel from, MonitoringLevel to,
92         Stats handback ) {
93     }
94
95     public void changeLevel(MonitoringLevel from, MonitoringLevel to,
96         MonitoredObjectType type)
97     {
98         //If we were called, the type has to be JDBC_CONN_POOL so
99
//we can safely ignore the type
100

101     if ( from == to ) {
102         //Its a no-op, so return
103
return;
104     }
105         AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
106             public Object JavaDoc run() {
107             ServerContext ctxt = ApplicationServer.getServerContext();
108         if (ctxt != null) {
109                     registry_ = ctxt.getMonitoringRegistry();
110         }
111                 return null;
112             }
113         });
114
115
116     if (from == OFF || from == LOW) {
117         if (to == HIGH) {
118         logFine("Changing level from " + from +" to HIGH");
119                 transitionToHigh();
120         }
121     }
122
123     if (from == HIGH || from == LOW ) {
124         if ( to == OFF ) {
125                 logFine("Switching level from " + from + " to OFF");
126                 switchOffMonitoring();
127             }
128     }
129
130         if (from == OFF || from == HIGH ) {
131         if ( to == LOW ) {
132                 logFine("Changing level from " + from + " to LOW");
133                 transitionToLow();
134         }
135     }
136     }
137
138     /*
139      * Query the resources util and get a list of jdbc pools
140      */

141     private MonitorableResourcePool[] getPoolList() {
142         ResourcesUtil resUtil = ResourcesUtil.getInstance();
143     JdbcConnectionPool[] jp = resUtil.getJdbcConnectionPools();
144
145     if (jp == null) {
146         return null;
147     }
148     
149         MonitorableResourcePool[] pools = new MonitorableResourcePool[ jp.length ];
150         ConcurrentHashMap JavaDoc poolTable = getPoolManager().getPoolTable();
151         
152     for( int i = 0 ; i < jp.length; i++ ) {
153             ResourcePool p = (ResourcePool) poolTable.get( jp[i].getName() );
154             if (p != null && (p instanceof MonitorableResourcePool ) ) {
155             pools[i] = (MonitorableResourcePool)p;
156             }
157     }
158     
159     return pools;
160     }
161
162     private PoolManager getPoolManager() {
163     return Switch.getSwitch().getPoolManager();
164     }
165
166     private void transitionToHigh() {
167         AccessController.doPrivileged( new PrivilegedAction JavaDoc() {
168             public Object JavaDoc run() {
169                 //we should create a new pool object everytime since
170
//the stats have to be collected afresh
171
MonitorableResourcePool[] pools = getPoolList();
172                 if (pools == null) {
173                     return null;
174                 }
175         
176                 for (int i = 0; i < pools.length; i++ ) {
177                     if ( pools[i] != null ) {
178                         try {
179                             JDBCConnectionPoolStatsImpl stats = new
180                                 JDBCConnectionPoolStatsImpl(pools[i]);
181                             getPoolManager().setMonitoringEnabledHigh(
182                                 pools[i].getPoolName() );
183                             registry_.registerJDBCConnectionPoolStats(
184                                 stats, pools[i].getPoolName(), null);
185                         } catch(Exception JavaDoc mre) {
186                             try {
187                                 _logger.log( Level.INFO, "poolmon.cannot_reg: "
188                                     +pools[i].getPoolName(), mre.getMessage() );
189                                 getPoolManager().disableMonitoring(
190                                     pools[i].getPoolName());
191                             } catch (Exception JavaDoc ex) {
192                                 //FIXME: ignore?
193
}
194                         }
195                     }
196                 }
197                 return null;
198             }
199         });
200     }
201
202     private void switchOffMonitoring() {
203         //deregister
204
AccessController.doPrivileged( new PrivilegedAction JavaDoc() {
205             public Object JavaDoc run() {
206                 MonitorableResourcePool[] pools = getPoolList();
207                 if (pools == null) {
208                     return null;
209                 }
210                 for( int i = 0; i < pools.length; i++) {
211                     if (pools[i] != null ) {
212                         try {
213                             registry_.unregisterJDBCConnectionPoolStats(
214                                 pools[i].getPoolName() );
215                             getPoolManager().disableMonitoring(
216                                 pools[i].getPoolName());
217                         } catch( Exception JavaDoc mre ) {
218                             _logger.log( Level.WARNING, "poolmon.cannot_unreg: "
219                                 + pools[i].getPoolName(), mre.getMessage() );
220                         }
221                     }
222                 }
223                 return null;
224             }
225             
226         });
227     }
228
229     private void transitionToLow() {
230         AccessController.doPrivileged( new PrivilegedAction JavaDoc() {
231             public Object JavaDoc run() {
232                 //we should create a new pool object everytime since
233
//the stats have to be collected afresh
234
MonitorableResourcePool[] pools = getPoolList();
235                 if (pools == null) {
236                     return null;
237                 }
238         
239                 for (int i = 0; i < pools.length; i++ ) {
240                     if ( pools[i] != null ) {
241                         try {
242                             JDBCConnectionPoolStatsImpl stats = new
243                                 JDBCConnectionPoolStatsImpl(pools[i]);
244                             getPoolManager().setMonitoringEnabledLow(
245                                 pools[i].getPoolName() );
246                             registry_.registerJDBCConnectionPoolStats(
247                                 stats, pools[i].getPoolName(), null);
248                         } catch(Exception JavaDoc mre) {
249                             try {
250                                 _logger.log( Level.INFO, "poolmon.cannot_reg: "
251                                     +pools[i].getPoolName(), mre.getMessage() );
252                                 getPoolManager().disableMonitoring(
253                                     pools[i].getPoolName() );
254                             } catch (Exception JavaDoc ex) {
255                                 //FIXME: ignore?
256
}
257                         }
258                     }
259                 }
260                 return null;
261             }
262         });
263     }
264     
265     private void logFine( String JavaDoc msg ) {
266         if ( msg != null && _logger.isLoggable( Level.FINE ) ) {
267             _logger.fine( msg );
268         }
269     }
270 }
271
Popular Tags