001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: * --------------------------------------------------------------------------
021: * $Id: AbsJmxServiceImpl.java 9234 2006-07-25 08:29:33Z durieuxp $
022: * --------------------------------------------------------------------------
023: */package org.objectweb.jonas.jmx;
024:
025: import java.util.List;
026:
027: import javax.management.InstanceAlreadyExistsException;
028: import javax.management.MBeanRegistrationException;
029: import javax.management.MBeanServer;
030: import javax.management.MBeanServerFactory;
031: import javax.management.MalformedObjectNameException;
032: import javax.management.NotCompliantMBeanException;
033: import javax.management.ObjectName;
034: import javax.management.remote.JMXServiceURL;
035: import javax.naming.Context;
036: import javax.naming.NamingException;
037:
038: import org.objectweb.jonas.common.Log;
039: import org.objectweb.jonas.service.AbsServiceImpl;
040: import org.objectweb.jonas.service.ServiceException;
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: * JMX Service implementation. This singleton class must exist in each jonas
046: * server that is to be administered via JMX. Its main role is to initialize the
047: * service (the singleton object).
048: * @author Guillaume Riviere
049: * @author Michel Bruno
050: * @author Adriana Danes
051: */
052:
053: public abstract class AbsJmxServiceImpl extends AbsServiceImpl
054: implements JmxService {
055:
056: /**
057: * Trace logger
058: */
059: private static Logger logger = null;
060:
061: /**
062: * @return Logger logger object
063: */
064: protected static Logger getLogger() {
065: return logger;
066: }
067:
068: /**
069: * JMX server (MBean server) instance
070: */
071: private MBeanServer jmxServer = null;
072:
073: /**
074: * Init the Service. Create the MBeanServer. Configuration information is
075: * passed through a Context object.
076: * @param ctx context containing service initilaisation parameters
077: * @exception ServiceException the service could not be initialized
078: */
079: public void doInit(Context ctx) throws ServiceException {
080: logger = Log.getLogger(Log.JONAS_JMX_PREFIX);
081: super .initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX));
082:
083: // Create the MBeanServer
084:
085: // get Id
086: String idMBeanServer = null;
087: try {
088: idMBeanServer = (String) ctx.lookup("idMBeanServer");
089: } catch (NamingException ne) {
090: throw new ServiceException(
091: "Cannot found the id of the MBean Server", ne);
092: }
093:
094: // ----------------------
095: List mbeanServers = MBeanServerFactory
096: .findMBeanServer(idMBeanServer);
097: if (mbeanServers.size() == 0) {
098: throw new ServiceException("No MBean server found for id '"
099: + idMBeanServer + "'.");
100: }
101: if (logger.isLoggable(BasicLevel.DEBUG)) {
102: logger.log(BasicLevel.DEBUG,
103: "Use first MBean server ID found");
104: }
105: jmxServer = (MBeanServer) mbeanServers.get(0);
106:
107: if (logger.isLoggable(BasicLevel.DEBUG)) {
108: logger.log(BasicLevel.DEBUG, "JMX Service initialized");
109: }
110: }
111:
112: /**
113: * Start the Service Initialization of the service is already done.
114: * @exception ServiceException the service could not be started
115: */
116: public abstract void doStart() throws ServiceException;
117:
118: /**
119: * Stop this service
120: * @exception ServiceException the service could not be stopped
121: */
122: public abstract void doStop() throws ServiceException;
123:
124: public abstract JMXServiceURL[] getConnectorServerURLs();
125:
126: /**
127: * @return The local reference of the MBean server
128: */
129: public MBeanServer getJmxServer() {
130: return this .jmxServer;
131: }
132:
133: /**
134: * Register an MBean on the JOnAS mbeanServer
135: * @param mbean
136: * @param objectName
137: */
138: public void registerMBean(Object mbean, String objectName) {
139: if (jmxServer == null) {
140: logger.log(BasicLevel.ERROR,
141: "JMX Server not initialized yet");
142: return;
143: }
144: try {
145: ObjectName on = ObjectName.getInstance(objectName);
146: jmxServer.registerMBean(mbean, on);
147: } catch (MalformedObjectNameException e) {
148: logger.log(BasicLevel.ERROR, objectName + ":" + e);
149: } catch (InstanceAlreadyExistsException e) {
150: logger.log(BasicLevel.WARN, objectName + ":" + e);
151: } catch (MBeanRegistrationException e) {
152: logger.log(BasicLevel.ERROR, objectName + ":" + e);
153: } catch (NotCompliantMBeanException e) {
154: logger.log(BasicLevel.ERROR, objectName + ":" + e);
155: }
156: }
157:
158: /**
159: * Remove internal references to the MBeanServer.
160: */
161: protected void releaseJmxServer() {
162: try {
163: MBeanServerFactory.releaseMBeanServer(jmxServer);
164: } catch (java.lang.IllegalArgumentException ex) {
165: // The mbeanServer is not found.
166: logger.log(BasicLevel.ERROR, "Cannot find the MBeanServer"
167: + ex);
168: }
169: }
170:
171: }
|