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.client;
019    
020    import java.util.Iterator;
021    import java.util.Map;
022    import java.util.Collections;
023    import javax.naming.InitialContext;
024    import javax.naming.Context;
025    import javax.naming.NamingException;
026    
027    import org.apache.geronimo.naming.java.RootContext;
028    import org.apache.geronimo.naming.reference.KernelAwareReference;
029    import org.apache.geronimo.naming.reference.ClassLoaderAwareReference;
030    import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
031    import org.apache.geronimo.gbean.GBeanInfo;
032    import org.apache.geronimo.gbean.GBeanInfoBuilder;
033    import org.apache.geronimo.gbean.AbstractName;
034    import org.apache.geronimo.kernel.Kernel;
035    import org.apache.xbean.naming.context.ImmutableContext;
036    
037    /**
038     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
039     */
040    public class StaticJndiContextPlugin implements AppClientPlugin {
041        private final Context context;
042    
043        public StaticJndiContextPlugin(Map context, Kernel kernel, ClassLoader classLoader) throws NamingException {
044            this.context = EnterpriseNamingContext.createEnterpriseNamingContext(context, null, kernel, classLoader);
045        }
046    
047        public void startClient(AbstractName appClientModuleName, Kernel kernel, ClassLoader classLoader) throws Exception {
048            RootContext.setComponentContext(context);
049            new InitialContext().lookup("java:comp/env");
050        }
051    
052        public void stopClient(AbstractName appClientModuleName) throws Exception {
053            RootContext.setComponentContext(null);
054        }
055    
056        public static final GBeanInfo GBEAN_INFO;
057    
058        static {
059            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(StaticJndiContextPlugin.class);
060    
061            infoFactory.addAttribute("context", Map.class, true);
062            infoFactory.addAttribute("kernel", Kernel.class, false);
063            infoFactory.addAttribute("classLoader", ClassLoader.class, false);
064            infoFactory.addInterface(AppClientPlugin.class);
065    
066            infoFactory.setConstructor(new String[]{"context", "kernel", "classLoader"});
067    
068            GBEAN_INFO = infoFactory.getBeanInfo();
069        }
070    
071        public static GBeanInfo getGBeanInfo() {
072            return GBEAN_INFO;
073        }
074    }