KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > jmx > JMXServer


1 /*
2  * Copyright (C) 2001 - 2005 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent Distributed Technologies
20  * Contributor(s):
21  */

22 package com.scalagent.jmx;
23
24 import java.io.*;
25 import java.util.*;
26 import java.lang.reflect.Method JavaDoc;
27
28 import javax.management.*;
29
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32 import org.objectweb.util.monolog.api.LoggerFactory;
33
34 import fr.dyade.aaa.util.management.*;
35
36 /**
37  *
38  */

39 public class JMXServer implements MXServer {
40   public MBeanServer mxserver = null;
41   public String JavaDoc domain = null;
42
43   public JMXServer(MBeanServer mxserver,
44                    String JavaDoc domain) {
45     this.mxserver = mxserver;
46     this.domain = domain;
47     MXWrapper.setMXServer(this);
48   }
49
50   public JMXServer() {
51     try {
52       // Try to get the default platform MBeanServer (since JDK 1.5)
53
Class JavaDoc clazz = Class.forName("java.lang.management.ManagementFactory");
54       Method JavaDoc method = clazz.getMethod("getPlatformMBeanServer", null);
55       this.mxserver = (MBeanServer) method.invoke(null, null);
56     } catch (Exception JavaDoc exc) {
57       // Prior JDK1.5 (with JMXRI implementation).
58
this.mxserver = MBeanServerFactory.createMBeanServer("AgentServer");
59     }
60     this.domain = "AgentServer";
61     MXWrapper.setMXServer(this);
62   }
63
64   public void registerMBean(Object JavaDoc bean,
65                             String JavaDoc domain,
66                             String JavaDoc name) throws Exception JavaDoc {
67     if (mxserver == null) return;
68
69     StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
70     strbuf.append(domain);
71     if (name != null)
72       strbuf.append(':').append(name);
73
74     try {
75       mxserver.registerMBean(bean, new ObjectName(strbuf.toString()));
76     } catch (InstanceAlreadyExistsException exc) {
77       // The MBean is already under the control of the MBean server.
78
throw exc;
79     } catch (MBeanRegistrationException exc) {
80       // The preRegister (MBeanRegistration interface) method of the MBean
81
// has thrown an exception. The MBean will not be registered.
82
throw exc;
83     } catch (NotCompliantMBeanException exc) {
84       // This object is not a JMX compliant MBean
85
throw exc;
86     } catch (RuntimeOperationsException exc) {
87       // Wraps a java.lang.IllegalArgumentException
88
throw exc;
89     }
90   }
91
92   public void unregisterMBean(String JavaDoc domain,
93                               String JavaDoc name) throws Exception JavaDoc {
94     if (mxserver == null) return;
95
96     StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
97     strbuf.append(domain);
98     strbuf.append(':').append(name);
99
100     try {
101       mxserver.unregisterMBean(new ObjectName(strbuf.toString()));
102     } catch (InstanceNotFoundException exc) {
103       // The MBean is not registered in the MBean server.
104
throw exc;
105     } catch (MBeanRegistrationException exc) {
106       // The preDeregister (MBeanRegistration interface) method of the MBean
107
// has thrown an exception.
108
throw exc;
109     } catch (RuntimeOperationsException exc) {
110       // Wraps a java.lang.IllegalArgumentException
111
throw exc;
112     }
113   }
114 }
115
Popular Tags