1 25 package org.objectweb.carol.jndi.spi; 26 27 import java.lang.reflect.Constructor ; 28 import java.util.HashMap ; 29 import java.util.Hashtable ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.naming.spi.InitialContextFactory ; 35 36 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 37 38 45 public abstract class AbsInitialContextFactory implements InitialContextFactory { 46 47 48 51 private static HashMap contexts = new HashMap (); 52 53 54 57 protected abstract String getReferencingFactory(); 58 59 62 protected abstract Class getWrapperClass(); 63 64 73 public Context getInitialContext(Hashtable environment) throws NamingException { 74 75 Context ctx = null; 77 String key = getKey(environment); 78 if (key != null) { 79 ctx = (Context ) contexts.get(key); 80 } 81 82 if (ctx != null) { 84 return ctx; 85 } 86 87 init(); 89 90 environment.put(Context.INITIAL_CONTEXT_FACTORY, getReferencingFactory()); 92 93 Class clazz = getWrapperClass(); 95 96 Constructor ctr = null; 98 try { 99 ctr = clazz.getConstructor(getClassConstructor()); 100 } catch (Exception e) { 101 throw NamingExceptionHelper.create("Cannot find the constructor with Context class as argument in class '" + clazz.getName() + "' : " + e.getMessage(), e); 102 } 103 104 addExtraConfInEnvironment(environment); 106 107 try { 109 ctx = (Context ) ctr.newInstance(getClassArgs(environment)); 110 } catch (Exception e) { 111 throw NamingExceptionHelper.create("Cannot build an instance of the class '" + clazz.getName() + "' : " + e.getMessage(), e); 112 } 113 114 if (key != null) { 116 contexts.put(key, ctx); 117 } 118 119 return ctx; 121 } 122 123 127 protected String getKey(Hashtable environment) { 128 String key = null; 129 if (environment != null) { 130 key = (String ) environment.get(Context.PROVIDER_URL); 131 } 132 return key; 133 } 134 135 141 protected Object [] getClassArgs(Hashtable environment) throws NamingException { 142 return new Object [] {new InitialContext (environment)}; 143 } 144 145 149 protected Class [] getClassConstructor() { 150 return new Class [] {Context .class}; 151 } 152 153 154 159 protected void addExtraConfInEnvironment(Hashtable environment) { 160 161 } 162 163 168 protected void init() throws NamingException { 169 } 170 171 } 172 | Popular Tags |