View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.services.resources;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.FileInputStream;
21  import java.util.Properties;
22  import java.util.Iterator;
23  import java.util.Map.Entry;
24  
25  import javax.servlet.ServletConfig;
26  
27  // Turbine stuff
28  import org.apache.turbine.services.resources.TurbineResourceService;
29  import org.apache.turbine.services.resources.ResourceService;
30  import org.apache.turbine.services.InitializationException;
31  
32  // Commons classes
33  import org.apache.commons.configuration.Configuration;
34  
35  import org.apache.jetspeed.services.resources.JetspeedResources;
36  
37  /***
38   * <p>This service subclasses <code>TurbineResourceService</code> and
39   * provides functionality for overriding properties in default resource
40   * files. This override behavior is extended to non-string properties</p>
41   * 
42   * <P>To override properties:
43   * <ul>
44   * <li>Define your own property file containing properties you want to override (for example, my.properties)</li>
45   * <li>Add the following property in my.properties file:
46   * <code>services.ResourceService.classname = org.apache.jetspeed.services.resources.JetspeedResourceService</code></li>
47   * <li>Include TurbineResources.properties at the end of my.properties file</li>
48   * <li>Set <code>properties</code> init parameter in web.xml to <code>my.properties</code></li>
49   * <li>Set <code>resources</code> init parameter in web.xml to
50   * <code>org.apache.jetspeed.services.resources.JetspeedResourceService</code></li>
51   * </ul>
52   * 
53   * <P><B>Important note on overriding services.</B>Order of initializing services may be important.
54   * Overriding a service may change this order. It is important that services attempt to initialize
55   * dependent services in their early init methods. For example, to make sure that ServletService is
56   * running, invoke the following code:
57   * <PRE>
58   * TurbineServices.getInstance().initService(ServletService.SERVICE_NAME, conf);
59   * </PRE>
60   * </P>
61   * 
62   * <P>Also, ${variable} substitution is extended to non-string properties. For example, the following
63   * property references are valid:
64   * <PRE>
65   * confRoot=/WEB-INF/conf
66   * 
67   * psmlMapFile=${confRoot}/psml-mapping.xml
68   * registryMapFile=${confRoot}/registry-mapping.xml
69   * 
70   * defaultRefresh=60
71   * 
72   * registryRefresh=${defaultRefresh}
73   * psmlRefresh=${defaultRefresh}
74   * </PRE>
75   * </P>
76   * 
77   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
78   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
79   * 
80   * @version $Id: JetspeedResourceService.java,v 1.8 2004/02/23 03:29:53 jford Exp $
81   */
82  public class JetspeedResourceService
83  extends TurbineResourceService
84  {
85      /***
86       * The purpose of this method is to get the configuration resource
87       * with the given name as a boolean value.
88       *
89       * @param name The resource name.
90       * @return The value of the named resource as a boolean.
91       */
92      public boolean getBoolean(String name)
93      {
94  
95          return new Boolean(interpolate(getConfiguration().getString(name))).booleanValue();
96      }
97  
98      /***
99       * The purppose of this method is to get the configuration
100      * resource with the given name as a boolean value, or a default
101      * value.
102      *
103      * @param name The resource name.
104      * @param def The default value of the resource.
105      * @return The value of the named resource as a boolean.
106      */
107     public boolean getBoolean(String name, boolean def)
108     {
109 
110         String temp = interpolate(getConfiguration().getString(name));
111         return temp != null ? new Boolean(temp).booleanValue() : def;
112     }
113 
114     /***
115      * The purpose of this method is to get the configuration resource
116      * with the given name as a double.
117      *
118      * @param name The resoource name.
119      * @return The value of the named resource as double.
120      */
121     public double getDouble(String name)
122     {
123 
124         return new Double(interpolate(getConfiguration().getString(name))).doubleValue();
125     }
126 
127     /***
128      * The purpose of this method is to get the configuration resource
129      * with the given name as a double, or a default value.
130      *
131      * @param name The resource name.
132      * @param def The default value of the resource.
133      * @return The value of the named resource as a double.
134      */
135     public double getDouble(String name, double def)
136     {
137 
138         String temp = interpolate(getConfiguration().getString(name));
139         return temp != null ? new Double(temp).doubleValue() : def;
140     }
141 
142     /***
143      * The purpose of this method is to get the configuration resource
144      * with the given name as a float.
145      *
146      * @param name The resource name.
147      * @return The value of the resource as a float.
148      */
149     public float getFloat(String name)
150     {
151 
152         return new Float(interpolate(getConfiguration().getString(name))).floatValue();
153     }
154 
155     /***
156      * The purpose of this method is to get the configuration resource
157      * with the given name as a float, or a default value.
158      *
159      * @param name The resource name.
160      * @param def The default value of the resource.
161      * @return The value of the resource as a float.
162      */
163     public float getFloat(String name, float def)
164     {
165 
166         String temp = interpolate(getConfiguration().getString(name));
167         return temp != null ? new Float(temp).floatValue() : def;
168     }
169 
170     /***
171      * The purpose of this method is to get the configuration resource
172      * with the given name as an integer.
173      *
174      * @param name The resource name.
175      * @return The value of the resource as an integer.
176      */
177     public int getInt(String name)
178     {
179 
180         return new Integer(interpolate(getConfiguration().getString(name))).intValue();
181     }
182 
183     /***
184      * The purpose of this method is to get the configuration resource
185      * with the given name as an integer, or a default value.
186      *
187      * @param name The resource name.
188      * @param def The default value of the resource.
189      * @return The value of the resource as an integer.
190      */
191     public int getInt(String name, int def)
192     {
193 
194         String temp = interpolate(getConfiguration().getString(name));
195         return temp != null ? new Integer(temp).intValue() : def;
196     }
197 
198     /***
199      * The purpose of this method is to get the configuration resource
200      * with the given name as a long.
201      *
202      * @param name The resource name.
203      * @return The value of the resource as a long.
204      */
205     public long getLong(String name)
206     {
207 
208         return new Long(interpolate(getConfiguration().getString(name))).longValue();
209     }
210 
211     /***
212      * The purpose of this method is to get the configuration resource
213      * with the given name as a long, or a default value.
214      *
215      * @param name The resource name.
216      * @param def The default value of the resource.
217      * @return The value of the resource as a long.
218      */
219     public long getLong(String name, long def)
220     {
221 
222         String temp = interpolate(getConfiguration().getString(name));
223         return temp != null ? new Long(temp).longValue() : def;
224     }
225 
226     /***
227      * The purpose of this method is to extract a subset of configuraton
228      * resources sharing a common name prefix. The prefix is stripped
229      * from the names of the resulting resources.
230      *
231      * @param prefix the common name prefix
232      * @return A ResourceService providing the subset of configuration.
233      */
234     public ResourceService getResources(String prefix)
235     {
236         Configuration config = getConfiguration().subset(prefix);
237 
238         if (config == null)
239         {
240             return null;
241         }
242 
243         JetspeedResourceService res = new JetspeedResourceService();
244         try 
245         {
246             res.init(config);
247         }
248         catch (Exception e)
249         {
250             System.err.println("Exception in init of JetspeedResourceService" + e.getMessage());
251             e.printStackTrace();
252         }
253 
254         return (ResourceService) res;
255     }
256 
257     /***
258      * This method is called when the Service is initialized
259      * It provides a way to override properties at runtime.
260      * To use 'runtime' time properties, define a directory where you are keeping your
261      * 'runtime' parameters and pass it in as a System Property named 'jetspeed.conf.dir'
262      * 
263      * This implementation will take the name of the web application (i.e. 'jetspeed') and use it to 
264      * find the name of a properties file. These properties are merged with the TurbineResources/JetspeedResources.properties
265      * Similarly, you can override Torque properties by naming a file in your runtime directory as ${webapp.name}_Torque.properties
266      * 
267      * Examples:
268      *    jetspeed.conf.dir = /Users/sysadmin/conf
269      *    
270      *    This directory contains two files:
271      *      1. jetspeed.properties - overrides properties in JetspeedResources.properties and TurbineResources.properties
272      *      2. jetspeed_torque.properties - overrides properties in Torque.properties
273      * 
274      * @param config a ServletConfig object
275      */
276     public void init()
277         throws InitializationException
278     {     
279         System.out.println("Jetspeed Services: Starting with no parameters");
280         super.init();
281     }
282        
283     public synchronized void init(ServletConfig config) throws InitializationException        
284     {               
285         String propsDir = null;
286         String appName = config.getServletName();
287         String deployFilename = appName + ".properties";
288         String torqueFilename = appName + "_torque.properties";
289         super.init(config);
290         
291         // Display product information
292         //System.out.println("Jetspeed Services: Starting servlet: [" + appName +"]");
293         String version = getString(JetspeedResources.JETSPEED_VERSION_KEY);
294         String name = getString(JetspeedResources.JETSPEED_NAME_KEY);
295         if (version != null && name != null)
296         {
297             System.out.println("");
298             System.out.println("Starting " + name + "/" + version);
299             System.out.println("");
300         }
301         
302         try
303         {
304             propsDir = System.getProperty("jetspeed.conf.dir", null);
305             if (null == propsDir)
306             {
307                 // no deploy-time directory defined to find properties, return
308                 return;
309             }
310 
311 
312             String torqueProps = makeFileNamePath(propsDir, torqueFilename);
313             String deployProps = makeFileNamePath(propsDir, deployFilename);
314 
315             System.out.println("torque props = " + torqueProps);
316             System.out.println("deploy props = " + deployProps);
317 
318             File deployFile = new File(deployProps);
319             if (deployFile.exists())
320             {
321                 FileInputStream is = new FileInputStream(deployProps);
322                 Properties props = new Properties();
323                 props.load(is);
324 
325                 Iterator it = props.entrySet().iterator();
326                 while (it.hasNext())
327                 {
328                     Entry entry = (Entry)it.next();
329                     //if (entry.getValue() != null && ((String)entry.getValue()).length() > 0)
330                     this.setProperty((String)entry.getKey(), (String)entry.getValue());
331                     System.out.println("setting key/value: " + entry.getKey() + ":" + entry.getValue());                        
332                 }
333             }
334             else
335             {
336                 String msg = "Failed to find Deploy properties: " + deployProps;
337                 System.err.println(msg);
338             }
339 
340             File torqueFile = new File(torqueProps);
341             if (torqueFile.exists())
342             {
343                 this.setProperty("component.torque.config", torqueProps);
344                 
345                 FileInputStream tis = new FileInputStream(torqueProps);
346                 Properties tprops = new Properties();
347                 tprops.load(tis);
348                 
349                 System.out.println("Connecting to: "+tprops.getProperty("database.default.url"));
350                 System.out.println("Database Username: "+tprops.getProperty("database.default.username"));
351             }
352         }
353         catch (IOException e)
354         {
355             StringBuffer msg = new StringBuffer("Error reading properties for appName: ");
356             msg.append(appName);
357             msg.append(", props Dir: " + propsDir);
358             System.err.println("Exception in loading properties: " + propsDir);
359             e.printStackTrace();
360         }
361     }
362 
363 
364     protected String makeFileNamePath(String propsDir, String fileName)
365     {
366         StringBuffer name = new StringBuffer(propsDir);
367     
368         if (!propsDir.endsWith(File.separator))
369         {
370             name.append(File.separator);
371         }
372         name.append(fileName);
373         return name.toString();
374     }
375 
376 }