1 23 package fr.dyade.aaa.jndi2.haclient; 24 25 import javax.naming.spi.*; 26 import javax.naming.*; 27 import java.util.*; 28 29 import fr.dyade.aaa.jndi2.client.*; 30 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.util.monolog.api.Logger; 33 34 public class HANamingContextFactory implements InitialContextFactory { 35 36 43 public Context getInitialContext(Hashtable env) 44 throws NamingException { 45 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 46 Trace.logger.log( 47 BasicLevel.DEBUG, 48 "NamingContextFactory.getInitialContext(" + env + ')'); 49 return new fr.dyade.aaa.jndi2.client.NamingContextImpl( 50 getNamingConnection(env), 51 new CompositeName()); 52 } 53 54 private static String getEnvProperty(Hashtable env, 55 String propName) { 56 String value = null; 57 if (env != null) { 58 value = (String ) env.get(propName); 59 } 60 if (value == null) { 61 value = System.getProperty(propName, null); 62 } 63 return value; 64 } 65 66 public static NamingConnection getNamingConnection( 67 Hashtable env) 68 throws NamingException { 69 try { 70 NamingConnection namingConnection; 71 72 String url = getEnvProperty(env, "scn.naming.provider.url"); 74 if (url == null) { 75 url = getEnvProperty(env, Context.PROVIDER_URL); 76 } 77 78 if (url != null) { 79 StringTokenizer tokenizer = new StringTokenizer(url, "/:,"); 80 if (! tokenizer.hasMoreElements()) 81 throw new NamingException("URL not valid:" + url); 82 String protocol = tokenizer.nextToken(); 83 if (protocol.equals("hascn")) { 84 HANamingConnection haNamingConnection = 85 new HANamingConnection(); 86 while (tokenizer.hasMoreElements()) { 87 haNamingConnection.addServerAddress( 88 tokenizer.nextToken(), 89 Integer.parseInt(tokenizer.nextToken())); 90 } 91 namingConnection = haNamingConnection; 92 } else { 93 throw new NamingException("Unknown protocol:" + protocol); 94 } 95 } else { 96 throw new NamingException( 97 "URL " + Context.PROVIDER_URL + " not defined"); 98 } 99 return namingConnection; 100 } catch (Exception e) { 101 NamingException nx = 102 new NamingException( 103 "exception creating NamingContext: " + 104 e.toString()); 105 nx.setRootCause(e); 106 throw nx; 107 } 108 } 109 } 110 | Popular Tags |