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.NoSuchBeanException;
028    import org.apache.camel.RuntimeCamelException;
029    import org.apache.camel.spi.Registry;
030    
031    /**
032     * A {@link Registry} implementation which looks up the objects in JNDI
033     * 
034     * @version 
035     */
036    public class JndiRegistry implements Registry {
037        private Context context;
038    
039        public JndiRegistry() {
040        }
041    
042        public JndiRegistry(Context context) {
043            this.context = context;
044        }
045    
046        public <T> T lookup(String name, Class<T> type) {
047            Object answer = lookup(name);
048    
049            // just to be safe
050            if (answer == null) {
051                return null;
052            }
053    
054            try {
055                return type.cast(answer);
056            } catch (Throwable e) {
057                String msg = "Found bean: " + name + " in JNDI Context: " + context
058                        + " of type: " + answer.getClass().getName() + " expected type was: " + type;
059                throw new NoSuchBeanException(name, msg, e);
060            }
061        }
062    
063        public Object lookup(String name) {
064            try {
065                return getContext().lookup(name);
066            } catch (NameNotFoundException e) {
067                return null;
068            } catch (NamingException e) {
069                return null;
070            }
071        }
072    
073        public <T> Map<String, T> lookupByType(Class<T> type) {
074            // not implemented so we return an empty map
075            return Collections.emptyMap();
076        }
077    
078        public void bind(String s, Object o) {
079            try {
080                getContext().bind(s, o);
081            } catch (NamingException e) {
082                throw new RuntimeCamelException(e);
083            }
084        }
085    
086        public void close() throws NamingException {
087            getContext().close();
088        }
089    
090        public Context getContext() throws NamingException {
091            if (context == null) {
092                context = createContext();
093            }
094            return context;
095        }
096    
097        public void setContext(Context context) {
098            this.context = context;
099        }
100    
101        protected Context createContext() throws NamingException {
102            Hashtable<?, ?> properties = new Hashtable<Object, Object>(System.getProperties());
103            return new InitialContext(properties);
104        }
105    }