KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > legacy > clickstream > config > ConfigLoader


1 package net.jforum.util.legacy.clickstream.config;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException 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 net.jforum.util.preferences.ConfigKeys;
11 import net.jforum.util.preferences.SystemGlobals;
12
13 import org.apache.log4j.Logger;
14 import org.xml.sax.Attributes JavaDoc;
15 import org.xml.sax.InputSource JavaDoc;
16 import org.xml.sax.SAXException JavaDoc;
17 import org.xml.sax.helpers.DefaultHandler JavaDoc;
18
19 /**
20  *Loads clickstream.xml for JForum.
21  *
22  * @author <a HREF="plightbo@hotmail.com">Patrick Lightbody</a>
23  * @author Rafael Steil (little hacks for JForum)
24  * @version $Id: ConfigLoader.java,v 1.6 2005/12/18 02:12:52 rafaelsteil Exp $
25  */

26 public class ConfigLoader
27 {
28     private static final Logger log = Logger.getLogger(ConfigLoader.class);
29
30     private ClickstreamConfig config;
31
32     private static ConfigLoader instance = new ConfigLoader();;
33
34     public static ConfigLoader instance()
35     {
36         return instance;
37     }
38
39     private ConfigLoader() {}
40
41     public ClickstreamConfig getConfig()
42     {
43         if (this.config != null) {
44             return this.config;
45         }
46
47         synchronized (instance) {
48             this.config = new ClickstreamConfig();
49     
50             try {
51                 SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
52     
53                 String JavaDoc path = SystemGlobals.getValue(ConfigKeys.CLICKSTREAM_CONFIG);
54                 
55                 if (path != null) {
56                     if (log.isInfoEnabled()) {
57                         log.info("Loading clickstream config from " + path);
58                     }
59                     
60                     File JavaDoc fileInput = new File JavaDoc(path);
61                     
62                     if (fileInput.exists()) {
63                         parser.parse(fileInput, new ConfigHandler());
64                     }
65                     else {
66                         parser.parse(new InputSource JavaDoc(path), new ConfigHandler());
67                     }
68                 }
69                 return config;
70             }
71             catch (SAXException JavaDoc e) {
72                 log.error("Could not parse clickstream XML", e);
73                 throw new RuntimeException JavaDoc(e.getMessage());
74             }
75             catch (IOException JavaDoc e) {
76                 log.error("Could not read clickstream config from stream", e);
77                 throw new RuntimeException JavaDoc(e.getMessage());
78             }
79             catch (ParserConfigurationException JavaDoc e) {
80                 log.fatal("Could not obtain SAX parser", e);
81                 throw new RuntimeException JavaDoc(e.getMessage());
82             }
83             catch (RuntimeException JavaDoc e) {
84                 log.fatal("RuntimeException", e);
85                 throw e;
86             }
87             catch (Throwable JavaDoc e) {
88                 log.fatal("Exception", e);
89                 throw new RuntimeException JavaDoc(e.getMessage());
90             }
91         }
92     }
93
94     /**
95      * SAX Handler implementation for handling tags in config file and building config objects.
96      */

97     private class ConfigHandler extends DefaultHandler JavaDoc
98     {
99         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc
100         {
101             if (qName.equals("bot-host")) {
102                 config.addBotHost(attributes.getValue("name"));
103             }
104             else if (qName.equals("bot-agent")) {
105                 config.addBotAgent(attributes.getValue("name"));
106             }
107         }
108     }
109 }
110
Popular Tags