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 1773378 2016-12-09 13:19:59Z 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 TurbineResources 053 * instance (if any) into the data.contexts Hashtable. 054 * 055 * @param pipelineData 056 * The PipelineRunData object for the current request. 057 * @throws NamingException 058 * could not create InitialContext 059 */ 060 @Override 061 public void doPerform(PipelineData pipelineData) 062 throws NamingException 063 { 064 RunData data = getRunData(pipelineData); 065 // Context properties are specified in lines in the properties 066 // file that begin with "context.contextname.", allowing 067 // multiple named contexts to be used. Everything after the 068 // "contextname." is the name of the property that will be 069 // used by the InitialContext class to create a new context 070 // instance. 071 072 Hashtable<String, Properties> contextPropsList = new Hashtable<String, Properties>(); 073 for (Iterator<String> contextKeys = conf.getKeys("context."); 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), conf.getString(key)); 089 contextPropsList.put(contextName, contextProps); 090 } 091 092 for (Entry<String, Properties> contextProps : contextPropsList.entrySet()) 093 { 094 InitialContext context = new InitialContext(contextProps.getValue()); 095 data.getJNDIContexts().put(contextProps.getKey(), context); 096 } 097 } 098}