KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > net > Configuration


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  */

17
18 /* $Id: Configuration.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.net;
21
22 import java.net.URL JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.log4j.Category;
26
27
28 /**
29  * Read configuration
30  */

31 public class Configuration {
32     static Category log = Category.getInstance(Configuration.class);
33     public static final String JavaDoc DEFAULT_CONFIGURATION_FILE = "org/apache/lenya/net/conf.properties";
34     public static final String JavaDoc DEFAULT_CONFIGURATION_KEY = "lenya.configuration";
35     public static final String JavaDoc OVERRIDE_DEFAULT_CONFIGURATION_KEY = "override.lenya.configuration";
36     public String JavaDoc configurationPath = null;
37     public String JavaDoc smtpHost = null;
38     public String JavaDoc smtpPort = null;
39     public String JavaDoc smtpDomain = null;
40
41     /**
42      * Creates a new Configuration object.
43      */

44     public Configuration() {
45         getProperties(load());
46     }
47
48     /**
49      * http://www.artima.com/java/answers/Mar2001/messages/164.html export
50      * CLASSPATH=/home/lenya/src/xps/build/properties:... java
51      * -Doverride.lenya.configuration=org/apache/lenya/altconf.properties org.apache.lenya.net.Configuration
52      *
53      * @param args DOCUMENT ME!
54      */

55     public static void main(String JavaDoc[] args) {
56         Configuration conf = new Configuration();
57
58         System.out.println("Proxy Manager Configuration Path: " + conf.configurationPath);
59         System.out.println("SMTP Host: " + conf.smtpHost);
60         System.out.println("SMTP Port: " + conf.smtpPort);
61         System.out.println("SMTP Domain: " + conf.smtpDomain);
62     }
63
64     /**
65      * DOCUMENT ME!
66      *
67      * @return DOCUMENT ME!
68      */

69     public static Properties JavaDoc load() {
70         String JavaDoc resourcePathRelativeToClasspath = System.getProperty(OVERRIDE_DEFAULT_CONFIGURATION_KEY);
71
72         if (resourcePathRelativeToClasspath == null) {
73             resourcePathRelativeToClasspath = System.getProperty(DEFAULT_CONFIGURATION_KEY, DEFAULT_CONFIGURATION_FILE);
74             log.debug(DEFAULT_CONFIGURATION_KEY + "=" + resourcePathRelativeToClasspath);
75         } else {
76             log.debug(OVERRIDE_DEFAULT_CONFIGURATION_KEY + "=" + resourcePathRelativeToClasspath);
77         }
78
79         URL JavaDoc url = Configuration.class.getClassLoader().getResource(resourcePathRelativeToClasspath);
80
81         if (url == null) {
82             log.error(".load(): Could not find resource on classpath: " + resourcePathRelativeToClasspath);
83         }
84
85     log.debug(url);
86
87         Properties JavaDoc properties = new Properties JavaDoc();
88
89         try {
90             properties.load(Configuration.class.getResourceAsStream("conf.properties"));
91         } catch (Exception JavaDoc e) {
92             log.error(e);
93         }
94
95         return properties;
96     }
97
98     /**
99      * DOCUMENT ME!
100      *
101      * @param properties DOCUMENT ME!
102      */

103     public void getProperties(Properties JavaDoc properties) {
104         if (properties != null) {
105             configurationPath = getProperty(properties, "org.apache.lenya.net.ProxyManager.configurationPath");
106             smtpHost = getProperty(properties, "org.apache.lenya.net.SMTP.host");
107             smtpPort = getProperty(properties, "org.apache.lenya.net.SMTP.port");
108             smtpDomain = getProperty(properties, "org.apache.lenya.net.SMTP.domain");
109         }
110     }
111
112     /**
113      * DOCUMENT ME!
114      *
115      * @param properties DOCUMENT ME!
116      * @param key DOCUMENT ME!
117      *
118      * @return DOCUMENT ME!
119      */

120     public String JavaDoc getProperty(Properties JavaDoc properties, String JavaDoc key) {
121         String JavaDoc value = properties.getProperty(key);
122
123         if (value != null) {
124             log.debug(key + "=" + value);
125
126             return value;
127         } else {
128             log.error("No such property: " + key);
129         }
130
131         return null;
132     }
133
134     /**
135      * DOCUMENT ME!
136      */

137     public static void register() {
138     }
139 }
140
Popular Tags