001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gjndi;
017:
018: import org.apache.geronimo.gbean.GBeanData;
019: import org.apache.geronimo.gbean.GBeanInfo;
020: import org.apache.geronimo.gbean.GBeanInfoBuilder;
021: import org.apache.geronimo.gbean.AbstractName;
022: import org.apache.geronimo.kernel.Kernel;
023: import org.apache.geronimo.kernel.KernelFactory;
024: import org.apache.geronimo.kernel.config.ConfigurationData;
025: import org.apache.geronimo.kernel.config.ConfigurationUtil;
026: import org.apache.geronimo.kernel.config.EditableConfigurationManager;
027: import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
028: import org.apache.geronimo.kernel.repository.Artifact;
029: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
030: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
031: import org.apache.geronimo.naming.java.RootContext;
032: import org.apache.xbean.naming.context.ImmutableContext;
033: import org.apache.xbean.naming.global.GlobalContextManager;
034:
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import java.util.HashMap;
038: import java.util.Hashtable;
039: import java.util.Map;
040: import java.util.Iterator;
041: import java.util.Collections;
042:
043: /**
044: * @version $Rev$ $Date$
045: */
046: public class KernelContextGBeanTest extends AbstractContextTest {
047: private Kernel kernel;
048: private EditableConfigurationManager configurationManager;
049: private ConfigurationData configurationData;
050: private GBeanInfo immutableContextGBeanInfo;
051: private Hashtable contextEnv;
052:
053: public void test() throws Exception {
054: Map globalBindings = new HashMap();
055: globalBindings.put("java:comp/string", "foo");
056: globalBindings.put("java:comp/nested/context/string", "bar");
057: globalBindings.put("java:comp/a/b/c/d/e/string", "beer");
058: globalBindings.put("java:comp/a/b/c/d/e/one", new Integer(1));
059: globalBindings.put("java:comp/a/b/c/d/e/two", new Integer(2));
060: globalBindings.put("java:comp/a/b/c/d/e/three", new Integer(3));
061: globalBindings.put("test/env/foo", new Integer(42));
062: globalBindings.put("test/baz", "caz");
063:
064: Map javaCompBindings = getNestedBindings(globalBindings,
065: "java:comp/");
066: ImmutableContext javaCompContext = new ImmutableContext(
067: javaCompBindings);
068: RootContext.setComponentContext(javaCompContext);
069:
070: GBeanData javaComp = configurationData.addGBean("JavaComp",
071: JavaCompContextGBean.GBEAN_INFO);
072: AbstractName javaCompName = javaComp.getAbstractName();
073:
074: GBeanData test = configurationData.addGBean("Test",
075: immutableContextGBeanInfo);
076: AbstractName testName = test.getAbstractName();
077: test.setAttribute("nameInNamespace", "test");
078: Map testBindings = getNestedBindings(globalBindings, "test/");
079: test.setAttribute("bindings", testBindings);
080:
081: configurationManager.loadConfiguration(configurationData);
082: configurationManager.startConfiguration(configurationData
083: .getId());
084:
085: InitialContext ctx = new InitialContext(contextEnv);
086: assertEq(globalBindings, ctx);
087:
088: //
089: // stop test context
090: //
091: kernel.stopGBean(testName);
092:
093: HashMap javaCompOnlyBindings = new HashMap(globalBindings);
094: javaCompOnlyBindings.remove("test/env/foo");
095: javaCompOnlyBindings.remove("test/baz");
096: assertEq(javaCompOnlyBindings, ctx);
097:
098: //
099: // stop java context
100: //
101: kernel.stopGBean(javaCompName);
102:
103: assertEq(Collections.EMPTY_MAP, ctx);
104:
105: //
106: // restart java context
107: //
108: kernel.startGBean(javaCompName);
109:
110: assertEq(javaCompOnlyBindings, ctx);
111:
112: //
113: // restart test context
114: //
115: kernel.startGBean(testName);
116:
117: assertEq(globalBindings, ctx);
118: }
119:
120: protected Map getNestedBindings(Map globalBindings,
121: String nestedPath) {
122: HashMap nestedBindings = new HashMap();
123: for (Iterator iterator = globalBindings.entrySet().iterator(); iterator
124: .hasNext();) {
125: Map.Entry entry = (Map.Entry) iterator.next();
126: String globalName = (String) entry.getKey();
127: Object value = entry.getValue();
128:
129: if (globalName.startsWith(nestedPath)) {
130: String nestedName = globalName.substring(nestedPath
131: .length());
132: nestedBindings.put(nestedName, value);
133: }
134: }
135: return nestedBindings;
136: }
137:
138: protected void setUp() throws Exception {
139: super .setUp();
140:
141: kernel = KernelFactory.newInstance().createKernel("test");
142: kernel.boot();
143:
144: ConfigurationData bootstrap = new ConfigurationData(
145: new Artifact("bootstrap", "bootstrap", "", "car"),
146: kernel.getNaming());
147:
148: GBeanData artifactManagerData = bootstrap.addGBean(
149: "ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
150:
151: GBeanData artifactResolverData = bootstrap.addGBean(
152: "ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
153: artifactResolverData.setReferencePattern("ArtifactManager",
154: artifactManagerData.getAbstractName());
155:
156: GBeanData configurationManagerData = bootstrap.addGBean(
157: "ConfigurationManager",
158: EditableKernelConfigurationManager.GBEAN_INFO);
159: configurationManagerData.setReferencePattern("ArtifactManager",
160: artifactManagerData.getAbstractName());
161: configurationManagerData.setReferencePattern(
162: "ArtifactResolver", artifactResolverData
163: .getAbstractName());
164:
165: ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap,
166: getClass().getClassLoader());
167:
168: configurationManager = ConfigurationUtil
169: .getEditableConfigurationManager(kernel);
170:
171: configurationData = new ConfigurationData(new Artifact("test",
172: "test", "", "car"), kernel.getNaming());
173: configurationData.addGBean("GlobalContext",
174: GlobalContextGBean.GBEAN_INFO);
175:
176: GBeanInfoBuilder builder = new GBeanInfoBuilder(
177: ImmutableContext.class);
178: builder.setConstructor(new String[] { "nameInNamespace",
179: "bindings", "cacheReferences" });
180: builder.addAttribute("nameInNamespace", String.class, true);
181: builder.addAttribute("bindings", Map.class, true);
182: builder.addAttribute("cacheReferences", boolean.class, true);
183: immutableContextGBeanInfo = builder.getBeanInfo();
184:
185: contextEnv = new Hashtable();
186: contextEnv.put(Context.INITIAL_CONTEXT_FACTORY,
187: GlobalContextManager.class.getName());
188: }
189:
190: protected void tearDown() throws Exception {
191: kernel.shutdown();
192: super.tearDown();
193: }
194: }
|