001package org.kuali.common.util.log4j; 002 003import java.io.ByteArrayInputStream; 004import java.io.File; 005import java.io.IOException; 006import java.io.InputStream; 007import java.io.OutputStream; 008import java.util.Properties; 009 010import javax.xml.parsers.DocumentBuilder; 011import javax.xml.parsers.DocumentBuilderFactory; 012import javax.xml.parsers.ParserConfigurationException; 013 014import org.apache.commons.io.FileUtils; 015import org.apache.commons.io.IOUtils; 016import org.apache.commons.lang3.StringUtils; 017import org.apache.log4j.LogManager; 018import org.apache.log4j.PropertyConfigurator; 019import org.apache.log4j.xml.DOMConfigurator; 020import org.kuali.common.util.Assert; 021import org.kuali.common.util.Encodings; 022import org.kuali.common.util.LocationUtils; 023import org.kuali.common.util.PropertyUtils; 024import org.kuali.common.util.xml.service.XmlService; 025import org.w3c.dom.Document; 026import org.w3c.dom.Element; 027import org.xml.sax.SAXException; 028 029/** 030 * @deprecated 031 */ 032@Deprecated 033public final class DefaultLog4JService implements Log4JService { 034 035 protected static final String ENCODING = Encodings.UTF8; 036 protected static final String PROPERTIES_SUFFIX = ".properties"; 037 protected static final String XML_SUFFIX = ".xml"; 038 protected static final String UNSUPPORTED_LOCATION_TYPE = "Only " + PROPERTIES_SUFFIX + " and " + XML_SUFFIX + " locations are supported"; 039 040 private final XmlService xmlService; 041 042 public DefaultLog4JService(XmlService xmlService) { 043 Assert.noNulls(xmlService); 044 this.xmlService = xmlService; 045 } 046 047 @Override 048 public void configure(org.kuali.common.util.log4j.model.Log4JContext context) { 049 String xml = toXml(context); 050 Document document = getDocument(xml); 051 configure(document); 052 } 053 054 @Override 055 public void reset() { 056 LogManager.resetConfiguration(); 057 } 058 059 @Override 060 public void configure(String location) { 061 062 // Make sure the location exists 063 Assert.isTrue(LocationUtils.exists(location), "[" + location + "] does not exist"); 064 065 // Make sure it is either a .properties or .xml 066 boolean properties = StringUtils.endsWithIgnoreCase(location, PROPERTIES_SUFFIX); 067 boolean xml = StringUtils.endsWithIgnoreCase(location, XML_SUFFIX); 068 Assert.isTrue(properties || xml, UNSUPPORTED_LOCATION_TYPE); 069 070 if (properties) { 071 configure(PropertyUtils.load(location, ENCODING)); 072 } else if (xml) { 073 configureFromXmlLocation(location); 074 } else { 075 // Should never get here since the earlier assertions guarantee it is either .xml or .properties 076 throw new IllegalArgumentException(UNSUPPORTED_LOCATION_TYPE); 077 } 078 } 079 080 @Override 081 public String toXml(org.kuali.common.util.log4j.model.Log4JContext context) { 082 org.kuali.common.util.log4j.model.Log4JContext clone = new org.kuali.common.util.log4j.model.Log4JContext(context); 083 new Log4JContextNullifier(clone).nullify(); 084 return xmlService.toXml(clone, ENCODING); 085 } 086 087 @Override 088 public void configure(Element element) { 089 DOMConfigurator.configure(element); 090 } 091 092 @Override 093 public void configure(Properties properties) { 094 PropertyConfigurator.configure(properties); 095 } 096 097 @Override 098 public void store(File file, org.kuali.common.util.log4j.model.Log4JContext context) { 099 OutputStream out = null; 100 try { 101 String xml = toXml(context); 102 out = FileUtils.openOutputStream(file); 103 IOUtils.write(xml, out, ENCODING); 104 } catch (IOException e) { 105 throw new IllegalStateException("Unexpected IO error", e); 106 } finally { 107 IOUtils.closeQuietly(out); 108 } 109 } 110 111 protected void configure(Document document) { 112 DOMConfigurator.configure(document.getDocumentElement()); 113 } 114 115 protected void configureFromXmlLocation(String location) { 116 InputStream in = null; 117 try { 118 in = LocationUtils.getInputStream(location); 119 Document document = getDocument(in); 120 configure(document); 121 } catch (Exception e) { 122 throw new IllegalStateException(e); 123 } finally { 124 IOUtils.closeQuietly(in); 125 } 126 } 127 128 protected Document getDocument(InputStream in) throws IOException, SAXException, ParserConfigurationException { 129 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 130 DocumentBuilder parser = dbf.newDocumentBuilder(); 131 return parser.parse(in); 132 } 133 134 protected Document getDocument(String xml) { 135 try { 136 ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes(ENCODING)); 137 return getDocument(in); 138 } catch (Exception e) { 139 throw new IllegalStateException(e); 140 } 141 } 142 143 public XmlService getXmlService() { 144 return xmlService; 145 } 146 147}