Home » geronimo-2.2-source-release » org.apache.geronimo.naming.java » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.naming.java;
   19   
   20   import java.util.Collections;
   21   
   22   import javax.naming.Context;
   23   import javax.naming.NameNotFoundException;
   24   import javax.naming.NamingException;
   25   
   26   import org.apache.xbean.naming.context.ImmutableContext;
   27   
   28   /**
   29    * The root context for the java: namespace.
   30    * Automatically handles switching the "java:comp" sub-context to the
   31    * appropriate one for the current thread.
   32    *
   33    * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
   34    */
   35   public class RootContext extends ImmutableContext {
   36       private static InheritableThreadLocal compContext = new InheritableThreadLocal();
   37   
   38       public RootContext() throws NamingException {
   39           super(Collections.EMPTY_MAP);
   40       }
   41   
   42       public Object lookup(String name) throws NamingException {
   43           if (name.startsWith("java:")) {
   44               name = name.substring(5);
   45               if (name.length() == 0) {
   46                   return this;
   47               }
   48   
   49               Context compCtx = (Context) compContext.get();
   50               if (compCtx == null) {
   51                   // the component context was not set for this thread
   52                   throw new NameNotFoundException(name);
   53               }
   54   
   55               if ("comp".equals(name)) {
   56                   return compCtx;
   57               } else if (name.startsWith("comp/")) {
   58                   return compCtx.lookup(name.substring(5));
   59               } else if ("/comp".equals(name)) {
   60                   return compCtx;
   61               } else if (name.startsWith("/comp/")) {
   62                   return compCtx.lookup(name.substring(6));
   63               } else {
   64                   throw new NameNotFoundException("Unrecognized name, does not start with expected 'comp': " + name);
   65               }
   66           }
   67           return super.lookup(name);
   68       }
   69   
   70       /**
   71        * Set the component context for the current thread. This will be returned
   72        * for all lookups of "java:comp"
   73        * @param ctx the current components context
   74        */
   75       public static void setComponentContext(Context ctx) {
   76           compContext.set(ctx);
   77       }
   78   
   79       /**
   80        * Get the component context for the current thread.
   81        * @return the current components context
   82        */
   83       public static Context getComponentContext() {
   84           return (Context) compContext.get();
   85       }
   86   }

Home » geronimo-2.2-source-release » org.apache.geronimo.naming.java » [javadoc | source]