KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > forrestbot > webapp > Config


1 /*
2 * Copyright 2002-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 /*
18  * Created on Oct 21, 2003
19  */

20 package org.apache.forrest.forrestbot.webapp;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.apache.log4j.PropertyConfigurator;
29
30 /**
31  * A singleton that takes care of any configuration
32  */

33 public class Config {
34     private static Logger log = Logger.getLogger(Config.class);
35     private static Properties JavaDoc p = new Properties JavaDoc();
36
37     // singleton holder
38
private static final class SingletonHolder {
39         static final Config a = new Config();
40     }
41     // get the singleton instance
42
public static Config getInstance() {
43         return SingletonHolder.a;
44     }
45
46     // private constructor to prevent construction
47
private Config() {
48             configureLog4j();
49             configureProperties();
50             validateProperties();
51             debugProperties();
52     }
53
54     private static void configureLog4j() {
55         Properties JavaDoc log4j = new Properties JavaDoc();
56         try {
57             log4j.load(Config.class.getClassLoader().getResourceAsStream("log4j.properties"));
58         } catch (IOException JavaDoc e1) {
59             log.warn("can't load log4j.properties", e1);
60         }
61         PropertyConfigurator.configure(log4j);
62     }
63
64     private static void configureProperties() {
65         log.info("loading settings.properties");
66         try {
67             p.load(
68             Config.class.getClassLoader().getResourceAsStream("settings.properties"));
69         } catch (IOException JavaDoc e) {
70             log.error("can't load settings.properties", e);
71         }
72     }
73
74     public static void validateProperties() {
75         String JavaDoc [] requiredProperties = { "forrest-exec", "config-dir", "build-dir", "logs-dir", "build-url", "refreshrate", "debug-exec", "targets.build", "targets.deploy" };
76         String JavaDoc [] filesToCheck = { "forrest-exec" };
77         String JavaDoc [] directoriesToCheck = { "config-dir", "build-dir", "logs-dir" };
78         
79         for (int i = 0; i < requiredProperties.length; i++) {
80             if (getProperty(requiredProperties[i]) == null) {
81                 log.error("Property " + requiredProperties[i] + " is required.");
82             }
83         }
84         for (int i = 0; i < filesToCheck.length; i++) {
85             File JavaDoc f = new File JavaDoc(getProperty(filesToCheck[i]));
86             if (!f.isFile()) {
87                 log.error("Property " + filesToCheck[i] + " must reference a file. Current value: " + f.toString());
88             }
89         }
90         for (int i = 0; i < directoriesToCheck.length; i++) {
91             File JavaDoc f = new File JavaDoc(getProperty(directoriesToCheck[i]));
92             if (!f.isDirectory()) {
93                 log.error("Property " + directoriesToCheck[i] + " must reference a directory. Current value: " + f.toString());
94             }
95         }
96     }
97     
98     protected static void debugProperties() {
99         log.debug("properties loaded from settings.properties:");
100         for (Iterator JavaDoc i = p.keySet().iterator(); i.hasNext();) {
101             String JavaDoc key = (String JavaDoc)i.next();
102             log.debug(key + "=" + p.getProperty(key));
103         }
104     }
105
106     public static String JavaDoc getProperty(String JavaDoc arg0, String JavaDoc arg1) {
107         return p.getProperty(arg0, arg1);
108     }
109
110     public static String JavaDoc getProperty(String JavaDoc arg0) {
111         return p.getProperty(arg0);
112     }
113
114 }
115
Popular Tags