com/scalagent/jmx/JMXServer.java

00001 /*
00002  * Copyright (C) 2001 - 2008 ScalAgent Distributed Technologies
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or any later version.
00008  * 
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00017  * USA.
00018  *
00019  * Initial developer(s): ScalAgent Distributed Technologies
00020  * Contributor(s): 
00021  */
00022 package com.scalagent.jmx;
00023 
00024 import java.io.Serializable;
00025 import java.lang.reflect.Method;
00026 import java.util.Set;
00027 
00028 import javax.management.InstanceAlreadyExistsException;
00029 import javax.management.InstanceNotFoundException;
00030 import javax.management.MBeanAttributeInfo;
00031 import javax.management.MBeanRegistrationException;
00032 import javax.management.MBeanServer;
00033 import javax.management.MBeanServerFactory;
00034 import javax.management.NotCompliantMBeanException;
00035 import javax.management.ObjectName;
00036 import javax.management.RuntimeOperationsException;
00037 
00038 import fr.dyade.aaa.util.management.MXServer;
00039 import fr.dyade.aaa.util.management.MXWrapper;
00040 
00044 public class JMXServer implements MXServer, Serializable {
00045   
00046   public MBeanServer mxserver = null;
00047 
00048   public JMXServer(MBeanServer mxserver) {
00049     this.mxserver = mxserver;
00050     MXWrapper.setMXServer(this);
00051   }
00052 
00053   public JMXServer() {
00054     try {
00055       // Try to get the default platform MBeanServer (since JDK 1.5)
00056       Class clazz = Class.forName("java.lang.management.ManagementFactory");
00057       Method method = clazz.getMethod("getPlatformMBeanServer", null);
00058       this.mxserver = (MBeanServer) method.invoke(null, null);
00059     } catch (Exception exc) {
00060       // Prior JDK1.5 (with JMXRI implementation).
00061       this.mxserver = MBeanServerFactory.createMBeanServer("AgentServer");
00062     }
00063     MXWrapper.setMXServer(this);
00064   }
00065 
00066   public void registerMBean(Object bean,
00067                             String domain,
00068                             String name) throws Exception {
00069     StringBuffer strbuf = new StringBuffer();
00070     strbuf.append(domain).append(':').append(name);
00071     registerMBean(bean, strbuf.toString());
00072   }
00073 
00074   public void unregisterMBean(String domain,
00075                               String name) throws Exception {
00076     StringBuffer strbuf = new StringBuffer();
00077     strbuf.append(domain);
00078     strbuf.append(':').append(name);
00079     unregisterMBean(strbuf.toString());
00080   }
00081 
00082   public void registerMBean(Object bean, String fullName) throws Exception {
00083     if (mxserver == null)
00084       return;
00085     try {
00086       mxserver.registerMBean(bean, new ObjectName(fullName));
00087     } catch (InstanceAlreadyExistsException exc) {
00088       // The MBean is already under the control of the MBean server.
00089       throw exc;
00090     } catch (MBeanRegistrationException exc) {
00091       // The preRegister (MBeanRegistration  interface) method of the MBean
00092       // has thrown an exception. The MBean will not be registered.
00093       throw exc;
00094     } catch (NotCompliantMBeanException exc) {
00095       // This object is not a JMX compliant MBean
00096       throw exc;
00097     } catch (RuntimeOperationsException exc) {
00098       // Wraps a java.lang.IllegalArgumentException
00099       throw exc;
00100     }
00101   }
00102 
00103   public void unregisterMBean(String fullName) throws Exception {
00104     if (mxserver == null)
00105       return;
00106     try {
00107       mxserver.unregisterMBean(new ObjectName(fullName));
00108     } catch (InstanceNotFoundException exc) {
00109       // The MBean is not registered in the MBean server.
00110       throw exc;
00111     } catch (MBeanRegistrationException exc) {
00112       // The preDeregister (MBeanRegistration  interface) method of the MBean
00113       // has thrown an exception.
00114       throw exc;
00115     } catch (RuntimeOperationsException exc) {
00116       // Wraps a java.lang.IllegalArgumentException
00117       throw exc;
00118     }
00119   }
00120   
00121   public Object getAttribute(ObjectName objectName, String attribute) throws Exception {
00122     if (mxserver == null) {
00123       return null;
00124     }
00125     return mxserver.getAttribute(objectName, attribute);
00126   }
00127   
00128   public MBeanAttributeInfo[] getAttributes(ObjectName objectName) throws Exception {
00129     if (mxserver == null) {
00130       return null;
00131     }
00132     return mxserver.getMBeanInfo(objectName).getAttributes();
00133   }
00134   
00135   public Set queryNames(ObjectName objectName) {
00136     if (mxserver == null) {
00137       return null;
00138     }
00139     return mxserver.queryNames(objectName, null);
00140   }
00141   
00142 }

Generated on Tue Sep 16 16:14:22 2008 for joram by  doxygen 1.5.0