mcardle     2005/12/14 17:30:53 CET

  Modified files:
    src/net/sf/j2ep      ConfigParser.java 
    src/org/jahia/esi/log Log4jInit.java 
  Log:
  * reviewed log4j init
  * refactorings
  
  Revision  Changes    Path
  1.9       +6 -8      esi_server/src/net/sf/j2ep/ConfigParser.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/esi_server/src/net/sf/j2ep/ConfigParser.java.diff?r1=1.8&r2=1.9&f=h
  1.3       +106 -21   esi_server/src/org/jahia/esi/log/Log4jInit.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/esi_server/src/org/jahia/esi/log/Log4jInit.java.diff?r1=1.2&r2=1.3&f=h
  
  
  
  Index: ConfigParser.java
  ===================================================================
  RCS file: /home/cvs/repository/esi_server/src/net/sf/j2ep/ConfigParser.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ConfigParser.java 5 Dec 2005 19:03:10 -0000       1.8
  +++ ConfigParser.java 14 Dec 2005 16:30:52 -0000      1.9
  @@ -25,8 +25,7 @@
   import org.apache.commons.digester.Digester;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.jahia.esi.settings.GeneralSettings;
  -import org.jahia.esi.invalidation.services.Invalidator;
  +import org.jahia.esi.settings.SettingsBean;
   
   /**
    * The config parser uses Digester to parse the config file. A rule chain 
with
  @@ -70,7 +69,7 @@
                   initGeneralSettings(data);
               }
               catch (Exception e) {
  -                System.err.println("Error parsing Configuration file ["+ 
data.getPath() + "]  \n due to exception:"+e);
  +                log.info("Error parsing Configuration file ["+ 
data.getPath() + "]  \n due to exception:"+e);
                   throw new RuntimeException(e);
               }
   
  @@ -94,7 +93,7 @@
           Digester digester = new Digester();
           digester.setUseContextClassLoader(true);
   
  -        digester.addObjectCreate("config/generalsettings", 
GeneralSettings.class);
  +        digester.addObjectCreate("config/generalsettings", 
SettingsBean.class);
           digester.addBeanPropertySetter( 
"config/generalsettings/invalidationPort", "invalidationPort" );
           digester.addBeanPropertySetter( 
"config/generalsettings/activateInvalidationServer", 
"activateInvalidationServer" );
           digester.addBeanPropertySetter( 
"config/generalsettings/enableCacheOffUrlParam", "enableCacheOff" );
  @@ -110,11 +109,10 @@
           digester.addBeanPropertySetter( 
"config/generalsettings/defaultExpirationDelayInSecs", "defaultExpiration" );
           digester.addBeanPropertySetter( 
"config/generalsettings/defaultMaxRemovalDelayInSecs", "defaultMaxRemovalDelay" 
);
   
  +        SettingsBean settingsBean = (SettingsBean) digester.parse(data);
  +        SettingsBean.setInstance(settingsBean);
   
  -        GeneralSettings generalSettings = (GeneralSettings) 
digester.parse(data);
  -        GeneralSettings.setInstance(generalSettings);
  -
  -        System.err.println(GeneralSettings.getInstance().toString());
  +        log.info(SettingsBean.getInstance().toString());
       }
   
       /**
  
  
  
  Index: Log4jInit.java
  ===================================================================
  RCS file: 
/home/cvs/repository/esi_server/src/org/jahia/esi/log/Log4jInit.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Log4jInit.java    6 Dec 2005 14:43:03 -0000       1.2
  +++ Log4jInit.java    14 Dec 2005 16:30:53 -0000      1.3
  @@ -50,27 +50,112 @@
    */
   
   import org.apache.log4j.PropertyConfigurator;
  -import javax.servlet.http.HttpServlet;
  -import javax.servlet.http.HttpServletRequest;
  -import javax.servlet.http.HttpServletResponse;
  -import java.io.PrintWriter;
  -import java.io.IOException;
  -
  -public class Log4jInit extends HttpServlet {
  -
  -  public
  -  void init() {
  -    String prefix =  getServletContext().getRealPath("/");
  -    String file = getInitParameter("log4j-init-file");
  -    // if the log4j-init-file is not set, then no point in trying
  -    System.err.println("--- Log4jInit --- Executing ");
  -    if(file != null) {
  -        System.err.println("--- Log4jInit --- filename :"+prefix+file);
  -        PropertyConfigurator.configure(prefix+file);
  +import org.apache.log4j.Logger;
  +import org.apache.log4j.LogManager;
  +import org.apache.log4j.xml.DOMConfigurator;
  +
  +import javax.servlet.ServletContext;
  +import javax.servlet.FilterConfig;
  +import java.io.InputStream;
  +import java.util.Properties;
  +import java.util.Enumeration;
  +
  +public class Log4jInit  {
  +
  + public static void init (FilterConfig filterConfig) {
  +
  +        ServletContext servletContext = filterConfig.getServletContext();
  +
  +        boolean isLog4jPathOk = false;
  +
  +        System.err.println("--- Log4jInit --- Executing ");
  +
  +        String realPath;
  +        String xmlFile = 
filterConfig.getInitParameter("log4j-xml-init-file");
  +
  +        // ------------------------------------
  +        // if the log4j-init-file is not set, then no point in trying
  +
  +        servletContext.log(">> log4j-xml-init-file = " + xmlFile);
  +        realPath = servletContext.getRealPath(xmlFile);
  +
  +        servletContext.log(">> realPath = " + realPath);
  +
  +        /*
  +        if (isLog4JConfigured()) {
  +            getServletContext().log("Log4J already configured, aborting 
configuration...");
  +            return;
  +        }
  +        */
  +
  +        if (xmlFile != null) {
  +            try {
  +
  +                DOMConfigurator.configureAndWatch(realPath);
  +                isLog4jPathOk = true;
  +
  +            } catch (Exception e) {
  +                servletContext.log("Exception(DOMConfigurator) : " +
  +                                        e.getMessage());
  +                servletContext.log("Looking for WAR file");
  +            }
  +
  +        } else {
  +            servletContext.log(
  +                "Error, couldn't find log4j configuration file " +
  +                xmlFile.toString());
  +            servletContext.log("Looking for WAR file");
  +        }
  +
  +        // -------------------------------------
  +
  +        InputStream input = servletContext.getResourceAsStream(xmlFile);
  +        if (input != null) {
  +
  +            try {
  +                Properties prop = new Properties();
  +                prop.load(input);
  +
  +                PropertyConfigurator.configure(prop);
  +            } catch (Exception e) {
  +                servletContext.log("Exception(PropertyConfigurator) : " +
  +                                        e.getMessage());
  +                servletContext.log(xmlFile.toString() +
  +                                        " has not been found in WAR file");
  +                throw new RuntimeException(e.getMessage());
  +            }
  +
  +        } else {
  +            servletContext.log(xmlFile.toString() +
  +                                    " has not been found in WAR file");
  +        }
  +
  +
  +        //log.info("---- testing Parser ----");
  +        //testParser testparser = new testParser();
  +        //testparser.goahead();
       }
  -  }
   
  -  public
  -  void doGet(HttpServletRequest req, HttpServletResponse res) {
  -  }
  +    /**
  +     * Returns true if it appears that log4j have been previously 
configured. This code
  +     * checks to see if there are any appenders defined for log4j which is 
the
  +     * definitive way to tell if log4j is already initialized
  +     * @return boolean true if log4j has already been configured, false
  +     * otherwise
  +     */
  +    private static boolean isLog4JConfigured() {
  +        final Enumeration appendersEnum = Logger.getRoot().getAllAppenders();
  +        if ((appendersEnum != null) && (!(appendersEnum instanceof 
org.apache.log4j.helpers.NullEnumeration))) {
  +            return true;
  +        }
  +        else {
  +            Enumeration loggers = LogManager.getCurrentLoggers() ;
  +            while (loggers.hasMoreElements()) {
  +                Logger c = (Logger) loggers.nextElement();
  +                if (!(c.getAllAppenders() instanceof 
org.apache.log4j.helpers.NullEnumeration))
  +                    return true;
  +            }
  +        }
  +        return false;
  +    }
   }
  

Reply via email to