1 package org.apache.turbine.services.resources;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.IOException;
58 import java.util.Enumeration;
59 import java.util.Iterator;
60 import java.util.Properties;
61 import java.util.Vector;
62 import javax.servlet.ServletConfig;
63 import org.apache.commons.configuration.BaseConfiguration;
64 import org.apache.commons.configuration.Configuration;
65 import org.apache.commons.configuration.ConfigurationConverter;
66 import org.apache.commons.configuration.PropertiesConfiguration;
67 import org.apache.turbine.Turbine;
68 import org.apache.turbine.services.InitializationException;
69 import org.apache.turbine.services.TurbineBaseService;
70 import org.apache.turbine.services.TurbineServices;
71 import org.apache.turbine.util.TurbineConfig;
72 import org.apache.turbine.util.TurbineException;
73
74
75 /***
76 * <p>This implementation of the <code>resourcesService</code> relies
77 * on an external properties file for storing the configuration keys
78 * and values.</p>
79 *
80 * <P>In order to be compatible with legacy applications, this implementation
81 * kept a static method for initializing the service, so it's still possible
82 * to write the following code:
83 * <p><code>
84 * TurbineResourceService.setPropertiesName("d:/conf/Turbine.properties");
85 * Vector myVar = TurbineResources.getVector("myvar");
86 * </code></p>
87 *
88 * <p>The new way to do things is to look at the org.apache.turbine.util.TurbineConfig
89 * class.</p>
90 *
91 * @author <a href="mailto:jm@mediaphil.de">Jonas Maurus</a>
92 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
93 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
94 * @author <a href="mailto:luta.raphael@networks.vivendi.net">Raphaël Luta</a>
95 * @author <a href="mailto:jvanzyl@periapt.com@">Jason van Zyl</a>
96 * @version $Id: TurbineResourceService.java,v 1.8 2002/07/11 16:53:25 mpoeschl Exp $
97 */
98 public class TurbineResourceService
99 extends TurbineBaseService
100 implements ResourceService
101 {
102 /*** The container for the generic resources. */
103 //private GenericResources generic = null;
104 private Configuration configuration = null;
105
106 private static final String START_TOKEN="${";
107 private static final String END_TOKEN="}";
108
109 /***
110 * Performs early initialization. Overrides init() method in
111 * BaseService to detect objects used in Turbine's Service
112 * initialization and pass them to apropriate init() methods.
113 *
114 * @param data An Object to use for initialization activities.
115 * @exception InitializationException, if initialization of this
116 * class was not successful.
117 */
118 public void init( Object data )
119 throws InitializationException
120 {
121 if (data instanceof ServletConfig)
122 {
123 init((ServletConfig)data);
124 }
125 else if (data instanceof Properties)
126 {
127 init((Properties)data);
128 }
129 else if (data instanceof Configuration)
130 {
131 init((Configuration)data);
132 }
133 }
134
135 /***
136 * This method is called when the Service is initialized
137 *
138 * @param config a ServletConfig object
139 */
140 public void init(ServletConfig config)
141 throws InitializationException
142 {
143 String props = config.getInitParameter(TurbineServices.PROPERTIES_PATH_KEY);
144
145 if(props == null)
146 {
147 props = TurbineServices.PROPERTIES_PATH_DEFAULT;
148 }
149
150 // This will attempt to find the location of the properties
151 // file from the relative path to the WAR archive (ie:
152 // docroot). Since JServ returns null for getRealPath()
153 // because it was never implemented correctly, then we know we
154 // will not have an issue with using it this way. I don't know
155 // if this will break other servlet engines, but it probably
156 // shouldn't since WAR files are the future anyways.
157 //props = ServletUtils.expandRelative(config, props);
158 props = Turbine.getRealPath(props);
159
160 try
161 {
162 init((Configuration) new PropertiesConfiguration(props));
163 }
164 catch (IOException e)
165 {
166 e.printStackTrace();
167 throw new InitializationException("Can't load file " + props);
168 }
169 }
170
171 /***
172 * Init the service with the given properties filename
173 *
174 * @deprecated
175 * @param propertiesFileName The file name.
176 * @exception IOException, if there was an I/O problem.
177 */
178 public static void setPropertiesFileName(String propertiesFileName)
179 throws TurbineException
180 {
181 Configuration mappings = (Configuration) new BaseConfiguration();
182
183 mappings.setProperty(ResourceService.SERVICE_NAME,
184 TurbineResourceService.class.getName());
185
186 TurbineServices services = (TurbineServices) TurbineServices.getInstance();
187 services.initMapping(mappings);
188 services.initServices(new TurbineConfig(".", propertiesFileName), true);
189 }
190
191 /***
192 * Init the service with the given properties object. Called
193 * from Cocoon to initialize Turbine.
194 *
195 * @param properties The java.util.Properties object sent from another process such as
196 * Cocoon. This Properties object contains all of the necessary properties
197 * found in the TurbineResources.properties file.
198 * @exception TurbineException, if there was an I/O problem.
199 */
200 public static void setProperties(Properties properties)
201 throws TurbineException
202 {
203 Configuration mappings = (Configuration) new BaseConfiguration();
204
205 mappings.setProperty(ResourceService.SERVICE_NAME,
206 TurbineResourceService.class.getName());
207
208 TurbineServices services = (TurbineServices) TurbineServices.getInstance();
209 services.initMapping(mappings);
210 services.initServices(properties, true);
211 }
212
213 /***
214 * Set a property in with a key=value pair.
215 *
216 * @param String key
217 * @param String value
218 */
219 public void setProperty(String key, String value)
220 {
221 configuration.setProperty(key,value);
222 }
223
224 protected String interpolate(String base)
225 {
226 if (base == null)
227 {
228 return null;
229 }
230
231 //Get the full ResourceService (we could be in a subset instance)
232 ResourceService top = TurbineResources.getService();
233
234 int begin = -1;
235 int end = -1;
236 int prec = 0-END_TOKEN.length();
237 String variable = null;
238 StringBuffer result = new StringBuffer();
239
240 // FIXME: we should probably allow the escaping of the start token
241 while ( ((begin=base.indexOf(START_TOKEN,prec+END_TOKEN.length()))>-1)
242 && ((end=base.indexOf(END_TOKEN,begin))>-1) )
243 {
244 result.append(base.substring(prec+END_TOKEN.length(),begin));
245 variable = base.substring(begin+START_TOKEN.length(),end);
246 if (top.getString(variable)!=null)
247 {
248 result.append(top.getString(variable));
249 }
250 prec=end;
251 }
252 result.append(base.substring(prec+END_TOKEN.length(),base.length()));
253
254 return result.toString();
255 }
256
257 /***
258 * Wrapper around the configuration resources.
259 *
260 * @return A Configuration.
261 */
262 public Configuration getConfiguration()
263 {
264 return configuration;
265 }
266
267 /***
268 * Initializer method that sets up the configuration resources.
269 *
270 * @param confs A Configurations object.
271 */
272 private void init(Configuration configuration)
273 {
274 this.configuration = configuration;
275 setInit(true);
276 }
277
278 /***
279 * The purpose of this method is to init the configuration
280 * resource with a Properties object sent from a different system.
281 * For example, a Properties sent over from Cocoon. The reason
282 * for this code is to provide a bridge between an
283 * org.apache.turbine.util.Configurations class and an
284 * org.apache.cocoon.framework.Configurations class.
285 *
286 * @param props A Properties object.
287 */
288 private void init(Properties props)
289 {
290 Configuration configuration = ConfigurationConverter
291 .getConfiguration(props);
292 init(configuration);
293 }
294
295 /***
296 * The purpose of this method is to get the configuration resource
297 * with the given name as a boolean value.
298 *
299 * @param name The resource name.
300 * @return The value of the named resource as a boolean.
301 */
302 public boolean getBoolean(String name)
303 {
304 return getConfiguration().getBoolean(name);
305 }
306
307 /***
308 * The purppose of this method is to get the configuration
309 * resource with the given name as a boolean value, or a default
310 * value.
311 *
312 * @param name The resource name.
313 * @param def The default value of the resource.
314 * @return The value of the named resource as a boolean.
315 */
316 public boolean getBoolean(String name, boolean def)
317 {
318 return getConfiguration().getBoolean(name, def);
319 }
320
321 /***
322 * The purpose of this method is to get the configuration resource
323 * with the given name as a double.
324 *
325 * @param name The resoource name.
326 * @return The value of the named resource as double.
327 */
328 public double getDouble(String name)
329 {
330 return getConfiguration().getDouble(name);
331 }
332
333 /***
334 * The purpose of this method is to get the configuration resource
335 * with the given name as a double, or a default value.
336 *
337 * @param name The resource name.
338 * @param def The default value of the resource.
339 * @return The value of the named resource as a double.
340 */
341 public double getDouble(String name, double def)
342 {
343 return getConfiguration().getDouble(name, def);
344 }
345
346 /***
347 * The purpose of this method is to get the configuration resource
348 * with the given name as a float.
349 *
350 * @param name The resource name.
351 * @return The value of the resource as a float.
352 */
353 public float getFloat(String name)
354 {
355 return getConfiguration().getFloat(name);
356 }
357
358 /***
359 * The purpose of this method is to get the configuration resource
360 * with the given name as a float, or a default value.
361 *
362 * @param name The resource name.
363 * @param def The default value of the resource.
364 * @return The value of the resource as a float.
365 */
366 public float getFloat(String name, float def)
367 {
368 return getConfiguration().getFloat(name, def);
369 }
370
371 /***
372 * The purpose of this method is to get the configuration resource
373 * with the given name as an integer.
374 *
375 * @param name The resource name.
376 * @return The value of the resource as an int.
377 */
378 public int getInt(String name)
379 {
380 return getConfiguration().getInt(name);
381 }
382
383 /***
384 * The purpose of this method is to get the configuration resource
385 * with the given name as an integer, or a default value.
386 *
387 * @param name The resource name.
388 * @param def The default value of the resource.
389 * @return The value of the resource as an integer.
390 */
391 public int getInt(String name, int def)
392 {
393 return getConfiguration().getInt(name, def);
394 }
395
396 /***
397 * Get the list of the keys contained in the configuration
398 * repository.
399 *
400 * @return An Enumeration with all the keys.
401 */
402 //public Enumeration getKeys()
403 public Iterator getKeys()
404 {
405 return getConfiguration().getKeys();
406 }
407
408 /***
409 * Get the list of the keys contained in the configuration
410 * repository that match the specified prefix.
411 *
412 * @param prefix A String prefix to test against.
413 * @return An Enumeration of keys that match the prefix.
414 */
415 public Iterator getKeys(String prefix)
416 {
417 return getConfiguration().getKeys(prefix);
418 }
419
420 /***
421 * The purpose of this method is to get the configuration resource
422 * with the given name as a long.
423 *
424 * @param name The resource name.
425 * @return The value of the resource as a long.
426 */
427 public long getLong(String name)
428 {
429 return getConfiguration().getLong(name);
430 }
431
432 /***
433 * The purpose of this method is to get the configuration resource
434 * with the given name as a long, or a default value.
435 *
436 * @param name The resource name.
437 * @param def The default value of the resource.
438 * @return The value of the resource as a long.
439 */
440 public long getLong(String name, long def)
441 {
442 return getConfiguration().getLong(name, def);
443 }
444
445 /***
446 * The purpose of this method is to get the configuration resource
447 * with the given name as a string.
448 *
449 * @param name The resource name.
450 * @return The value of the resource as a string.
451 */
452 public String getString(String name)
453 {
454 return interpolate(getConfiguration().getString(name));
455 }
456
457 /***
458 * The purpose of this method is to get the configuration resource
459 * with the given name as a string, or a default value.
460 *
461 * @param name The resource name.
462 * @param def The default value of the resource.
463 * @return The value of the resource as a string.
464 */
465 public String getString(String name, String def)
466 {
467 return interpolate(getConfiguration().getString(name, def));
468 }
469
470 /***
471 * The purpose of this method is to get the configuration resource
472 * with the given name as a string array.
473 *
474 * @param name The resource name.
475 * @return The value of the resource as a string array.
476 */
477 public String[] getStringArray(String name)
478 {
479 String[] std = getConfiguration().getStringArray(name);
480
481 if (std != null)
482 {
483 for(int i=0; i<std.length; i++)
484 {
485 std[i]=interpolate(std[i]);
486 }
487 }
488
489 return std;
490 }
491
492 /***
493 * The purpose of this method is to get the configuration resource
494 * with the given name as a vector.
495 *
496 * @param name The resource name.
497 * @return The value of the resource as a vector.
498 */
499 public Vector getVector(String name)
500 {
501 Vector std = getConfiguration().getVector(name);
502
503 if (std != null)
504 {
505 Vector newstd = new Vector();
506 Enumeration en = std.elements();
507 while (en.hasMoreElements())
508 {
509 newstd.addElement(interpolate((String)en.nextElement()));
510 }
511 std = newstd;
512 }
513
514 return std;
515 }
516
517 /***
518 * The purpose of this method is to get the configuration resource
519 * with the given name as a vector, or a default value.
520 *
521 * @param name The resource name.
522 * @param def The default value of the resource.
523 * @return The value of the resource as a vector.
524 */
525 public Vector getVector(String name, Vector def)
526 {
527 Vector std = getVector(name);
528 if (std == null)
529 {
530 if (def != null)
531 {
532 std = new Vector();
533 Enumeration en = def.elements();
534 while (en.hasMoreElements())
535 {
536 std.addElement(interpolate((String)en.nextElement()));
537 }
538 }
539 }
540
541 return std;
542 }
543
544 /***
545 * The purpose of this method is to extract a subset of configuraton
546 * resources sharing a common name prefix. The prefix is stripped
547 * from the names of the resulting resources.
548 *
549 * @param prefix the common name prefix
550 * @return A ResourceService providing the subset of configuration.
551 */
552 public ResourceService getResources(String prefix)
553 {
554 Configuration config = getConfiguration().subset(prefix);
555
556 if (config == null)
557 {
558 return null;
559 }
560
561 TurbineResourceService res = new TurbineResourceService();
562 res.init(config);
563 return (ResourceService)res;
564 }
565
566 /***
567 * The purpose of this method is to extract a subset of configuraton
568 * resources sharing a common name prefix. The prefix is stripped
569 * from the names of the resulting resources.
570 *
571 * @param prefix the common name prefix
572 * @return A Configuration providing the subset of configuration.
573 */
574 public Configuration getConfiguration(String prefix)
575 {
576 Configuration config = getConfiguration().subset(prefix);
577
578 if (config == null)
579 {
580 return null;
581 }
582
583 return config;
584 }
585 }
This page was automatically generated by Maven