    001package org.apache.turbine.modules.actions;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022import java.util.Hashtable;
    023import java.util.Iterator;
    024import java.util.Map.Entry;
    025import java.util.Properties;
    026
    027import javax.naming.InitialContext;
    028import javax.naming.NamingException;
    029
    030import org.apache.commons.configuration.Configuration;
    031import org.apache.turbine.annotation.TurbineConfiguration;
    032import org.apache.turbine.modules.Action;
    033import org.apache.turbine.pipeline.PipelineData;
    034import org.apache.turbine.util.RunData;
    035
    036/**
    037 * Used to initialize JNDI contexts.
    038 *
    039 * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
    040 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    041 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    042 * @version $Id: InitContextsAction.java 1695634 2015-08-13 00:35:47Z tv $
    043 */
    044public class InitContextsAction
    045        extends Action
    046{
    047    /** Injected configuration instance */
    048    @TurbineConfiguration
    049    private Configuration conf;
    050
    051    /**
    052     * This action will place the contexts defined in the
    053     * TurbineResources instance (if any) into the data.contexts
    054     * Hashtable.
    055     *
    056     * @param pipelineData The PipelineRunData object for the current request.
    057     * @exception NamingException could not create InitialContext
    058     */
    059    @Override
    060    public void doPerform(PipelineData pipelineData)
    061    throws NamingException
    062    {
    063        RunData data = getRunData(pipelineData);
    064        // Context properties are specified in lines in the properties
    065        // file that begin with "context.contextname.", allowing
    066        // multiple named contexts to be used.  Everything after the
    067        // "contextname." is the name of the property that will be
    068        // used by the InitialContext class to create a new context
    069        // instance.
    070
    071        Hashtable<String, Properties> contextPropsList = new Hashtable<String, Properties>();
    072        for (Iterator<String> contextKeys = conf.getKeys("context.");
    073                contextKeys.hasNext();)
    074        {
    075            String key = contextKeys.next();
    076            int start = key.indexOf(".") + 1;
    077            int end = key.indexOf(".", start);
    078            String contextName = key.substring(start, end);
    079            Properties contextProps = null;
    080            if (contextPropsList.containsKey(contextName))
    081            {
    082                contextProps = contextPropsList.get(contextName);
    083            }
    084            else
    085            {
    086                contextProps = new Properties();
    087            }
    088            contextProps.put(key.substring(end + 1),
    089                    conf.getString(key));
    090            contextPropsList.put(contextName, contextProps);
    091        }
    092
    093        for (Entry<String, Properties> contextProps : contextPropsList.entrySet())
    094        {
    095            InitialContext context = new InitialContext(contextProps.getValue());
    096            data.getJNDIContexts().put(contextProps.getKey(), context);
    097        }
    098    }
    099}