001 /*
002 * Copyright (C) The Apache Software Foundation. All rights reserved.
003 *
004 * This software is published under the terms of the Apache Software License
005 * version 1.1, a copy of which has been included with this distribution in
006 * the LICENSE file.
007 *
008 * $Id: MessageDrivenObjectSupport.java 155459 2005-02-26 13:24:44Z dirkv $
009 */
010 package org.apache.commons.messagelet;
011
012 import javax.servlet.ServletContext;
013 import javax.servlet.ServletException;
014
015 import org.apache.commons.logging.Log;
016 import org.apache.commons.logging.LogFactory;
017
018 /** <p><code>MessageDrivenObjectSupport</code> is an abstract base
019 * class for implementing your own MessageDrivenObject instance
020 * with some useful implementation level methods.</p>
021 *
022 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
023 * @version $Revision: 155459 $
024 */
025 public abstract class MessageDrivenObjectSupport implements MessageDrivenObject {
026
027
028 /** The Log to which logging calls will be made. */
029 private Log log = LogFactory.getLog( getClass() );
030
031 /**
032 * The ServletContext with which this dispatcher is associated.
033 */
034 private ServletContext context;
035
036
037
038 public MessageDrivenObjectSupport() {
039 }
040
041 public ServletContext getServletContext() {
042 return context;
043 }
044
045 /**
046 * This method allows the init() method to be overriden without having to
047 * call the super.init( ServletContext ) method first. This is similar
048 * to the init() method in {@link javax.servlet.GenericServlet}.
049 */
050 public void init() throws ServletException {
051 }
052
053 // MessageDrivenObject methods
054 //-------------------------------------------------------------------------
055 public void init(ServletContext context) throws ServletException {
056 this.context = context;
057 init();
058 }
059
060 public void destroy() {
061 this.context = null;
062 }
063
064
065
066 // Implementation methods
067 //-------------------------------------------------------------------------
068
069 /**
070 * Provides access to a logger which can use JDK 1.4 logging, log4j or logkit
071 * under the covers.
072 */
073 protected Log getLog() {
074 return log;
075 }
076
077 /** Logs a message to the current ServletContext */
078 protected void log( String message ) {
079 if ( context == null ) {
080 log.error( "No Servletcontext available so cannot use it for logging" );
081 log.info( message );
082 }
083 else {
084 context.log( message );
085 }
086 }
087
088 /** Logs a message and exception the current ServletContext */
089 protected void log( String message, Throwable t) {
090 if ( context == null ) {
091 log.error( "No Servletcontext available so cannot use it for logging" );
092 log.error( message, t );
093 }
094 else {
095 context.log( message, t );
096 }
097 }
098 }