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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16packageorg.apache.jetspeed.services.resources;
1718import java.io.File;
19import java.io.IOException;
20import java.io.FileInputStream;
21import java.util.Properties;
22import java.util.Iterator;
23import java.util.Map.Entry;
2425import javax.servlet.ServletConfig;
2627// Turbine stuff28import org.apache.turbine.services.resources.TurbineResourceService;
29import org.apache.turbine.services.resources.ResourceService;
30import org.apache.turbine.services.InitializationException;
3132// Commons classes33import org.apache.commons.configuration.Configuration;
3435import org.apache.jetspeed.services.resources.JetspeedResources;
3637/***38 * <p>This service subclasses <code>TurbineResourceService</code> and39 * provides functionality for overriding properties in default resource40 * 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 to50 * <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 initialize55 * dependent services in their early init methods. For example, to make sure that ServletService is56 * 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 following63 * property references are valid:64 * <PRE>65 * confRoot=/WEB-INF/conf66 * 67 * psmlMapFile=${confRoot}/psml-mapping.xml68 * registryMapFile=${confRoot}/registry-mapping.xml69 * 70 * defaultRefresh=6071 * 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 */82publicclassJetspeedResourceService83extends TurbineResourceService
84 {
85/***86 * The purpose of this method is to get the configuration resource87 * 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 */92publicboolean getBoolean(String name)
93 {
9495returnnew Boolean(interpolate(getConfiguration().getString(name))).booleanValue();
96 }
9798/***99 * The purppose of this method is to get the configuration100 * resource with the given name as a boolean value, or a default101 * 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 */107publicboolean getBoolean(String name, boolean def)
108 {
109110 String temp = interpolate(getConfiguration().getString(name));
111return temp != null ? new Boolean(temp).booleanValue() : def;
112 }
113114/***115 * The purpose of this method is to get the configuration resource116 * 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 */121publicdouble getDouble(String name)
122 {
123124returnnew Double(interpolate(getConfiguration().getString(name))).doubleValue();
125 }
126127/***128 * The purpose of this method is to get the configuration resource129 * 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 */135publicdouble getDouble(String name, double def)
136 {
137138 String temp = interpolate(getConfiguration().getString(name));
139return temp != null ? new Double(temp).doubleValue() : def;
140 }
141142/***143 * The purpose of this method is to get the configuration resource144 * 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 */149publicfloat getFloat(String name)
150 {
151152returnnew Float(interpolate(getConfiguration().getString(name))).floatValue();
153 }
154155/***156 * The purpose of this method is to get the configuration resource157 * 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 */163publicfloat getFloat(String name, float def)
164 {
165166 String temp = interpolate(getConfiguration().getString(name));
167return temp != null ? new Float(temp).floatValue() : def;
168 }
169170/***171 * The purpose of this method is to get the configuration resource172 * 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 */177publicint getInt(String name)
178 {
179180returnnew Integer(interpolate(getConfiguration().getString(name))).intValue();
181 }
182183/***184 * The purpose of this method is to get the configuration resource185 * 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 */191publicint getInt(String name, int def)
192 {
193194 String temp = interpolate(getConfiguration().getString(name));
195return temp != null ? new Integer(temp).intValue() : def;
196 }
197198/***199 * The purpose of this method is to get the configuration resource200 * 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 */205publiclong getLong(String name)
206 {
207208returnnew Long(interpolate(getConfiguration().getString(name))).longValue();
209 }
210211/***212 * The purpose of this method is to get the configuration resource213 * 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 */219publiclong getLong(String name, long def)
220 {
221222 String temp = interpolate(getConfiguration().getString(name));
223return temp != null ? new Long(temp).longValue() : def;
224 }
225226/***227 * The purpose of this method is to extract a subset of configuraton228 * resources sharing a common name prefix. The prefix is stripped229 * from the names of the resulting resources.230 *231 * @param prefix the common name prefix232 * @return A ResourceService providing the subset of configuration.233 */234public ResourceService getResources(String prefix)
235 {
236 Configuration config = getConfiguration().subset(prefix);
237238if (config == null)
239 {
240returnnull;
241 }
242243JetspeedResourceService res = newJetspeedResourceService();
244try245 {
246 res.init(config);
247 }
248catch (Exception e)
249 {
250 System.err.println("Exception in init of JetspeedResourceService" + e.getMessage());
251 e.printStackTrace();
252 }
253254return (ResourceService) res;
255 }
256257/***258 * This method is called when the Service is initialized259 * It provides a way to override properties at runtime.260 * To use 'runtime' time properties, define a directory where you are keeping your261 * '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.properties265 * Similarly, you can override Torque properties by naming a file in your runtime directory as ${webapp.name}_Torque.properties266 * 267 * Examples:268 * jetspeed.conf.dir = /Users/sysadmin/conf269 * 270 * This directory contains two files:271 * 1. jetspeed.properties - overrides properties in JetspeedResources.properties and TurbineResources.properties272 * 2. jetspeed_torque.properties - overrides properties in Torque.properties273 * 274 * @param config a ServletConfig object275 */276publicvoid init()
277 throws InitializationException
278 {
279 System.out.println("Jetspeed Services: Starting with no parameters");
280super.init();
281 }
282283publicsynchronizedvoid 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";
289super.init(config);
290291// Display product information292//System.out.println("Jetspeed Services: Starting servlet: [" + appName +"]");293 String version = getString(JetspeedResources.JETSPEED_VERSION_KEY);
294 String name = getString(JetspeedResources.JETSPEED_NAME_KEY);
295if (version != null && name != null)
296 {
297 System.out.println("");
298 System.out.println("Starting " + name + "/" + version);
299 System.out.println("");
300 }
301302try303 {
304 propsDir = System.getProperty("jetspeed.conf.dir", null);
305if (null == propsDir)
306 {
307// no deploy-time directory defined to find properties, return308return;
309 }
310311312 String torqueProps = makeFileNamePath(propsDir, torqueFilename);
313 String deployProps = makeFileNamePath(propsDir, deployFilename);
314315 System.out.println("torque props = " + torqueProps);
316 System.out.println("deploy props = " + deployProps);
317318 File deployFile = new File(deployProps);
319if (deployFile.exists())
320 {
321 FileInputStream is = new FileInputStream(deployProps);
322 Properties props = new Properties();
323 props.load(is);
324325 Iterator it = props.entrySet().iterator();
326while (it.hasNext())
327 {
328 Entry entry = (Entry)it.next();
329//if (entry.getValue() != null && ((String)entry.getValue()).length() > 0)330this.setProperty((String)entry.getKey(), (String)entry.getValue());
331 System.out.println("setting key/value: " + entry.getKey() + ":" + entry.getValue());
332 }
333 }
334else335 {
336 String msg = "Failed to find Deploy properties: " + deployProps;
337 System.err.println(msg);
338 }
339340 File torqueFile = new File(torqueProps);
341if (torqueFile.exists())
342 {
343this.setProperty("component.torque.config", torqueProps);
344345 FileInputStream tis = new FileInputStream(torqueProps);
346 Properties tprops = new Properties();
347 tprops.load(tis);
348349 System.out.println("Connecting to: "+tprops.getProperty("database.default.url"));
350 System.out.println("Database Username: "+tprops.getProperty("database.default.username"));
351 }
352 }
353catch (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 }
362363364protected String makeFileNamePath(String propsDir, String fileName)
365 {
366 StringBuffer name = new StringBuffer(propsDir);
367368if (!propsDir.endsWith(File.separator))
369 {
370 name.append(File.separator);
371 }
372 name.append(fileName);
373return name.toString();
374 }
375376 }