1 24 package fr.dyade.aaa.jndi2.client; 25 26 import javax.naming.spi.*; 27 import javax.naming.*; 28 import java.util.*; 29 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.util.monolog.api.Logger; 32 33 public class NamingContextFactory implements InitialContextFactory { 34 35 39 public final static String JAVA_PORT_PROPERTY = "java.naming.factory.port"; 40 41 45 public final static String JAVA_HOST_PROPERTY = "java.naming.factory.host"; 46 47 51 public final static String SCN_PORT_PROPERTY = "scn.naming.factory.port"; 52 53 57 public final static String SCN_HOST_PROPERTY = "scn.naming.factory.host"; 58 59 63 public final static String SCN_PROVIDER_URL = "scn.naming.provider.url"; 64 65 72 public Context getInitialContext(Hashtable env) 73 throws NamingException { 74 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 75 Trace.logger.log( 76 BasicLevel.DEBUG, 77 "NamingContextFactory.getInitialContext(" + env + ')'); 78 return new fr.dyade.aaa.jndi2.client.NamingContextImpl( 79 getNamingConnection(env), 80 new CompositeName()); 81 } 82 83 public static NamingConnection getNamingConnection( 84 Hashtable env) 85 throws NamingException { 86 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 87 Trace.logger.log( 88 BasicLevel.DEBUG, 89 "NamingContextFactory.getNamingConnection(" + env + ')'); 90 try { 91 NamingConnection namingConnection; 92 String url = (String ) env.get(SCN_PROVIDER_URL); 94 if (url == null) url = (String ) env.get(Context.PROVIDER_URL); 95 if (url != null && !url.equals("")) { 96 StringTokenizer tokenizer = new StringTokenizer(url, "/:,"); 97 if (! tokenizer.hasMoreElements()) 98 throw new NamingException("URL not valid:" + url); 99 String protocol = tokenizer.nextToken(); 100 if (protocol.equals("scn")) { 101 String host = tokenizer.nextToken(); 102 String portStr = tokenizer.nextToken(); 103 int port = Integer.parseInt(portStr); 104 namingConnection = new SimpleNamingConnection(host, port, env); 105 } else { 106 throw new NamingException("Unknown protocol:" + protocol); 107 } 108 } else { 109 String host = (String ) env.get(SCN_HOST_PROPERTY); 110 if (host == null) host = (String ) System.getProperty(SCN_HOST_PROPERTY); 111 if (host == null) host = (String ) env.get(JAVA_HOST_PROPERTY); 112 if (host == null) host = (String ) System.getProperty(JAVA_HOST_PROPERTY); 113 if (host == null) host = "localhost"; 114 115 String portStr = (String ) env.get(SCN_PORT_PROPERTY); 116 if (portStr == null) portStr = (String ) System.getProperty(SCN_PORT_PROPERTY); 117 if (portStr == null) portStr = (String ) env.get(JAVA_PORT_PROPERTY); 118 if (portStr == null) portStr = (String ) System.getProperty(JAVA_PORT_PROPERTY); 119 if (portStr == null) portStr = "16400"; 120 121 int port = Integer.parseInt(portStr); 122 namingConnection = new SimpleNamingConnection( 123 host, port, env); 124 } 125 return namingConnection; 126 } catch (NumberFormatException e) { 127 NamingException nx = new NamingException(); 128 nx.setRootCause(e); 129 throw nx; 130 } catch (Exception e) { 131 NamingException nx = new NamingException(); 132 nx.setRootCause(e); 133 throw nx; 134 } 135 } 136 } 137 | Popular Tags |