001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.impl;
018    
019    import java.util.Hashtable;
020    
021    import javax.naming.Context;
022    import javax.naming.InitialContext;
023    import javax.naming.NameNotFoundException;
024    import javax.naming.NamingException;
025    
026    import org.apache.camel.RuntimeCamelException;
027    import org.apache.camel.spi.Registry;
028    
029    /**
030     * A {@link Registry} implementation which looks up the objects in JNDI
031     * 
032     * @version $Revision: 303 $
033     */
034    public class JndiRegistry implements Registry {
035        private Context context;
036    
037        public JndiRegistry() {
038        }
039    
040        public JndiRegistry(Context context) {
041            this.context = context;
042        }
043    
044        public <T> T lookup(String name, Class<T> type) {
045            Object value = lookup(name);
046            return type.cast(value);
047        }
048    
049        public Object lookup(String name) {
050            try {
051                return getContext().lookup(name);
052            } catch (NameNotFoundException e) {
053                return null;
054            } catch (NamingException e) {
055                return null;
056            }
057        }
058    
059        public void bind(String s, Object o) {
060            try {
061                getContext().bind(s, o);
062            } catch (NamingException e) {
063                throw new RuntimeCamelException(e);
064            }
065        }
066    
067        public void close() throws NamingException {
068            getContext().close();
069        }
070    
071        public Context getContext() throws NamingException {
072            if (context == null) {
073                context = createContext();
074            }
075            return context;
076        }
077    
078        public void setContext(Context context) {
079            this.context = context;
080        }
081    
082        protected Context createContext() throws NamingException {
083            Hashtable properties = new Hashtable(System.getProperties());
084            return new InitialContext(properties);
085        }
086    }