1 25 26 package org.objectweb.easybeans.component.smartclient.spi; 27 28 import java.util.Hashtable ; 29 import java.util.StringTokenizer ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 import javax.naming.spi.InitialContextFactory ; 37 38 import org.objectweb.easybeans.component.smartclient.client.AskingClassLoader; 39 40 46 public class SmartContextFactory implements InitialContextFactory { 47 48 51 private static Logger logger = Logger.getLogger(SmartContextFactory.class.getName()); 52 53 56 private static final String DEFAULT_URL = "smart://localhost:2503"; 57 58 61 private static final String CAROL_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"; 62 63 66 public static final String EASYBEANS_DELEGATING_FACTORY = "easybeans.smart.delegate.factory"; 67 68 71 public static final String EASYBEANS_FACTORY = "easybeans.rpc.rmi.factory"; 72 73 77 private static AskingClassLoader classLoader = null; 78 79 82 private static String providerURL = null; 83 84 88 public SmartContextFactory() { 89 System.setProperty(EASYBEANS_FACTORY, System.getProperty(EASYBEANS_DELEGATING_FACTORY, this.getClass().getName())); 91 System.setProperty("javax.rmi.CORBA.PortableRemoteObjectClass", ProDelegate.class.getName()); 92 } 93 94 101 @SuppressWarnings ("unchecked") 102 public Context getInitialContext(final Hashtable environment) 103 throws NamingException { 104 105 if (classLoader == null) { 106 try { 107 String currentProviderURL = (String ) environment.get(Context.PROVIDER_URL); 109 if (currentProviderURL == null) { 110 logger.log(Level.WARNING, "No PROVIDER_URL setting found, use the default URL '" + DEFAULT_URL + "'."); 111 currentProviderURL = DEFAULT_URL; 112 } 113 114 logger.log(Level.INFO, "Initializing Smart Factory with remote URL '" + currentProviderURL + "'."); 115 116 String host = getHostOfUrl(currentProviderURL); 118 119 int portNumber = getPortOfUrl(currentProviderURL); 121 122 classLoader = new AskingClassLoader(host, portNumber); 124 125 126 providerURL = classLoader.getProviderURL(); 127 logger.log(Level.INFO, "Got remote PROVIDER_URL '" + providerURL + "'."); 128 129 ProDelegate.setClassLoader(classLoader); 131 132 } catch (Exception e) { 133 NamingException ne = new NamingException ("Cannot get a remote ClassLoader"); 134 ne.initCause(e); 135 throw ne; 136 } 137 } 138 139 ClassLoader old = Thread.currentThread().getContextClassLoader(); 140 Thread.currentThread().setContextClassLoader(classLoader); 141 try { 142 environment.put(Context.INITIAL_CONTEXT_FACTORY, CAROL_FACTORY); 144 environment.put(Context.PROVIDER_URL, providerURL); 145 146 return new SmartContext(new InitialContext (environment), classLoader); 148 } finally { 149 Thread.currentThread().setContextClassLoader(old); 150 } 151 152 } 153 154 155 162 public int getPortOfUrl(final String url) throws NamingException { 163 int portNumber = 0; 164 try { 165 StringTokenizer st = new StringTokenizer (url, ":"); 166 st.nextToken(); 167 st.nextToken(); 168 if (st.hasMoreTokens()) { 169 StringTokenizer lastst = new StringTokenizer (st.nextToken(), "/"); 170 String pts = lastst.nextToken().trim(); 171 int i = pts.indexOf(','); 172 if (i > 0) { 173 pts = pts.substring(0, i); 174 } 175 portNumber = new Integer (pts).intValue(); 176 } 177 return portNumber; 178 } catch (Exception e) { 179 throw new NamingException ("Invalid URL '" + url + "'. It should be on the format <protocol>://<hostname>:<port>"); 181 } 182 } 183 184 190 private String getHostOfUrl(final String url) throws NamingException { 191 String host = null; 192 try { 194 String [] tmpSplitStr = url.split(":"); 196 197 String tmpHost = tmpSplitStr[1]; 200 201 String [] tmpSplitHost = tmpHost.split("/"); 203 204 host = tmpSplitHost[tmpSplitHost.length - 1]; 206 } catch (Exception e) { 207 throw new NamingException ("Invalid URL '" + url + "'. It should be on the format <protocol>://<hostname>:<port>"); 209 } 210 return host; 211 } 212 } 213 | Popular Tags |