01: /*
02: * Copyright 2001-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
25: import java.io.PrintWriter;
26: import java.util.Date;
27:
28: public class LogHandler extends BasicHandler {
29: public void invoke(MessageContext msgContext) throws AxisFault {
30: /** Log an access each time we get invoked.
31: */
32: try {
33: Handler serviceHandler = msgContext.getService();
34: String filename = (String) getOption("filename");
35: if ((filename == null) || (filename.equals("")))
36: throw new AxisFault("Server.NoLogFile",
37: "No log file configured for the LogHandler!",
38: null, null);
39: FileOutputStream fos = new FileOutputStream(filename, true);
40:
41: PrintWriter writer = new PrintWriter(fos);
42:
43: Integer numAccesses = (Integer) serviceHandler
44: .getOption("accesses");
45: if (numAccesses == null)
46: numAccesses = new Integer(0);
47:
48: numAccesses = new Integer(numAccesses.intValue() + 1);
49:
50: Date date = new Date();
51: String result = date + ": service "
52: + msgContext.getTargetService() + " accessed "
53: + numAccesses + " time(s).";
54: serviceHandler.setOption("accesses", numAccesses);
55:
56: writer.println(result);
57:
58: writer.close();
59: } catch (Exception e) {
60: throw AxisFault.makeFault(e);
61: }
62: }
63: }
|