001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.pipeline.valve.impl;
018:
019: import org.apache.commons.configuration.ConfigurationException;
020: import org.apache.commons.configuration.PropertiesConfiguration;
021: import org.apache.jetspeed.pipeline.PipelineException;
022: import org.apache.jetspeed.pipeline.valve.Valve;
023: import org.apache.jetspeed.pipeline.valve.ValveContext;
024: import org.apache.jetspeed.request.RequestContext;
025:
026: /**
027: * The purpose of this valve is to load a property file and make the information
028: * available on the request as an attribute. The name of the attribute is the
029: * key that is passed into the constructor.
030: *
031: * There are 3 different ways to use this object: 1. Provide a key and a
032: * PropertiesConfiguration object 2. Provide a key and a path to a properties
033: * file 3. Provide a string which is used for both the key and as an environment
034: * lookup to find the path to a properties file.
035: *
036: * The PropertiesConfiguration object is put on the request and can be consumed
037: * by anyone downstream of this valve
038: *
039: * Multiple valves can be configured via Spring and put into the pipeline if
040: * more than one property file is needed.
041: *
042: * Spring configuration samples are shown below: <bean
043: * id="ProductionConfiguration"
044: * class="org.apache.commons.configuration.PropertiesConfiguration">
045: * <constructor-arg> <value>/apps/jetspeed/etc/jetspeed-production.properties</value>
046: * </constructor-arg> </bean>
047: *
048: * <bean id="propertyLoaderValve_1"
049: * class="com.fmr.portal.pipeline.impl.PropertyLoaderValve"
050: * init-method="initialize"> <constructor-arg index="0"> <value>php-properties</value>
051: * </constructor-arg> <constructor-arg index="1"
052: * type="org.apache.commons.configuration.PropertiesConfiguration"> <ref
053: * bean="ProductionConfiguration"/> </constructor-arg> </bean>
054: *
055: * <bean id="propertyLoaderValve_2"
056: * class="com.fmr.portal.pipeline.impl.PropertyLoaderValve"
057: * init-method="initialize"> <constructor-arg index="0"> <value>php-properties</value>
058: * </constructor-arg>
059: *
060: * <constructor-arg index="1">
061: * <value>/apps/jetspeed/etc/jetspeed-production.properties</value>
062: * </constructor-arg> </bean>
063: *
064: * <bean id="propertyLoaderValve_3"
065: * class="com.fmr.portal.pipeline.impl.PropertyLoaderValve"
066: * init-method="initialize"> <constructor-arg index="0"> <value>app.props</value>
067: * </constructor-arg> </bean>
068: *
069: * For this last one, an environment variable with the name "app.props" would
070: * contain the file path to the properties file.
071: *
072: *
073: * @author David Gurney
074: *
075: */
076: public class PropertyLoaderValve implements Valve {
077: protected String m_sKey = null;
078:
079: protected PropertiesConfiguration m_oPropertiesConfiguration = null;
080:
081: protected String m_sPropertyFilePath = null;
082:
083: public PropertyLoaderValve(String p_sKey,
084: PropertiesConfiguration p_oPropertiesConfiguration) {
085: m_sKey = p_sKey;
086: m_oPropertiesConfiguration = p_oPropertiesConfiguration;
087: }
088:
089: public PropertyLoaderValve(String p_sKey, String p_sPropertyFilePath) {
090: m_sKey = p_sKey;
091: m_sPropertyFilePath = p_sPropertyFilePath;
092: }
093:
094: /**
095: *
096: * @param p_sEnvironmentKey -
097: * This value will be used both as the storage key and as the
098: * name to use when looking up an environment variable. The
099: * environment variable should contain the file path of the
100: * properties file to be loaded
101: */
102: public PropertyLoaderValve(String p_sEnvironmentKey) {
103: m_sKey = p_sEnvironmentKey;
104: }
105:
106: public void initialize() throws PipelineException {
107: // Get the property file path if necessary
108: if (m_sPropertyFilePath == null
109: && m_oPropertiesConfiguration == null) {
110: m_sPropertyFilePath = System.getProperty(m_sKey);
111: }
112:
113: // Load the file if the path is provided
114: if (m_sPropertyFilePath != null
115: && m_oPropertiesConfiguration == null) {
116: try {
117: m_oPropertiesConfiguration = new PropertiesConfiguration(
118: m_sPropertyFilePath);
119: } catch (ConfigurationException e) {
120: throw new PipelineException(e);
121: }
122: }
123:
124: // If we still have a null, create an empty properties configuration
125: // anyway
126: if (m_oPropertiesConfiguration == null) {
127: m_oPropertiesConfiguration = new PropertiesConfiguration();
128: }
129: }
130:
131: public void invoke(RequestContext p_oRequest,
132: ValveContext p_oContext) throws PipelineException {
133: p_oRequest.getRequest().setAttribute(m_sKey,
134: m_oPropertiesConfiguration);
135:
136: if (p_oContext != null) {
137: p_oContext.invokeNext(p_oRequest);
138: }
139: }
140: }
|