ConnectorFactory.java

00001 
00026 package org.objectweb.jonas.jmx;
00027 
00028 import java.util.HashSet;
00029 import java.util.Properties;
00030 import java.util.Set;
00031 import javax.naming.Context;
00032 import javax.naming.InitialContext;
00033 import javax.naming.NamingException;
00034 import javax.naming.NameClassPair;
00035 import javax.naming.NamingEnumeration;
00036 import javax.rmi.PortableRemoteObject;
00037 
00038 // JOnAS Log
00039 import org.objectweb.jonas.common.Log;
00040 
00041 // Monolog
00042 import org.objectweb.util.monolog.api.Logger;
00043 import org.objectweb.util.monolog.api.BasicLevel;
00048 public class ConnectorFactory {
00049 
00050     private static String currentRMIConnectorName = null;
00051     private static RMIConnector rmic = null; // current RMIConnector
00052     private static Context context = null;
00053     private static Logger logger = Log.getLogger("objectweb.org.jonas.jmx");
00054 
00059     public static Context getContext() throws javax.naming.NamingException {
00060         if (context == null) {
00061             context = new InitialContext();
00062         }
00063         return context;
00064     }
00065 
00072     public static String getCurrentRMIConnectorName() {
00073         try {
00074             if (currentRMIConnectorName == null) { // may be not set yet
00075                 HashSet connectorNames = (HashSet)getRMIConnectorsNames();
00076                 if (! connectorNames.isEmpty()) {
00077                     // Pick up the first connector name in the list
00078                     String firstName = (String)connectorNames.toArray()[0];
00079                     setCurrentRMIConnectorName(firstName);
00080                 } // else, currentRMIConnector rests null
00081             }
00082             return currentRMIConnectorName;
00083         } catch (javax.naming.NamingException ne) {
00084             return null;
00085         }
00086     }
00087 
00091     public static void setCurrentRMIConnectorName(String name) throws NamingException {
00092         currentRMIConnectorName = name;
00093         // The rmi connector name may have changed, so we lookup for the corresponding rmi connector
00094         lookupRMIConnector();
00095     }
00096 
00100     public static void resetCurrentRMIConnectorName() {
00101         currentRMIConnectorName = null;
00102     }
00103 
00107     public static Set getRMIConnectorsNames() throws javax.naming.NamingException {
00108         try {
00109             HashSet res = new HashSet();
00110             Context ctx = getContext();
00111             // looking for all object registered in this registry
00112             for (NamingEnumeration e = ctx.list(""); e.hasMoreElements(); ) {
00113                 String name = ((NameClassPair)e.nextElement()).getName();
00114                 // we select only rmi connectors (start with 'RMIConnector')
00115                 if (name.startsWith("RMIConnector"))
00116                     res.add(name);
00117             }
00118             return res;
00119         } catch (javax.naming.NamingException ne) {
00120             throw new javax.naming.NamingException("Cannot enumerates the names bound in the named context: '"
00121                                                    +getJonasNamingServiceURL()
00122                                                    +"' (registry probably not launched).");
00123         }
00124     }
00125 
00130     public static String getJonasNamingServiceURL() {
00131         try {
00132             return (String)getContext().getEnvironment().get(Context.PROVIDER_URL);
00133         } catch (javax.naming.NamingException ne) {
00134             return ne.getMessage();
00135         }
00136     }
00137 
00142     public static void setNamingEnvCtx(Properties env) throws javax.naming.NamingException {
00143         context = new InitialContext(env);
00144         rmic = null;
00145         currentRMIConnectorName = null;
00146     }
00147 
00151     public static void setJonasNamingServiceURL(String url) throws javax.naming.NamingException {
00152 
00153         // Properties for the new naming context
00154         Properties p = new Properties();
00155 
00156         // Current context
00157         Context ctx = getContext();
00158         // Re-use the same values for the INITIAL_CONTEXT_FACTORY and URL_PKG_PREFIXES
00159         p.put(Context.INITIAL_CONTEXT_FACTORY, ctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY));
00160         p.put(Context.URL_PKG_PREFIXES, ctx.getEnvironment().get(Context.URL_PKG_PREFIXES));
00161         // Use the url parameter for the PROVIDER_URL property
00162         p.put(Context.PROVIDER_URL, url);
00163 
00164         try {
00165             context = new InitialContext(p);
00166         } catch (NamingException e) {
00167             logger.log(BasicLevel.DEBUG, "Can' create context : " + e);
00168             logger.log(BasicLevel.DEBUG, "Environment used :");
00169             logger.log(BasicLevel.DEBUG, Context.INITIAL_CONTEXT_FACTORY + " = " + p.get(Context.INITIAL_CONTEXT_FACTORY));
00170             logger.log(BasicLevel.DEBUG, Context.URL_PKG_PREFIXES + " = " + p.get(Context.URL_PKG_PREFIXES));
00171             logger.log(BasicLevel.DEBUG, Context.PROVIDER_URL + " = " + p.get(Context.PROVIDER_URL));
00172             throw e;
00173         }
00174 
00175         rmic = null;
00176         currentRMIConnectorName = null;
00177 
00178     }
00179 
00183     public static RMIConnector getRMIConnector() throws NamingException {
00184         if (rmic == null) {
00185             lookupRMIConnector();
00186         }
00187         return rmic;
00188     }
00189 
00193     public static void lookupRMIConnector() throws NamingException {
00194         String _currentRMIConnectorName = getCurrentRMIConnectorName();
00195         if (_currentRMIConnectorName != null) {
00196             rmic = (RMIConnector)PortableRemoteObject.narrow(getContext().lookup(_currentRMIConnectorName), RMIConnector.class);
00197         }
00198     }
00199 }

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