Coverage Report - org.kuali.rice.core.impl.config.module.Log4jLifeCycle
 
Classes in this File Line Coverage Branch Coverage Complexity
Log4jLifeCycle
0%
0/49
0%
0/12
4.667
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.impl.config.module;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.apache.log4j.PropertyConfigurator;
 21  
 import org.apache.log4j.xml.DOMConfigurator;
 22  
 import org.kuali.rice.core.api.config.property.Config;
 23  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 24  
 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
 25  
 import org.springframework.util.ResourceUtils;
 26  
 import org.w3c.dom.Document;
 27  
 
 28  
 import javax.xml.parsers.DocumentBuilder;
 29  
 import javax.xml.parsers.DocumentBuilderFactory;
 30  
 import java.io.ByteArrayInputStream;
 31  
 import java.io.FileNotFoundException;
 32  
 import java.io.IOException;
 33  
 import java.util.Properties;
 34  
 
 35  
 /**
 36  
  * Lifecycle implementation that initializes and shuts down Log4J logging
 37  
  */
 38  0
 class Log4jLifeCycle extends BaseLifecycle {
 39  
 
 40  
     private static final String LOG4J_FILE_NOT_FOUND = "log4j settings file not found at location: ";
 41  
 
 42  
         /**
 43  
      * Convenience constant representing a minute in milliseconds
 44  
      */
 45  
     private static final int MINUTE = 60 * 1000;
 46  
 
 47  
     /**
 48  
      * Location of default/automatic Log4J configuration properties, in Spring ResourceUtils resource/url syntax
 49  
      */
 50  
     private static final String AUTOMATIC_LOGGING_CONFIG_URL = "classpath:org/kuali/rice/core/logging/default-log4j.properties";
 51  
 
 52  
     /**
 53  
      * Default settings reload interval to use in the case that the settings are reloadable (i.e. they originate from a file)
 54  
      */
 55  
     private static final int DEFAULT_RELOAD_INTERVAL = 5 * MINUTE; // 5 minutes
 56  
 
 57  
     /**
 58  
      * Non-static and non-final so that it can be reset after configuration is read
 59  
      */
 60  0
     private Logger log = Logger.getLogger(getClass());
 61  
 
 62  
     @Override
 63  
         public void start() throws Exception {
 64  
         // obtain the root workflow config
 65  0
                 Config config = ConfigContext.getCurrentContextConfig();
 66  
 
 67  0
         boolean log4jFileExists = checkPropertiesFileExists(config.getProperty(Config.LOG4J_SETTINGS_PATH));
 68  
 
 69  
         // first check for in-line xml configuration
 70  0
                 String log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_XML);
 71  0
                 if (log4jconfig != null) {
 72  
                         try {
 73  0
                                 DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 74  0
                                 Document doc = b.parse(new ByteArrayInputStream(log4jconfig.getBytes()));
 75  0
                                 DOMConfigurator.configure(doc.getDocumentElement());
 76  
                                 // now get the reconfigured log instance
 77  0
                                 log = Logger.getLogger(getClass());
 78  0
                         } catch (Exception e) {
 79  0
                                 log.error("Error parsing Log4J configuration settings: " + log4jconfig, e);
 80  0
                         }
 81  
         // next check for in-line properties configuration
 82  0
                 } else if ((log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_PROPS)) != null) {
 83  0
                         Properties p = new Properties(config.getProperties());
 84  
                         try {
 85  0
                                 p.load(new ByteArrayInputStream(log4jconfig.getBytes()));
 86  0
                                 PropertyConfigurator.configure(p);
 87  0
                                 log = Logger.getLogger(getClass());
 88  0
                         } catch (IOException ioe) {
 89  0
                                 log.error("Error loading Log4J configuration settings: " + log4jconfig, ioe);
 90  0
                         }
 91  
         // check for an external file location specification
 92  0
                 } else if (log4jFileExists) {
 93  0
                         log.info("Configuring Log4J logging.");
 94  0
                         log4jconfig = config.getProperty(Config.LOG4J_SETTINGS_PATH);
 95  
 
 96  0
             int reloadInterval = DEFAULT_RELOAD_INTERVAL;
 97  
 
 98  0
             String log4jReloadInterval = config.getProperty(Config.LOG4J_SETTINGS_RELOADINTERVAL_MINS);
 99  0
                         if (log4jReloadInterval != null) {
 100  
                                 try {
 101  0
                     reloadInterval = Integer.parseInt(log4jReloadInterval) * MINUTE;
 102  0
                                 } catch (NumberFormatException nfe) {
 103  0
                                         log.warn("Invalid reload interval: " + log4jReloadInterval + ", using default: " + DEFAULT_RELOAD_INTERVAL + " milliseconds");
 104  0
                                 }
 105  
                         }
 106  0
             PropertyConfigurator.configureAndWatch(log4jconfig, reloadInterval);
 107  0
                         log = Logger.getLogger(getClass());
 108  0
                 } else {
 109  
 
 110  0
             PropertyConfigurator.configureAndWatch(AUTOMATIC_LOGGING_CONFIG_URL, DEFAULT_RELOAD_INTERVAL);
 111  0
             log = Logger.getLogger(getClass());
 112  
                 }
 113  0
                 super.start();
 114  0
         }
 115  
 
 116  
     /**
 117  
          * Checks if the passed in file exists.
 118  
          *
 119  
          * @param log4jSettingsPath the file
 120  
          * @return true if exists
 121  
          */
 122  
         private boolean checkPropertiesFileExists(String log4jSettingsPath) {
 123  0
                 if (StringUtils.isBlank(log4jSettingsPath)) {
 124  0
                         return false;
 125  
                 }
 126  
                 
 127  
                 boolean exists;
 128  
 
 129  
                 try {
 130  0
                         exists = ResourceUtils.getFile(log4jSettingsPath).exists();
 131  0
                 } catch (FileNotFoundException e) {
 132  0
                         exists = false;
 133  0
                 }
 134  
 
 135  0
                 if (!exists) {
 136  0
                         System.out.println(LOG4J_FILE_NOT_FOUND + log4jSettingsPath);
 137  
                 }
 138  
 
 139  0
                 return exists;
 140  
         }
 141  
 
 142  
     @Override
 143  
     public void stop() throws Exception {
 144  
             // commenting out LogManager.shutdown() for now because it kills logging before shutdown of the rest of the system is complete
 145  
             // so if there are other errors that are encountered during shutdown, they won't be logged!
 146  
 
 147  
             // move this to the standalone initialize listener instead
 148  
 
 149  
                 //LogManager.shutdown();
 150  0
                 super.stop();
 151  0
         }
 152  
 
 153  
 }