View Javadoc

    1   // ========================================================================
    2   // $Id: javaRootURLContext.java 887 2006-09-05 13:46:42Z janb $
    3   // Copyright 1999-2006 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.naming.java;
    17
    18
    19  import java.util.Hashtable;
    20
    21  import javax.naming.Context;
    22  import javax.naming.Name;
    23  import javax.naming.NameParser;
    24  import javax.naming.NamingEnumeration;
    25  import javax.naming.NamingException;
    26  import javax.naming.Reference;
    27  import javax.naming.StringRefAddr;
    28
    29  import org.mortbay.log.Log;
    30  import org.mortbay.naming.ContextFactory;
    31  import org.mortbay.naming.NamingContext;
    32
    33
    34
    35
    36  /** javaRootURLContext
    37   * <p>This is the root of the java: url namespace
    38   *
    39   * <p><h4>Notes</h4>
    40   * <p>Thanks to Rickard Oberg for the idea of binding an ObjectFactory at "comp".
    41   *
    42   * <p><h4>Usage</h4>
    43   * <pre>
    44   */
    45  /*
    46  * </pre>
    47  *
    48  * @see
    49  *
    50  * @author <a href="mailto:janb@mortbay.com">Jan Bartel</a>
    51  * @version 1.0
    52  */
    53  public class javaRootURLContext implements Context
    54  {
    55      public static final String URL_PREFIX = "java:";
    56
    57      protected Hashtable _env;
    58
    59      protected static NamingContext _nameRoot;
    60
    61      protected static NameParser _javaNameParser;
    62
    63
    64      static
    65      {
    66          try
    67          {
    68              _javaNameParser = new javaNameParser();
    69              _nameRoot = new NamingContext();
    70              _nameRoot.setNameParser(_javaNameParser);
    71
    72              StringRefAddr parserAddr = new StringRefAddr("parser", _javaNameParser.getClass().getName());
    73
    74              Reference ref = new Reference ("javax.naming.Context",
    75                                             parserAddr,
    76                                             ContextFactory.class.getName(),
    77                                             (String)null);
    78
    79              //bind special object factory at comp
    80              _nameRoot.bind ("comp", ref);
    81          }
    82          catch (Exception e)
    83          {
    84              Log.warn(e);
    85          }
    86      }
    87
    88
    89
    90      /*------------------------------------------------*/
    91      /**
    92       * Creates a new <code>javaRootURLContext</code> instance.
    93       *
    94       * @param env a <code>Hashtable</code> value
    95       */
    96      public javaRootURLContext(Hashtable env)
    97      {
    98          _env = env;
    99      }
    100
    101
    102     public Object lookup(Name name)
    103         throws NamingException
    104     {
    105         return getRoot().lookup(stripProtocol(name));
    106     }
    107
    108
    109     public Object lookup(String name)
    110         throws NamingException
    111     {
    112         return getRoot().lookup(stripProtocol(name));
    113     }
    114
    115     public void bind(Name name, Object obj)
    116         throws NamingException
    117     {
    118         getRoot().bind(stripProtocol(name), obj);
    119     }
    120
    121     public void bind(String name, Object obj)
    122         throws NamingException
    123     {
    124         getRoot().bind(stripProtocol(name), obj);
    125     }
    126
    127     public void unbind (String name)
    128         throws NamingException
    129     {
    130         getRoot().unbind(stripProtocol(name));
    131     }
    132
    133     public void unbind (Name name)
    134         throws NamingException
    135     {
    136         getRoot().unbind(stripProtocol(name));
    137     }
    138
    139     public void rename (String oldStr, String newStr)
    140         throws NamingException
    141     {
    142         getRoot().rename (stripProtocol(oldStr), stripProtocol(newStr));
    143     }
    144
    145     public void rename (Name oldName, Name newName)
    146         throws NamingException
    147     {
    148         getRoot().rename (stripProtocol(oldName), stripProtocol(newName));
    149     }
    150
    151     public void rebind (Name name, Object obj)
    152         throws NamingException
    153     {
    154         getRoot().rebind(stripProtocol(name), obj);
    155     }
    156
    157     public void rebind (String name, Object obj)
    158         throws NamingException
    159     {
    160         getRoot().rebind(stripProtocol(name), obj);
    161     }
    162
    163
    164     public Object lookupLink (Name name)
    165         throws NamingException
    166     {
    167         return getRoot().lookupLink(stripProtocol(name));
    168     }
    169
    170     public Object lookupLink (String name)
    171         throws NamingException
    172     {
    173         return getRoot().lookupLink(stripProtocol(name));
    174     }
    175
    176
    177     public Context createSubcontext (Name name)
    178         throws NamingException
    179     {
    180         return getRoot().createSubcontext(stripProtocol(name));
    181     }
    182
    183     public Context createSubcontext (String name)
    184         throws NamingException
    185     {
    186         return getRoot().createSubcontext(stripProtocol(name));
    187     }
    188
    189
    190     public void destroySubcontext (Name name)
    191         throws NamingException
    192     {
    193         getRoot().destroySubcontext(stripProtocol(name));
    194     }
    195
    196     public void destroySubcontext (String name)
    197         throws NamingException
    198     {
    199         getRoot().destroySubcontext(stripProtocol(name));
    200     }
    201
    202
    203     public NamingEnumeration list(Name name)
    204         throws NamingException
    205     {
    206         return getRoot().list(stripProtocol(name));
    207     }
    208
    209
    210     public NamingEnumeration list(String name)
    211         throws NamingException
    212     {
    213         return getRoot().list(stripProtocol(name));
    214     }
    215
    216     public NamingEnumeration listBindings(Name name)
    217         throws NamingException
    218     {
    219         return getRoot().listBindings(stripProtocol(name));
    220     }
    221
    222     public NamingEnumeration listBindings(String name)
    223         throws NamingException
    224     {
    225         return getRoot().listBindings(stripProtocol(name));
    226     }
    227
    228
    229     public Name composeName (Name name,
    230                              Name prefix)
    231         throws NamingException
    232     {
    233         return getRoot().composeName(name, prefix);
    234     }
    235
    236     public String composeName (String name,
    237                                String prefix)
    238         throws NamingException
    239     {
    240         return getRoot().composeName(name, prefix);
    241     }
    242
    243
    244     public void close ()
    245         throws NamingException
    246     {
    247     }
    248
    249     public String getNameInNamespace ()
    250         throws NamingException
    251     {
    252         return URL_PREFIX;
    253     }
    254
    255     public NameParser getNameParser (Name name)
    256         throws NamingException
    257     {
    258         return _javaNameParser;
    259     }
    260
    261     public NameParser getNameParser (String name)
    262         throws NamingException
    263     {
    264         return _javaNameParser;
    265     }
    266
    267
    268     public Object addToEnvironment(String propName,
    269                                    Object propVal)
    270         throws NamingException
    271     {
    272        return _env.put (propName,propVal);
    273     }
    274
    275     public Object removeFromEnvironment(String propName)
    276         throws NamingException
    277     {
    278         return _env.remove (propName);
    279     }
    280
    281     public Hashtable getEnvironment ()
    282     {
    283         return _env;
    284     }
    285
    286
    287     protected NamingContext getRoot ()
    288     {
    289         return _nameRoot;
    290     }
    291
    292
    293     protected Name stripProtocol (Name name)
    294         throws NamingException
    295     {
    296         if ((name != null) && (name.size() > 0))
    297         {
    298             String head = name.get(0);
    299
    300             if(Log.isDebugEnabled())Log.debug("Head element of name is: "+head);
    301
    302             if (head.startsWith(URL_PREFIX))
    303             {
    304                 head = head.substring (URL_PREFIX.length());
    305                 name.remove(0);
    306                 if (head.length() > 0)
    307                     name.add(0, head);
    308
    309                 if(Log.isDebugEnabled())Log.debug("name modified to "+name.toString());
    310             }
    311         }
    312
    313         return name;
    314     }
    315
    316
    317
    318     protected String stripProtocol (String name)
    319     {
    320         String newName = name;
    321
    322         if ((name != null) && (!name.equals("")))
    323         {
    324             if (name.startsWith(URL_PREFIX))
    325                newName = name.substring(URL_PREFIX.length());
    326         }
    327
    328         return newName;
    329     }
    330
    331 }