KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > log > Log4jService


1 package org.sapia.soto.log;
2
3 import org.apache.log4j.BasicConfigurator;
4 import org.apache.log4j.Logger;
5 import org.apache.log4j.xml.DOMConfigurator;
6
7 import org.sapia.soto.Env;
8 import org.sapia.soto.EnvAware;
9 import org.sapia.soto.Service;
10 import org.sapia.soto.util.EntityResolverImpl;
11
12 import org.w3c.dom.Document JavaDoc;
13
14 import java.io.InputStream JavaDoc;
15
16 import javax.xml.parsers.DocumentBuilder JavaDoc;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18
19
20 /**
21  * @author Yanick Duchesne
22  *
23  * <dl>
24  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
25  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
26  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
27  * </dl>
28  */

29 public class Log4jService implements Service, EnvAware {
30   private Env _env;
31   private String JavaDoc _configPath;
32
33   /**
34    * @see org.sapia.soto.Service#dispose()
35    */

36   public void dispose() {
37   }
38
39   /**
40    * @see org.sapia.soto.Service#init()
41    */

42   public void init() throws Exception JavaDoc {
43     if (_configPath == null) {
44       BasicConfigurator.configure();
45       Logger.getInstance(getClass()).debug("XML config file not specified; using basic configurator");
46     } else {
47       DocumentBuilderFactory JavaDoc fac = DocumentBuilderFactory.newInstance();
48       fac.setValidating(false);
49
50       DocumentBuilder JavaDoc builder = fac.newDocumentBuilder();
51       InputStream JavaDoc is = _env.resolveResource(_configPath).getInputStream();
52       builder.setEntityResolver(new EntityResolverImpl(_env));
53
54       try {
55         Document JavaDoc doc = builder.parse(is);
56         DOMConfigurator.configure(doc.getDocumentElement());
57         Logger.getInstance(getClass()).debug("XML config file successfully loaded");
58       } finally {
59         is.close();
60       }
61     }
62   }
63
64   /**
65    * @see org.sapia.soto.Service#start()
66    */

67   public void start() throws Exception JavaDoc {
68   }
69
70   /**
71    * @see org.sapia.soto.EnvAware#setEnv(org.sapia.soto.Env)
72    */

73   public void setEnv(Env env) {
74     _env = env;
75   }
76
77   public void setConfigPath(String JavaDoc configPath) {
78     _configPath = configPath;
79   }
80 }
81
Popular Tags