001 package 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
022 import java.util.Hashtable;
023 import java.util.Iterator;
024 import java.util.Properties;
025 import javax.naming.InitialContext;
026 import javax.naming.NamingException;
027
028 import org.apache.commons.configuration.Configuration;
029
030 import org.apache.turbine.Turbine;
031 import org.apache.turbine.modules.Action;
032 import org.apache.turbine.pipeline.PipelineData;
033 import org.apache.turbine.util.RunData;
034
035 /**
036 * Used to initialize JNDI contexts.
037 *
038 * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
039 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
041 * @version $Id: InitContextsAction.java 1066529 2011-02-02 17:01:46Z ludwig $
042 */
043 public class InitContextsAction
044 extends Action
045 {
046 /**
047 * This action will place the contexts defined in the
048 * TurbineResources instance (if any) into the data.contexts
049 * Hashtable.
050 *
051 * @deprecated Use PipelineData version instead.
052 * @param data The RunData object for the current request.
053 * @exception NamingException could not create InitialContext
054 */
055 @SuppressWarnings("unchecked")
056 @Deprecated
057 @Override
058 public void doPerform(RunData data)
059 throws NamingException
060 {
061 Configuration conf = Turbine.getConfiguration();
062
063 // Context properties are specified in lines in the properties
064 // file that begin with "context.contextname.", allowing
065 // multiple named contexts to be used. Everything after the
066 // "contextname." is the name of the property that will be
067 // used by the InitialContext class to create a new context
068 // instance.
069
070 Hashtable<String, Properties> contextPropsList = new Hashtable<String, Properties>();
071 for (Iterator<String> contextKeys = conf.getKeys("context.");
072 contextKeys.hasNext();)
073 {
074 String key = contextKeys.next();
075 int start = key.indexOf(".") + 1;
076 int end = key.indexOf(".", start);
077 String contextName = key.substring(start, end);
078 Properties contextProps = null;
079 if (contextPropsList.containsKey(contextName))
080 {
081 contextProps = contextPropsList.get(contextName);
082 }
083 else
084 {
085 contextProps = new Properties();
086 }
087 contextProps.put(key.substring(end + 1),
088 conf.getString(key));
089 contextPropsList.put(contextName, contextProps);
090 }
091 for (Iterator<String> contextPropsKeys = contextPropsList.keySet().iterator();
092 contextPropsKeys.hasNext();)
093 {
094 String key = contextPropsKeys.next();
095 Properties contextProps = contextPropsList.get(key);
096 InitialContext context = new InitialContext(contextProps);
097 data.getJNDIContexts().put(key, context);
098 }
099 }
100
101 /**
102 * This action will place the contexts defined in the
103 * TurbineResources instance (if any) into the data.contexts
104 * Hashtable.
105 *
106 * @param pipelineData The PipelineRunData object for the current request.
107 * @exception NamingException could not create InitialContext
108 */
109 @Override
110 public void doPerform(PipelineData pipelineData)
111 throws NamingException
112 {
113 RunData data = getRunData(pipelineData);
114 doPerform(data);
115 }
116
117
118 }