001: package org.contineo.util.config;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.apache.commons.lang.text.StrSubstitutor;
009: import org.contineo.util.config.XMLBean;
010: import org.jdom.Element;
011:
012: /**
013: *
014: * @author Michael Scholz
015: */
016: public class LoggingConfigurator {
017:
018: private XMLBean xml;
019:
020: public LoggingConfigurator() {
021: ClassLoader loader = Thread.currentThread()
022: .getContextClassLoader();
023: xml = new XMLBean(loader.getResource("log4j.xml"));
024: }
025:
026: /**
027: * This method selects all file appenders.
028: */
029: public Collection getLoggingFiles() {
030: Collection<String> result = new ArrayList<String>();
031: List list = xml.getAllChild("appender");
032: Iterator iter = list.iterator();
033:
034: while (iter.hasNext()) {
035: Element elem = (Element) iter.next();
036: List childs = elem.getChildren("param");
037: Iterator children = childs.iterator();
038:
039: while (children.hasNext()) {
040: Element child = (Element) children.next();
041:
042: if (child.getAttributeValue("name").equals("File")) {
043: result.add(elem.getAttributeValue("name"));
044: }
045: }
046: }
047:
048: return result;
049: }
050:
051: /**
052: * This method selects all file appenders suitable for web visualization
053: */
054: public Collection<String> getWebLoggingFiles() {
055: Collection<String> result = new ArrayList<String>();
056: // Filter appenders not ending by '_WEB'
057: for (Iterator iter = getLoggingFiles().iterator(); iter
058: .hasNext();) {
059: String element = (String) iter.next();
060: if (element.endsWith("_WEB"))
061: result.add(element);
062: }
063: return result;
064: }
065:
066: /**
067: * This method select all file appenders and filepath.
068: */
069: @SuppressWarnings("unchecked")
070: public Collection getFiles() {
071: Collection result = new ArrayList();
072: List list = xml.getAllChild("appender");
073: Iterator iter = list.iterator();
074:
075: while (iter.hasNext()) {
076: Element elem = (Element) iter.next();
077: List childs = elem.getChildren("param");
078: Iterator children = childs.iterator();
079:
080: while (children.hasNext()) {
081: Element child = (Element) children.next();
082:
083: if (child.getAttributeValue("name").equals("File")) {
084: String appender = elem.getAttributeValue("name");
085: String file = getFile(appender);
086: LoggerProperty logger = new LoggerProperty();
087: logger.setAppender(appender.toLowerCase());
088: logger.setFile(file);
089: result.add(logger);
090: }
091: }
092: }
093:
094: return result;
095: }
096:
097: /**
098: * same as getFile(appender, true)
099: */
100: public String getFile(String appender) {
101: return getFile(appender, true);
102: }
103:
104: /**
105: * This method selects a filepath of an appender.
106: *
107: * @param appender The appender name
108: * @param replaceVariables If true all variables(${var}) in the file path
109: * will be substituted
110: * @return The log file path
111: */
112: public String getFile(String appender, boolean replaceVariables) {
113: String result = "";
114: Element elem = xml.getChild("appender", "name", appender);
115: List childs = elem.getChildren("param");
116: Iterator children = childs.iterator();
117:
118: while (children.hasNext()) {
119: Element child = (Element) children.next();
120:
121: if (child.getAttributeValue("name").equals("File")) {
122: result = child.getAttributeValue("value");
123: }
124: }
125:
126: if (replaceVariables) {
127: result = StrSubstitutor.replaceSystemProperties(result);
128: }
129:
130: return result;
131: }
132:
133: /**
134: * This method sets a file of an appender.
135: */
136: public void setFile(String appender, String file) {
137: Element elem = xml.getChild("appender", "name", appender);
138: List childs = elem.getChildren("param");
139: Iterator children = childs.iterator();
140:
141: while (children.hasNext()) {
142: Element child = (Element) children.next();
143:
144: if (child.getAttributeValue("name").equals("File")) {
145: child.setAttribute("value", file);
146: }
147: }
148: }
149:
150: public boolean write() {
151: return xml.writeXMLDoc();
152: }
153: }
|