1 25 26 package org.objectweb.jonas.jmx; 27 28 import java.util.HashSet ; 29 import java.util.Properties ; 30 import java.util.Set ; 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.naming.NameClassPair ; 35 import javax.naming.NamingEnumeration ; 36 import javax.rmi.PortableRemoteObject ; 37 38 import org.objectweb.jonas.common.Log; 40 41 import org.objectweb.util.monolog.api.Logger; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 48 public class ConnectorFactory { 49 50 private static String currentRMIConnectorName = null; 51 private static RMIConnector rmic = null; private static Context context = null; 53 private static Logger logger = Log.getLogger("objectweb.org.jonas.jmx"); 54 55 59 public static Context getContext() throws javax.naming.NamingException { 60 if (context == null) { 61 context = new InitialContext (); 62 } 63 return context; 64 } 65 66 72 public static String getCurrentRMIConnectorName() { 73 try { 74 if (currentRMIConnectorName == null) { HashSet connectorNames = (HashSet )getRMIConnectorsNames(); 76 if (! connectorNames.isEmpty()) { 77 String firstName = (String )connectorNames.toArray()[0]; 79 setCurrentRMIConnectorName(firstName); 80 } } 82 return currentRMIConnectorName; 83 } catch (javax.naming.NamingException ne) { 84 return null; 85 } 86 } 87 88 91 public static void setCurrentRMIConnectorName(String name) throws NamingException { 92 currentRMIConnectorName = name; 93 lookupRMIConnector(); 95 } 96 97 100 public static void resetCurrentRMIConnectorName() { 101 currentRMIConnectorName = null; 102 } 103 104 107 public static Set getRMIConnectorsNames() throws javax.naming.NamingException { 108 try { 109 HashSet res = new HashSet (); 110 Context ctx = getContext(); 111 for (NamingEnumeration e = ctx.list(""); e.hasMoreElements(); ) { 113 String name = ((NameClassPair )e.nextElement()).getName(); 114 if (name.startsWith("RMIConnector")) 116 res.add(name); 117 } 118 return res; 119 } catch (javax.naming.NamingException ne) { 120 throw new javax.naming.NamingException ("Cannot enumerates the names bound in the named context: '" 121 +getJonasNamingServiceURL() 122 +"' (registry probably not launched)."); 123 } 124 } 125 126 130 public static String getJonasNamingServiceURL() { 131 try { 132 return (String )getContext().getEnvironment().get(Context.PROVIDER_URL); 133 } catch (javax.naming.NamingException ne) { 134 return ne.getMessage(); 135 } 136 } 137 138 142 public static void setNamingEnvCtx(Properties env) throws javax.naming.NamingException { 143 context = new InitialContext (env); 144 rmic = null; 145 currentRMIConnectorName = null; 146 } 147 148 151 public static void setJonasNamingServiceURL(String url) throws javax.naming.NamingException { 152 153 Properties p = new Properties (); 155 156 Context ctx = getContext(); 158 p.put(Context.INITIAL_CONTEXT_FACTORY, ctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY)); 160 p.put(Context.URL_PKG_PREFIXES, ctx.getEnvironment().get(Context.URL_PKG_PREFIXES)); 161 p.put(Context.PROVIDER_URL, url); 163 164 try { 165 context = new InitialContext (p); 166 } catch (NamingException e) { 167 if (logger.isLoggable(BasicLevel.DEBUG)) { 168 logger.log(BasicLevel.DEBUG, "Can' create context : " + e); 169 logger.log(BasicLevel.DEBUG, "Environment used :"); 170 logger.log(BasicLevel.DEBUG, Context.INITIAL_CONTEXT_FACTORY + " = " + p.get(Context.INITIAL_CONTEXT_FACTORY)); 171 logger.log(BasicLevel.DEBUG, Context.URL_PKG_PREFIXES + " = " + p.get(Context.URL_PKG_PREFIXES)); 172 logger.log(BasicLevel.DEBUG, Context.PROVIDER_URL + " = " + p.get(Context.PROVIDER_URL)); 173 } 174 throw e; 175 } 176 177 rmic = null; 178 currentRMIConnectorName = null; 179 180 } 181 182 185 public static RMIConnector getRMIConnector() throws NamingException { 186 if (rmic == null) { 187 lookupRMIConnector(); 188 } 189 return rmic; 190 } 191 192 195 public static void lookupRMIConnector() throws NamingException { 196 String _currentRMIConnectorName = getCurrentRMIConnectorName(); 197 if (_currentRMIConnectorName != null) { 198 rmic = (RMIConnector)PortableRemoteObject.narrow(getContext().lookup(_currentRMIConnectorName), RMIConnector.class); 199 } 200 } 201 } 202 | Popular Tags |