Search in sources :

Example 1 with AddonPropertiesException

use of org.openecard.addon.AddonPropertiesException in project open-ecard by ecsec.

the class LogSettingsGroup method setRootlevel.

private static void setRootlevel(Document conf, String level) throws AddonPropertiesException {
    try {
        XPath p = XPathFactory.newInstance().newXPath();
        XPathExpression exp = p.compile("/configuration/root");
        Element e = (Element) exp.evaluate(conf, XPathConstants.NODE);
        // no root is beyond our capability
        if (e == null) {
            throw new AddonPropertiesException("Logging config is invalid, the root element is missing.");
        }
        e.setAttribute("level", level);
    } catch (DOMException | XPathExpressionException ex) {
        throw new AddonPropertiesException("Failed to operate on log config document.");
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DOMException(org.w3c.dom.DOMException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) AddonPropertiesException(org.openecard.addon.AddonPropertiesException)

Example 2 with AddonPropertiesException

use of org.openecard.addon.AddonPropertiesException in project open-ecard by ecsec.

the class LogSettingsGroup method setLoglevel.

private static void setLoglevel(Document conf, String path, String level) throws AddonPropertiesException {
    try {
        XPath p = XPathFactory.newInstance().newXPath();
        XPathExpression exp = p.compile(String.format("/configuration/logger[@name='%s']", path));
        Element e = (Element) exp.evaluate(conf, XPathConstants.NODE);
        // create new element
        if (e == null) {
            if (!level.isEmpty()) {
                e = conf.createElement("logger");
                e.setAttribute("name", path);
                e.setAttribute("level", level);
                conf.getDocumentElement().appendChild(e);
            }
            return;
        }
        // we have a node let's see what to do next
        if (level.isEmpty()) {
            // delete entry
            e.getParentNode().removeChild(e);
        } else {
            // modify level
            e.setAttribute("level", level);
        }
    } catch (DOMException | XPathExpressionException ex) {
        throw new AddonPropertiesException("Failed to operate on log config document.");
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DOMException(org.w3c.dom.DOMException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) AddonPropertiesException(org.openecard.addon.AddonPropertiesException)

Example 3 with AddonPropertiesException

use of org.openecard.addon.AddonPropertiesException in project open-ecard by ecsec.

the class LogSettingsGroup method loadProperties.

private static Properties loadProperties() {
    try {
        File confFile = LogbackConfig.getConfFile();
        if (!confFile.exists()) {
            // no file yet
            return new Properties();
        } else {
            // load file into a Document
            WSMarshaller m = WSMarshallerFactory.createInstance();
            m.removeAllTypeClasses();
            FileInputStream fin = new FileInputStream(confFile);
            Document conf = m.str2doc(fin);
            // fill the properties
            Properties p = new Properties();
            p.setProperty(ROOT_KEY, getRootlevel(conf));
            p.setProperty(PAOS_KEY, getLoglevel(conf, PAOS_KEY));
            p.setProperty(EAC_KEY, getLoglevel(conf, EAC_KEY));
            p.setProperty(PACE_KEY, getLoglevel(conf, PACE_KEY));
            p.setProperty(TRCHECKS_KEY, getLoglevel(conf, TRCHECKS_KEY));
            p.setProperty(TCTOKEN_KEY, getLoglevel(conf, TCTOKEN_KEY));
            p.setProperty(EVENT_KEY, getLoglevel(conf, EVENT_KEY));
            p.setProperty(HTTPBIND_KEY, getLoglevel(conf, HTTPBIND_KEY));
            p.setProperty(ADDON_KEY, getLoglevel(conf, ADDON_KEY));
            p.setProperty(SALSTATE_KEY, getLoglevel(conf, SALSTATE_KEY));
            p.setProperty(MDLW_KEY, getLoglevel(conf, MDLW_KEY));
            p.setProperty(MDLW_EVENT_KEY, getLoglevel(conf, MDLW_EVENT_KEY));
            p.setProperty(CG_KEY, getLoglevel(conf, CG_KEY));
            return p;
        }
    } catch (IOException | SAXException | WSMarshallerException | AddonPropertiesException ex) {
        // something else went wrong
        return new Properties();
    }
}
Also used : WSMarshallerException(org.openecard.ws.marshal.WSMarshallerException) WSMarshaller(org.openecard.ws.marshal.WSMarshaller) AddonPropertiesException(org.openecard.addon.AddonPropertiesException) IOException(java.io.IOException) Properties(java.util.Properties) Document(org.w3c.dom.Document) HTMLDocument(javax.swing.text.html.HTMLDocument) File(java.io.File) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 4 with AddonPropertiesException

use of org.openecard.addon.AddonPropertiesException in project open-ecard by ecsec.

the class LogSettingsGroup method saveProperties.

@Override
protected void saveProperties() throws IOException, SecurityException, AddonPropertiesException {
    try {
        File confFile = LogbackConfig.getConfFile();
        // create file if needed
        // if (! confFile.exists()) {
        {
            // overwrite with default settings
            InputStream is = FileUtils.resolveResourceAsStream(LogSettingsGroup.class, "/logback.xml");
            try (FileOutputStream os = new FileOutputStream(confFile)) {
                byte[] buffer = new byte[4096];
                int n;
                while ((n = is.read(buffer)) > 0) {
                    os.write(buffer, 0, n);
                }
            }
        }
        // load file into a Document
        WSMarshaller m = WSMarshallerFactory.createInstance();
        m.removeAllTypeClasses();
        FileInputStream fin = new FileInputStream(confFile);
        Document conf = m.str2doc(fin);
        // process root value
        String val = properties.getProperty(ROOT_KEY);
        val = (val != null) ? val : "ERROR";
        setRootlevel(conf, val);
        // process every value
        val = properties.getProperty(PAOS_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, PAOS_KEY, val);
        val = properties.getProperty(EAC_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, EAC_KEY, val);
        val = properties.getProperty(PACE_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, PACE_KEY, val);
        val = properties.getProperty(TRCHECKS_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, TRCHECKS_KEY, val);
        val = properties.getProperty(TCTOKEN_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, TCTOKEN_KEY, val);
        val = properties.getProperty(EVENT_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, EVENT_KEY, val);
        val = properties.getProperty(HTTPBIND_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, HTTPBIND_KEY, val);
        val = properties.getProperty(ADDON_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, ADDON_KEY, val);
        val = properties.getProperty(SALSTATE_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, SALSTATE_KEY, val);
        val = properties.getProperty(MDLW_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, MDLW_KEY, val);
        val = properties.getProperty(MDLW_EVENT_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, MDLW_EVENT_KEY, val);
        val = properties.getProperty(CG_KEY);
        val = (val != null) ? val : "";
        setLoglevel(conf, CG_KEY, val);
        try (// write log to file
        FileWriter w = new FileWriter(confFile)) {
            String confStr = m.doc2str(conf);
            w.write(confStr);
        }
        // reload log config
        LogbackConfig.load();
    } catch (JoranException ex) {
        throw new AddonPropertiesException(ex.getMessage(), ex);
    } catch (WSMarshallerException | SAXException | TransformerException ex) {
        throw new IOException(ex.getMessage(), ex);
    }
}
Also used : WSMarshallerException(org.openecard.ws.marshal.WSMarshallerException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) WSMarshaller(org.openecard.ws.marshal.WSMarshaller) AddonPropertiesException(org.openecard.addon.AddonPropertiesException) IOException(java.io.IOException) Document(org.w3c.dom.Document) HTMLDocument(javax.swing.text.html.HTMLDocument) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) JoranException(ch.qos.logback.core.joran.spi.JoranException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 5 with AddonPropertiesException

use of org.openecard.addon.AddonPropertiesException in project open-ecard by ecsec.

the class LogSettingsGroup method getRootlevel.

private static String getRootlevel(Document conf) throws AddonPropertiesException {
    try {
        XPath p = XPathFactory.newInstance().newXPath();
        XPathExpression exp = p.compile("/configuration/root");
        Element e = (Element) exp.evaluate(conf, XPathConstants.NODE);
        // no root is beyond our capability
        if (e == null) {
            throw new AddonPropertiesException("Logging config is invalid, the root element is missing.");
        }
        return e.getAttribute("level");
    } catch (DOMException | XPathExpressionException ex) {
        throw new AddonPropertiesException("Failed to operate on log config document.");
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DOMException(org.w3c.dom.DOMException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Element(org.w3c.dom.Element) AddonPropertiesException(org.openecard.addon.AddonPropertiesException)

Aggregations

AddonPropertiesException (org.openecard.addon.AddonPropertiesException)6 XPath (javax.xml.xpath.XPath)4 XPathExpression (javax.xml.xpath.XPathExpression)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 DOMException (org.w3c.dom.DOMException)4 Element (org.w3c.dom.Element)4 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 HTMLDocument (javax.swing.text.html.HTMLDocument)2 WSMarshaller (org.openecard.ws.marshal.WSMarshaller)2 WSMarshallerException (org.openecard.ws.marshal.WSMarshallerException)2 Document (org.w3c.dom.Document)2 SAXException (org.xml.sax.SAXException)2 JoranException (ch.qos.logback.core.joran.spi.JoranException)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 TransformerException (javax.xml.transform.TransformerException)1