KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > config > ConfigLoader


1 package com.dotmarketing.config;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 import javax.xml.parsers.ParserConfigurationException JavaDoc;
7 import javax.xml.parsers.SAXParser JavaDoc;
8 import javax.xml.parsers.SAXParserFactory JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.xml.sax.Attributes JavaDoc;
13 import org.xml.sax.SAXException JavaDoc;
14 import org.xml.sax.helpers.DefaultHandler JavaDoc;
15
16 /**
17  * Loads up either clickstream.xml or clickstream-default.xml and
18  * returns a singleton instance of ClickstreamConfig.
19  *
20  * @author
21  */

22 public class ConfigLoader {
23     private static final Log log = LogFactory.getLog(ConfigLoader.class);
24
25     private ClickstreamConfig config;
26     private static ConfigLoader singleton;
27
28     public static ConfigLoader getInstance() {
29         if (singleton == null) {
30             singleton = new ConfigLoader();
31         }
32
33         return singleton;
34     }
35
36     private ConfigLoader() {
37     }
38
39     public synchronized ClickstreamConfig getConfig() {
40         if (config != null) {
41             return config;
42         }
43
44         InputStream JavaDoc is = getInputStream("clickstream.xml");
45         
46         if (is == null) {
47             is = getInputStream("/clickstream.xml");
48         }
49         if (is == null) {
50             is = getInputStream("META-INF/clickstream-default.xml");
51         }
52         if (is == null) {
53             is = getInputStream("/META-INF/clickstream-default.xml");
54         }
55
56         config = new ClickstreamConfig();
57
58         try {
59             log.debug("Loading config");
60             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
61             parser.parse(is, new ConfigHandler());
62             return config;
63         }
64         catch (SAXException JavaDoc e) {
65             log.error("Could not parse config XML", e);
66             throw new RuntimeException JavaDoc(e.getMessage());
67         }
68         catch (IOException JavaDoc e) {
69             log.error("Could not read config from stream", e);
70             throw new RuntimeException JavaDoc(e.getMessage());
71         }
72         catch (ParserConfigurationException JavaDoc e) {
73             log.fatal("Could not obtain SAX parser", e);
74             throw new RuntimeException JavaDoc(e.getMessage());
75         }
76         catch (RuntimeException JavaDoc e) {
77             log.fatal("RuntimeException", e);
78             throw e;
79         }
80         catch (Throwable JavaDoc e) {
81             log.fatal("Exception", e);
82             throw new RuntimeException JavaDoc(e.getMessage());
83         }
84     }
85
86     /**
87      * SAX Handler implementation for handling tags in config file and building
88      * config objects.
89      */

90     private class ConfigHandler extends DefaultHandler JavaDoc {
91         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
92             if (qName.equals("logger")) {
93                 config.setLoggerClass(attributes.getValue("class"));
94             }
95             else if (qName.equals("bot-host")) {
96                 config.addBotHost(attributes.getValue("name"));
97             }
98             else if (qName.equals("bot-agent")) {
99                 config.addBotAgent(attributes.getValue("name"));
100             }
101         }
102     }
103
104     private InputStream JavaDoc getInputStream(String JavaDoc resourceName) {
105         InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
106
107         if (is == null) {
108             ConfigLoader.class.getClassLoader().getResourceAsStream(resourceName);
109         }
110
111         return is;
112     }
113 }
114
Popular Tags