KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > config > LoggingConfigurator


1 /*
2  * LoggingConfigurator.java
3  *
4  * Created on 3. Dezember 2003, 22:07
5  */

6
7 package org.contineo.core.config;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.contineo.core.XMLBean;
15 import org.jdom.Element;
16 /**
17  *
18  * @author Michael Scholz
19  */

20 public class LoggingConfigurator {
21
22     /**
23      *
24      * @uml.property name="xml"
25      * @uml.associationEnd
26      * @uml.property name="xml" multiplicity="(1 1)"
27      */

28     private XMLBean xml;
29
30     
31     /** Creates a new instance of LoggingConfigurator */
32     public LoggingConfigurator() {
33         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
34         xml = new XMLBean(loader.getResource("logging.xml"));
35     }
36     
37     /**
38      * This method selects all file appenders.
39      */

40     public Collection JavaDoc getLoggingFiles() {
41         Collection JavaDoc result = new ArrayList JavaDoc();
42         List JavaDoc list = xml.getAllChild("appender");
43         Iterator JavaDoc iter = list.iterator();
44         while (iter.hasNext()) {
45             Element elem = (Element)iter.next();
46             List JavaDoc childs = elem.getChildren("param");
47             Iterator JavaDoc children = childs.iterator();
48             while (children.hasNext()) {
49                 Element child = (Element)children.next();
50                 if (child.getAttributeValue("name").equals("file"))
51                     result.add(elem.getAttributeValue("name"));
52             }
53         }
54         return result;
55     }
56     
57     /**
58      * This method select all file appenders and filepath.
59      */

60     public Collection JavaDoc getFiles() {
61         Collection JavaDoc result = new ArrayList JavaDoc();
62         List JavaDoc list = xml.getAllChild("appender");
63         Iterator JavaDoc iter = list.iterator();
64         while (iter.hasNext()) {
65             Element elem = (Element)iter.next();
66             List JavaDoc childs = elem.getChildren("param");
67             Iterator JavaDoc children = childs.iterator();
68             while (children.hasNext()) {
69                 Element child = (Element)children.next();
70                 if (child.getAttributeValue("name").equals("file")) {
71                     String JavaDoc appender = elem.getAttributeValue("name");
72                     String JavaDoc file = getFile(appender);
73                     LoggerProperty logger = new LoggerProperty();
74                     logger.setAppender(appender.toLowerCase());
75                     logger.setFile(file);
76                     result.add(logger);
77                 }
78             }
79         }
80         return result;
81     }
82     
83     /**
84      * This method selects a filepath of an appender.
85      */

86     public String JavaDoc getFile(String JavaDoc appender) {
87         String JavaDoc result = "";
88         Element elem = xml.getChild("appender", "name", appender);
89         List JavaDoc childs = elem.getChildren("param");
90         Iterator JavaDoc children = childs.iterator();
91         while (children.hasNext()) {
92             Element child = (Element)children.next();
93             if (child.getAttributeValue("name").equals("file"))
94                 result = child.getAttributeValue("value");
95         }
96         return result;
97     }
98     
99     /**
100      * This method sets a file of an appender.
101      */

102     public void setFile(String JavaDoc appender, String JavaDoc file) {
103                 Element elem = xml.getChild("appender", "name", appender);
104         List JavaDoc childs = elem.getChildren("param");
105         Iterator JavaDoc children = childs.iterator();
106         while (children.hasNext()) {
107             Element child = (Element)children.next();
108             if (child.getAttributeValue("name").equals("file"))
109                 child.setAttribute("value", file);
110         }
111     }
112     
113     public boolean write() {
114         return xml.writeXMLDoc();
115     }
116 }
117
Popular Tags