Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.util » [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 this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); 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 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.util;
   15   
   16   import javax.naming.Context;
   17   import javax.naming.InitialContext;
   18   import javax.naming.NameNotFoundException;
   19   import javax.naming.NamingException;
   20   
   21   import org.apache.webbeans.exception.WebBeansException;
   22   import org.apache.webbeans.logger.WebBeansLogger;
   23   
   24   /**
   25    * This is the internal helper class for low level access to JNDI
   26    * @see org.apache.webbeans.spi.JNDIService for transparent access over SPI 
   27    */
   28   public final class JNDIUtil
   29   {
   30       private static InitialContext initialContext = null;
   31   
   32       private static final WebBeansLogger LOGGER = WebBeansLogger.getLogger(JNDIUtil.class);
   33   
   34       static
   35       {
   36           try
   37           {
   38               initialContext = new InitialContext();
   39   
   40           }
   41           catch (Exception e)
   42           {
   43               LOGGER.error("Unable to initialize InitialContext object", e);
   44               throw new ExceptionInInitializerError(e);
   45           }
   46       }
   47   
   48       private JNDIUtil()
   49       {
   50   
   51       }
   52   
   53       public static InitialContext getInitialContext()
   54       {
   55           return initialContext;
   56       }
   57   
   58       public static void bind(String name, Object object)
   59       {
   60           Asserts.assertNotNull(name, "name parameter can not be null");
   61           Asserts.assertNotNull(object, "object parameter can not be null");
   62   
   63           try
   64           {
   65               Context context = initialContext;
   66               
   67               String[] parts = name.split("/");
   68               
   69               for(int i=0;i< parts.length -1;i++)
   70               {
   71                   try
   72                   {
   73                       context = (Context)initialContext.lookup(parts[i]);
   74                       
   75                   }catch(NameNotFoundException e)
   76                   {
   77                       context = initialContext.createSubcontext(parts[i]);   
   78                   }
   79                   
   80               }
   81               
   82               context.bind(parts[parts.length -1], object);
   83           }
   84           catch (NamingException e)
   85           {
   86               LOGGER.error("Unable to bind object with name : " + name, e);
   87           }
   88       }
   89   
   90       public static void unbind(String name)
   91       {
   92           Asserts.assertNotNull(name, "name parameter can not be null");
   93   
   94           try
   95           {
   96               initialContext.unbind(name);
   97   
   98           }
   99           catch (NamingException e)
  100           {
  101               LOGGER.error(e);
  102               throw new WebBeansException("Unable to unbind object with name : " + name, e);
  103           }
  104       }
  105       
  106       @SuppressWarnings("unchecked")
  107       public static <T> T lookup(String name, Class<? extends T> expectedClass) throws WebBeansException
  108       {
  109           Asserts.assertNotNull(name, "name parameter can not be null");
  110   
  111           try
  112           {
  113               return (T) initialContext.lookup(name);
  114           } catch (NamingException e)
  115           {
  116               LOGGER.error(e);
  117               throw new WebBeansException("Unable to lookup object with name : " + name, e);
  118           }
  119       }
  120   
  121   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.util » [javadoc | source]