KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > extension > GrizzlyConfig


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.web.connector.extension;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.management.Attribute JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.j2ee.statistics.Stats JavaDoc;
31
32 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistry;
33 import com.sun.enterprise.admin.monitor.registry.MonitoringRegistrationException;
34 import com.sun.enterprise.admin.monitor.registry.MonitoringLevel;
35 import com.sun.enterprise.admin.monitor.registry.MonitoringLevelListener;
36 import com.sun.enterprise.admin.monitor.registry.MonitoredObjectType;
37 import com.sun.enterprise.config.serverbeans.ModuleMonitoringLevels;
38
39 import com.sun.enterprise.config.ConfigContext;
40 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
41 import com.sun.enterprise.config.serverbeans.Config;
42 import com.sun.enterprise.web.WebContainer;
43 import com.sun.enterprise.config.serverbeans.HttpService;
44 import com.sun.enterprise.config.serverbeans.HttpFileCache;
45 import com.sun.logging.LogDomains;
46 import com.sun.enterprise.config.ConfigException;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import java.text.MessageFormat JavaDoc;
52 import javax.management.ObjectName JavaDoc;
53 import javax.management.MBeanServerFactory JavaDoc;
54 import javax.management.MBeanServer JavaDoc;
55
56 /**
57  * This class track monitoring or Grizzly, using JMX to invoke Grizzly main
58  * classes.
59  *
60  * @author Jeanfrancois Arcand
61  */

62 public class GrizzlyConfig implements MonitoringLevelListener{
63     private final static Logger JavaDoc logger
64         = LogDomains.getLogger(LogDomains.WEB_LOGGER);
65     
66     /**
67      * The mbean server used to lookup Grizzly.
68      */

69     private MBeanServer JavaDoc mBeanServer;
70
71
72     /**
73      * Is monitoring already started.
74      */

75     private boolean isMonitoringEnabled = false;
76     
77     /**
78      * The JMX domain
79      */

80     private String JavaDoc domain;
81     
82
83     /**
84      * The port used to lookup Grizzly's Selector
85      */

86     private int port;
87
88
89     /**
90      * The list of instance created. This list is not thread-safe.
91      */

92     private static ArrayList JavaDoc grizzlyConfigList = new ArrayList JavaDoc();
93     
94
95     // --------------------------------------------------------------- //
96

97     
98     /**
99      * Creates the monitoring helper.
100      */

101     public GrizzlyConfig(String JavaDoc domain, int port){
102         this.domain = domain;
103         this.port = port;
104         
105         // get an instance of the MBeanServer
106
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
107         if(!servers.isEmpty())
108             mBeanServer = (MBeanServer JavaDoc)servers.get(0);
109         else
110             mBeanServer = MBeanServerFactory.createMBeanServer();
111         
112         grizzlyConfigList.add(this);
113     }
114     
115     
116     public void initConfig(){
117         initMonitoringLevel();
118     }
119     
120     
121     private void initMonitoringLevel() {
122         try{
123             Config cfg = ServerBeansFactory
124                 .getConfigBean(
125                     WebContainer.getInstance().getServerContext().getConfigContext());
126
127             MonitoringLevel monitoringLevel = MonitoringLevel.OFF; // default per DTD
128

129             if (cfg.getMonitoringService() != null) {
130                 ModuleMonitoringLevels levels =
131                     cfg.getMonitoringService().getModuleMonitoringLevels();
132                 if (levels != null) {
133                     monitoringLevel = MonitoringLevel.instance(
134                                                     levels.getHttpService());
135                 }
136             }
137         
138             if(MonitoringLevel.OFF.equals(monitoringLevel)) {
139                 isMonitoringEnabled = false;
140             } else {
141                 isMonitoringEnabled = true;
142             }
143             
144             String JavaDoc methodToInvoke = isMonitoringEnabled ? "enableMonitoring" :
145                 "disableMonitoring";
146             invokeGrizzly(methodToInvoke);
147         } catch (Exception JavaDoc ex) {
148             logger.log(Level.WARNING,
149                 "selectorThread.initMonitoringException",
150                  new Object JavaDoc[]{new Integer JavaDoc(port),ex});
151         }
152     }
153     
154     
155     public void registerMonitoringLevelEvents() {
156         MonitoringRegistry monitoringRegistry =
157             WebContainer.getInstance().getServerContext().getMonitoringRegistry();
158         monitoringRegistry.registerMonitoringLevelListener(
159             this, MonitoredObjectType.HTTP_LISTENER);
160     }
161
162     
163     public void unregisterMonitoringLevelEvents() {
164         MonitoringRegistry monitoringRegistry =
165             WebContainer.getInstance().getServerContext().getMonitoringRegistry();
166         monitoringRegistry.unregisterMonitoringLevelListener(this);
167     }
168
169     
170     public void setLevel(MonitoringLevel level) {
171         // deprecated, ignore
172
}
173     
174     
175     public void changeLevel(MonitoringLevel from, MonitoringLevel to,
176                             MonitoredObjectType type) {
177         if (MonitoredObjectType.HTTP_LISTENER.equals(type)) {
178             if(MonitoringLevel.OFF.equals(to)) {
179                 isMonitoringEnabled = false;
180             } else {
181                 isMonitoringEnabled = true;
182             }
183         }
184         String JavaDoc methodToInvoke = isMonitoringEnabled ? "enableMonitoring" :
185             "disabledMonitoring";
186         invokeGrizzly(methodToInvoke);
187     }
188     
189     
190     public void changeLevel(MonitoringLevel from, MonitoringLevel to,
191                 Stats JavaDoc handback) {
192         // deprecated, ignore
193
}
194     
195      
196     private final void invokeGrizzly(String JavaDoc methodToInvoke) {
197         try{
198             String JavaDoc onStr = domain + ":type=Selector,name=http" + port;
199             ObjectName JavaDoc objectName = new ObjectName JavaDoc(onStr);
200             mBeanServer.invoke(objectName,methodToInvoke,null,null);
201         } catch ( Exception JavaDoc ex ){
202             throw new RuntimeException JavaDoc(ex);
203         }
204     }
205
206     
207     /**
208      * Enable CallFlow gathering mechanism.
209      */

210     public final void setEnableCallFlow(boolean enableCallFlow){
211         String JavaDoc methodToInvoke = enableCallFlow ? "enableMonitoring" :
212             "disabledMonitoring";
213         invokeGrizzly(methodToInvoke);
214     }
215
216     
217     /**
218      * Return the list of all instance of this class.
219      */

220     public static ArrayList JavaDoc getGrizzlyConfigInstances(){
221         return grizzlyConfigList;
222     }
223     
224
225 }
226
Popular Tags