1 package net.jforum.util.legacy.clickstream.config; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 6 import javax.xml.parsers.ParserConfigurationException ; 7 import javax.xml.parsers.SAXParser ; 8 import javax.xml.parsers.SAXParserFactory ; 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 ; 15 import org.xml.sax.InputSource ; 16 import org.xml.sax.SAXException ; 17 import org.xml.sax.helpers.DefaultHandler ; 18 19 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 parser = SAXParserFactory.newInstance().newSAXParser(); 52 53 String 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 fileInput = new File (path); 61 62 if (fileInput.exists()) { 63 parser.parse(fileInput, new ConfigHandler()); 64 } 65 else { 66 parser.parse(new InputSource (path), new ConfigHandler()); 67 } 68 } 69 return config; 70 } 71 catch (SAXException e) { 72 log.error("Could not parse clickstream XML", e); 73 throw new RuntimeException (e.getMessage()); 74 } 75 catch (IOException e) { 76 log.error("Could not read clickstream config from stream", e); 77 throw new RuntimeException (e.getMessage()); 78 } 79 catch (ParserConfigurationException e) { 80 log.fatal("Could not obtain SAX parser", e); 81 throw new RuntimeException (e.getMessage()); 82 } 83 catch (RuntimeException e) { 84 log.fatal("RuntimeException", e); 85 throw e; 86 } 87 catch (Throwable e) { 88 log.fatal("Exception", e); 89 throw new RuntimeException (e.getMessage()); 90 } 91 } 92 } 93 94 97 private class ConfigHandler extends DefaultHandler 98 { 99 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 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 |