KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jmx > ManagementReprFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ManagementReprFactory.java,v 1.13 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jmx;
26
27 // JOnAS Log
28
import javax.management.MBeanServer JavaDoc;
29 import javax.management.MBeanServerConnection JavaDoc;
30
31 import org.objectweb.jonas.common.Log;
32 import org.objectweb.jonas.service.ServiceException;
33 import org.objectweb.jonas.service.ServiceManager;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  * Provides an appropriate management representative.
39  * @author Adriana Danes
40  */

41 public class ManagementReprFactory {
42     /**
43      * Private constructor
44      */

45     private ManagementReprFactory() {
46     }
47
48     /**
49      * Logger
50      */

51     private static Logger logger = Log.getLogger("org.objectweb.jonas.jmx");
52
53     /**
54      * jmx service impl
55      */

56     private static JmxService jmxService = null;
57
58     /**
59      * Create a ManagementReprImp instance
60      * This method is deprecated. ManagementReprImpl objects will no longer be used in rthe future.
61      * @return ManagementReprImp instance
62      */

63     public static ManagementRepr getManagementRepr() {
64         ManagementRepr representative = null;
65         ClassLoader JavaDoc classLoader = ManagementReprFactory.class.getClassLoader();
66         try {
67             Class JavaDoc managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImpl");
68             if (logger.isLoggable(BasicLevel.DEBUG)) {
69                 logger.log(BasicLevel.DEBUG, "ManagemntReprImp class loaded");
70             }
71             representative = (ManagementRepr) managemntRepClass.newInstance();
72             if (logger.isLoggable(BasicLevel.DEBUG)) {
73                 logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
74             }
75         } catch (Exception JavaDoc e) {
76             if (logger.isLoggable(BasicLevel.DEBUG)) {
77                 logger.log(BasicLevel.DEBUG, "ManagementReprFactory exception : " + e.toString());
78             }
79         }
80         return representative;
81     }
82
83     /**
84      * Create a ManagementReprImpJSR160 instance
85      * @param serverName The server to be managed via the created ManagementRepr
86      * @return ManagementReprImpJSR160 instance
87      */

88     public static ManagementRepr getManagementRepr(String JavaDoc serverName) {
89         ManagementRepr representative = null;
90         ClassLoader JavaDoc classLoader = ManagementReprFactory.class.getClassLoader();
91         try {
92             Class JavaDoc managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImplJSR160");
93             if (logger.isLoggable(BasicLevel.DEBUG)) {
94                 logger.log(BasicLevel.DEBUG, "ManagemntReprImplJSR160 class loaded");
95             }
96             representative = (ManagementRepr) managemntRepClass.newInstance();
97             if (logger.isLoggable(BasicLevel.DEBUG)) {
98                 logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
99             }
100             // Set the MBean server connection:
101
// - a JSR 160 connection object if the server can be remotelly managed
102
// - local MBeanServer reference
103
MBeanServerConnection JavaDoc connection = null;
104             if (jmxService == null) {
105                 jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
106             }
107             connection = jmxService.getServerConnection(serverName);
108             if (connection == null) {
109                 // Couldn't get remote connection - maybe this is due to the fact that we need local connection
110
// to the current MBeanServer
111
if (serverName.equals(jmxService.getJonasServerName())) {
112                     connection = getLocalManagementRepr();
113                 }
114             }
115             ((ManagementReprImplJSR160) representative).setMBeanServerConnection(connection);
116             if (logger.isLoggable(BasicLevel.DEBUG)) {
117                 logger.log(BasicLevel.DEBUG, "ManagementRepr has set connection for server " + serverName);
118             }
119             if (connection == null) {
120                 logger.log(BasicLevel.WARN, "ManagementRepr couldn't get connection for server " + serverName);
121             }
122         } catch (Exception JavaDoc e) {
123             logger.log(BasicLevel.WARN, "Exception when trying to create ManagementRepr: " + e.toString());
124         }
125         return representative;
126     }
127
128     /**
129      * @return MBeanServerConnection corresponding to the local MBeanServer
130      */

131     public static MBeanServerConnection JavaDoc getLocalManagementRepr() {
132         MBeanServer JavaDoc myMBeanServer = null;
133         JmxService jmxService = null;
134         try {
135             jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
136             myMBeanServer = jmxService.getJmxServer();
137         } catch (ServiceException e) {
138             // TODO Auto-generated catch block
139
e.printStackTrace();
140         } catch (Exception JavaDoc e) {
141             // TODO Auto-generated catch block
142
e.printStackTrace();
143         }
144         return (MBeanServerConnection JavaDoc) myMBeanServer;
145     }
146 }
147
Popular Tags