View Javadoc
1   package org.kuali.common.util.log.log4j;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.File;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.OutputStream;
8   import java.util.Properties;
9   
10  import javax.xml.parsers.DocumentBuilder;
11  import javax.xml.parsers.DocumentBuilderFactory;
12  import javax.xml.parsers.ParserConfigurationException;
13  
14  import org.apache.commons.io.FileUtils;
15  import org.apache.commons.io.IOUtils;
16  import org.apache.commons.lang3.StringUtils;
17  import org.apache.log4j.LogManager;
18  import org.apache.log4j.PropertyConfigurator;
19  import org.apache.log4j.xml.DOMConfigurator;
20  import org.kuali.common.util.Assert;
21  import org.kuali.common.util.Encodings;
22  import org.kuali.common.util.LocationUtils;
23  import org.kuali.common.util.PropertyUtils;
24  import org.kuali.common.util.log.log4j.model.Log4JConfiguration;
25  import org.kuali.common.util.xml.service.XmlService;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  import org.xml.sax.SAXException;
29  
30  public final class DefaultLog4JService implements Log4JService {
31  
32  	// ASCII should actually be good enough, log4j config files shouldn't contain special characters
33  	private static final String ENCODING = Encodings.UTF8;
34  	private static final String PROPERTIES_SUFFIX = ".properties";
35  	private static final String XML_SUFFIX = ".xml";
36  	private static final String UNSUPPORTED_LOCATION_TYPE = "Only " + PROPERTIES_SUFFIX + " and " + XML_SUFFIX + " locations are supported";
37  
38  	private final XmlService service;
39  
40  	public DefaultLog4JService(XmlService service) {
41  		Assert.noNulls(service);
42  		this.service = service;
43  	}
44  
45  	@Override
46  	public void configure(Log4JConfiguration config) {
47  		String xml = toXml(config);
48  		Document document = getDocument(xml);
49  		configure(document);
50  	}
51  
52  	@Override
53  	public void reset() {
54  		LogManager.resetConfiguration();
55  	}
56  
57  	@Override
58  	public void configure(String location) {
59  
60  		// Make sure the location exists
61  		Assert.isTrue(LocationUtils.exists(location), "[" + location + "] does not exist");
62  
63  		// Make sure it is either a .properties or .xml
64  		boolean properties = StringUtils.endsWithIgnoreCase(location, PROPERTIES_SUFFIX);
65  		boolean xml = StringUtils.endsWithIgnoreCase(location, XML_SUFFIX);
66  		Assert.isTrue(properties || xml, UNSUPPORTED_LOCATION_TYPE);
67  
68  		if (properties) {
69  			configure(PropertyUtils.load(location, ENCODING));
70  		} else if (xml) {
71  			configureFromXmlLocation(location);
72  		} else {
73  			// Should never get here since the earlier assertions guarantee it is either .xml or .properties
74  			throw new IllegalArgumentException(UNSUPPORTED_LOCATION_TYPE);
75  		}
76  	}
77  
78  	@Override
79  	public String toXml(Log4JConfiguration config) {
80  		return service.toXml(config, ENCODING);
81  	}
82  
83  	@Override
84  	public void configure(Element element) {
85  		DOMConfigurator.configure(element);
86  	}
87  
88  	@Override
89  	public void configure(Properties properties) {
90  		PropertyConfigurator.configure(properties);
91  	}
92  
93  	@Override
94  	public void write(File file, Log4JConfiguration config) {
95  		OutputStream out = null;
96  		try {
97  			String xml = toXml(config);
98  			out = FileUtils.openOutputStream(file);
99  			IOUtils.write(xml, out, ENCODING);
100 		} catch (IOException e) {
101 			throw new IllegalStateException("Unexpected IO error", e);
102 		} finally {
103 			IOUtils.closeQuietly(out);
104 		}
105 	}
106 
107 	protected void configure(Document document) {
108 		DOMConfigurator.configure(document.getDocumentElement());
109 	}
110 
111 	protected void configureFromXmlLocation(String location) {
112 		InputStream in = null;
113 		try {
114 			in = LocationUtils.getInputStream(location);
115 			Document document = getDocument(in);
116 			configure(document);
117 		} catch (Exception e) {
118 			throw new IllegalStateException(e);
119 		} finally {
120 			IOUtils.closeQuietly(in);
121 		}
122 	}
123 
124 	protected Document getDocument(InputStream in) throws IOException, SAXException, ParserConfigurationException {
125 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
126 		DocumentBuilder parser = dbf.newDocumentBuilder();
127 		return parser.parse(in);
128 	}
129 
130 	protected Document getDocument(String xml) {
131 		try {
132 			ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes(ENCODING));
133 			return getDocument(in);
134 		} catch (Exception e) {
135 			throw new IllegalStateException(e);
136 		}
137 	}
138 
139 	public XmlService getXmlService() {
140 		return service;
141 	}
142 
143 }