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.util.jndi;
018
019import java.util.Hashtable;
020
021import javax.naming.Context;
022import javax.naming.NamingException;
023
024/**
025 * A factory of the Camel {@link javax.naming.InitialContext} which allows a {@link java.util.Map} to be used to create a
026 * JNDI context.
027 * <p/>
028 * This implementation is singleton based, by creating a <b>new</b> context once, and reusing it on each call to
029 * {@link #getInitialContext(java.util.Hashtable)}.
030 *
031 * @version
032 */
033public class CamelSingletonInitialContextFactory extends CamelInitialContextFactory {
034
035    private static volatile Context context;
036
037    /**
038     * Gets or creates the context with the given environment.
039     * <p/>
040     * This implementation will create the context once, and then return the same instance
041     * on multiple calls.
042     *
043     * @param  environment  the environment, must not be <tt>null</tt>
044     * @return the created context.
045     * @throws javax.naming.NamingException is thrown if creation failed.
046     */
047    public synchronized Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
048        if (context == null) {
049            context = super.getInitialContext(environment);
050        }
051        return context;
052    }
053}