1 package com.dotmarketing.config; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 6 import javax.xml.parsers.ParserConfigurationException ; 7 import javax.xml.parsers.SAXParser ; 8 import javax.xml.parsers.SAXParserFactory ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.xml.sax.Attributes ; 13 import org.xml.sax.SAXException ; 14 import org.xml.sax.helpers.DefaultHandler ; 15 16 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 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 parser = SAXParserFactory.newInstance().newSAXParser(); 61 parser.parse(is, new ConfigHandler()); 62 return config; 63 } 64 catch (SAXException e) { 65 log.error("Could not parse config XML", e); 66 throw new RuntimeException (e.getMessage()); 67 } 68 catch (IOException e) { 69 log.error("Could not read config from stream", e); 70 throw new RuntimeException (e.getMessage()); 71 } 72 catch (ParserConfigurationException e) { 73 log.fatal("Could not obtain SAX parser", e); 74 throw new RuntimeException (e.getMessage()); 75 } 76 catch (RuntimeException e) { 77 log.fatal("RuntimeException", e); 78 throw e; 79 } 80 catch (Throwable e) { 81 log.fatal("Exception", e); 82 throw new RuntimeException (e.getMessage()); 83 } 84 } 85 86 90 private class ConfigHandler extends DefaultHandler { 91 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 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 getInputStream(String resourceName) { 105 InputStream 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 |