KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > conf > AntPropertiesModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.forrest.conf;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.Serviceable;
30 import org.apache.avalon.framework.thread.ThreadSafe;
31 import org.apache.cocoon.components.modules.input.AbstractJXPathModule;
32 import org.apache.cocoon.components.modules.input.InputModule;
33 import org.apache.excalibur.source.Source;
34 import org.apache.excalibur.source.SourceResolver;
35
36 /**
37  * Input module for accessing properties in a properties file
38  * roughly compatible with Ant property files, where ${name}
39  * is replaced with the value of the property 'name' if
40  * declared beforehand.
41  *
42  * <p>
43  * The properties file can only be configured statically and
44  * is resolved via the SourceResolver system.
45  * </p>
46  *
47 */

48 public class AntPropertiesModule extends AbstractJXPathModule
49 implements InputModule, Serviceable, Configurable, ThreadSafe {
50     
51     private SourceResolver m_resolver;
52     private Properties JavaDoc m_properties;
53     
54     public void service(ServiceManager manager) throws ServiceException {
55         m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
56     }
57     
58     /**
59      * Configure the location of the properties file:
60      * <p>
61      * <code>&lt;file SRC="resource://my.properties" /&gt;</code>
62      * </p>
63      */

64     public void configure(Configuration configuration) throws ConfigurationException {
65         super.configure(configuration);
66         String JavaDoc file = configuration.getChild("file").getAttribute("src");
67         load(file);
68     }
69     
70     protected void load(String JavaDoc file) throws ConfigurationException {
71         Source source = null;
72         InputStream JavaDoc in = null;
73         try {
74             source = m_resolver.resolveURI(file);
75            
76             in = source.getInputStream();
77             
78             m_properties = new AntProperties();
79             m_properties.load(in);
80  
81         }
82         catch (IOException JavaDoc e) {
83             throw new ConfigurationException("Cannot load properties file " + file);
84         }
85         finally {
86             if (source != null) {
87                 m_resolver.release(source);
88             }
89             if (in != null) {
90                 try {
91                     in.close();
92                 }
93                 catch (IOException JavaDoc e) {
94                 }
95             }
96         }
97     }
98     
99     protected Object JavaDoc getContextObject(Configuration modeConf, Map JavaDoc objectModel)
100         throws ConfigurationException {
101         
102         return m_properties;
103     }
104
105 }
106
Popular Tags