1 17 package org.apache.geronimo.gjndi; 18 19 import org.apache.geronimo.gbean.GBeanData; 20 import org.apache.geronimo.kernel.Kernel; 21 import org.apache.geronimo.kernel.KernelFactory; 22 import org.apache.geronimo.kernel.config.ConfigurationData; 23 import org.apache.geronimo.kernel.config.ConfigurationUtil; 24 import org.apache.geronimo.kernel.config.EditableConfigurationManager; 25 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager; 26 import org.apache.geronimo.kernel.repository.Artifact; 27 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 28 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 29 import org.apache.geronimo.naming.enc.EnterpriseNamingContext; 30 import org.apache.geronimo.naming.java.RootContext; 31 import org.apache.xbean.naming.context.ImmutableContext; 32 import org.apache.xbean.naming.global.GlobalContextManager; 33 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 import javax.naming.NameNotFoundException ; 38 import java.util.HashMap ; 39 import java.util.Hashtable ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 46 public class JavaCompGBeanTest extends AbstractContextTest { 47 private Kernel kernel; 48 private Hashtable contextEnv; 49 50 public void testLookupEnv() throws Exception { 51 Map javaCompBindings = new HashMap (); 52 javaCompBindings.put("foo", "bar"); 53 54 RootContext.setComponentContext(new ImmutableContext(javaCompBindings)); 56 try { 57 new InitialContext (contextEnv).lookup("java:comp/env"); 58 fail("Expected NameNotFoundException"); 59 } catch (NameNotFoundException expected) { 60 } 62 63 RootContext.setComponentContext(EnterpriseNamingContext.createEnterpriseNamingContext(javaCompBindings, null, null, null)); 65 new InitialContext (contextEnv).lookup("java:comp/env"); 66 } 67 68 protected Map getNestedBindings(Map globalBindings, String nestedPath) { 69 HashMap nestedBindings = new HashMap (); 70 for (Iterator iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) { 71 Map.Entry entry = (Map.Entry ) iterator.next(); 72 String globalName = (String ) entry.getKey(); 73 Object value = entry.getValue(); 74 75 if (globalName.startsWith(nestedPath)) { 76 String nestedName = globalName.substring(nestedPath.length()); 77 nestedBindings.put(nestedName, value); 78 } 79 } 80 return nestedBindings; 81 } 82 83 protected void setUp() throws Exception { 84 super.setUp(); 85 86 kernel = KernelFactory.newInstance().createKernel("test"); 87 kernel.boot(); 88 89 ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming()); 90 91 GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO); 92 93 GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO); 94 artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 95 96 GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO); 97 configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 98 configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName()); 99 100 ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader()); 101 102 EditableConfigurationManager configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel); 103 104 ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming()); 105 configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO); 106 configurationData.addGBean("JavaComp", JavaCompContextGBean.GBEAN_INFO); 107 108 configurationManager.loadConfiguration(configurationData); 109 configurationManager.startConfiguration(configurationData.getId()); 110 111 112 contextEnv = new Hashtable (); 113 contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName()); 114 } 115 116 protected void tearDown() throws Exception { 117 kernel.shutdown(); 118 super.tearDown(); 119 } 120 } 121 | Popular Tags |