001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.log4j;
017
018import java.io.ByteArrayInputStream;
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.util.Properties;
024
025import javax.xml.parsers.DocumentBuilder;
026import javax.xml.parsers.DocumentBuilderFactory;
027import javax.xml.parsers.ParserConfigurationException;
028
029import org.apache.commons.io.FileUtils;
030import org.apache.commons.io.IOUtils;
031import org.apache.commons.lang3.StringUtils;
032import org.apache.log4j.LogManager;
033import org.apache.log4j.PropertyConfigurator;
034import org.apache.log4j.xml.DOMConfigurator;
035import org.kuali.common.util.Assert;
036import org.kuali.common.util.Encodings;
037import org.kuali.common.util.LocationUtils;
038import org.kuali.common.util.PropertyUtils;
039import org.kuali.common.util.xml.service.XmlService;
040import org.w3c.dom.Document;
041import org.w3c.dom.Element;
042import org.xml.sax.SAXException;
043
044/**
045 * @deprecated
046 */
047@Deprecated
048public final class DefaultLog4JService implements Log4JService {
049
050        protected static final String ENCODING = Encodings.UTF8;
051        protected static final String PROPERTIES_SUFFIX = ".properties";
052        protected static final String XML_SUFFIX = ".xml";
053        protected static final String UNSUPPORTED_LOCATION_TYPE = "Only " + PROPERTIES_SUFFIX + " and " + XML_SUFFIX + " locations are supported";
054
055        private final XmlService xmlService;
056
057        public DefaultLog4JService(XmlService xmlService) {
058                Assert.noNulls(xmlService);
059                this.xmlService = xmlService;
060        }
061
062        @Override
063        public void configure(org.kuali.common.util.log4j.model.Log4JContext context) {
064                String xml = toXml(context);
065                Document document = getDocument(xml);
066                configure(document);
067        }
068
069        @Override
070        public void reset() {
071                LogManager.resetConfiguration();
072        }
073
074        @Override
075        public void configure(String location) {
076
077                // Make sure the location exists
078                Assert.isTrue(LocationUtils.exists(location), "[" + location + "] does not exist");
079
080                // Make sure it is either a .properties or .xml
081                boolean properties = StringUtils.endsWithIgnoreCase(location, PROPERTIES_SUFFIX);
082                boolean xml = StringUtils.endsWithIgnoreCase(location, XML_SUFFIX);
083                Assert.isTrue(properties || xml, UNSUPPORTED_LOCATION_TYPE);
084
085                if (properties) {
086                        configure(PropertyUtils.load(location, ENCODING));
087                } else if (xml) {
088                        configureFromXmlLocation(location);
089                } else {
090                        // Should never get here since the earlier assertions guarantee it is either .xml or .properties
091                        throw new IllegalArgumentException(UNSUPPORTED_LOCATION_TYPE);
092                }
093        }
094
095        @Override
096        public String toXml(org.kuali.common.util.log4j.model.Log4JContext context) {
097                org.kuali.common.util.log4j.model.Log4JContext clone = new org.kuali.common.util.log4j.model.Log4JContext(context);
098                new Log4JContextNullifier(clone).nullify();
099                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}