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    
019    package org.apache.geronimo.naming.java;
020    
021    import java.util.Collections;
022    
023    import javax.naming.Context;
024    import javax.naming.NameNotFoundException;
025    import javax.naming.NamingException;
026    
027    import org.apache.xbean.naming.context.ImmutableContext;
028    
029    /**
030     * The root context for the java: namespace.
031     * Automatically handles switching the "java:comp" sub-context to the
032     * appropriate one for the current thread.
033     *
034     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
035     */
036    public class RootContext extends ImmutableContext {
037        private static InheritableThreadLocal compContext = new InheritableThreadLocal();
038    
039        public RootContext() throws NamingException {
040            super(Collections.EMPTY_MAP);
041        }
042    
043        public Object lookup(String name) throws NamingException {
044            if (name.startsWith("java:")) {
045                name = name.substring(5);
046                if (name.length() == 0) {
047                    return this;
048                }
049    
050                Context compCtx = (Context) compContext.get();
051                if (compCtx == null) {
052                    // the component context was not set for this thread
053                    throw new NameNotFoundException(name);
054                }
055    
056                if ("comp".equals(name)) {
057                    return compCtx;
058                } else if (name.startsWith("comp/")) {
059                    return compCtx.lookup(name.substring(5));
060                } else if ("/comp".equals(name)) {
061                    return compCtx;
062                } else if (name.startsWith("/comp/")) {
063                    return compCtx.lookup(name.substring(6));
064                } else {
065                    throw new NameNotFoundException("Unrecognized name, does not start with expected 'comp': " + name);
066                }
067            }
068            return super.lookup(name);
069        }
070    
071        /**
072         * Set the component context for the current thread. This will be returned
073         * for all lookups of "java:comp"
074         * @param ctx the current components context
075         */
076        public static void setComponentContext(Context ctx) {
077            compContext.set(ctx);
078        }
079    
080        /**
081         * Get the component context for the current thread.
082         * @return the current components context
083         */
084        public static Context getComponentContext() {
085            return (Context) compContext.get();
086        }
087    }