1 45 package org.openejb.client; 46 47 import org.openejb.loader.OpenEJBInstance; 48 import org.openejb.loader.SystemInstance; 49 50 import javax.naming.Context ; 51 import javax.naming.NamingException ; 52 import javax.naming.spi.InitialContextFactory ; 53 import java.util.Hashtable ; 54 import java.util.Properties ; 55 56 62 public class LocalInitialContextFactory implements javax.naming.spi.InitialContextFactory { 63 64 static Context intraVmContext; 65 private static OpenEJBInstance openejb; 66 67 public Context getInitialContext(Hashtable env) throws javax.naming.NamingException { 68 if (intraVmContext == null) { 69 try { 70 Properties properties = new Properties (); 71 properties.putAll(env); 72 init(properties); 73 } catch (Exception e) { 74 throw (NamingException ) new NamingException ("Attempted to load OpenEJB. " + e.getMessage()).initCause(e); 75 } 76 intraVmContext = getIntraVmContext(env); 77 } 78 return intraVmContext; 79 } 80 81 public void init(Properties properties) throws Exception { 82 if (openejb != null) return; 83 SystemInstance.init(properties); 84 openejb = new OpenEJBInstance(); 85 if (openejb.isInitialized()) return; 86 openejb.init(properties); 87 } 88 89 private Context getIntraVmContext(Hashtable env) throws javax.naming.NamingException { 90 Context context = null; 91 try { 92 InitialContextFactory factory = null; 93 ClassLoader cl = SystemInstance.get().getClassLoader(); 94 Class ivmFactoryClass = Class.forName("org.openejb.core.ivm.naming.InitContextFactory", true, cl); 95 96 factory = (InitialContextFactory ) ivmFactoryClass.newInstance(); 97 context = factory.getInitialContext(env); 98 } catch (Exception e) { 99 throw new javax.naming.NamingException ("Cannot instantiate an IntraVM InitialContext. Exception: " 100 + e.getClass().getName() + " " + e.getMessage()); 101 } 102 103 return context; 104 } 105 } 106 107 | Popular Tags |