View Javadoc
1 /* ==================================================================== 2 * License: 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. The end-user documentation included with the redistribution, 17 * if any, must include the following acknowledgment: 18 * "This product includes software developed by 19 * Robert Half International (http://www.rhi.com/)." 20 * Alternately, this acknowledgment may appear in the software itself, 21 * if and wherever such third-party acknowledgments normally appear. 22 * 23 * 4. The names "Parc", "RHI", and "Robert Half International" must 24 * not be used to endorse or promote products derived from this 25 * software without prior written permission. For written 26 * permission, please contact pete.mckinstry@rhi.com. 27 * 28 * 5. Products derived from this software may not be called "PARC", 29 * nor may "PARC" appear in their name, without prior written 30 * permission of Robert Half International. 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * ==================================================================== 45 * 46 */ 47 package com.rhi.architecture.logging; 48 49 import com.rhi.architecture.config.ConfigurationException; 50 import com.rhi.architecture.resource.InitializationException; 51 52 import java.io.IOException; 53 import java.io.InputStream; 54 import java.util.HashMap; 55 import java.util.Properties; 56 57 import org.apache.log4j.PropertyConfigurator; 58 59 /*** 60 * The Log4JFactory/Log4JLogger are holdovers from a specific system that 61 * required some special configuration. These special settings complicated the 62 * usage pattern, and so this class has been provided for other users who wish 63 * to use Log4j, but dont' want/need to inherit the baggage from that system. 64 * It performs only the absolute minimum configuration & leaves everything 65 * else up to Log4j itself. 66 * 67 * @author Pete McKinstry 68 * @version 1.0 69 */ 70 public class BasicLog4JFactory extends LogFactory { 71 72 /*** 73 * config entry specifying the log4j configuration file to use. 74 */ 75 public static final String LOG4J_PROPERTYFILE = "Log4j.Properties"; 76 /*** 77 * the default category to use when logging. 78 */ 79 public static final String LOG4J_DEFAULTCATEGORY = "Log4j.DefaultCategory"; 80 81 // application properties 82 private Properties props; 83 84 // cache Categories 85 private static HashMap categories = new HashMap(); 86 87 // Default logger name 88 private static String defaultLoggerName = Logger.DEFAULT_CATEGORY; 89 90 /* 91 * Inititialization flag. 92 * Only allows the logging factory to be initialized once per 93 * application. If you run multiple processes w/in the same 94 * application (e.g. GIT->GST->FILE) you would attempt to 95 * initialize the logfactory 3 times since it is a Resource 96 * & would be reloaded each time the ResourceContext is 97 * re-initialized for each process. Ensure that this doesn't 98 * happen by holding on to a static flag that ensures init() 99 * only happens once. 100 */ 101 private static boolean loaded = false; 102 103 /*** 104 * default constructor - protected 105 * 106 * @since 1.0 107 */ 108 public BasicLog4JFactory() { 109 super(); 110 } 111 112 /*** 113 * Perform whatever initialization is required of the resource. 114 * This method should only be called once per application instance. 115 * 116 * @param p 117 * @throws InitializationException 118 * 119 * @since 1.2 120 */ 121 public void init(Properties p) throws InitializationException { 122 this.props = p; 123 124 /* in the case of multi-process applications, the init() may 125 * be called more than once. Ensure that log4j only creates one file by 126 * only configuring it one time per application. 127 */ 128 if (!isLoaded()) { 129 // configure log4j if a log4j config file is provided in the 130 // parc configuration. If not, ignore configuration on the assumption 131 // that log4j will handle it internally. 132 String log4j_properties = props.getProperty(LOG4J_PROPERTYFILE); 133 if (log4j_properties != null) { 134 // create a temporary properties object into which the Log4J 135 // properties from the file will be loaded for passing to the 136 // Log4J configurator 137 Properties tempProps = new Properties(); 138 InputStream is = Thread.currentThread() 139 .getContextClassLoader().getResourceAsStream(log4j_properties); 140 if (is != null) { 141 try { 142 // create an input stream for reading the properties from 143 // the file and load it into the temporary properties object 144 is.available(); 145 tempProps.load(is); 146 } catch (IOException ioe) { 147 throw new ConfigurationException("File not found in classpath"); 148 } 149 } 150 else { 151 throw new ConfigurationException("File not found in classpath"); 152 } 153 PropertyConfigurator.configure(tempProps); 154 } 155 156 // log4j initialized. 157 loaded = true; 158 159 // If there is a default logger configured in the properties file then 160 // use that. Otherwise use use Logger.DEFAULT_CATEGORY 161 defaultLoggerName = 162 p.getProperty(LOG4J_DEFAULTCATEGORY, Logger.DEFAULT_CATEGORY); 163 } 164 } 165 166 /*** 167 * Returns the properties. 168 * 169 * @return Properties 170 */ 171 protected Properties getProperties() { 172 return this.props; 173 } 174 175 /*** 176 * Perform any required cleanup. 177 * 178 * @since 1.0 179 */ 180 public void close() { 181 categories.clear(); 182 } 183 184 /*** 185 * @param type 186 * @see com.rhi.architecture.logging.LogFactory#getLogger(String) 187 * @return 188 * @throws ConfigurationException 189 */ 190 public Logger getLogger(String type) throws ConfigurationException { 191 if (categories.containsKey(type) == false) { 192 categories.put(type, new Log4JLogger(type)); 193 } 194 return (Logger) categories.get(type); 195 } 196 197 /*** 198 * Get default logger. This method exists to support backward 199 * compatibility prior to the factory class. Prefer 200 * getLogger(String type) instead. 201 * 202 * @return Logger 203 * @throws ConfigurationException 204 */ 205 public Logger getDefaultLogger() throws ConfigurationException { 206 return getLogger(defaultLoggerName); 207 } 208 209 /*** 210 * Returns the loaded. 211 * @return boolean 212 */ 213 protected boolean isLoaded() { 214 return loaded; 215 } 216 217 }

This page was automatically generated by Maven