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

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   
   21   package org.apache.geronimo.openejb;
   22   
   23   import org.apache.geronimo.gbean.annotation.GBean;
   24   import org.apache.geronimo.gbean.annotation.ParamAttribute;
   25   import org.apache.openejb.SystemException;
   26   import org.apache.openejb.core.JndiFactory;
   27   import org.apache.xbean.naming.context.ContextAccess;
   28   import org.apache.xbean.naming.context.WritableContext;
   29   
   30   import javax.naming.Binding;
   31   import javax.naming.CompositeName;
   32   import javax.naming.Context;
   33   import javax.naming.InitialContext;
   34   import javax.naming.Name;
   35   import javax.naming.NameClassPair;
   36   import javax.naming.NameParser;
   37   import javax.naming.NamingEnumeration;
   38   import javax.naming.NamingException;
   39   import java.util.Collections;
   40   import java.util.Hashtable;
   41   import java.util.Map;
   42   
   43   /**
   44    * @version $Rev: 793150 $ $Date: 2009-07-10 17:02:03 -0700 (Fri, 10 Jul 2009) $
   45    */
   46   @GBean
   47   public class DeepBindableContext extends WritableContext {
   48   
   49       public DeepBindableContext(@ParamAttribute(name = "nameInNamespace") String nameInNamespace,
   50                                  @ParamAttribute(name = "cacheReferences") boolean cacheReferences,
   51                                  @ParamAttribute(name = "supportReferenceable") boolean supportReferenceable,
   52                                  @ParamAttribute(name = "checkDereferenceDifferent") boolean checkDereferenceDifferent,
   53                                  @ParamAttribute(name = "assumeDereferenceBound") boolean assumeDereferenceBound) throws NamingException {
   54           super(nameInNamespace, Collections.<String, Object>emptyMap(), ContextAccess.MODIFIABLE, cacheReferences, supportReferenceable, checkDereferenceDifferent, assumeDereferenceBound);
   55       }
   56   
   57       void addDeepBinding(String name, Object value) throws NamingException {
   58           addDeepBinding(new CompositeName(name), value, false, true);
   59       }
   60   
   61       void removeDeepBinding(Name name) throws NamingException {
   62           removeDeepBinding(name, true, false);
   63       }
   64   
   65       public JndiFactory newJndiFactory() throws NamingException {
   66           return new XBeanJndiFactory();
   67       }
   68   
   69       class XBeanJndiFactory implements JndiFactory {
   70           private final Context rootContext;
   71   
   72           XBeanJndiFactory() throws NamingException {
   73               rootContext = new ContextWrapper((Context) new InitialContext().lookup(""));
   74           }
   75   
   76           public Context createComponentContext(Map<String, Object> bindings) throws SystemException {
   77               boolean hasEnv = false;
   78               for (String name : bindings.keySet()) {
   79                   if (name.startsWith("java:comp/env")) {
   80                       hasEnv = true;
   81                       break;
   82                   }
   83               }
   84               if (!hasEnv) bindings.put("java:comp/env/dummy", "dummy");
   85   
   86               WritableContext context = null;
   87               try {
   88                   context = new WritableContext("", bindings);
   89               } catch (NamingException e) {
   90                   throw new IllegalStateException(e);
   91               }
   92               return context;
   93           }
   94   
   95           public Context createRootContext() {
   96               return rootContext;
   97           }
   98       }
   99   
  100       class ContextWrapper implements Context {
  101           private final Context rootContext;
  102           private final String shortPrefix;
  103           private final String longPrefix;
  104   
  105   
  106           ContextWrapper(Context rootContext) throws NamingException {
  107               this.rootContext = rootContext;
  108               shortPrefix = DeepBindableContext.this.getNameInNamespace();
  109               longPrefix = "java:" + shortPrefix;
  110           }
  111   
  112           public Object lookup(Name name) throws NamingException {
  113               return rootContext.lookup(name);
  114           }
  115   
  116           public Object lookup(String name) throws NamingException {
  117               return rootContext.lookup(name);
  118           }
  119   
  120           public void bind(Name name, Object value) throws NamingException {
  121               bind(name.toString(), value);
  122           }
  123   
  124           public void bind(String name, Object value) throws NamingException {
  125               if (name.startsWith(longPrefix + "/")) {
  126                   name = name.substring(longPrefix.length() + 1);
  127               } else if (name.startsWith(shortPrefix + "/")) {
  128                   name = name.substring(shortPrefix.length() + 1);
  129               }
  130               addDeepBinding(name, value);
  131           }
  132   
  133           public void rebind(Name name, Object o) throws NamingException {
  134               rootContext.rebind(name, o);
  135           }
  136   
  137           public void rebind(String s, Object o) throws NamingException {
  138               rootContext.rebind(s, o);
  139           }
  140   
  141           public void unbind(Name name) throws NamingException {
  142               if (name.get(0).equals(shortPrefix) || name.get(0).equals(longPrefix)) {
  143                   name = (Name) name.clone();
  144                   name.remove(0);
  145               }
  146               removeDeepBinding(name);
  147           }
  148   
  149           public void unbind(String name) throws NamingException {
  150               unbind(getNameParser(name).parse(name));
  151            }
  152   
  153           public void rename(Name name, Name name1) throws NamingException {
  154               rootContext.rename(name, name1);
  155           }
  156   
  157           public void rename(String s, String s1) throws NamingException {
  158               rootContext.rename(s, s1);
  159           }
  160   
  161           public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
  162               return rootContext.list(name);
  163           }
  164   
  165           public NamingEnumeration<NameClassPair> list(String s) throws NamingException {
  166               return rootContext.list(s);
  167           }
  168   
  169           public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
  170               return rootContext.listBindings(name);
  171           }
  172   
  173           public NamingEnumeration<Binding> listBindings(String s) throws NamingException {
  174               return rootContext.listBindings(s);
  175           }
  176   
  177           public void destroySubcontext(Name name) throws NamingException {
  178               rootContext.destroySubcontext(name);
  179           }
  180   
  181           public void destroySubcontext(String s) throws NamingException {
  182               rootContext.destroySubcontext(s);
  183           }
  184   
  185           public Context createSubcontext(Name name) throws NamingException {
  186               return rootContext.createSubcontext(name);
  187           }
  188   
  189           public Context createSubcontext(String s) throws NamingException {
  190               return rootContext.createSubcontext(s);
  191           }
  192   
  193           public Object lookupLink(Name name) throws NamingException {
  194               return rootContext.lookupLink(name);
  195           }
  196   
  197           public Object lookupLink(String s) throws NamingException {
  198               return rootContext.lookupLink(s);
  199           }
  200   
  201           public NameParser getNameParser(Name name) throws NamingException {
  202               return rootContext.getNameParser(name);
  203           }
  204   
  205           public NameParser getNameParser(String s) throws NamingException {
  206               return rootContext.getNameParser(s);
  207           }
  208   
  209           public Name composeName(Name name, Name name1) throws NamingException {
  210               return rootContext.composeName(name, name1);
  211           }
  212   
  213           public String composeName(String s, String s1) throws NamingException {
  214               return rootContext.composeName(s, s1);
  215           }
  216   
  217           public Object addToEnvironment(String s, Object o) throws NamingException {
  218               return rootContext.addToEnvironment(s, o);
  219           }
  220   
  221           public Object removeFromEnvironment(String s) throws NamingException {
  222               return rootContext.removeFromEnvironment(s);
  223           }
  224   
  225           public Hashtable<?, ?> getEnvironment() throws NamingException {
  226               return rootContext.getEnvironment();
  227           }
  228   
  229           public void close() throws NamingException {
  230               rootContext.close();
  231           }
  232   
  233           public String getNameInNamespace() throws NamingException {
  234               return rootContext.getNameInNamespace();
  235           }
  236       }
  237   }

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