1 45 package org.exolab.jms.jndi; 46 47 import java.io.IOException ; 48 import java.rmi.NotBoundException ; 49 import java.rmi.RemoteException ; 50 import java.util.Hashtable ; 51 import javax.naming.CommunicationException ; 52 import javax.naming.ConfigurationException ; 53 import javax.naming.Context ; 54 import javax.naming.NameParser ; 55 import javax.naming.NamingException ; 56 import javax.naming.ServiceUnavailableException ; 57 58 import org.codehaus.spice.jndikit.Namespace; 59 import org.codehaus.spice.jndikit.NamingProvider; 60 import org.codehaus.spice.jndikit.RemoteContext; 61 import org.codehaus.spice.jndikit.StandardNamespace; 62 63 import org.exolab.jms.client.net.SharedORB; 64 import org.exolab.jms.net.orb.ORB; 65 import org.exolab.jms.net.registry.Registry; 66 import org.exolab.jms.net.uri.URI; 67 68 69 76 public class InitialContextFactory 77 implements javax.naming.spi.InitialContextFactory { 78 79 82 private static final String HTTP_SCHEME = "http"; 83 84 87 private static final String HTTPS_SCHEME = "https"; 88 89 92 private static final String SERVLET = "/openjms-tunnel/tunnel"; 93 94 97 private static final String EMBEDDED_SCHEME = "embedded"; 98 99 102 private static final String VM_SCHEME = "vm"; 103 104 107 private static final String VM_PATH = "openjms"; 108 109 110 119 public Context getInitialContext(Hashtable environment) 120 throws NamingException { 121 if (environment == null) { 122 throw new ConfigurationException ( 123 "Cannot connect to JNDI provider - environment not set"); 124 } 125 String url = (String ) environment.get(Context.PROVIDER_URL); 126 if (url == null) { 127 throw new ConfigurationException ("Cannot connect to JNDI provider - " 128 + Context.PROVIDER_URL 129 + " not set "); 130 } 131 132 Hashtable properties = new Hashtable (environment); 134 properties.put(Context.PROVIDER_URL, getProviderURI(url)); 135 map(properties, Context.PROVIDER_URL, ORB.PROVIDER_URI); 136 map(properties, Context.SECURITY_PRINCIPAL, ORB.SECURITY_PRINCIPAL); 137 map(properties, Context.SECURITY_CREDENTIALS, ORB.SECURITY_CREDENTIALS); 138 139 ORB orb; 140 Registry registry; 141 try { 142 orb = SharedORB.getInstance(); 143 registry = orb.getRegistry(properties); 144 } catch (RemoteException exception) { 145 NamingException error = new CommunicationException ( 146 "Failed to get registry service for URL: " + url); 147 error.setRootCause(exception); 148 throw error; 149 } 150 151 NamingProvider provider; 152 try { 153 provider = (NamingProvider) registry.lookup("jndi"); 154 } catch (NotBoundException exception) { 155 throw new ServiceUnavailableException ( 156 "JNDI service is not bound in the registry for URL: " 157 + url); 158 } catch (RemoteException exception) { 159 NamingException error = new CommunicationException ( 160 "Failed to lookup JNDI provider for URL: " + url); 161 error.setRootCause(exception); 162 throw error; 163 } 164 165 NameParser parser; 166 try { 167 parser = provider.getNameParser(); 168 } catch (NamingException exception) { 169 throw exception; 170 } catch (Exception exception) { 171 NamingException error = new ServiceUnavailableException ( 172 exception.getMessage()); 173 error.setRootCause(exception); 174 throw error; 175 } 176 Namespace namespace = new StandardNamespace(parser); 177 properties.put(RemoteContext.NAMING_PROVIDER, provider); 178 properties.put(RemoteContext.NAMESPACE, namespace); 179 return new RemoteContext(properties, parser.parse("")); 180 } 181 182 190 private String getProviderURI(String uri) 191 throws ConfigurationException { 192 URI parsed; 193 try { 194 parsed = new URI(uri); 195 String scheme = parsed.getScheme(); 196 if (scheme.equals(HTTP_SCHEME) || scheme.equals(HTTPS_SCHEME)) { 197 String path = parsed.getPath(); 198 if (path == null || path.length() == 0) { 199 parsed.setPath(SERVLET); 200 } 201 } else if (scheme.equals(EMBEDDED_SCHEME)) { 202 parsed.setScheme(VM_SCHEME); 203 parsed.setPath(VM_PATH); 204 } 205 } catch (IOException exception) { 206 throw new ConfigurationException (exception.getMessage()); 207 } 208 return parsed.toString(); 209 } 210 211 218 private void map(Hashtable properties, String from, String to) { 219 String value = (String ) properties.get(from); 220 if (value != null) { 221 properties.put(to, value); 222 } 223 } 224 225 } 226 | Popular Tags |