configloader.java

来自「clickstream是开源的JAVA项目」· Java 代码 · 共 95 行

JAVA
95
字号
package com.opensymphony.clickstream.config;import com.opensymphony.util.Logger;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import java.io.IOException;import java.io.InputStream;/** * Loads up either clickstream.xml or clickstream-default.xml and * returns a singleton instance of ClickstreamConfig. * * @author <a href="plightbo@hotmail.com">Patrick Lightbody</a> */public class ConfigLoader {    private static final Logger logger = new Logger(ConfigLoader.class);    private ClickstreamConfig config;    private static ConfigLoader singleton;    public static ConfigLoader getInstance() {        if (singleton == null) {            singleton = new ConfigLoader();        }        return singleton;    }    private ConfigLoader() {    }    public synchronized ClickstreamConfig getConfig() {        if (config != null) {            return config;        }        InputStream is;        is = getClass().getClassLoader().getResourceAsStream("clickstream.xml");        if (is == null) {            is = getClass().getClassLoader().getResourceAsStream("/clickstream.xml");        }        if (is == null) {            is = getClass().getClassLoader().getResourceAsStream("META-INF/clickstream-default.xml");        }        if (is == null) {            is = getClass().getClassLoader().getResourceAsStream("/META-INF/clickstream-default.xml");        }        config = new ClickstreamConfig();        try {            logger.debug("Loading config");            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();            parser.parse(is, new ConfigHandler());            return config;        } catch (SAXException e) {            logger.error("Could not parse config XML", e);            throw new RuntimeException(e.getMessage());        } catch (IOException e) {            logger.error("Could not read config from stream", e);            throw new RuntimeException(e.getMessage());        } catch (ParserConfigurationException e) {            logger.fatal("Could not obtain SAX parser", e);            throw new RuntimeException(e.getMessage());        } catch (RuntimeException e) {            logger.fatal("RuntimeException", e);            throw e;        } catch (Throwable e) {            logger.fatal("Exception", e);            throw new RuntimeException(e.getMessage());        }    }    /**     * SAX Handler implementation for handling tags in config file and building     * config objects.     */    private class ConfigHandler extends DefaultHandler {        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {            if (qName.equals("logger")) {                config.setLoggerClass(attributes.getValue("class"));            } else if (qName.equals("bot-host")) {                config.addBotHost(attributes.getValue("name"));            } else if (qName.equals("bot-agent")) {                config.addBotAgent(attributes.getValue("name"));            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?