1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.gjndi;
19
20 import org.apache.geronimo.gbean.GBeanInfo;
21 import org.apache.geronimo.gbean.GBeanInfoBuilder;
22 import org.apache.geronimo.gbean.GBeanLifecycle;
23 import org.apache.geronimo.gbean.AbstractNameQuery;
24 import org.apache.geronimo.gbean.AbstractName;
25 import org.apache.geronimo.kernel.Kernel;
26 import org.apache.xbean.naming.global.GlobalContextManager;
27
28 import javax.naming.Context;
29 import javax.naming.NamingException;
30 import javax.naming.Name;
31 import java.util.Collections;
32
33 /**
34 * @version $Rev$ $Date$
35 */
36 public class GlobalContextGBean extends KernelContextGBean implements GBeanLifecycle {
37 public GlobalContextGBean(Kernel kernel) throws NamingException {
38 super("", new AbstractNameQuery(null, Collections.EMPTY_MAP, Context.class.getName()), kernel);
39 }
40
41 public void doStart() {
42 super.doStart();
43 GlobalContextManager.setGlobalContext(this);
44 }
45
46 public void doStop() {
47 GlobalContextManager.setGlobalContext(null);
48 super.doStop();
49 }
50
51 public void doFail() {
52 GlobalContextManager.setGlobalContext(null);
53 super.doFail();
54 }
55
56 protected Name createBindingName(AbstractName abstractName, Object value) throws NamingException {
57 if (value instanceof Context) {
58
59 if (value == this) return null;
60
61 Context context = (Context) value;
62 String nameInNamespace = context.getNameInNamespace();
63 return getNameParser().parse(nameInNamespace);
64 }
65 throw new NamingException("value is not a context: abstractName=" + abstractName + " valueType=" + value.getClass().getName());
66 }
67
68 public static final GBeanInfo GBEAN_INFO;
69
70 public static GBeanInfo getGBeanInfo() {
71 return GBEAN_INFO;
72 }
73
74 static {
75 GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(GlobalContextGBean.class, "GlobalContext");
76 builder.setConstructor(new String[]{"kernel"});
77 GBEAN_INFO = builder.getBeanInfo();
78 }
79 }