1 17 package org.apache.geronimo.gjndi; 18 19 import org.apache.geronimo.gbean.GBeanData; 20 import org.apache.geronimo.gbean.GBeanInfo; 21 import org.apache.geronimo.gbean.GBeanInfoBuilder; 22 import org.apache.geronimo.gbean.AbstractName; 23 import org.apache.geronimo.kernel.Kernel; 24 import org.apache.geronimo.kernel.KernelFactory; 25 import org.apache.geronimo.kernel.config.ConfigurationData; 26 import org.apache.geronimo.kernel.config.ConfigurationUtil; 27 import org.apache.geronimo.kernel.config.EditableConfigurationManager; 28 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager; 29 import org.apache.geronimo.kernel.repository.Artifact; 30 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 31 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 32 import org.apache.geronimo.naming.java.RootContext; 33 import org.apache.xbean.naming.context.ImmutableContext; 34 import org.apache.xbean.naming.global.GlobalContextManager; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import java.util.HashMap ; 39 import java.util.Hashtable ; 40 import java.util.Map ; 41 import java.util.Iterator ; 42 import java.util.Collections ; 43 44 47 public class KernelContextGBeanTest extends AbstractContextTest { 48 private Kernel kernel; 49 private EditableConfigurationManager configurationManager; 50 private ConfigurationData configurationData; 51 private GBeanInfo immutableContextGBeanInfo; 52 private Hashtable contextEnv; 53 54 public void test() throws Exception { 55 Map globalBindings = new HashMap (); 56 globalBindings.put("java:comp/string", "foo"); 57 globalBindings.put("java:comp/nested/context/string", "bar"); 58 globalBindings.put("java:comp/a/b/c/d/e/string", "beer"); 59 globalBindings.put("java:comp/a/b/c/d/e/one", new Integer (1)); 60 globalBindings.put("java:comp/a/b/c/d/e/two", new Integer (2)); 61 globalBindings.put("java:comp/a/b/c/d/e/three", new Integer (3)); 62 globalBindings.put("test/env/foo", new Integer (42)); 63 globalBindings.put("test/baz", "caz"); 64 65 Map javaCompBindings = getNestedBindings(globalBindings, "java:comp/"); 66 ImmutableContext javaCompContext = new ImmutableContext(javaCompBindings); 67 RootContext.setComponentContext(javaCompContext); 68 69 GBeanData javaComp = configurationData.addGBean("JavaComp", JavaCompContextGBean.GBEAN_INFO); 70 AbstractName javaCompName = javaComp.getAbstractName(); 71 72 GBeanData test = configurationData.addGBean("Test", immutableContextGBeanInfo); 73 AbstractName testName = test.getAbstractName(); 74 test.setAttribute("nameInNamespace", "test"); 75 Map testBindings = getNestedBindings(globalBindings, "test/"); 76 test.setAttribute("bindings", testBindings); 77 78 configurationManager.loadConfiguration(configurationData); 79 configurationManager.startConfiguration(configurationData.getId()); 80 81 InitialContext ctx = new InitialContext (contextEnv); 82 assertEq(globalBindings, ctx); 83 84 kernel.stopGBean(testName); 88 89 HashMap javaCompOnlyBindings = new HashMap (globalBindings); 90 javaCompOnlyBindings.remove("test/env/foo"); 91 javaCompOnlyBindings.remove("test/baz"); 92 assertEq(javaCompOnlyBindings, ctx); 93 94 kernel.stopGBean(javaCompName); 98 99 assertEq(Collections.EMPTY_MAP, ctx); 100 101 102 kernel.startGBean(javaCompName); 106 107 assertEq(javaCompOnlyBindings, ctx); 108 109 kernel.startGBean(testName); 113 114 assertEq(globalBindings, ctx); 115 } 116 117 protected Map getNestedBindings(Map globalBindings, String nestedPath) { 118 HashMap nestedBindings = new HashMap (); 119 for (Iterator iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) { 120 Map.Entry entry = (Map.Entry ) iterator.next(); 121 String globalName = (String ) entry.getKey(); 122 Object value = entry.getValue(); 123 124 if (globalName.startsWith(nestedPath)) { 125 String nestedName = globalName.substring(nestedPath.length()); 126 nestedBindings.put(nestedName, value); 127 } 128 } 129 return nestedBindings; 130 } 131 132 protected void setUp() throws Exception { 133 super.setUp(); 134 135 kernel = KernelFactory.newInstance().createKernel("test"); 136 kernel.boot(); 137 138 ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming()); 139 140 GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO); 141 142 GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO); 143 artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 144 145 GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO); 146 configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 147 configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName()); 148 149 ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader()); 150 151 configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel); 152 153 configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming()); 154 configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO); 155 156 GBeanInfoBuilder builder = new GBeanInfoBuilder(ImmutableContext.class); 157 builder.setConstructor(new String [] {"nameInNamespace", "bindings", "cacheReferences"}); 158 builder.addAttribute("nameInNamespace", String .class, true); 159 builder.addAttribute("bindings", Map .class, true); 160 builder.addAttribute("cacheReferences", boolean.class, true); 161 immutableContextGBeanInfo = builder.getBeanInfo(); 162 163 contextEnv = new Hashtable (); 164 contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName()); 165 } 166 167 protected void tearDown() throws Exception { 168 kernel.shutdown(); 169 super.tearDown(); 170 } 171 } 172 | Popular Tags |