KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > handlers > LogHandler


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.handlers;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.Message;
60 import org.jboss.axis.MessageContext;
61 import org.jboss.axis.utils.Messages;
62 import org.jboss.logging.Logger;
63
64 import java.io.FileWriter JavaDoc;
65 import java.io.IOException JavaDoc;
66 import java.io.PrintWriter JavaDoc;
67
68 /**
69  * A simple Handler which logs the request and response messages to either
70  * the console or a specified file (default "axis.log").
71  * <p/>
72  * To use this, deploy it either in both the request and response flows
73  * (global, service, or transport) or in just the response flow. If deployed
74  * in both places, you'll also get an elapsed time indication, which can be
75  * handy for debugging.
76  *
77  * @author Doug Davis (dug@us.ibm.com)
78  * @author Glen Daniels (gdaniels@apache.org)
79  */

80 public class LogHandler extends BasicHandler
81 {
82    private static Logger log = Logger.getLogger(LogHandler.class.getName());
83
84    long start = -1;
85    private boolean writeToConsole = false;
86    private String JavaDoc filename = "axis.log";
87
88    public void init()
89    {
90       super.init();
91
92       Object JavaDoc opt = this.getOption("LogHandler.writeToConsole");
93       if (opt != null && opt instanceof String JavaDoc &&
94               "true".equalsIgnoreCase((String JavaDoc)opt))
95          writeToConsole = true;
96
97       opt = this.getOption("LogHandler.fileName");
98       if (opt != null && opt instanceof String JavaDoc)
99          filename = (String JavaDoc)opt;
100    }
101
102    public void invoke(MessageContext msgContext) throws AxisFault
103    {
104       log.debug("Enter: LogHandler::invoke");
105       if (msgContext.getPastPivot() == false)
106       {
107          start = System.currentTimeMillis();
108       }
109       else
110       {
111          logMessages(msgContext);
112       }
113       log.debug("Exit: LogHandler::invoke");
114    }
115
116    private void logMessages(MessageContext msgContext) throws AxisFault
117    {
118       try
119       {
120          PrintWriter JavaDoc writer = null;
121
122          writer = getWriter(msgContext);
123
124          Message inMsg = msgContext.getRequestMessage();
125          Message outMsg = msgContext.getResponseMessage();
126
127          writer.println("=======================================================");
128          if (start != -1)
129          {
130             writer.println("= " + Messages.getMessage("elapsed00",
131                     "" + (System.currentTimeMillis() - start)));
132          }
133          writer.println("= " + Messages.getMessage("inMsg00",
134                  (inMsg == null ? "null" : inMsg.getSOAPPartAsString())));
135          writer.println("= " + Messages.getMessage("outMsg00",
136                  (outMsg == null ? "null" : outMsg.getSOAPPartAsString())));
137          writer.println("=======================================================");
138
139          //START FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16646
140
if (!writeToConsole)
141          {
142             writer.close();
143          }
144          //END FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16646
145

146       }
147       catch (Exception JavaDoc e)
148       {
149          log.error(Messages.getMessage("exception00"), e);
150          throw AxisFault.makeFault(e);
151       }
152    }
153
154    private PrintWriter JavaDoc getWriter(MessageContext msgContext) throws IOException JavaDoc
155    {
156       PrintWriter JavaDoc writer;
157
158       // Allow config info to control where we write.
159
if (writeToConsole)
160       {
161          // Writing to the console
162
writer = new PrintWriter JavaDoc(System.out);
163       }
164       else
165       {
166          // Writing to a file.
167
if (filename == null)
168          {
169             filename = "axis.log";
170          }
171          writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(filename, true));
172       }
173       return writer;
174    }
175
176
177    public void onFault(MessageContext msgContext)
178    {
179       try
180       {
181          logMessages(msgContext);
182       }
183       catch (AxisFault axisFault)
184       {
185          log.error(Messages.getMessage("exception00"), axisFault);
186       }
187    }
188 }
189
190 ;
191
Popular Tags