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.configuration2.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$ 043 */ 044public class InitContextsAction implements Action 045{ 046 /** Injected configuration instance */ 047 @TurbineConfiguration 048 private Configuration conf; 049 050 /** 051 * This action will place the contexts defined in the TurbineResources 052 * instance (if any) into the data.contexts Hashtable. 053 * 054 * @param pipelineData 055 * The PipelineRunData object for the current request. 056 * @throws NamingException 057 * could not create InitialContext 058 */ 059 @Override 060 public void doPerform(PipelineData pipelineData) 061 throws NamingException 062 { 063 RunData data = pipelineData.getRunData(); 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<>(); 072 for (Iterator<String> contextKeys = conf.getKeys("context."); 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), conf.getString(key)); 088 contextPropsList.put(contextName, contextProps); 089 } 090 091 for (Entry<String, Properties> contextProps : contextPropsList.entrySet()) 092 { 093 InitialContext context = new InitialContext(contextProps.getValue()); 094 data.getJNDIContexts().put(contextProps.getKey(), context); 095 } 096 } 097}