View Javadoc
1   /*
2    * 
3    * $Revision: 1.3 $
4    * $Date: 2004/06/20 12:18:39 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004 - strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * created: 2004-06-05  Manfred Wolff
23   */
24  package org.strutsit.chain.configuration;
25  
26  import java.io.InputStream;
27  import java.util.Properties;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * Reads the configuration of the chain-conf framework. If no configuration file
34   * named <code>chain.properties</code> is found in the classpath defaults will
35   * be set.
36   * 
37   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff </a>
38   * @since JDK 1.4
39   * @version $Revision: 1.3 $
40   */
41  public final class PropertyReader {
42  
43      /***
44       * XML mode
45       */
46      public static final String XML_MODE = "xml";
47  
48      /***
49       * Property mode
50       */
51      public static final String PROP_MODE = "prop";
52  
53      /***
54       * Class mode
55       */
56      public static final String CLASS_MODE = "class";
57  
58      private static final String CONF_FILE = "/chain.properties";
59  
60      private static final String DEFAULT_PATH = "/org/strutsit/chain/config";
61  
62      private static final String DEFAULT_NAME = "/chain-config.xml";
63  
64      private static final String PROP_MODE_STRING = "chain.configuration";
65  
66      private static final String CONF_FILE_STRING = "configuration.name";
67  
68      private static final String CONF_XML_CLASS = "chain.configuration.xml";
69  
70      /***
71       * The logger for this class (commons-logging).
72       */
73      private static Log log = LogFactory.getLog(PropertyReader.class);
74  
75      /***
76       * Configuration mode. Maybe xml, prop or class.
77       */
78      private String configMode = XML_MODE;
79  
80      /***
81       * Name of the configuration file
82       */
83      private String confName = DEFAULT_PATH + DEFAULT_NAME;
84  
85      /***
86       * The Default-Configurationclass for the XML Configuration
87       */
88      private String xmlClass = "org.strutsit.chain.configuration.ChainXMLConfiguration";
89  
90      /***
91       * The Instance of this singleton
92       */
93      private static PropertyReader instance;
94  
95      /***
96       * @return Returns the configMode.
97       */
98      public String getConfigMode() {
99          return configMode;
100     }
101 
102     /***
103      * @param configMode
104      *            The configMode to set.
105      */
106     public void setConfigMode(String configMode) {
107         this.configMode = configMode;
108     }
109 
110     /***
111      * @return Returns the confName.
112      */
113     public String getConfName() {
114         return confName;
115     }
116 
117     /***
118      * @param confName
119      *            The confName to set.
120      */
121     public void setConfName(String confName) {
122         this.confName = confName;
123     }
124 
125     /***
126      * @return Returns the confName.
127      */
128     public String getXmlClass() {
129         return xmlClass;
130     }
131 
132     /***
133      * @param className
134      *            The className to set.
135      */
136     public void setXmlClass(String className) {
137         this.xmlClass = className;
138     }
139 
140     /***
141      * Hiding the default constructor
142      */
143     private PropertyReader() {
144         super();
145     }
146 
147     /***
148      * Factorymethod for the Reader-Object.
149      * 
150      * @return The one and only instance of this class.
151      */
152     public static synchronized PropertyReader getInstance() {
153 
154         if (log.isTraceEnabled()) {
155             log.trace("getInstance -> START");
156         }
157         if (instance == null) {
158             instance = new PropertyReader();
159             instance.init();
160         }
161         if (log.isTraceEnabled()) {
162             log.trace("getInstance -> END");
163         }
164         return instance;
165     }
166 
167     /***
168      * Inits the Reader.
169      */
170     private void init() {
171 
172         if (log.isTraceEnabled()) {
173             log.trace("init -> START");
174         }
175         // First look, if there is a chain.properties file in the classpath
176         InputStream stream = this.getClass().getResourceAsStream(CONF_FILE);
177         if (stream != null) {
178 
179             Properties prop = new Properties();
180             try {
181                 prop.load(stream);
182             } catch (Exception e) {
183                 ; // nothing to do
184                 log.warn("init -> No Property file available");
185             }
186 
187             // Set confMode only if it is xml|prop|class
188             String tempConfMode = prop.getProperty(PROP_MODE_STRING);
189             if (XML_MODE.equals(tempConfMode) || PROP_MODE.equals(tempConfMode)
190                     || CLASS_MODE.equals(tempConfMode)) {
191                 setConfigMode(tempConfMode);
192 
193             }
194 
195             String tempName = prop.getProperty(CONF_FILE_STRING);
196             if (tempName != null) {
197                 setConfName(tempName);
198             }
199 
200             String xmlclass = prop.getProperty(CONF_XML_CLASS);
201             if (xmlclass != null) {
202                 setXmlClass(xmlclass);
203             }
204         }
205         if (log.isTraceEnabled()) {
206             log.trace("init -> END");
207         }
208     }
209 }