| 1 | | | /* |
| 2 | | | * $Id: LogExtender.java.sample 101 2006-12-05 20:34:26Z amandel $ |
| 3 | | | * |
| 4 | | | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
| 5 | | | * |
| 6 | | | * Redistribution and use in source and binary forms, with or without |
| 7 | | | * modification, are permitted provided that the following conditions are |
| 8 | | | * met: |
| 9 | | | * |
| 10 | | | * * Redistributions of source code must retain the above copyright |
| 11 | | | * notice, this list of conditions and the following disclaimer. |
| 12 | | | * * Redistributions in binary form must reproduce the above |
| 13 | | | * copyright notice, this list of conditions and the following |
| 14 | | | * disclaimer in the documentation and/or other materials |
| 15 | | | * provided with the distribution. |
| 16 | | | * * Neither the name of the jCoderZ.org Project nor the names of |
| 17 | | | * its contributors may be used to endorse or promote products |
| 18 | | | * derived from this software without specific prior written |
| 19 | | | * permission. |
| 20 | | | * |
| 21 | | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
| 22 | | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
| 25 | | | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | | | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | | | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 30 | | | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 31 | | | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | | | */ |
| 33 | | | package org.jcoderz.commons.logging.bea; |
| 34 | | | |
| 35 | | | import java.util.ArrayList; |
| 36 | | | import java.util.Arrays; |
| 37 | | | import java.util.Iterator; |
| 38 | | | import java.util.List; |
| 39 | | | import java.util.StringTokenizer; |
| 40 | | | import java.util.logging.ErrorManager; |
| 41 | | | import java.util.logging.Handler; |
| 42 | | | import java.util.logging.LogManager; |
| 43 | | | import java.util.logging.Logger; |
| 44 | | | |
| 45 | | | import weblogic.application.ApplicationLifecycleEvent; |
| 46 | | | import weblogic.application.ApplicationLifecycleListener; |
| 47 | | | |
| 48 | | | /** |
| 49 | | | * This class listens for application lifecyle events and installs additional |
| 50 | | | * log handlers accordingly. |
| 51 | | | * It queries the LogManager for the property: |
| 52 | | | * <ul> |
| 53 | | | * <li><code>org.jcoderz.commons.logging.bea.LogExtender.handlers</code> |
| 54 | | | * which gives a list of handlers to be installed at application preStart and |
| 55 | | | * to be deinstalled at application postStop. |
| 56 | | | * <li><code>org.jcoderz.commons.logging.bea.LogExtender.rootloggers</code> |
| 57 | | | * which gives a list of logger names, for which the handlers are set. All |
| 58 | | | * loggers having them as parent loggers will use the newly installed handlers, |
| 59 | | | * unless they have explicitely the parent handlers disabled.<p> |
| 60 | | | * </ul> |
| 61 | | | * |
| 62 | | | * For installing this LogExtender within an application the ear must contain |
| 63 | | | * a weblogic-application.xml file with the following entry:<p> |
| 64 | | | * <pre> |
| 65 | | | * <listener> |
| 66 | | | * <listener-class> |
| 67 | | | * org.jcoderz.commons.logging.bea.LogExtender |
| 68 | | | * </listener-class> |
| 69 | | | * </listener> |
| 70 | | | * </pre> |
| 71 | | | * <p> |
| 72 | | | * For more information see Bea Weblogic documentation on application |
| 73 | | | * development. |
| 74 | | | * |
| 75 | | | */ |
| 76 | | | public class LogExtender |
| 77 | | | extends ApplicationLifecycleListener |
| 78 | | | { |
| 79 | | | private static final String CLASSNAME = LogExtender.class.getName(); |
| 80 | | | private static final String HANDLERS_PROPERTY |
| 81 | | | = CLASSNAME + ".handlers"; |
| 82 | | | private static final String LOGGERS_PROPERTY |
| 83 | | | = CLASSNAME + ".rootloggers"; |
| 84 | | | |
| 85 | | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 86 | | | |
| 87 | | | private final List mHandlerNames = new ArrayList(); |
| 88 | | | private final List mHandlers = new ArrayList(); |
| 89 | | | private final List mLoggerNames = new ArrayList(); |
| 90 | | | |
| 91 | | | /** {@inheritDoc} */ |
| 92 | | | public void postStop (final ApplicationLifecycleEvent appEvent) |
| 93 | | | { |
| 94 | | | super.postStop(appEvent); |
| 95 | | | |
| 96 | | | deinstallHandlers(); |
| 97 | | | |
| 98 | | | mHandlers.clear(); |
| 99 | | | mHandlerNames.clear(); |
| 100 | | | mLoggerNames.clear(); |
| 101 | | | } |
| 102 | | | |
| 103 | | | /** {@inheritDoc} */ |
| 104 | | | public void preStart (final ApplicationLifecycleEvent appEvent) |
| 105 | | | { |
| 106 | | | super.preStart(appEvent); |
| 107 | | | |
| 108 | | | mHandlers.clear(); |
| 109 | | | mHandlerNames.clear(); |
| 110 | | | mLoggerNames.clear(); |
| 111 | | | |
| 112 | | | configureHandlers(); |
| 113 | | | configureLoggers(); |
| 114 | | | installHandlers(); |
| 115 | | | } |
| 116 | | | |
| 117 | | | private void configureHandlers () |
| 118 | | | { |
| 119 | | | final String handlers |
| 120 | | | = LogManager.getLogManager().getProperty(HANDLERS_PROPERTY); |
| 121 | | | |
| 122 | | | if ((handlers != null) && (handlers.length() > 0)) |
| 123 | | | { |
| 124 | | | final StringTokenizer tokenizer |
| 125 | | | = new StringTokenizer(handlers, " ,/t"); |
| 126 | | | |
| 127 | | | while (tokenizer.hasMoreTokens()) |
| 128 | | | { |
| 129 | | | mHandlerNames.add(tokenizer.nextToken()); |
| 130 | | | } |
| 131 | | | } |
| 132 | | | } |
| 133 | | | |
| 134 | | | private void configureLoggers () |
| 135 | | | { |
| 136 | | | final String loggers |
| 137 | | | = LogManager.getLogManager().getProperty(LOGGERS_PROPERTY); |
| 138 | | | |
| 139 | | | if ((loggers != null) && (loggers.length() > 0)) |
| 140 | | | { |
| 141 | | | final StringTokenizer tokenizer |
| 142 | | | = new StringTokenizer(loggers, " ,/t"); |
| 143 | | | |
| 144 | | | while (tokenizer.hasMoreTokens()) |
| 145 | | | { |
| 146 | | | mLoggerNames.add(tokenizer.nextToken()); |
| 147 | | | } |
| 148 | | | } |
| 149 | | | } |
| 150 | | | |
| 151 | | | private void installHandlers () |
| 152 | | | { |
| 153 | | | for (final Iterator iter = mHandlerNames.iterator(); iter.hasNext(); ) |
| 154 | | | { |
| 155 | | | final String name = (String) iter.next(); |
| 156 | | | try |
| 157 | | | { |
| 158 | | | mHandlers.add(Class.forName(name).newInstance()); |
| 159 | | | } |
| 160 | | | catch (Exception ex) |
| 161 | | | { |
| 162 | | | reportError("Cannot instantiate new handler class " + name, ex); |
| 163 | | | } |
| 164 | | | } |
| 165 | | | for (final Iterator lit = mLoggerNames.iterator(); lit.hasNext(); ) |
| 166 | | | { |
| 167 | | | final Logger curLogger = Logger.getLogger((String) lit.next()); |
| 168 | | | |
| 169 | | | for (final Iterator hit = mHandlers.iterator(); hit.hasNext(); ) |
| 170 | | | { |
| 171 | | | curLogger.addHandler((Handler) hit.next()); |
| 172 | | | } |
| 173 | | | } |
| 174 | | | } |
| 175 | | | |
| 176 | | | private void deinstallHandlers () |
| 177 | | | { |
| 178 | | | for (final Iterator hit = mHandlers.iterator(); hit.hasNext(); ) |
| 179 | | | { |
| 180 | | | final Handler handler = (Handler) hit.next(); |
| 181 | | | for (final Iterator lit = mLoggerNames.iterator(); lit.hasNext(); ) |
| 182 | | | { |
| 183 | | | final Logger curLogger = Logger.getLogger((String) lit.next()); |
| 184 | | | try |
| 185 | | | { |
| 186 | | | curLogger.removeHandler(handler); |
| 187 | | | } |
| 188 | | | catch (Exception ex) |
| 189 | | | { |
| 190 | | | reportError("Cannot remove handler " + handler + " from logger " |
| 191 | | | + curLogger.getName(), ex); |
| 192 | | | } |
| 193 | | | } |
| 194 | | | try |
| 195 | | | { |
| 196 | | | handler.close(); |
| 197 | | | } |
| 198 | | | catch (Exception ex) |
| 199 | | | { |
| 200 | | | reportError("Cannot close handler " + handler, ex); |
| 201 | | | } |
| 202 | | | } |
| 203 | | | } |
| 204 | | | |
| 205 | | | /** |
| 206 | | | * Reports an error to each ErrorManager installed for the internal logger. |
| 207 | | | * |
| 208 | | | * @param msg The message of the error. |
| 209 | | | * @param ex THe exception leading to the error. |
| 210 | | | */ |
| 211 | | | private void reportError (final String msg, final Exception ex) |
| 212 | | | { |
| 213 | | | final List handlers = Arrays.asList(logger.getHandlers()); |
| 214 | | | |
| 215 | | | for (final Iterator iter = handlers.iterator(); iter.hasNext(); ) |
| 216 | | | { |
| 217 | | | final Handler handler = (Handler) iter.next(); |
| 218 | | | handler.getErrorManager().error(msg, ex, ErrorManager.GENERIC_FAILURE); |
| 219 | | | } |
| 220 | | | } |
| 221 | | | } |