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.Collections;
020    import java.util.Hashtable;
021    import java.util.Map;
022    import javax.naming.Context;
023    import javax.naming.InitialContext;
024    import javax.naming.NameNotFoundException;
025    import javax.naming.NamingException;
026    
027    import org.apache.camel.RuntimeCamelException;
028    import org.apache.camel.spi.Registry;
029    
030    /**
031     * A {@link Registry} implementation which looks up the objects in JNDI
032     * 
033     * @version $Revision: 762641 $
034     */
035    public class JndiRegistry implements Registry {
036        private Context context;
037    
038        public JndiRegistry() {
039        }
040    
041        public JndiRegistry(Context context) {
042            this.context = context;
043        }
044    
045        public <T> T lookup(String name, Class<T> type) {
046            Object value = lookup(name);
047            return type.cast(value);
048        }
049    
050        public Object lookup(String name) {
051            try {
052                return getContext().lookup(name);
053            } catch (NameNotFoundException e) {
054                return null;
055            } catch (NamingException e) {
056                return null;
057            }
058        }
059    
060        @SuppressWarnings("unchecked")
061        public <T> Map<String, T> lookupByType(Class<T> type) {
062            // not implemented so we return an empty map
063            return Collections.EMPTY_MAP;
064        }
065    
066        public void bind(String s, Object o) {
067            try {
068                getContext().bind(s, o);
069            } catch (NamingException e) {
070                throw new RuntimeCamelException(e);
071            }
072        }
073    
074        public void close() throws NamingException {
075            getContext().close();
076        }
077    
078        public Context getContext() throws NamingException {
079            if (context == null) {
080                context = createContext();
081            }
082            return context;
083        }
084    
085        public void setContext(Context context) {
086            this.context = context;
087        }
088    
089        @SuppressWarnings("unchecked")
090        protected Context createContext() throws NamingException {
091            Hashtable properties = new Hashtable(System.getProperties());
092            return new InitialContext(properties);
093        }
094    }