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     */
017    package org.apache.geronimo.gjndi;
018    
019    import org.apache.geronimo.gbean.GBeanInfo;
020    import org.apache.geronimo.gbean.GBeanInfoBuilder;
021    import org.apache.geronimo.gbean.GBeanLifecycle;
022    import org.apache.geronimo.gbean.AbstractNameQuery;
023    import org.apache.geronimo.gbean.AbstractName;
024    import org.apache.geronimo.kernel.Kernel;
025    import org.apache.xbean.naming.global.GlobalContextManager;
026    
027    import javax.naming.Context;
028    import javax.naming.NamingException;
029    import javax.naming.Name;
030    import java.util.Collections;
031    
032    /**
033     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
034     */
035    public class GlobalContextGBean extends KernelContextGBean implements GBeanLifecycle {
036        public GlobalContextGBean(Kernel kernel) throws NamingException {
037            super("", new AbstractNameQuery(null, Collections.EMPTY_MAP, Context.class.getName()), kernel);
038        }
039    
040        @Override
041        public void doStart() {
042            super.doStart();
043            GlobalContextManager.setGlobalContext(this);
044        }
045    
046        @Override
047        public void doStop() {
048            GlobalContextManager.setGlobalContext(null);
049            super.doStop();
050        }
051    
052        @Override
053        public void doFail() {
054            GlobalContextManager.setGlobalContext(null);
055            super.doFail();
056        }
057    
058        @Override
059        protected Name createBindingName(AbstractName abstractName, Object value) throws NamingException {
060            if (value instanceof Context) {
061                // don't bind yourself
062                if (value == this) return null;
063    
064                Context context = (Context) value;
065                String nameInNamespace = context.getNameInNamespace();
066                return getNameParser().parse(nameInNamespace);
067            }
068            throw new NamingException("value is not a context: abstractName=" + abstractName + " valueType=" + value.getClass().getName());
069        }
070    
071        public static final GBeanInfo GBEAN_INFO;
072    
073        public static GBeanInfo getGBeanInfo() {
074            return GBEAN_INFO;
075        }
076    
077        static {
078            GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(GlobalContextGBean.class, "GlobalContext");
079            builder.setConstructor(new String[]{"kernel"});
080            GBEAN_INFO = builder.getBeanInfo();
081        }
082    }