KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.handlers ;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Message;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.utils.Messages;
24 import org.apache.commons.logging.Log;
25
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 /**
31  * A simple Handler which logs the request and response messages to either
32  * the console or a specified file (default "axis.log").
33  *
34  * To use this, deploy it either in both the request and response flows
35  * (global, service, or transport) or in just the response flow. If deployed
36  * in both places, you'll also get an elapsed time indication, which can be
37  * handy for debugging.
38  *
39  * @author Doug Davis (dug@us.ibm.com)
40  * @author Glen Daniels (gdaniels@apache.org)
41  */

42 public class LogHandler extends BasicHandler {
43     protected static Log log =
44         LogFactory.getLog(LogHandler.class.getName());
45
46     long start = -1;
47     private boolean writeToConsole = false;
48     private String JavaDoc filename = "axis.log";
49
50     public void init() {
51         super.init();
52
53         Object JavaDoc opt = this.getOption("LogHandler.writeToConsole");
54         if (opt != null && opt instanceof String JavaDoc &&
55                 "true".equalsIgnoreCase((String JavaDoc)opt))
56             writeToConsole = true;
57
58         opt = this.getOption("LogHandler.fileName");
59         if (opt != null && opt instanceof String JavaDoc)
60             filename = (String JavaDoc)opt;
61     }
62
63     public void invoke(MessageContext msgContext) throws AxisFault {
64         log.debug("Enter: LogHandler::invoke");
65         if (msgContext.getPastPivot() == false) {
66            start = System.currentTimeMillis();
67         } else {
68             logMessages(msgContext);
69         }
70         log.debug("Exit: LogHandler::invoke");
71     }
72
73     private void logMessages(MessageContext msgContext) throws AxisFault {
74         try {
75             PrintWriter JavaDoc writer = null;
76
77             writer = getWriter();
78
79             Message inMsg = msgContext.getRequestMessage();
80             Message outMsg = msgContext.getResponseMessage();
81
82             writer.println( "=======================================================" );
83             if (start != -1) {
84                 writer.println( "= " + Messages.getMessage("elapsed00",
85                        "" + (System.currentTimeMillis() - start)));
86             }
87             writer.println( "= " + Messages.getMessage("inMsg00",
88                    (inMsg == null ? "null" : inMsg.getSOAPPartAsString())));
89             writer.println( "= " + Messages.getMessage("outMsg00",
90                    (outMsg == null ? "null" : outMsg.getSOAPPartAsString())));
91             writer.println( "=======================================================" );
92
93             //START FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16646
94
if (!writeToConsole) {
95               writer.close();
96             }
97             //END FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16646
98

99         } catch( Exception JavaDoc e ) {
100             log.error( Messages.getMessage("exception00"), e );
101             throw AxisFault.makeFault(e);
102         }
103     }
104
105     private PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
106         PrintWriter JavaDoc writer;
107
108         // Allow config info to control where we write.
109
if (writeToConsole) {
110             // Writing to the console
111
writer = new PrintWriter JavaDoc(System.out);
112         } else {
113             // Writing to a file.
114
if (filename == null) {
115                 filename = "axis.log";
116             }
117             writer = new PrintWriter JavaDoc(new FileWriter JavaDoc( filename, true ));
118         }
119         return writer;
120     }
121
122
123     public void onFault(MessageContext msgContext) {
124         try {
125             logMessages(msgContext);
126         } catch (AxisFault axisFault) {
127             log.error(Messages.getMessage("exception00"), axisFault);
128         }
129     }
130 };
131
Popular Tags