View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.log4j;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.util.Properties;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.parsers.ParserConfigurationException;
28  
29  import org.apache.commons.io.FileUtils;
30  import org.apache.commons.io.IOUtils;
31  import org.apache.commons.lang3.StringUtils;
32  import org.apache.log4j.LogManager;
33  import org.apache.log4j.PropertyConfigurator;
34  import org.apache.log4j.xml.DOMConfigurator;
35  import org.kuali.common.util.Assert;
36  import org.kuali.common.util.Encodings;
37  import org.kuali.common.util.LocationUtils;
38  import org.kuali.common.util.PropertyUtils;
39  import org.kuali.common.util.xml.service.XmlService;
40  import org.w3c.dom.Document;
41  import org.w3c.dom.Element;
42  import org.xml.sax.SAXException;
43  
44  /**
45   * @deprecated
46   */
47  @Deprecated
48  public final class DefaultLog4JService implements Log4JService {
49  
50  	protected static final String ENCODING = Encodings.UTF8;
51  	protected static final String PROPERTIES_SUFFIX = ".properties";
52  	protected static final String XML_SUFFIX = ".xml";
53  	protected static final String UNSUPPORTED_LOCATION_TYPE = "Only " + PROPERTIES_SUFFIX + " and " + XML_SUFFIX + " locations are supported";
54  
55  	private final XmlService xmlService;
56  
57  	public DefaultLog4JService(XmlService xmlService) {
58  		Assert.noNulls(xmlService);
59  		this.xmlService = xmlService;
60  	}
61  
62  	@Override
63  	public void configure(org.kuali.common.util.log4j.model.Log4JContext context) {
64  		String xml = toXml(context);
65  		Document document = getDocument(xml);
66  		configure(document);
67  	}
68  
69  	@Override
70  	public void reset() {
71  		LogManager.resetConfiguration();
72  	}
73  
74  	@Override
75  	public void configure(String location) {
76  
77  		// Make sure the location exists
78  		Assert.isTrue(LocationUtils.exists(location), "[" + location + "] does not exist");
79  
80  		// Make sure it is either a .properties or .xml
81  		boolean properties = StringUtils.endsWithIgnoreCase(location, PROPERTIES_SUFFIX);
82  		boolean xml = StringUtils.endsWithIgnoreCase(location, XML_SUFFIX);
83  		Assert.isTrue(properties || xml, UNSUPPORTED_LOCATION_TYPE);
84  
85  		if (properties) {
86  			configure(PropertyUtils.load(location, ENCODING));
87  		} else if (xml) {
88  			configureFromXmlLocation(location);
89  		} else {
90  			// Should never get here since the earlier assertions guarantee it is either .xml or .properties
91  			throw new IllegalArgumentException(UNSUPPORTED_LOCATION_TYPE);
92  		}
93  	}
94  
95  	@Override
96  	public String toXml(org.kuali.common.util.log4j.model.Log4JContext context) {
97  		org.kuali.common.util.log4j.model.Log4JContext clone = new org.kuali.common.util.log4j.model.Log4JContext(context);
98  		new Log4JContextNullifier(clone).nullify();
99  		return xmlService.toXml(clone, ENCODING);
100 	}
101 
102 	@Override
103 	public void configure(Element element) {
104 		DOMConfigurator.configure(element);
105 	}
106 
107 	@Override
108 	public void configure(Properties properties) {
109 		PropertyConfigurator.configure(properties);
110 	}
111 
112 	@Override
113 	public void store(File file, org.kuali.common.util.log4j.model.Log4JContext context) {
114 		OutputStream out = null;
115 		try {
116 			String xml = toXml(context);
117 			out = FileUtils.openOutputStream(file);
118 			IOUtils.write(xml, out, ENCODING);
119 		} catch (IOException e) {
120 			throw new IllegalStateException("Unexpected IO error", e);
121 		} finally {
122 			IOUtils.closeQuietly(out);
123 		}
124 	}
125 
126 	protected void configure(Document document) {
127 		DOMConfigurator.configure(document.getDocumentElement());
128 	}
129 
130 	protected void configureFromXmlLocation(String location) {
131 		InputStream in = null;
132 		try {
133 			in = LocationUtils.getInputStream(location);
134 			Document document = getDocument(in);
135 			configure(document);
136 		} catch (Exception e) {
137 			throw new IllegalStateException(e);
138 		} finally {
139 			IOUtils.closeQuietly(in);
140 		}
141 	}
142 
143 	protected Document getDocument(InputStream in) throws IOException, SAXException, ParserConfigurationException {
144 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
145 		DocumentBuilder parser = dbf.newDocumentBuilder();
146 		return parser.parse(in);
147 	}
148 
149 	protected Document getDocument(String xml) {
150 		try {
151 			ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes(ENCODING));
152 			return getDocument(in);
153 		} catch (Exception e) {
154 			throw new IllegalStateException(e);
155 		}
156 	}
157 
158 	public XmlService getXmlService() {
159 		return xmlService;
160 	}
161 
162 }