1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.pipeline.valve.impl;
1819import org.apache.commons.configuration.ConfigurationException;
20import org.apache.commons.configuration.PropertiesConfiguration;
21import org.apache.jetspeed.pipeline.PipelineException;
22import org.apache.jetspeed.pipeline.valve.Valve;
23import org.apache.jetspeed.pipeline.valve.ValveContext;
24import org.apache.jetspeed.request.RequestContext;
2526/***27 * The purpose of this valve is to load a property file and make the information28 * available on the request as an attribute. The name of the attribute is the29 * key that is passed into the constructor.30 * 31 * There are 3 different ways to use this object: 1. Provide a key and a32 * PropertiesConfiguration object 2. Provide a key and a path to a properties33 * file 3. Provide a string which is used for both the key and as an environment34 * lookup to find the path to a properties file.35 * 36 * The PropertiesConfiguration object is put on the request and can be consumed37 * by anyone downstream of this valve38 * 39 * Multiple valves can be configured via Spring and put into the pipeline if40 * more than one property file is needed.41 * 42 * Spring configuration samples are shown below: <bean43 * id="ProductionConfiguration"44 * class="org.apache.commons.configuration.PropertiesConfiguration">45 * <constructor-arg> <value>/apps/jetspeed/etc/jetspeed-production.properties</value>46 * </constructor-arg> </bean>47 * 48 * <bean id="propertyLoaderValve_1"49 * class="com.fmr.portal.pipeline.impl.PropertyLoaderValve"50 * init-method="initialize"> <constructor-arg index="0"> <value>php-properties</value>51 * </constructor-arg> <constructor-arg index="1"52 * type="org.apache.commons.configuration.PropertiesConfiguration"> <ref53 * bean="ProductionConfiguration"/> </constructor-arg> </bean>54 * 55 * <bean id="propertyLoaderValve_2"56 * class="com.fmr.portal.pipeline.impl.PropertyLoaderValve"57 * init-method="initialize"> <constructor-arg index="0"> <value>php-properties</value>58 * </constructor-arg>59 * 60 * <constructor-arg index="1">61 * <value>/apps/jetspeed/etc/jetspeed-production.properties</value>62 * </constructor-arg> </bean>63 * 64 * <bean id="propertyLoaderValve_3"65 * class="com.fmr.portal.pipeline.impl.PropertyLoaderValve"66 * init-method="initialize"> <constructor-arg index="0"> <value>app.props</value>67 * </constructor-arg> </bean>68 * 69 * For this last one, an environment variable with the name "app.props" would70 * contain the file path to the properties file.71 * 72 * 73 * @author David Gurney74 * 75 */76publicclassPropertyLoaderValve implements Valve
77 {
78protected String m_sKey = null;
7980protected PropertiesConfiguration m_oPropertiesConfiguration = null;
8182protected String m_sPropertyFilePath = null;
8384publicPropertyLoaderValve(String p_sKey,
85 PropertiesConfiguration p_oPropertiesConfiguration)
86 {
87 m_sKey = p_sKey;
88 m_oPropertiesConfiguration = p_oPropertiesConfiguration;
89 }
9091publicPropertyLoaderValve(String p_sKey, String p_sPropertyFilePath)
92 {
93 m_sKey = p_sKey;
94 m_sPropertyFilePath = p_sPropertyFilePath;
95 }
9697/***98 * 99 * @param p_sEnvironmentKey -100 * This value will be used both as the storage key and as the101 * name to use when looking up an environment variable. The102 * environment variable should contain the file path of the103 * properties file to be loaded104 */105publicPropertyLoaderValve(String p_sEnvironmentKey)
106 {
107 m_sKey = p_sEnvironmentKey;
108 }
109110publicvoid initialize() throws PipelineException
111 {
112// Get the property file path if necessary113if (m_sPropertyFilePath == null && m_oPropertiesConfiguration == null)
114 {
115 m_sPropertyFilePath = System.getProperty(m_sKey);
116 }
117118// Load the file if the path is provided119if (m_sPropertyFilePath != null && m_oPropertiesConfiguration == null)
120 {
121try122 {
123 m_oPropertiesConfiguration = new PropertiesConfiguration(
124 m_sPropertyFilePath);
125 } catch (ConfigurationException e)
126 {
127thrownew PipelineException(e);
128 }
129 }
130131// If we still have a null, create an empty properties configuration132// anyway133if (m_oPropertiesConfiguration == null)
134 {
135 m_oPropertiesConfiguration = new PropertiesConfiguration();
136 }
137 }
138139publicvoid invoke(RequestContext p_oRequest, ValveContext p_oContext)
140 throws PipelineException
141 {
142 p_oRequest.getRequest()
143 .setAttribute(m_sKey, m_oPropertiesConfiguration);
144145if (p_oContext != null)
146 {
147 p_oContext.invokeNext(p_oRequest);
148 }
149 }
150 }