KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > PropertiesFileModule


1 /*
2  * Copyright 1999-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.cocoon.components.modules.input;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.excalibur.source.Source;
31 import org.apache.excalibur.source.SourceResolver;
32
33 /**
34  * Input module for accessing properties in a properties file.
35  *
36  * <p>
37  * The properties file can only be configured statically and
38  * is resolved via the SourceResolver system.
39  * </p>
40  *
41  * @author <a HREF="mailto:unico@apache.org">Unico Hommes</a>
42  */

43 public class PropertiesFileModule extends AbstractJXPathModule
44 implements InputModule, Serviceable, Configurable, ThreadSafe {
45     
46     private ServiceManager m_manager;
47     private SourceResolver m_resolver;
48     private Properties JavaDoc m_properties;
49     
50     
51     /* (non-Javadoc)
52      * @see Serviceable#service(ServiceManager)
53      */

54     public void service(ServiceManager manager) throws ServiceException {
55         m_manager = manager;
56         m_resolver = (SourceResolver) m_manager.lookup(SourceResolver.ROLE);
57     }
58     
59     /* (non-Javadoc)
60      * @see org.apache.avalon.framework.activity.Disposable#dispose()
61      */

62     public void dispose() {
63         super.dispose();
64         if ( this.m_manager != null ) {
65             this.m_manager.release( this.m_resolver );
66             this.m_manager = null;
67             this.m_resolver = null;
68         }
69     }
70
71     /**
72      * Configure the location of the properties file:
73      * <p>
74      * <code>&lt;file SRC="resource://my.properties" /&gt;</code>
75      * </p>
76      */

77     public void configure(Configuration configuration) throws ConfigurationException {
78         super.configure(configuration);
79         String JavaDoc file = configuration.getChild("file").getAttribute("src");
80         load(file);
81     }
82     
83     private void load(String JavaDoc file) throws ConfigurationException {
84         Source source = null;
85         InputStream JavaDoc stream = null;
86         try {
87             source = m_resolver.resolveURI(file);
88             stream = source.getInputStream();
89             m_properties = new Properties JavaDoc();
90             m_properties.load(stream);
91         } catch (IOException JavaDoc e) {
92             throw new ConfigurationException("Cannot load properties file " + file);
93         } finally {
94             if (source != null) {
95                 m_resolver.release(source);
96             }
97             if (stream != null) {
98                 try {
99                     stream.close();
100                 } catch (IOException JavaDoc ignored) {
101                 }
102             }
103         }
104     }
105     
106     protected Object JavaDoc getContextObject(Configuration modeConf, Map JavaDoc objectModel)
107         throws ConfigurationException {
108         
109         return m_properties;
110     }
111 }
112
Popular Tags