Coverage Report - org.apache.camel.impl.JndiRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
JndiRegistry
50% 
100% 
0
 
 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  
 package org.apache.camel.impl;
 18  
 
 19  
 import java.util.Hashtable;
 20  
 
 21  
 import javax.naming.Context;
 22  
 import javax.naming.InitialContext;
 23  
 import javax.naming.NameNotFoundException;
 24  
 import javax.naming.NamingException;
 25  
 
 26  
 import org.apache.camel.RuntimeCamelException;
 27  
 import org.apache.camel.spi.Registry;
 28  
 
 29  
 /**
 30  
  * A {@link Registry} implementation which looks up the objects in JNDI
 31  
  * 
 32  
  * @version $Revision: 1.1 $
 33  
  */
 34  
 public class JndiRegistry implements Registry {
 35  
     private Context context;
 36  
 
 37  0
     public JndiRegistry() {
 38  0
     }
 39  
 
 40  222
     public JndiRegistry(Context context) {
 41  222
         this.context = context;
 42  222
     }
 43  
 
 44  
     public <T> T lookup(String name, Class<T> type) {
 45  9
         Object value = lookup(name);
 46  9
         return type.cast(value);
 47  
     }
 48  
 
 49  
     public Object lookup(String name) {
 50  
         try {
 51  27
             return getContext().lookup(name);
 52  15
         } catch (NameNotFoundException e) {
 53  15
             return null;
 54  0
         } catch (NamingException e) {
 55  0
             throw new RuntimeCamelException(e);
 56  
         }
 57  
     }
 58  
 
 59  
     public void bind(String s, Object o) {
 60  
         try {
 61  3
             getContext().bind(s, o);
 62  0
         } catch (NamingException e) {
 63  0
             throw new RuntimeCamelException(e);
 64  3
         }
 65  3
     }
 66  
 
 67  
     public void close() throws NamingException {
 68  0
         getContext().close();
 69  0
     }
 70  
 
 71  
     public Context getContext() throws NamingException {
 72  30
         if (context == null) {
 73  0
             context = createContext();
 74  
         }
 75  30
         return context;
 76  
     }
 77  
 
 78  
     public void setContext(Context context) {
 79  0
         this.context = context;
 80  0
     }
 81  
 
 82  
     protected Context createContext() throws NamingException {
 83  0
         Hashtable properties = new Hashtable(System.getProperties());
 84  0
         return new InitialContext(properties);
 85  
     }
 86  
 }