001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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: * --------------------------------------------------------------------------
022: * $Id: ManagementReprFactory.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jmx;
025:
026: // JOnAS Log
027: import javax.management.MBeanServer;
028: import javax.management.MBeanServerConnection;
029:
030: import org.objectweb.jonas.common.Log;
031: import org.objectweb.jonas.management.j2eemanagement.J2EEDomain;
032: import org.objectweb.jonas.service.ServiceException;
033: import org.objectweb.jonas.service.ServiceManager;
034:
035: import org.objectweb.util.monolog.api.BasicLevel;
036: import org.objectweb.util.monolog.api.Logger;
037:
038: /**
039: * Provides an appropriate management representative.
040: * @author Adriana Danes
041: */
042: public class ManagementReprFactory {
043: /**
044: * Private constructor
045: */
046: private ManagementReprFactory() {
047: }
048:
049: /**
050: * Logger
051: */
052: private static Logger logger = Log
053: .getLogger("org.objectweb.jonas.jmx");
054:
055: /**
056: * Create a ManagementReprImp instance
057: * This method is deprecated. ManagementReprImpl objects will no longer be used in rthe future.
058: * @return ManagementReprImp instance
059: */
060: /*public static ManagementRepr getManagementRepr() {
061: ManagementRepr representative = null;
062: ClassLoader classLoader = ManagementReprFactory.class.getClassLoader();
063: try {
064: Class managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImpl");
065: if (logger.isLoggable(BasicLevel.DEBUG)) {
066: logger.log(BasicLevel.DEBUG, "ManagemntReprImp class loaded");
067: }
068: representative = (ManagementRepr) managemntRepClass.newInstance();
069: if (logger.isLoggable(BasicLevel.DEBUG)) {
070: logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
071: }
072: } catch (Exception e) {
073: if (logger.isLoggable(BasicLevel.DEBUG)) {
074: logger.log(BasicLevel.DEBUG, "ManagementReprFactory exception : " + e.toString());
075: }
076: }
077: return representative;
078: }*/
079:
080: /**
081: * Create a ManagementReprImpJSR160 instance
082: * @param serverName The server to be managed via the created ManagementRepr
083: * @return ManagementReprImpJSR160 instance
084: */
085: public static ManagementRepr getManagementRepr(String serverName) {
086: ManagementRepr representative = null;
087: ClassLoader classLoader = ManagementReprFactory.class
088: .getClassLoader();
089: try {
090: Class managemntRepClass = classLoader
091: .loadClass("org.objectweb.jonas.jmx.ManagementReprImplJSR160");
092: if (logger.isLoggable(BasicLevel.DEBUG)) {
093: logger.log(BasicLevel.DEBUG,
094: "ManagemntReprImplJSR160 class loaded");
095: }
096: representative = (ManagementRepr) managemntRepClass
097: .newInstance();
098: if (logger.isLoggable(BasicLevel.DEBUG)) {
099: logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
100: }
101: // Get the MBean server connection:
102: // - a JSR 160 connection object if the server can be remotelly managed
103: // - local MBeanServer reference
104: MBeanServerConnection connection = J2EEDomain.getInstance()
105: .getConnection(serverName);
106: ((ManagementReprImplJSR160) representative)
107: .setMBeanServerConnection(connection);
108: if (logger.isLoggable(BasicLevel.DEBUG)) {
109: logger.log(BasicLevel.DEBUG,
110: "ManagementRepr has set connection for server "
111: + serverName);
112: }
113: if (connection == null) {
114: // Couldn't get connection this can arrive only for remote connections
115: logger.log(BasicLevel.WARN,
116: "ManagementRepr couldn't get connection for server "
117: + serverName);
118: } else {
119: if (logger.isLoggable(BasicLevel.DEBUG)) {
120: logger.log(BasicLevel.DEBUG,
121: "ManagementRepr has set connection for server "
122: + serverName);
123: }
124: }
125: } catch (Exception e) {
126: logger.log(BasicLevel.WARN,
127: "Exception when trying to create ManagementRepr: "
128: + e.toString());
129: }
130: return representative;
131: }
132:
133: /**
134: * Create a ManagementReprImpJSR160 instance
135: * @param serverName The server to be managed via the created ManagementRepr
136: * @return ManagementReprImpJSR160 instance
137: */
138: /*
139: public static ManagementRepr getManagementRepr(String serverName, MBeanServerConnection connection) {
140: ManagementRepr representative = null;
141: ClassLoader classLoader = ManagementReprFactory.class.getClassLoader();
142: try {
143: Class managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImplJSR160");
144: if (logger.isLoggable(BasicLevel.DEBUG)) {
145: logger.log(BasicLevel.DEBUG, "ManagemntReprImplJSR160 class loaded");
146: }
147: representative = (ManagementRepr) managemntRepClass.newInstance();
148: if (logger.isLoggable(BasicLevel.DEBUG)) {
149: logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
150: }
151: ((ManagementReprImplJSR160) representative).setMBeanServerConnection(connection);
152: ((ManagementReprImplJSR160) representative).setServerName(serverName);
153: if (logger.isLoggable(BasicLevel.DEBUG)) {
154: logger.log(BasicLevel.DEBUG, "ManagementRepr has set connection for server " + serverName);
155: }
156: } catch (Exception e) {
157: logger.log(BasicLevel.WARN, "Exception when trying to create ManagementRepr: " + e.toString());
158: }
159: return representative;
160: }*/
161:
162: /**
163: * @return MBeanServerConnection corresponding to the local MBeanServer
164: */
165: public static MBeanServerConnection getLocalManagementRepr() {
166: MBeanServer myMBeanServer = null;
167: JmxService jmxService = null;
168: try {
169: jmxService = (JmxService) ServiceManager.getInstance()
170: .getJmxService();
171: myMBeanServer = jmxService.getJmxServer();
172: } catch (ServiceException e) {
173: // TODO Auto-generated catch block
174: e.printStackTrace();
175: } catch (Exception e) {
176: // TODO Auto-generated catch block
177: e.printStackTrace();
178: }
179: return (MBeanServerConnection) myMBeanServer;
180: }
181: }
|