View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * 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 and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.pipeline.valve.impl;
18  
19  import org.apache.commons.configuration.ConfigurationException;
20  import org.apache.commons.configuration.PropertiesConfiguration;
21  import org.apache.jetspeed.pipeline.PipelineException;
22  import org.apache.jetspeed.pipeline.valve.Valve;
23  import org.apache.jetspeed.pipeline.valve.ValveContext;
24  import org.apache.jetspeed.request.RequestContext;
25  
26  /***
27   * The purpose of this valve is to load a property file and make the information
28   * available on the request as an attribute. The name of the attribute is the
29   * key that is passed into the constructor.
30   * 
31   * There are 3 different ways to use this object: 1. Provide a key and a
32   * PropertiesConfiguration object 2. Provide a key and a path to a properties
33   * file 3. Provide a string which is used for both the key and as an environment
34   * lookup to find the path to a properties file.
35   * 
36   * The PropertiesConfiguration object is put on the request and can be consumed
37   * by anyone downstream of this valve
38   * 
39   * Multiple valves can be configured via Spring and put into the pipeline if
40   * more than one property file is needed.
41   * 
42   * Spring configuration samples are shown below: <bean
43   * 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"> <ref
53   * 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" would
70   * contain the file path to the properties file.
71   * 
72   * 
73   * @author David Gurney
74   * 
75   */
76  public class PropertyLoaderValve implements Valve
77  {
78      protected String m_sKey = null;
79  
80      protected PropertiesConfiguration m_oPropertiesConfiguration = null;
81  
82      protected String m_sPropertyFilePath = null;
83  
84      public PropertyLoaderValve(String p_sKey,
85              PropertiesConfiguration p_oPropertiesConfiguration)
86      {
87          m_sKey = p_sKey;
88          m_oPropertiesConfiguration = p_oPropertiesConfiguration;
89      }
90  
91      public PropertyLoaderValve(String p_sKey, String p_sPropertyFilePath)
92      {
93          m_sKey = p_sKey;
94          m_sPropertyFilePath = p_sPropertyFilePath;
95      }
96  
97      /***
98       * 
99       * @param p_sEnvironmentKey -
100      *            This value will be used both as the storage key and as the
101      *            name to use when looking up an environment variable. The
102      *            environment variable should contain the file path of the
103      *            properties file to be loaded
104      */
105     public PropertyLoaderValve(String p_sEnvironmentKey)
106     {
107         m_sKey = p_sEnvironmentKey;
108     }
109 
110     public void initialize() throws PipelineException
111     {
112         // Get the property file path if necessary
113         if (m_sPropertyFilePath == null && m_oPropertiesConfiguration == null)
114         {
115             m_sPropertyFilePath = System.getProperty(m_sKey);
116         }
117 
118         // Load the file if the path is provided
119         if (m_sPropertyFilePath != null && m_oPropertiesConfiguration == null)
120         {
121             try
122             {
123                 m_oPropertiesConfiguration = new PropertiesConfiguration(
124                         m_sPropertyFilePath);
125             } catch (ConfigurationException e)
126             {
127                 throw new PipelineException(e);
128             }
129         }
130 
131         // If we still have a null, create an empty properties configuration
132         // anyway
133         if (m_oPropertiesConfiguration == null)
134         {
135             m_oPropertiesConfiguration = new PropertiesConfiguration();
136         }
137     }
138 
139     public void invoke(RequestContext p_oRequest, ValveContext p_oContext)
140             throws PipelineException
141     {
142         p_oRequest.getRequest()
143                 .setAttribute(m_sKey, m_oPropertiesConfiguration);
144 
145         if (p_oContext != null)
146         {
147             p_oContext.invokeNext(p_oRequest);
148         }
149     }
150 }