001: package net.jforum.util.legacy.clickstream.config;
002:
003: import java.io.File;
004: import java.io.IOException;
005:
006: import javax.xml.parsers.ParserConfigurationException;
007: import javax.xml.parsers.SAXParser;
008: import javax.xml.parsers.SAXParserFactory;
009:
010: import net.jforum.util.preferences.ConfigKeys;
011: import net.jforum.util.preferences.SystemGlobals;
012:
013: import org.apache.log4j.Logger;
014: import org.xml.sax.Attributes;
015: import org.xml.sax.InputSource;
016: import org.xml.sax.SAXException;
017: import org.xml.sax.helpers.DefaultHandler;
018:
019: /**
020: *Loads clickstream.xml for JForum.
021: *
022: * @author <a href="plightbo@hotmail.com">Patrick Lightbody</a>
023: * @author Rafael Steil (little hacks for JForum)
024: * @version $Id: ConfigLoader.java,v 1.6 2005/12/18 02:12:52 rafaelsteil Exp $
025: */
026: public class ConfigLoader {
027: private static final Logger log = Logger
028: .getLogger(ConfigLoader.class);
029:
030: private ClickstreamConfig config;
031:
032: private static ConfigLoader instance = new ConfigLoader();;
033:
034: public static ConfigLoader instance() {
035: return instance;
036: }
037:
038: private ConfigLoader() {
039: }
040:
041: public ClickstreamConfig getConfig() {
042: if (this .config != null) {
043: return this .config;
044: }
045:
046: synchronized (instance) {
047: this .config = new ClickstreamConfig();
048:
049: try {
050: SAXParser parser = SAXParserFactory.newInstance()
051: .newSAXParser();
052:
053: String path = SystemGlobals
054: .getValue(ConfigKeys.CLICKSTREAM_CONFIG);
055:
056: if (path != null) {
057: if (log.isInfoEnabled()) {
058: log.info("Loading clickstream config from "
059: + path);
060: }
061:
062: File fileInput = new File(path);
063:
064: if (fileInput.exists()) {
065: parser.parse(fileInput, new ConfigHandler());
066: } else {
067: parser.parse(new InputSource(path),
068: new ConfigHandler());
069: }
070: }
071: return config;
072: } catch (SAXException e) {
073: log.error("Could not parse clickstream XML", e);
074: throw new RuntimeException(e.getMessage());
075: } catch (IOException e) {
076: log
077: .error(
078: "Could not read clickstream config from stream",
079: e);
080: throw new RuntimeException(e.getMessage());
081: } catch (ParserConfigurationException e) {
082: log.fatal("Could not obtain SAX parser", e);
083: throw new RuntimeException(e.getMessage());
084: } catch (RuntimeException e) {
085: log.fatal("RuntimeException", e);
086: throw e;
087: } catch (Throwable e) {
088: log.fatal("Exception", e);
089: throw new RuntimeException(e.getMessage());
090: }
091: }
092: }
093:
094: /**
095: * SAX Handler implementation for handling tags in config file and building config objects.
096: */
097: private class ConfigHandler extends DefaultHandler {
098: public void startElement(String uri, String localName,
099: String qName, Attributes attributes)
100: throws SAXException {
101: if (qName.equals("bot-host")) {
102: config.addBotHost(attributes.getValue("name"));
103: } else if (qName.equals("bot-agent")) {
104: config.addBotAgent(attributes.getValue("name"));
105: }
106: }
107: }
108: }
|