KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > userguide > example4 > 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 samples.userguide.example4;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Handler;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.handlers.BasicHandler;
23
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.Date JavaDoc;
27
28 public class LogHandler extends BasicHandler {
29     public void invoke(MessageContext msgContext) throws AxisFault
30     {
31         /** Log an access each time we get invoked.
32          */

33         try {
34             Handler serviceHandler = msgContext.getService();
35             String JavaDoc filename = (String JavaDoc)getOption("filename");
36             if ((filename == null) || (filename.equals("")))
37                 throw new AxisFault("Server.NoLogFile",
38                                  "No log file configured for the LogHandler!",
39                                     null, null);
40             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(filename, true);
41             
42             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(fos);
43             
44             Integer JavaDoc numAccesses =
45                              (Integer JavaDoc)serviceHandler.getOption("accesses");
46             if (numAccesses == null)
47                 numAccesses = new Integer JavaDoc(0);
48             
49             numAccesses = new Integer JavaDoc(numAccesses.intValue() + 1);
50             
51             Date JavaDoc date = new Date JavaDoc();
52             String JavaDoc result = date + ": service " +
53                             msgContext.getTargetService() +
54                             " accessed " + numAccesses + " time(s).";
55             serviceHandler.setOption("accesses", numAccesses);
56             
57             writer.println(result);
58             
59             writer.close();
60         } catch (Exception JavaDoc e) {
61             throw AxisFault.makeFault(e);
62         }
63     }
64 }
65
Popular Tags