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 ; 13 14 import java.io.InputStream ; 15 16 import javax.xml.parsers.DocumentBuilder ; 17 import javax.xml.parsers.DocumentBuilderFactory ; 18 19 20 29 public class Log4jService implements Service, EnvAware { 30 private Env _env; 31 private String _configPath; 32 33 36 public void dispose() { 37 } 38 39 42 public void init() throws Exception { 43 if (_configPath == null) { 44 BasicConfigurator.configure(); 45 Logger.getInstance(getClass()).debug("XML config file not specified; using basic configurator"); 46 } else { 47 DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); 48 fac.setValidating(false); 49 50 DocumentBuilder builder = fac.newDocumentBuilder(); 51 InputStream is = _env.resolveResource(_configPath).getInputStream(); 52 builder.setEntityResolver(new EntityResolverImpl(_env)); 53 54 try { 55 Document 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 67 public void start() throws Exception { 68 } 69 70 73 public void setEnv(Env env) { 74 _env = env; 75 } 76 77 public void setConfigPath(String configPath) { 78 _configPath = configPath; 79 } 80 } 81 | Popular Tags |