001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.web.jmx;
018:
019: import org.apache.activemq.Service;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import javax.jms.JMSException;
024: import javax.management.JMException;
025: import javax.management.MBeanServer;
026: import javax.management.MBeanServerFactory;
027: import javax.management.MalformedObjectNameException;
028: import javax.management.ObjectName;
029:
030: import java.util.List;
031:
032: /**
033: * A Flow provides different dispatch policies within the NMR
034: *
035: * @version $Revision: 356583 $
036: */
037: public class ManagementContext implements Service {
038: /**
039: * Default servicemix domain
040: */
041: public static String DEFAULT_DOMAIN = "org.apache.activemq";
042:
043: private final static Log log = LogFactory
044: .getLog(ManagementContext.class);
045:
046: private MBeanServer beanServer;
047: private String jmxDomainName = DEFAULT_DOMAIN;
048: private boolean useMBeanServer = true;
049: private boolean createMBeanServer = true;
050: private boolean locallyCreateMBeanServer = false;
051:
052: public ManagementContext() {
053: this (null);
054: }
055:
056: public ManagementContext(MBeanServer server) {
057: this .beanServer = server;
058: }
059:
060: public void start() throws JMSException {
061: // lets force the MBeanServer to be created if needed
062: getMBeanServer();
063: }
064:
065: public void stop() throws JMSException {
066: if (locallyCreateMBeanServer && beanServer != null) {
067: // check to see if the factory knows about this server
068: List list = MBeanServerFactory.findMBeanServer(null);
069: if (list != null && !list.isEmpty()
070: && list.contains(beanServer)) {
071: MBeanServerFactory.releaseMBeanServer(beanServer);
072: }
073: }
074: }
075:
076: /**
077: * @return Returns the jmxDomainName.
078: */
079: public String getJmxDomainName() {
080: return jmxDomainName;
081: }
082:
083: /**
084: * @param jmxDomainName
085: * The jmxDomainName to set.
086: */
087: public void setJmxDomainName(String jmxDomainName) {
088: this .jmxDomainName = jmxDomainName;
089: }
090:
091: /**
092: * Get the MBeanServer
093: *
094: * @return the MBeanServer
095: */
096: public MBeanServer getMBeanServer() {
097: if (this .beanServer == null) {
098: this .beanServer = findMBeanServer();
099: }
100: return beanServer;
101: }
102:
103: /**
104: * @return Returns the useMBeanServer.
105: */
106: public boolean isUseMBeanServer() {
107: return useMBeanServer;
108: }
109:
110: /**
111: * @param useMBeanServer
112: * The useMBeanServer to set.
113: */
114: public void setUseMBeanServer(boolean useMBeanServer) {
115: this .useMBeanServer = useMBeanServer;
116: }
117:
118: /**
119: * @return Returns the createMBeanServer flag.
120: */
121: public boolean isCreateMBeanServer() {
122: return createMBeanServer;
123: }
124:
125: /**
126: * @param enableJMX
127: * Set createMBeanServer.
128: */
129: public void setCreateMBeanServer(boolean enableJMX) {
130: this .createMBeanServer = enableJMX;
131: }
132:
133: /**
134: * Formulate and return the MBean ObjectName of a custom control MBean
135: *
136: * @param type
137: * @param name
138: * @return the JMX ObjectName of the MBean, or <code>null</code> if
139: * <code>customName</code> is invalid.
140: */
141: public ObjectName createCustomComponentMBeanName(String type,
142: String name) {
143: ObjectName result = null;
144: String tmp = jmxDomainName + ":" + "type="
145: + sanitizeString(type) + ",name="
146: + sanitizeString(name);
147: try {
148: result = new ObjectName(tmp);
149: } catch (MalformedObjectNameException e) {
150: log.error("Couldn't create ObjectName from: " + type
151: + " , " + name);
152: }
153: return result;
154: }
155:
156: /**
157: * The ':' and '/' characters are reserved in ObjectNames
158: *
159: * @param in
160: * @return sanitized String
161: */
162: private static String sanitizeString(String in) {
163: String result = null;
164: if (in != null) {
165: result = in.replace(':', '_');
166: result = result.replace('/', '_');
167: result = result.replace('\\', '_');
168: }
169: return result;
170: }
171:
172: /**
173: * Retrive an System ObjectName
174: *
175: * @param domainName
176: * @param containerName
177: * @param theClass
178: * @return the ObjectName
179: * @throws MalformedObjectNameException
180: */
181: public static ObjectName getSystemObjectName(String domainName,
182: String containerName, Class theClass)
183: throws MalformedObjectNameException, NullPointerException {
184: String tmp = domainName + ":" + "type=" + theClass.getName()
185: + ",name=" + getRelativeName(containerName, theClass);
186: return new ObjectName(tmp);
187: }
188:
189: private static String getRelativeName(String containerName,
190: Class theClass) {
191: String name = theClass.getName();
192: int index = name.lastIndexOf(".");
193: if (index >= 0 && (index + 1) < name.length()) {
194: name = name.substring(index + 1);
195: }
196: return containerName + "." + name;
197: }
198:
199: /**
200: * Unregister an MBean
201: *
202: * @param name
203: * @throws JMException
204: */
205: public void unregisterMBean(ObjectName name) throws JMException {
206: if (beanServer != null && beanServer.isRegistered(name)) {
207: beanServer.unregisterMBean(name);
208: }
209: }
210:
211: protected synchronized MBeanServer findMBeanServer() {
212: MBeanServer result = null;
213: // create the mbean server
214: try {
215: if (useMBeanServer) {
216: // lets piggy back on another MBeanServer -
217: // we could be in an appserver!
218: List list = MBeanServerFactory.findMBeanServer(null);
219: if (list != null && list.size() > 0) {
220: result = (MBeanServer) list.get(0);
221: }
222: }
223:
224: if (result == null && createMBeanServer) {
225: result = MBeanServerFactory
226: .createMBeanServer(jmxDomainName);
227: locallyCreateMBeanServer = true;
228: }
229: } catch (NoClassDefFoundError e) {
230: log.error("Couldnot load MBeanServer", e);
231: } catch (Throwable e) {
232: // probably don't have access to system properties
233: log.error("Failed to initialize MBeanServer", e);
234: }
235: return result;
236: }
237: }
|