ManagementBean.java

00001 
00025 package org.objectweb.jonas.mejb;
00026 
00027 import java.io.IOException;
00028 import java.rmi.RemoteException;
00029 import java.util.Set;
00030 
00031 import javax.ejb.SessionBean;
00032 import javax.ejb.SessionContext;
00033 import javax.management.Attribute;
00034 import javax.management.AttributeList;
00035 import javax.management.AttributeNotFoundException;
00036 import javax.management.InstanceAlreadyExistsException;
00037 import javax.management.InstanceNotFoundException;
00038 import javax.management.IntrospectionException;
00039 import javax.management.InvalidAttributeValueException;
00040 import javax.management.MBeanException;
00041 import javax.management.MBeanInfo;
00042 import javax.management.MBeanServer;
00043 import javax.management.MBeanServerConnection;
00044 import javax.management.ObjectName;
00045 import javax.management.QueryExp;
00046 import javax.management.ReflectionException;
00047 import javax.management.j2ee.ListenerRegistration;
00048 import javax.management.remote.JMXConnector;
00049 import javax.management.remote.JMXConnectorFactory;
00050 import javax.management.remote.JMXServiceURL;
00051 
00052 import org.objectweb.jonas.common.Log;
00053 import org.objectweb.jonas.jmx.J2eeObjectName;
00054 import org.objectweb.jonas.jmx.JmxService;
00055 import org.objectweb.jonas.management.j2eemanagement.ManagementListener;
00056 import org.objectweb.jonas.management.j2eemanagement.ManagementListenerMBean;
00057 import org.objectweb.jonas.service.ServiceManager;
00058 
00059 import org.objectweb.util.monolog.api.BasicLevel;
00060 import org.objectweb.util.monolog.api.Logger;
00061 
00073 public class ManagementBean implements SessionBean {
00074 
00078     private static Logger logger = Log.getLogger(Log.JONAS_MEJB);
00082     private SessionContext sessionContext = null;
00083 
00087     private MBeanServer jmxServer = null;
00088 
00092     private MBeanServerConnection currentServerConnection = null;
00093 
00097     private boolean connected = false;
00101     private String serverName = null;
00105     private String domainName = null;
00109     private String proxyName = "MEJB_listener";
00110 
00114     public void ejbCreate() {
00115         // Get the MBeanServer reference to allow local access
00116         JmxService jmxService = null;
00117         try {
00118             jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
00119         } catch (Exception e) {
00120             // should never occurs
00121         }
00122 //        JMXServiceURL[] jmxServiceUrls = jmxService.getConnectorServerURLs();
00123 //        int nbAvailableConnections = jmxServiceUrls.length;
00124 //        for (int i = 0; i < nbAvailableConnections; i++) {
00125 //            JMXServiceURL url = jmxServiceUrls[i];
00126 //            logger.log(BasicLevel.DEBUG, "Try to connect to MBeanServer using url:" + url);
00127 //            try {
00128 //                JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, null);
00129 //                connector.connect();
00130 //                currentServerConnection = connector.getMBeanServerConnection();
00131 //                connected = true;
00132 //                logger.log(BasicLevel.DEBUG, "Connected via url:" + url);
00133 //                break;
00134 //            } catch (IOException ioe) {
00135 //                // This may not happen normally as we are not trying to connect to a remote server
00136 //                logger.log(BasicLevel.DEBUG, "Cannot connect to MBeanServer using url:" + url);
00137 //            }
00138 //        }
00139         if (!connected) {
00140             jmxServer = jmxService.getJmxServer();
00141         }
00142         serverName = jmxService.getJonasServerName();
00143         domainName = jmxService.getDomainName();
00144 
00145     }
00146 
00147     /*====================== javax.ejb.SessionBean implementation =================*/
00148 
00149     public void ejbActivate() {
00150         // Nothing to do when the MEJB is activated
00151     }
00152     public void ejbPassivate() {
00153         // Nothing to do when the MEJB is passivated
00154     }
00155     public void ejbRemove() {
00156         // Nothing to do when the MEJB is removed
00157     }
00162     public void setSessionContext(SessionContext sessionContext) {
00163         this.sessionContext = sessionContext;
00164     }
00165 
00166     /*========================= ManagementBean implementation ============================*/
00167     /*=============== The management methods are invoked on the local MBeanServer ========*/
00168 
00169     public Object getAttribute(ObjectName name, String attribute) throws
00170         MBeanException,
00171         AttributeNotFoundException,
00172         InstanceNotFoundException,
00173         ReflectionException,
00174         RemoteException {
00175 
00176         if (!connected) {
00177             return jmxServer.getAttribute(name, attribute);
00178         } else {
00179             try {
00180                 return currentServerConnection.getAttribute(name, attribute);
00181             } catch (java.io.IOException ioe) {
00182                 throw new RemoteException("Object getAttribute(ObjectName, String) failed", ioe);
00183             }
00184         }
00185     }
00186 
00187     public AttributeList getAttributes(ObjectName name, String[] attributes) throws
00188         InstanceNotFoundException,
00189         ReflectionException,
00190         RemoteException {
00191 
00192         if (!connected) {
00193             return jmxServer.getAttributes(name, attributes);
00194         } else {
00195             try {
00196                 return currentServerConnection.getAttributes(name, attributes);
00197             } catch (java.io.IOException ioe) {
00198                 throw new RemoteException("AttributeList getAttributes(ObjectName, String[]) failed", ioe);
00199             }
00200         }
00201     }
00202 
00203     public String getDefaultDomain() throws RemoteException {
00204 
00205         if (!connected) {
00206             return jmxServer.getDefaultDomain();
00207         } else {
00208             try {
00209                 return currentServerConnection.getDefaultDomain();
00210             } catch (java.io.IOException ioe) {
00211                 throw new RemoteException("String getDefaultDomain() failed", ioe);
00212             }
00213         }
00214     }
00215 
00216     public Integer getMBeanCount() throws RemoteException {
00217 
00218         if (!connected) {
00219             return jmxServer.getMBeanCount();
00220         } else {
00221             try {
00222                 return currentServerConnection.getMBeanCount();
00223             } catch (java.io.IOException ioe) {
00224                 throw new RemoteException("Integer getMBeanCount() failed", ioe);
00225             }
00226         }
00227     }
00228 
00229     public MBeanInfo getMBeanInfo(ObjectName name) throws
00230         IntrospectionException,
00231         InstanceNotFoundException,
00232         ReflectionException,
00233         RemoteException {
00234 
00235         if (!connected) {
00236             return jmxServer.getMBeanInfo(name);
00237         } else {
00238             try {
00239                 return currentServerConnection.getMBeanInfo(name);
00240             } catch (java.io.IOException ioe) {
00241                 throw new RemoteException("MBeanInfo getMBeanInfo(ObjectName) failed", ioe);
00242             }
00243         }
00244     }
00245 
00246     public Object invoke(ObjectName name, String operationName, Object[] params, String[] signature) throws
00247         MBeanException,
00248         InstanceNotFoundException,
00249         ReflectionException,
00250         RemoteException {
00251 
00252         if (!connected) {
00253             return jmxServer.invoke(name, operationName, params, signature);
00254         } else {
00255             try {
00256                 return currentServerConnection.invoke(name, operationName, params, signature);
00257             } catch (java.io.IOException ioe) {
00258                 throw new RemoteException("Object invoke(ObjectName, String, Object[], String[]) failed", ioe);
00259             }
00260         }
00261     }
00262 
00263     public boolean isRegistered(ObjectName name) throws RemoteException {
00264 
00265         if (!connected) {
00266             return jmxServer.isRegistered(name);
00267         } else {
00268             try {
00269                 return currentServerConnection.isRegistered(name);
00270             } catch (java.io.IOException ioe) {
00271                 throw new RemoteException("boolean isRegistered(ObjectName) failed", ioe);
00272             }
00273         }
00274     }
00275 
00276     public Set queryNames(ObjectName name, QueryExp query) throws RemoteException {
00277         if (!connected) {
00278             return jmxServer.queryNames(name, query);
00279         } else {
00280             try {
00281                 return currentServerConnection.queryNames(name, query);
00282             } catch (java.io.IOException ioe) {
00283                 throw new RemoteException("Set queryNames(ObjectName, QueryExp)  failed", ioe);
00284             }
00285         }
00286     }
00287 
00288     public void setAttribute(ObjectName name, Attribute attribute) throws
00289         MBeanException,
00290         AttributeNotFoundException,
00291         InstanceNotFoundException,
00292         InvalidAttributeValueException,
00293         ReflectionException,
00294         RemoteException {
00295 
00296         if (!connected) {
00297             jmxServer.setAttribute(name, attribute);
00298         } else {
00299             try {
00300                 currentServerConnection.setAttribute(name, attribute);
00301             } catch (java.io.IOException ioe) {
00302                 throw new RemoteException("void setAttribute(ObjectName, Attribute) failed", ioe);
00303             }
00304         }
00305     }
00306 
00307     public AttributeList setAttributes(ObjectName name, AttributeList attributes) throws
00308         InstanceNotFoundException,
00309         ReflectionException,
00310         RemoteException {
00311 
00312         if (!connected) {
00313             return jmxServer.setAttributes(name, attributes);
00314         } else {
00315             try {
00316                 return currentServerConnection.setAttributes(name, attributes);
00317             } catch (java.io.IOException ioe) {
00318                 throw new RemoteException("AttributeList setAttributes(ObjectName, AttributeList) failed", ioe);
00319             }
00320         }
00321     }
00322 
00330     public ListenerRegistration getListenerRegistry() throws RemoteException {
00331         if (!connected) {
00332             // Create and register the ManagemenentListener MBean if this is not already done
00333             ObjectName listenerOn = J2eeObjectName.ManagementListener(domainName, serverName);
00334             boolean isRegisteredListener;
00335             isRegisteredListener = jmxServer.isRegistered(listenerOn);
00336             if (!isRegisteredListener) {
00337                 ManagementListenerMBean listenerMBean = new ManagementListener(proxyName);
00338                 try {
00339                     jmxServer.registerMBean(listenerMBean, listenerOn);
00340                 } catch (InstanceAlreadyExistsException ae) {
00341                     // Not possible as tested before
00342                 } catch (Exception e) {
00343                     // Could not register MBean -> the management bean can't handle notifications
00344                     throw new RemoteException("Can not return ListenerRegistration implementation", e);
00345                 }
00346             }
00347             // Create ListenerRegistration object
00348             return new ListenerRegistrationImpl(listenerOn, proxyName);
00349         } else {
00350             // JMX Remote way of adding remote listeners not yet implemented
00351             return null;
00352         }
00353     }
00354 }

Generated on Tue Feb 15 15:05:19 2005 for JOnAS by  doxygen 1.3.9.1