1 17 package org.apache.geronimo.gjndi.binding; 18 19 import org.apache.geronimo.gbean.AbstractName; 20 import org.apache.geronimo.gbean.AbstractNameQuery; 21 import org.apache.geronimo.gbean.GBeanData; 22 import org.apache.geronimo.gjndi.AbstractContextTest; 23 import org.apache.geronimo.gjndi.GlobalContextGBean; 24 import org.apache.geronimo.gjndi.WritableContextGBean; 25 import org.apache.geronimo.kernel.Kernel; 26 import org.apache.geronimo.kernel.KernelFactory; 27 import org.apache.geronimo.kernel.config.ConfigurationData; 28 import org.apache.geronimo.kernel.config.ConfigurationUtil; 29 import org.apache.geronimo.kernel.config.EditableConfigurationManager; 30 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager; 31 import org.apache.geronimo.kernel.repository.Artifact; 32 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 33 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 34 import org.apache.xbean.naming.global.GlobalContextManager; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 38 import javax.sql.DataSource ; 39 import java.util.Collections ; 40 import java.util.HashMap ; 41 import java.util.Hashtable ; 42 import java.util.Iterator ; 43 import java.util.Map ; 44 45 48 public class GBeanBindingTest extends AbstractContextTest { 49 private Kernel kernel; 50 51 private Hashtable contextEnv; 52 private Map globalBindings; 53 private AbstractName ds1Name; 54 private AbstractName ds2Name; 55 56 public void testBasics() throws Exception { 57 InitialContext ctx = new InitialContext (contextEnv); 58 assertEq(globalBindings, ctx); 59 60 kernel.stopGBean(ds1Name); 64 DataSource ds2 = (DataSource ) kernel.getGBean(ds2Name); 65 globalBindings.put("writable/ds", ds2); 66 globalBindings.remove("writable/ds1"); 67 assertEq(globalBindings, ctx); 68 69 kernel.startGBean(ds1Name); 73 DataSource ds1 = (DataSource ) kernel.getGBean(ds1Name); 74 globalBindings.put("writable/ds1", ds1); 75 assertEq(globalBindings, ctx); 76 } 77 78 protected Map getNestedBindings(Map globalBindings, String nestedPath) { 79 HashMap nestedBindings = new HashMap (); 80 for (Iterator iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) { 81 Map.Entry entry = (Map.Entry ) iterator.next(); 82 String globalName = (String ) entry.getKey(); 83 Object value = entry.getValue(); 84 85 if (globalName.startsWith(nestedPath)) { 86 String nestedName = globalName.substring(nestedPath.length()); 87 nestedBindings.put(nestedName, value); 88 } 89 } 90 return nestedBindings; 91 } 92 93 protected void setUp() throws Exception { 94 super.setUp(); 95 96 kernel = KernelFactory.newInstance().createKernel("test"); 97 kernel.boot(); 98 99 ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming()); 100 101 GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO); 102 103 GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO); 104 artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 105 106 GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO); 107 configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName()); 108 configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName()); 109 110 ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader()); 111 112 EditableConfigurationManager configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel); 113 114 ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming()); 115 configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO); 116 117 contextEnv = new Hashtable (); 118 contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName()); 119 120 GBeanData ds1GBean = configurationData.addGBean("ds1", MockDataSource.GBEAN_INFO); 122 ds1Name = ds1GBean.getAbstractName(); 123 124 GBeanData ds2GBean = configurationData.addGBean("ds2", MockDataSource.GBEAN_INFO); 125 ds2Name = ds2GBean.getAbstractName(); 126 127 GBeanData writableGBean = configurationData.addGBean("writable", WritableContextGBean.GBEAN_INFO); 129 AbstractName writableName = writableGBean.getAbstractName(); 130 writableGBean.setAttribute("nameInNamespace", "writable"); 131 132 GBeanData dsBinding = configurationData.addGBean("dsBinding", GBeanBinding.GBEAN_INFO); 133 dsBinding.setReferencePattern("Context", writableName); 134 dsBinding.setAttribute("name", "ds"); 135 dsBinding.setAttribute("abstractNameQuery", new AbstractNameQuery(null, Collections.EMPTY_MAP, DataSource .class.getName())); 136 137 GBeanData ds1Binding = configurationData.addGBean("ds1Binding", GBeanBinding.GBEAN_INFO); 138 ds1Binding.setReferencePattern("Context", writableName); 139 ds1Binding.setAttribute("name", "ds1"); 140 ds1Binding.setAttribute("abstractNameQuery", new AbstractNameQuery(null, 141 Collections.singletonMap("name", "ds1"), 142 DataSource .class.getName())); 143 144 GBeanData ds2Binding = configurationData.addGBean("ds2Binding", GBeanBinding.GBEAN_INFO); 145 ds2Binding.setReferencePattern("Context", writableName); 146 ds2Binding.setAttribute("name", "ds2"); 147 ds2Binding.setAttribute("abstractNameQuery", new AbstractNameQuery(null, 148 Collections.singletonMap("name", "ds2"), 149 DataSource .class.getName())); 150 151 configurationManager.loadConfiguration(configurationData); 152 configurationManager.startConfiguration(configurationData.getId()); 153 154 DataSource ds1 = (DataSource ) kernel.getGBean(ds1Name); 155 DataSource ds2 = (DataSource ) kernel.getGBean(ds2Name); 156 157 globalBindings = new HashMap (); 159 globalBindings.put("writable/ds", ds1); 160 globalBindings.put("writable/ds1", ds1); 161 globalBindings.put("writable/ds2", ds2); 162 } 163 164 protected void tearDown() throws Exception { 165 kernel.shutdown(); 166 super.tearDown(); 167 } 168 } 169 | Popular Tags |