001package org.apache.turbine.services.naming;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.Map;
027import java.util.Properties;
028
029import javax.naming.Context;
030import javax.naming.InitialContext;
031import javax.naming.NamingException;
032
033import org.apache.commons.configuration2.Configuration;
034import org.apache.logging.log4j.Logger;
035import org.apache.logging.log4j.LogManager;
036import org.apache.turbine.Turbine;
037import org.apache.turbine.services.InitializationException;
038import org.apache.turbine.services.TurbineBaseService;
039
040/**
041 * This class is the default implementation of NamingService, which
042 * provides JNDI naming contexts.
043 *
044 * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
045 * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
046 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
047 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
048 * @version $Id$
049 */
050public class TurbineNamingService
051        extends TurbineBaseService
052        implements NamingService
053{
054    /** Logging */
055    private static Logger log = LogManager.getLogger(TurbineNamingService.class);
056
057    /**
058     * A global Map of Property objects which are initialised using
059     * parameters from the ResourcesFile
060     */
061    private static Map<String, Properties> contextPropsList = null;
062
063    /** All initial contexts known to this service */
064    private final Map<String, InitialContext> initialContexts = new HashMap<>();
065
066    /**
067     * Called the first time the Service is used.<br>
068     *
069     */
070    @Override
071    public void init()
072            throws InitializationException
073    {
074        // Context properties are specified in lines in the properties
075        // file that begin with "context.contextname.", allowing
076        // multiple named contexts to be used.  Everything after the
077        // "contextname."  is the name of the property that will be
078        // used by the InitialContext class to create a new context
079        // instance.
080
081        Configuration conf = Turbine.getConfiguration();
082        try
083        {
084            contextPropsList = new HashMap<>();
085
086            for (Iterator<String> contextKeys = conf.subset("context").getKeys();
087                 contextKeys.hasNext();)
088            {
089                String key = contextKeys.next();
090                int end = key.indexOf(".");
091
092                if (end == -1)
093                {
094                    continue;
095                }
096
097                String contextName = key.substring(0, end);
098                Properties contextProps = null;
099
100                if (contextPropsList.containsKey(contextName))
101                {
102                    contextProps = contextPropsList.get(contextName);
103                }
104                else
105                {
106                    contextProps = new Properties();
107                }
108
109                contextProps.put(key.substring(end + 1),
110                        conf.getString(key));
111
112                contextPropsList.put(contextName, contextProps);
113            }
114
115            for (Map.Entry<String, Properties> entry : contextPropsList.entrySet())
116            {
117                String key = entry.getKey();
118                Properties contextProps = entry.getValue();
119                InitialContext context = new InitialContext(contextProps);
120                initialContexts.put(key, context);
121            }
122
123            setInit(true);
124        }
125        catch (NamingException e)
126        {
127            log.error("Failed to initialize JDNI contexts!", e);
128
129            throw new InitializationException(
130                    "Failed to initialize JDNI contexts!");
131        }
132    }
133
134
135    /**
136     * Return the Context with the specified name.  The Context is
137     * constructed using the properties for the context with the
138     * specified name; ie. those properties that start with
139     * "services.servicename.properties.name.".
140     *
141     * @param contextName The name of the context.
142     * @return The context with the specified name, or null if no
143     * context exists with that name.
144     */
145    @Override
146    public Context getContext(String contextName)
147    {
148        // Get just the properties for the context with the specified
149        // name.
150        Properties contextProps = null;
151
152        if (contextPropsList.containsKey(contextName))
153        {
154            contextProps = contextPropsList.get(contextName);
155        }
156        else
157        {
158            contextProps = new Properties();
159        }
160
161        // Construct a new context with the properties.
162        try
163        {
164            return new InitialContext(contextProps);
165        }
166        catch (Exception e)
167        {
168            return null;
169        }
170    }
171}