001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.gjndi;
019    
020    import org.apache.geronimo.gbean.GBeanInfo;
021    import org.apache.geronimo.gbean.GBeanInfoBuilder;
022    import org.apache.geronimo.gbean.GBeanLifecycle;
023    import org.apache.geronimo.gbean.AbstractNameQuery;
024    import org.apache.geronimo.gbean.AbstractName;
025    import org.apache.geronimo.kernel.Kernel;
026    import org.apache.xbean.naming.global.GlobalContextManager;
027    
028    import javax.naming.Context;
029    import javax.naming.NamingException;
030    import javax.naming.Name;
031    import java.util.Collections;
032    
033    /**
034     * @version $Rev$ $Date$
035     */
036    public class GlobalContextGBean extends KernelContextGBean implements GBeanLifecycle {
037        public GlobalContextGBean(Kernel kernel) throws NamingException {
038            super("", new AbstractNameQuery(null, Collections.EMPTY_MAP, Context.class.getName()), kernel);
039        }
040    
041        public void doStart() {
042            super.doStart();
043            GlobalContextManager.setGlobalContext(this);
044        }
045    
046        public void doStop() {
047            GlobalContextManager.setGlobalContext(null);
048            super.doStop();
049        }
050    
051        public void doFail() {
052            GlobalContextManager.setGlobalContext(null);
053            super.doFail();
054        }
055    
056        protected Name createBindingName(AbstractName abstractName, Object value) throws NamingException {
057            if (value instanceof Context) {
058                // don't bind yourself
059                if (value == this) return null;
060    
061                Context context = (Context) value;
062                String nameInNamespace = context.getNameInNamespace();
063                return getNameParser().parse(nameInNamespace);
064            }
065            throw new NamingException("value is not a context: abstractName=" + abstractName + " valueType=" + value.getClass().getName());
066        }
067    
068        public static final GBeanInfo GBEAN_INFO;
069    
070        public static GBeanInfo getGBeanInfo() {
071            return GBEAN_INFO;
072        }
073    
074        static {
075            GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(GlobalContextGBean.class, "GlobalContext");
076            builder.setConstructor(new String[]{"kernel"});
077            GBEAN_INFO = builder.getBeanInfo();
078        }
079    }