J2EEDomain.java

00001 
00025 package org.objectweb.jonas.management.j2eemanagement;
00026 
00027 import java.io.IOException;
00028 import java.net.MalformedURLException;
00029 import java.util.ArrayList;
00030 import java.util.HashMap;
00031 import java.util.Iterator;
00032 import java.util.Map;
00033 
00034 import javax.management.InstanceNotFoundException;
00035 import javax.management.JMException;
00036 import javax.management.ListenerNotFoundException;
00037 import javax.management.MBeanRegistration;
00038 import javax.management.MBeanServer;
00039 import javax.management.MBeanServerConnection;
00040 import javax.management.MBeanServerNotification;
00041 import javax.management.Notification;
00042 import javax.management.NotificationFilter;
00043 import javax.management.NotificationListener;
00044 import javax.management.ObjectName;
00045 import javax.management.remote.JMXConnector;
00046 import javax.management.remote.JMXConnectorFactory;
00047 import javax.management.remote.JMXServiceURL;
00048 
00049 import org.objectweb.jonas.common.Log;
00050 import org.objectweb.jonas.discovery.DiscEvent;
00051 import org.objectweb.jonas.jmx.J2eeObjectName;
00052 import org.objectweb.util.monolog.api.BasicLevel;
00053 import org.objectweb.util.monolog.api.Logger;
00054 
00059 public class J2EEDomain extends J2EEManagedObject implements MBeanRegistration, NotificationListener {
00060 
00064     private String name = null;
00068     private ArrayList myServers = null;
00069 
00073     private static Logger logger = null;
00074 
00078     private MBeanServer mbeanServer = null;
00079 
00083     private Map managedServersToUrls = null;
00087     private Map managedServersToConnectors = null;
00091     private Map managedServersToConnections = null;
00092 
00101     public J2EEDomain(String objectName, boolean stateManageable, boolean statisticsProvider, boolean eventProvider) throws JMException {
00102         super(objectName, stateManageable, statisticsProvider, eventProvider);
00103         ObjectName myObjectName = ObjectName.getInstance(objectName);
00104         name = myObjectName.getKeyProperty("name");
00105         myServers = new ArrayList();
00106         managedServersToConnectors = new HashMap();
00107         managedServersToConnections = new HashMap();
00108         managedServersToUrls = new HashMap();
00109         logger = Log.getLogger("org.objectweb.jonas.management.j2eemanagement");
00110         if (logger.isLoggable(BasicLevel.DEBUG)) {
00111             logger.log(BasicLevel.DEBUG, "J2EEDomain managed object created. OBJECT_NAME = " + objectName);
00112         }
00113     }
00114 
00118     public String[] getServers() {
00119         //return (String[]) myServers.toArray();
00120         String[] sb = new String[myServers.size()];
00121         int i = 0;
00122         for (Iterator it = myServers.iterator(); it.hasNext();) {
00123             sb[i++] = (String) it.next();
00124         }
00125         return sb;
00126     }
00127 
00132     public void addServer(String serverName) {
00133         if (myServers.contains(serverName)) {
00134             if (logger.isLoggable(BasicLevel.DEBUG)) {
00135                 logger.log(BasicLevel.DEBUG, "The object name: " + serverName + " is already in the servers list");
00136             }
00137         } else {
00138             myServers.add(serverName);
00139         }
00140     }
00141 
00147     public String removeServer(String serverName) {
00148         int index = myServers.indexOf(serverName);
00149         if (index == -1) {
00150             // name not found
00151             return null;
00152         } else {
00153             return (String) myServers.remove(index);
00154         }
00155     }
00156 
00163     public MBeanServerConnection getConnection(String serverName) {
00164         MBeanServerConnection connection = null;
00165         connection = (MBeanServerConnection) managedServersToConnections.get(serverName);
00166         testConnection(connection, serverName);
00167         return connection;
00168     }
00177     public void addServer(String domainName, String serverName, String connectorServerURL) {
00178         logger = Log.getLogger("org.objectweb.jonas.management.j2eemanagement");
00179         if (logger.isLoggable(BasicLevel.DEBUG)) {
00180             logger.log(BasicLevel.DEBUG, "domain name: " + domainName + ", server name: " + serverName + ", url: " + connectorServerURL);
00181         }
00182         if (domainName.equals(name)) {
00183             // Server thet belongs to the current management domain
00184             if (managedServersToUrls.containsKey(serverName)) {
00185                 // This server is already known
00186                 ArrayList urls = (ArrayList) managedServersToUrls.get(serverName);
00187                 if (!urls.contains(connectorServerURL)) {
00188                     // Add this new connectorServerURL in the connectors list
00189                     urls.add(connectorServerURL);
00190                     // try to create a connection for this connectorServerURL
00191                     createConnection(serverName, connectorServerURL);
00192                 }
00193             } else {
00194                 // Add new server
00195                 ArrayList urls = new ArrayList();
00196                 urls.add(connectorServerURL);
00197                 managedServersToUrls.put(serverName, urls);
00198                 // try to create a connection
00199                 boolean created = createConnection(serverName, connectorServerURL);
00200                 if (created) {
00201                     // Update the servers list
00202                     ObjectName on = J2eeObjectName.J2EEServer(domainName, serverName);
00203                     addServer(on.toString());
00204                 }
00205             }
00206         } else {
00207             if (logger.isLoggable(BasicLevel.WARN)) {
00208                 logger.log(BasicLevel.WARN, "The server named " + serverName + " was not started in the management domain "
00209                         + name + ", but in management domain " + domainName);
00210             }
00211         }
00212     }
00213 
00219     public void removeServer(String domainName, String serverName) {
00220         if (domainName.equals(name)) {
00221             // Server thet belongs to the current management domain
00222             managedServersToConnections.remove(serverName);
00223             JMXConnector connector = (JMXConnector) managedServersToConnectors.remove(serverName);
00224             if (connector != null) {
00225             try {
00226                 connector.close();
00227             } catch (IOException e1) {
00228             }
00229             }
00230             managedServersToUrls.remove(serverName);
00231             removeServer(J2eeObjectName.J2EEServer(domainName, serverName).toString());
00232         } else {
00233             if (logger.isLoggable(BasicLevel.WARN)) {
00234                 logger.log(BasicLevel.WARN, "The server named " + serverName + " was not started in the management domain "
00235                         + name + ", but in management domain " + domainName);
00236             }
00237         }
00238     }
00245     private boolean createConnection(String serverName, String connectorServerURL) {
00246         boolean created = false;
00247         MBeanServerConnection connection;
00248         // create a connector client for the connector server at the given url
00249         JMXConnector connector = null;
00250         try {
00251             JMXServiceURL url = new JMXServiceURL(connectorServerURL);
00252             connector = JMXConnectorFactory.newJMXConnector(url, null);
00253             connector.connect(null);
00254             connection = connector.getMBeanServerConnection();
00255         } catch (MalformedURLException e) {
00256             // there is no provider for the protocol in url
00257             connection = null;
00258         } catch (IOException e) {
00259             // connector client or connection cannot be made because of a communication problem.
00260             connection = null;
00261         } catch (java.lang.SecurityException e) {
00262             // connection cannot be made for security reasons
00263             connection = null;
00264         }
00265         if (connection != null) {
00266             // OK, connection was established to the connector server.
00267             testConnection(connection, serverName);
00268             if (!managedServersToConnections.containsKey(serverName)) {
00269                 // add new connection and connector to managed data structure
00270                 managedServersToConnections.put(serverName, connection);
00271                 managedServersToConnectors.put(serverName, connector);
00272                 created = true;
00273                 logger.log(BasicLevel.INFO, "First MBeanServerConnection created for connecting to server " + serverName + " using URL: " + connectorServerURL);
00274             } else {
00275                 // A connection already exists.
00276                 // We have the following possibilities:
00277                 // - ignore this connection (close it), so continue to use the old one
00278                 // - replace the old one by the new one (close the old)
00279                 // - keep several connections
00280                 //
00281                 // Currently we keep only one connection, the new one (second approach)
00282                 JMXConnector oldConnector = (JMXConnector) managedServersToConnectors.get(serverName);
00283                 try {
00284                     oldConnector.close();
00285                 } catch (IOException e1) {
00286                 }
00287                 managedServersToConnectors.put(serverName, connector);
00288                 managedServersToConnections.put(serverName, connection);
00289                 created = true;
00290                 logger.log(BasicLevel.INFO, "New MBeanServerConnection created for connecting to server " + serverName + " using URL: " + connectorServerURL + "(replaces the old connection)");
00291             }
00292         } else {
00293             if (connector != null) {
00294                 try {
00295                     connector.close();
00296                 } catch (IOException e1) {
00297                 }
00298             }
00299         }
00300         return created;
00301     }
00302 
00303     private boolean testConnection(MBeanServerConnection connection, String serverName) {
00304         boolean ok = false;
00305         try {
00306             Integer nb = connection.getMBeanCount();
00307             ok = true;
00308         } catch(IOException ioe) {
00309             ioe.printStackTrace();
00310         }
00311         return ok;
00312     }
00313 
00319     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
00320         if (name == null) {
00321             return J2eeObjectName.J2EEDomain(this.name);
00322         }
00323         String myName = getObjectName().toString();
00324         String argName = name.toString();
00325         if (myName.equals(argName)) {
00326             // OK
00327             mbeanServer = server;
00328             return name;
00329         } else {
00330             if (logger.isLoggable(BasicLevel.WARN)) {
00331                 logger.log(BasicLevel.WARN, "Trying to register J2EEDomain MBean with the following name: "
00332                         + argName + " when the expected name is: " + myName);
00333             }
00334             return ObjectName.getInstance(this.name);
00335         }
00336     }
00337 
00343     public void postRegister(Boolean registrationDone) {
00344         if (registrationDone.booleanValue()) {
00345             try {
00346                 // MBeanServerDelegate is the MBean which sends registration/unregistration notifications
00347                 ObjectName delegate = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
00348                 mbeanServer.addNotificationListener(delegate, this, null, null);
00349             } catch (JMException me) {
00350                 if (logger.isLoggable(BasicLevel.DEBUG)) {
00351                     logger.log(BasicLevel.DEBUG, "J2EEDomain MBean could not be added as notification listener for MBeanServerNotifications " +
00352                             "related to the REGISTRATION or UNREGISTRETION of JOnAS management MBeans");
00353                 }
00354             }
00355         }
00356     }
00357 
00361     public void preDeregister() throws Exception {
00362         // TODO Auto-generated method stub
00363 
00364     }
00365 
00369     public void postDeregister() {
00370         // TODO Auto-generated method stub
00371 
00372     }
00373 
00380     public void handleNotification(Notification notification, Object handback) {
00381         if (notification instanceof MBeanServerNotification) {
00382             String type = notification.getType();
00383             // ObjectName of the MBean that caused the notification
00384             ObjectName registeredOn = ((MBeanServerNotification) notification).getMBeanName();
00385             String name = registeredOn.getKeyProperty("name");
00386             // The names below are defined in JonasObjectName class
00387             if ((name != null) && (name.equals("discoveryEnroller") || name.equals("discoveryClient"))) {
00388                 if (type.equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
00389                     try {
00390                         // register myself as listener of notifications emitted by the Enroller or DiscoveryClient MBean
00391                         mbeanServer.addNotificationListener(registeredOn, this, null, null);
00392                         if (logger.isLoggable(BasicLevel.DEBUG)) {
00393                             logger.log(BasicLevel.DEBUG, "J2EEDomain (this) registered as listener to notifs emitted by " + registeredOn);
00394                         }
00395                     } catch (InstanceNotFoundException e) {
00396                         // TODO Auto-generated catch block
00397                         e.printStackTrace();
00398                     }
00399                 }
00400                 if (type.equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
00401                     try {
00402                         // unregister myself as listener of notifications emitted by the Enroller or DiscoveryClient MBean
00403                         mbeanServer.removeNotificationListener(registeredOn, this);
00404                         if (logger.isLoggable(BasicLevel.DEBUG)) {
00405                             logger.log(BasicLevel.DEBUG, "J2EEDomain (this) removed listener for notifs emitted by " + registeredOn);
00406                         }
00407                     } catch (InstanceNotFoundException e) {
00408                         // TODO Auto-generated catch block
00409                         e.printStackTrace();
00410                     } catch (ListenerNotFoundException e) {
00411                         // TODO Auto-generated catch block
00412                         e.printStackTrace();
00413                     }
00414                 }
00415             }
00416         } else {
00417             // Tread discovery notification
00418             String type = notification.getType();
00419             String message = notification.getMessage();
00420             DiscEvent userData = (DiscEvent) notification.getUserData();
00421             String source = ((ObjectName) notification.getSource()).toString();
00422             String state = userData.getState();
00423             if (logger.isLoggable(BasicLevel.DEBUG)) {
00424                 logger.log(BasicLevel.DEBUG, "Treat notification:");
00425                 logger.log(BasicLevel.DEBUG, "- source: " + source);
00426                 logger.log(BasicLevel.DEBUG, "- type: " + type);
00427                 logger.log(BasicLevel.DEBUG, "- data: ");
00428                 logger.log(BasicLevel.DEBUG, "--- state: " + state);
00429                 logger.log(BasicLevel.DEBUG, "--- serverName : " + userData.getServerName());
00430                 logger.log(BasicLevel.DEBUG, "--- domainName : " + userData.getDomainName());
00431                 if (userData.getConnectorURL() != null) {
00432                     String[] urls = userData.getConnectorURL();
00433                     for (int i = 0; i < urls.length; i++) {
00434                         logger.log(BasicLevel.DEBUG, "--- urls : " + urls[i]);
00435                     }
00436                 }
00437                 logger.log(BasicLevel.DEBUG, "");
00438             }
00439             if (state.equals(DiscEvent.RUNNING)) {
00440                 String[] urls = userData.getConnectorURL();
00441                 for (int i = 0; i < urls.length; i++) {
00442                     addServer(userData.getDomainName(), userData.getServerName(), urls[i]);
00443                 }
00444             } else if (state.equals(DiscEvent.STOPPING)) {
00445                 removeServer(userData.getDomainName(), userData.getServerName());
00446             }
00447         }
00448     }
00449 }

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