Search in sources :

Example 1 with Log4jConfigException

use of org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException in project jaffa-framework by jaffa-projects.

the class Log4jConfigComponent method performSave.

/**
 * This will perform the following tasks.
 * - Saves the contents to Log4j.xml.
 * @throws ApplicationExceptions if any error occurs while writing the file.
 * @throws FrameworkException if any error occurs.
 */
protected void performSave() throws FrameworkException, ApplicationExceptions {
    String prop = (String) Config.getProperty(Config.PROP_LOG4J_CONFIG, NONE_LOG4J_CONFIG_PROP);
    ApplicationExceptions appExps = new ApplicationExceptions();
    BufferedWriter writer = null;
    URL url = null;
    if (prop.equalsIgnoreCase(NONE_LOG4J_CONFIG_PROP)) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_LOG4JCONFIG_NONE_ERROR));
        throw appExps;
    } else if (prop.equalsIgnoreCase(DEFAULT_LOG4J_CONFIG_PROP)) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_LOG4JCONFIG_DEFAULT_ERROR));
        throw appExps;
    } else {
        try {
            url = URLHelper.newExtendedURL(prop);
            String absoluteFileName = url.getPath();
            // update the contents of the file
            writer = new BufferedWriter(new FileWriter(absoluteFileName));
            writer.write(getFileContents());
            writer.flush();
        } catch (MalformedURLException e) {
            appExps.add(new Log4jConfigException(Log4jConfigException.PROP_BADURL_ERROR, prop, StringHelper.convertToHTML(e.getMessage())));
            throw appExps;
        } catch (IOException e) {
            appExps.add(new Log4jConfigException(Log4jConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
            throw appExps;
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (IOException e) {
                    String str = "Exception thrown while closing the Writer Stream";
                    log.error(str, e);
                    appExps.add(new Log4jConfigException(Log4jConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
                    throw appExps;
                }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) URL(java.net.URL) Log4jConfigException(org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException)

Example 2 with Log4jConfigException

use of org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException in project jaffa-framework by jaffa-projects.

the class Log4jConfigComponent method retrieveFileContents.

/**
 * This retrieves the the file contents of the log4j xml file specified in the Configuration properties file.
 * @throws ApplicationExceptions if any error occurs while reading the file.
 * @throws FrameworkException if any error occurs.
 */
protected void retrieveFileContents() throws FrameworkException, ApplicationExceptions {
    String prop = (String) Config.getProperty(Config.PROP_LOG4J_CONFIG, NONE_LOG4J_CONFIG_PROP);
    ApplicationExceptions appExps = new ApplicationExceptions();
    BufferedReader reader = null;
    URL url = null;
    if (prop.equalsIgnoreCase(NONE_LOG4J_CONFIG_PROP)) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_LOG4JCONFIG_NONE_ERROR));
        throw appExps;
    } else if (prop.equalsIgnoreCase(DEFAULT_LOG4J_CONFIG_PROP)) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_LOG4JCONFIG_DEFAULT_ERROR));
        throw appExps;
    } else {
        // clear the widget cache
        getUserSession().getWidgetCache(getComponentId()).clear();
        try {
            url = URLHelper.newExtendedURL(prop);
            String absoluteFileName = url.getPath();
            // read the contents of the file
            reader = new BufferedReader(new FileReader(absoluteFileName));
            StringBuffer buf = new StringBuffer();
            String str = null;
            while ((str = reader.readLine()) != null) {
                buf.append(str);
                buf.append("\r\n");
            }
            m_fileContents = buf.toString();
        } catch (MalformedURLException e) {
            appExps.add(new Log4jConfigException(Log4jConfigException.PROP_BADURL_ERROR, StringHelper.convertToHTML(prop), StringHelper.convertToHTML(e.getMessage())));
            throw appExps;
        } catch (IOException e) {
            appExps.add(new Log4jConfigException(Log4jConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
            throw appExps;
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    String str = "Exception thrown while closing the Reader Stream";
                    log.error(str, e);
                    appExps.add(new Log4jConfigException(Log4jConfigException.PROP_FILEREAD_ERROR, StringHelper.convertToHTML(e.getMessage())));
                    throw appExps;
                }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) URL(java.net.URL) Log4jConfigException(org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException)

Example 3 with Log4jConfigException

use of org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException in project jaffa-framework by jaffa-projects.

the class Log4jConfigForm method doValidate.

/**
 * This method should be invoked to copy the fields from the FormBean to the component after successful validation.
 * @return true of the values are copied successfully
 */
public boolean doValidate() throws FrameworkException, ApplicationExceptions {
    String value = null;
    ApplicationExceptions appExps = new ApplicationExceptions();
    value = getFileContentsWM().getValue();
    if (value == null || (value != null && value.trim().length() == 0)) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(MessageHelper.findMessage("label.Jaffa.Admin.Log4JConfig.NoContent", null))));
        throw appExps;
    }
    try {
        // Create a factory object for creating DOM parsers
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Specifies that the parser produced by this factory will validate documents as they are parsed.
        factory.setValidating(true);
        // Now use the factory to create a DOM parser
        DocumentBuilder parser = factory.newDocumentBuilder();
        // Specifies the EntityResolver onceto resolve DTD used in XML documents
        parser.setEntityResolver(new Log4jEntityResolver());
        // Specifies the ErrorHandler to handle warning/error/fatalError conditions
        parser.setErrorHandler(new DefaultErrorHandler());
        Document document = parser.parse(new InputSource(new StringReader(value)));
    } catch (ParserConfigurationException e) {
        // Cannot pass e.toString() and pass as parameter as the the meesage contains quotes
        // which dows not work properly with displaying the messages
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (SAXException e) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    } catch (IOException e) {
        appExps.add(new Log4jConfigException(Log4jConfigException.PROP_XML_FILE_PARSE_ERROR, StringHelper.convertToHTML(e.getMessage())));
        throw appExps;
    }
    setFileContents(value);
    return true;
}
Also used : InputSource(org.xml.sax.InputSource) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Log4jEntityResolver(org.apache.log4j.xml.Log4jEntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DefaultErrorHandler(org.jaffa.util.DefaultErrorHandler) Log4jConfigException(org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException)

Aggregations

Log4jConfigException (org.jaffa.applications.jaffa.modules.admin.components.log4jconfig.ui.exceptions.Log4jConfigException)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Log4jEntityResolver (org.apache.log4j.xml.Log4jEntityResolver)1 DefaultErrorHandler (org.jaffa.util.DefaultErrorHandler)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1