1 package org.jahia.bin; 2 3 import javax.servlet.http.HttpServlet ; 4 import javax.servlet.http.HttpServletRequest ; 5 import javax.servlet.http.HttpServletResponse ; 6 7 import java.io.InputStream ; 8 import java.util.Properties ; 9 import java.lang.RuntimeException ; 10 11 import org.apache.log4j.xml.*; 12 import org.apache.log4j.PropertyConfigurator; 13 import java.util.Enumeration ; 14 import org.apache.log4j.Logger; 15 import org.apache.log4j.LogManager; 16 17 27 public class Log4jInitServlet extends HttpServlet { 28 29 public void init () { 30 boolean isLog4jPathOk = false; 31 32 String realPath; 33 String xmlFile = getInitParameter("log4j-xml-init-file"); 34 35 38 getServletContext().log(">> log4j-xml-init-file = " + xmlFile); 39 realPath = getServletContext().getRealPath(xmlFile); 40 41 getServletContext().log(">> realPath = " + realPath); 42 43 49 50 if (xmlFile != null) { 51 try { 52 53 DOMConfigurator.configureAndWatch(realPath); 54 isLog4jPathOk = true; 55 56 } catch (Exception e) { 57 getServletContext().log("Exception(DOMConfigurator) : " + 58 e.getMessage()); 59 getServletContext().log("Looking for WAR file"); 60 } 61 62 } else { 63 getServletContext().log( 64 "Error, couldn't find log4j configuration file " + 65 xmlFile.toString()); 66 getServletContext().log("Looking for WAR file"); 67 } 68 69 71 InputStream input = getServletContext().getResourceAsStream(xmlFile); 72 if (input != null) { 73 74 try { 75 Properties prop = new Properties (); 76 prop.load(input); 77 78 PropertyConfigurator.configure(prop); 79 } catch (Exception e) { 80 getServletContext().log("Exception(PropertyConfigurator) : " + 81 e.getMessage()); 82 getServletContext().log(xmlFile.toString() + 83 " has not been found in WAR file"); 84 throw new RuntimeException (e.getMessage()); 85 } 86 87 } else { 88 getServletContext().log(xmlFile.toString() + 89 " has not been found in WAR file"); 90 } 91 92 } 93 94 public void destroy() { 95 } 96 97 104 private static boolean isLog4JConfigured() { 105 Enumeration enumeration = Logger.getRoot().getAllAppenders(); 106 if ((enumeration != null) && (!(enumeration instanceof org.apache.log4j.helpers.NullEnumeration))) { 107 return true; 108 } 109 else { 110 Enumeration loggers = LogManager.getCurrentLoggers() ; 111 while (loggers.hasMoreElements()) { 112 Logger c = (Logger) loggers.nextElement(); 113 if (!(c.getAllAppenders() instanceof org.apache.log4j.helpers.NullEnumeration)) 114 return true; 115 } 116 } 117 return false; 118 } 119 120 121 122 public void doGet (HttpServletRequest req, HttpServletResponse res) { 123 getServletContext().log(">>---------------------------------------"); 124 getServletContext().log(">> In org.jahia.bin.Log4jInitServlet "); 125 getServletContext().log(">>---------------------------------------"); 126 127 } 128 129 } 130 | Popular Tags |