KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > web > jmx > ManagementContext


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.web.jmx;
18
19 import org.apache.activemq.Service;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.jms.JMSException JavaDoc;
24 import javax.management.JMException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MBeanServerFactory JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import java.util.List JavaDoc;
31
32 /**
33  * A Flow provides different dispatch policies within the NMR
34  *
35  * @version $Revision: 356583 $
36  */

37 public class ManagementContext implements Service {
38     /**
39      * Default servicemix domain
40      */

41     public static String JavaDoc DEFAULT_DOMAIN = "org.apache.activemq";
42
43     private final static Log log = LogFactory.getLog(ManagementContext.class);
44
45     private MBeanServer JavaDoc beanServer;
46     private String JavaDoc jmxDomainName = DEFAULT_DOMAIN;
47     private boolean useMBeanServer = true;
48     private boolean createMBeanServer = true;
49     private boolean locallyCreateMBeanServer = false;
50
51     public ManagementContext() {
52         this(null);
53     }
54
55     public ManagementContext(MBeanServer JavaDoc server) {
56         this.beanServer = server;
57     }
58
59     public void start() throws JMSException JavaDoc {
60         // lets force the MBeanServer to be created if needed
61
getMBeanServer();
62     }
63
64     public void stop() throws JMSException JavaDoc {
65         if (locallyCreateMBeanServer && beanServer != null) {
66             // check to see if the factory knows about this server
67
List JavaDoc list = MBeanServerFactory.findMBeanServer(null);
68             if (list != null && !list.isEmpty() && list.contains(beanServer)) {
69                 MBeanServerFactory.releaseMBeanServer(beanServer);
70             }
71         }
72     }
73
74     /**
75      * @return Returns the jmxDomainName.
76      */

77     public String JavaDoc getJmxDomainName() {
78         return jmxDomainName;
79     }
80
81     /**
82      * @param jmxDomainName
83      * The jmxDomainName to set.
84      */

85     public void setJmxDomainName(String JavaDoc jmxDomainName) {
86         this.jmxDomainName = jmxDomainName;
87     }
88
89     /**
90      * Get the MBeanServer
91      *
92      * @return the MBeanServer
93      */

94     public MBeanServer JavaDoc getMBeanServer() {
95         if (this.beanServer == null) {
96             this.beanServer = findMBeanServer();
97         }
98         return beanServer;
99     }
100
101     /**
102      * @return Returns the useMBeanServer.
103      */

104     public boolean isUseMBeanServer() {
105         return useMBeanServer;
106     }
107
108     /**
109      * @param useMBeanServer
110      * The useMBeanServer to set.
111      */

112     public void setUseMBeanServer(boolean useMBeanServer) {
113         this.useMBeanServer = useMBeanServer;
114     }
115
116     /**
117      * @return Returns the createMBeanServer flag.
118      */

119     public boolean isCreateMBeanServer() {
120         return createMBeanServer;
121     }
122
123     /**
124      * @param enableJMX
125      * Set createMBeanServer.
126      */

127     public void setCreateMBeanServer(boolean enableJMX) {
128         this.createMBeanServer = enableJMX;
129     }
130
131     /**
132      * Formulate and return the MBean ObjectName of a custom control MBean
133      *
134      * @param type
135      * @param name
136      * @return the JMX ObjectName of the MBean, or <code>null</code> if
137      * <code>customName</code> is invalid.
138      */

139     public ObjectName JavaDoc createCustomComponentMBeanName(String JavaDoc type, String JavaDoc name) {
140         ObjectName JavaDoc result = null;
141         String JavaDoc tmp = jmxDomainName + ":" + "type=" + sanitizeString(type) + ",name=" + sanitizeString(name);
142         try {
143             result = new ObjectName JavaDoc(tmp);
144         }
145         catch (MalformedObjectNameException JavaDoc e) {
146             log.error("Couldn't create ObjectName from: " + type + " , " + name);
147         }
148         return result;
149     }
150
151     /**
152      * The ':' and '/' characters are reserved in ObjectNames
153      *
154      * @param in
155      * @return sanitized String
156      */

157     private static String JavaDoc sanitizeString(String JavaDoc in) {
158         String JavaDoc result = null;
159         if (in != null) {
160             result = in.replace(':', '_');
161             result = result.replace('/', '_');
162             result = result.replace('\\', '_');
163         }
164         return result;
165     }
166
167     /**
168      * Retrive an System ObjectName
169      *
170      * @param domainName
171      * @param containerName
172      * @param theClass
173      * @return the ObjectName
174      * @throws MalformedObjectNameException
175      */

176     public static ObjectName JavaDoc getSystemObjectName(String JavaDoc domainName, String JavaDoc containerName, Class JavaDoc theClass)
177             throws MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
178         String JavaDoc tmp = domainName + ":" + "type=" + theClass.getName() + ",name="
179                 + getRelativeName(containerName, theClass);
180         return new ObjectName JavaDoc(tmp);
181     }
182
183     private static String JavaDoc getRelativeName(String JavaDoc containerName, Class JavaDoc theClass) {
184         String JavaDoc name = theClass.getName();
185         int index = name.lastIndexOf(".");
186         if (index >= 0 && (index + 1) < name.length()) {
187             name = name.substring(index + 1);
188         }
189         return containerName + "." + name;
190     }
191
192     /**
193      * Unregister an MBean
194      *
195      * @param name
196      * @throws JMException
197      */

198     public void unregisterMBean(ObjectName JavaDoc name) throws JMException JavaDoc {
199         if (beanServer != null && beanServer.isRegistered(name)) {
200             beanServer.unregisterMBean(name);
201         }
202     }
203
204     protected synchronized MBeanServer JavaDoc findMBeanServer() {
205         MBeanServer JavaDoc result = null;
206         // create the mbean server
207
try {
208             if (useMBeanServer) {
209                 // lets piggy back on another MBeanServer -
210
// we could be in an appserver!
211
List JavaDoc list = MBeanServerFactory.findMBeanServer(null);
212                 if (list != null && list.size() > 0) {
213                     result = (MBeanServer JavaDoc) list.get(0);
214                 }
215             }
216
217             if (result == null && createMBeanServer) {
218                 result = MBeanServerFactory.createMBeanServer(jmxDomainName);
219                 locallyCreateMBeanServer = true;
220             }
221         }
222         catch (NoClassDefFoundError JavaDoc e) {
223             log.error("Couldnot load MBeanServer", e);
224         }
225         catch (Throwable JavaDoc e) {
226             // probably don't have access to system properties
227
log.error("Failed to initialize MBeanServer", e);
228         }
229         return result;
230     }
231 }
232
Popular Tags