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 */
017package org.apache.camel.impl;
018
019import java.util.Hashtable;
020import java.util.LinkedHashMap;
021import java.util.LinkedHashSet;
022import java.util.Map;
023import java.util.Set;
024import javax.naming.Context;
025import javax.naming.InitialContext;
026import javax.naming.NameClassPair;
027import javax.naming.NameNotFoundException;
028import javax.naming.NamingEnumeration;
029import javax.naming.NamingException;
030
031import org.apache.camel.NoSuchBeanException;
032import org.apache.camel.RuntimeCamelException;
033import org.apache.camel.spi.Registry;
034import org.apache.camel.util.jndi.CamelInitialContextFactory;
035
036/**
037 * A {@link Registry} implementation which looks up the objects in JNDI
038 */
039public class JndiRegistry implements Registry {
040    private Context context;
041    private Map environment;
042    private final boolean standalone;
043
044    public JndiRegistry() {
045        this.standalone = false;
046    }
047
048    public JndiRegistry(Map environment) {
049        this.environment = environment;
050        this.standalone = false;
051    }
052
053    public JndiRegistry(Context context) {
054        this.context = context;
055        this.standalone = false;
056    }
057
058    /**
059     * Whether to use standalone mode, where the JNDI initial context factory is using
060     * {@link CamelInitialContextFactory}.
061     */
062    public JndiRegistry(boolean standalone) {
063        this.standalone = true;
064    }
065
066    public <T> T lookupByNameAndType(String name, Class<T> type) {
067        Object answer = lookupByName(name);
068
069        // just to be safe
070        if (answer == null) {
071            return null;
072        }
073
074        try {
075            return type.cast(answer);
076        } catch (Throwable e) {
077            String msg = "Found bean: " + name + " in JNDI Context: " + context
078                    + " of type: " + answer.getClass().getName() + " expected type was: " + type;
079            throw new NoSuchBeanException(name, msg, e);
080        }
081    }
082
083    public Object lookupByName(String name) {
084        try {
085            return getContext().lookup(name);
086        } catch (NameNotFoundException e) {
087            return null;
088        } catch (NamingException e) {
089            return null;
090        }
091    }
092
093    public <T> Map<String, T> findByTypeWithName(Class<T> type) {
094        Map<String, T> answer = new LinkedHashMap<>();
095        try {
096            NamingEnumeration<NameClassPair> list = getContext().list("");
097            while (list.hasMore()) {
098                NameClassPair pair = list.next();
099                Object instance = context.lookup(pair.getName());
100                if (type.isInstance(instance)) {
101                    answer.put(pair.getName(), type.cast(instance));
102                }
103            }
104        } catch (NamingException e) {
105            // ignore
106        }
107
108        return answer;
109    }
110
111    public <T> Set<T> findByType(Class<T> type) {
112        Set<T> answer = new LinkedHashSet<>();
113        try {
114            NamingEnumeration<NameClassPair> list = getContext().list("");
115            while (list.hasMore()) {
116                NameClassPair pair = list.next();
117                Object instance = context.lookup(pair.getName());
118                if (type.isInstance(instance)) {
119                    answer.add(type.cast(instance));
120                }
121            }
122        } catch (NamingException e) {
123            // ignore
124        }
125        return answer;
126    }
127
128    public Object lookup(String name) {
129        return lookupByName(name);
130    }
131
132    public <T> T lookup(String name, Class<T> type) {
133        return lookupByNameAndType(name, type);
134    }
135
136    public <T> Map<String, T> lookupByType(Class<T> type) {
137        return findByTypeWithName(type);
138    }
139
140    public void bind(String name, Object object) {
141        try {
142            getContext().bind(name, object);
143        } catch (NamingException e) {
144            throw new RuntimeCamelException(e);
145        }
146    }
147
148    public void close() throws NamingException {
149        if (context != null) {
150            context.close();
151        }
152    }
153
154    public Context getContext() throws NamingException {
155        if (context == null) {
156            context = createContext();
157        }
158        return context;
159    }
160
161    public void setContext(Context context) {
162        this.context = context;
163    }
164
165    protected Context createContext() throws NamingException {
166        Hashtable<Object, Object> properties = new Hashtable<>(System.getProperties());
167        if (environment != null) {
168            properties.putAll(environment);
169        }
170        // must include a factory if none provided in standalone mode
171        if (standalone && !properties.containsKey("java.naming.factory.initial")) {
172            properties.put("java.naming.factory.initial", "org.apache.camel.util.jndi.CamelInitialContextFactory");
173        }
174        return new InitialContext(properties);
175    }
176}