View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    * 
8    * $Id: MessageDrivenObjectSupport.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messagelet;
11  
12  import javax.servlet.ServletContext;
13  import javax.servlet.ServletException;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /** <p><code>MessageDrivenObjectSupport</code> is an abstract base
19    * class for implementing your own MessageDrivenObject instance
20    * with some useful implementation level methods.</p>
21    *
22    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
23    * @version $Revision: 155459 $
24    */
25  public abstract class MessageDrivenObjectSupport implements MessageDrivenObject {
26  
27      
28      /** The Log to which logging calls will be made. */
29      private Log log = LogFactory.getLog( getClass() );
30      
31      /**
32       * The ServletContext with which this dispatcher is associated.
33       */
34      private ServletContext context;
35  
36  
37      
38      public MessageDrivenObjectSupport() {
39      }
40  
41      public ServletContext getServletContext() {
42          return context;
43      }
44      
45      /** 
46       * This method allows the init() method to be overriden without having to 
47       * call the super.init( ServletContext ) method first. This is similar
48       * to the init() method in {@link javax.servlet.GenericServlet}.
49       */
50      public void init() throws ServletException {
51      }
52      
53      // MessageDrivenObject methods
54      //-------------------------------------------------------------------------        
55      public void init(ServletContext context) throws ServletException {
56          this.context = context;
57          init();
58      }
59  
60      public void destroy() {
61          this.context = null;
62      }
63  
64      
65      
66      // Implementation methods
67      //-------------------------------------------------------------------------        
68  
69      /**
70       * Provides access to a logger which can use JDK 1.4 logging, log4j or logkit 
71       * under the covers.
72       */
73      protected Log getLog() {
74          return log;
75      }
76      
77      /** Logs a message to the current ServletContext */
78      protected void log( String message ) {
79          if ( context == null ) {
80              log.error( "No Servletcontext available so cannot use it for logging" );
81              log.info( message );
82          }
83          else {
84              context.log( message );
85          }
86      }
87      
88      /** Logs a message and exception the current ServletContext */
89      protected void log( String message, Throwable t) {
90          if ( context == null ) {
91              log.error( "No Servletcontext available so cannot use it for logging" );
92              log.error( message, t );
93          }
94          else {
95              context.log( message, t );
96          }
97      }    
98  }