001: /*
002: * $Id: Configuration.java,v 1.14 2004/10/03 11:49:36 csaltos Exp $
003: *
004: * Copyright 2001 PUCE [http://www.puce.edu.ec]
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.oxyus.admin;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.util.MissingResourceException;
026: import java.util.PropertyResourceBundle;
027:
028: import org.apache.log4j.Logger;
029: import org.oxyus.util.Constants;
030:
031: /**
032: * Oxyus configuration reader.
033: *
034: * @author Carlos Saltos (csaltos[@]users.sourceforge.net)
035: */
036: public class Configuration {
037:
038: private static String home = null;
039:
040: private static boolean loggerActive = false;
041:
042: /**
043: * Reads the Oxyus home directory. To set this directory the option
044: * <code>-D</code> may be used when calling the JVM
045: * (i.e. <code>java -Doxyus.home=/usr/local/oxyus</code>)
046: * @return The oxyus home directory
047: */
048: public static String getHome() throws ConfigurationException {
049: if (home != null) {
050: return home;
051: }
052: home = System.getProperty(Constants.OXYUS_HOME);
053: if (home == null) {
054: String msg = "Unable to locate the oxyus home "
055: + "directory, please use the -D option of your java "
056: + "interpreter with the "
057: + Constants.CONFIGURATION_OXYUS + " property";
058: Logger log = Logger.getLogger(Configuration.class);
059: log.error(msg);
060: throw new ConfigurationException(msg);
061: }
062: return home;
063: }
064:
065: public static void setHome(String home) {
066: Configuration.home = home;
067: }
068:
069: public static boolean isLoggerActive() {
070: return Configuration.loggerActive;
071: }
072:
073: static void setLoggerActive(boolean loggerActive) {
074: Configuration.loggerActive = loggerActive;
075: }
076:
077: /**
078: * Reads the value of a property form the oxyus configuration
079: * @param property name
080: * @return value of the property
081: */
082: public static String getProperty(String property)
083: throws ConfigurationException {
084: String configFilename = getHome() + File.separator
085: + Constants.CONFIG_DIRECTORY + File.separator
086: + Constants.CONFIGURATION_OXYUS;
087: return getProperty(configFilename, property);
088: }
089:
090: /**
091: * Reads the value of a property from the test configuration
092: * @param property name
093: * @return value of the property
094: */
095: public static String getTestProperty(String property)
096: throws ConfigurationException {
097: String configFilename = getHome() + File.separator
098: + Constants.CONFIG_DIRECTORY + File.separator
099: + Constants.CONFIGURATION_TEST;
100: return getProperty(configFilename, property);
101: }
102:
103: /**
104: * Reads the value of a property
105: * @param configFilename file
106: * @param property name
107: * @return value of the property
108: */
109: public static String getProperty(String configFilename,
110: String property) throws ConfigurationException {
111: Logger log = Logger.getLogger(Configuration.class);
112: String value;
113: try {
114: FileInputStream configFile = new FileInputStream(
115: configFilename);
116: PropertyResourceBundle configResource = new PropertyResourceBundle(
117: configFile);
118: value = configResource.getString(property);
119: } catch (FileNotFoundException fnfe) {
120: String msg = "The config file " + configFilename
121: + " was not found";
122: log.error(msg, fnfe);
123: throw new ConfigurationException(msg, fnfe);
124: } catch (IOException ioe) {
125: String msg = "Error reading the config file "
126: + configFilename;
127: log.error(msg, ioe);
128: throw new ConfigurationException(msg, ioe);
129: } catch (MissingResourceException mre) {
130: String msg = "Unable to find the value of the "
131: + "property " + property + " in the config file "
132: + configFilename;
133: log.error(msg, mre);
134: throw new ConfigurationException(msg, mre);
135: }
136: return value;
137: }
138:
139: /**
140: * Read the path of the oxyus index directory
141: * @return value of the property <code>oxyus.index.directory</code>
142: */
143: public static String getIndexDirectory()
144: throws ConfigurationException {
145: return translateProperty(getProperty(Constants.PROPERTY_INDEX_DIRECTORY));
146: }
147:
148: /**
149: * Read the path of the oxyus database scripts support.
150: */
151: public static String getSuppportDirectory()
152: throws ConfigurationException {
153: return getHome() + File.separator + Constants.SUPPORT_DIRECTORY;
154: }
155:
156: public static String translateProperty(String value)
157: throws ConfigurationException {
158: /*
159: * FIXING 1032676: replacing File.separator with slash before
160: * translation for this to work in windows.
161: * TODO: use a direct translation with regex.
162: */
163: // Translate File.separator
164: String homeNormalized = getHome().replace(File.separatorChar,
165: '/');
166: // replace variables
167: String valueNormalized = value.replaceAll(
168: "\\$\\{oxyus\\.home\\}", homeNormalized);
169: return valueNormalized.replace('/', File.separatorChar);
170: }
171:
172: /**
173: * Read the store type. <code>internal</code> when using the hsqldb provided with the oxyus
174: * distribution. <code>external</code> for using other kind of database like postgresql.
175: * Note: when using an external database the scripts should be executed manually and the
176: * config store properties setted accurately.
177: * @return value of the property <code>oxyus.store.type</code>
178: */
179: public static String getStoreType() throws ConfigurationException {
180: return getProperty(Constants.PROPERTY_STORE_TYPE);
181: }
182:
183: /**
184: * Read the class name of the driver to connect to the oxyus database
185: * @return value of the property <code>oxyus.store.classdriver</code>
186: */
187: public static String getStoreClassDriver()
188: throws ConfigurationException {
189: return getProperty(Constants.PROPERTY_STORE_CLASSDRIVER);
190: }
191:
192: /**
193: * Read the oxyus database url to localize the database
194: * @return value of the property <code>oxyus.store.url</code>
195: */
196: public static String getStoreURL() throws ConfigurationException {
197: return translateProperty(getProperty(Constants.PROPERTY_STORE_URL));
198: }
199:
200: /**
201: * Reads the oxyus database user
202: * @return Value of the property <code>oxyus.store.user</code>
203: * @throws ConfigurationException a potential configuration error
204: */
205: public static String getStoreUser() throws ConfigurationException {
206: return getProperty(Constants.PROPERTY_STORE_USER);
207: }
208:
209: /**
210: * Reads the oxyus database password
211: * @return value of the property <code>oxyus.store.password</code>
212: */
213: public static String getStorePassword()
214: throws ConfigurationException {
215: return getProperty(Constants.PROPERTY_STORE_PASSWORD);
216: }
217:
218: /**
219: * Read the oxyus database catalog name (i.e. in Oracle the schema name)
220: * @return value of the property <code>oxyus.store.url</code>
221: */
222: public static String getStoreCatalog() {
223: try {
224: return getProperty(Constants.PROPERTY_STORE_CATALOG);
225: } catch (ConfigurationException ce) {
226: Logger log = Logger.getLogger(Configuration.class);
227: log.warn("Default catalog selected for oxyus repository");
228: return null;
229: }
230: }
231:
232: /**
233: * Reads the select statement to get the next value of a sequence
234: * for primary key generation in the oxyus database
235: * @return value of the property <code>oxyus.store.sequencequery</code>
236: */
237: public static String getStoreSequenceQuery()
238: throws ConfigurationException {
239: return getProperty(Constants.PROPERTY_STORE_SEQUENCE_QUERY);
240: }
241:
242: /**
243: * Reads the number of collectors for oxyus crawler
244: * @return value of the property <code>oxyus.crawler.collectors</code>
245: */
246: public static int getCrawlerCollectors()
247: throws ConfigurationException {
248: String collectorsValue = getProperty(Constants.PROPERTY_CRAWLER_COLLECTORS);
249: int collectors = 0;
250: try {
251: collectors = Integer.parseInt(collectorsValue);
252: } catch (NumberFormatException e) {
253: Logger log = Logger.getLogger(Configuration.class);
254: String msg = "The property "
255: + Constants.PROPERTY_CRAWLER_COLLECTORS + " in "
256: + Constants.CONFIGURATION_OXYUS + " should be a "
257: + "number";
258: log.error(msg);
259: throw new ConfigurationException(msg);
260: }
261: if (collectors <= 0) {
262: Logger log = Logger.getLogger(Configuration.class);
263: String msg = "The property "
264: + Constants.PROPERTY_CRAWLER_COLLECTORS + " in "
265: + Constants.CONFIGURATION_OXYUS + "should be a "
266: + "positive number";
267: log.error(msg);
268: throw new ConfigurationException(msg);
269: }
270: return collectors;
271: }
272:
273: /**
274: * Reads the number of collectors for oxyus crawler
275: * @return value of the property <code>oxyus.crawler.collectors</code>
276: */
277: public static int getMaxContentSize() throws ConfigurationException {
278: String collectorsValue = getProperty(Constants.PROPERTY_CRAWLER_MAX_CONTENT_SIZE);
279: int collectors = 0;
280: try {
281: collectors = Integer.parseInt(collectorsValue);
282: } catch (NumberFormatException e) {
283: Logger log = Logger.getLogger(Configuration.class);
284: String msg = "The property "
285: + Constants.PROPERTY_CRAWLER_MAX_CONTENT_SIZE
286: + " in " + Constants.CONFIGURATION_OXYUS
287: + " should be a " + "number";
288: log.error(msg);
289: throw new ConfigurationException(msg);
290: }
291: if (collectors <= 0) {
292: Logger log = Logger.getLogger(Configuration.class);
293: String msg = "The property "
294: + Constants.PROPERTY_CRAWLER_MAX_CONTENT_SIZE
295: + " in " + Constants.CONFIGURATION_OXYUS
296: + "should be a " + "positive number";
297: log.error(msg);
298: throw new ConfigurationException(msg);
299: }
300: return collectors;
301: }
302:
303: public static String getScopeConfigFilename()
304: throws ConfigurationException {
305: return getHome() + File.separator + Constants.CONFIG_DIRECTORY
306: + File.separator + Constants.CONFIGURATION_SCOPE;
307: }
308:
309: public static String getSchedulerConfigFilename()
310: throws ConfigurationException {
311: return getHome() + File.separator + Constants.CONFIG_DIRECTORY
312: + File.separator + Constants.SCHEDULER_CONFIGURATION;
313: }
314:
315: public static String getInitialPagesConfigFilename()
316: throws ConfigurationException {
317: return getHome() + File.separator + Constants.CONFIG_DIRECTORY
318: + File.separator
319: + Constants.CONFIGURATION_INITIAL_PAGES;
320: }
321:
322: public static String getLoggerConfigFilename()
323: throws ConfigurationException {
324: return Configuration.getHome() + File.separator
325: + Constants.CONFIG_DIRECTORY + File.separator
326: + Constants.LOGGER_CONFIGURATION;
327: }
328:
329: }
|