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