1 /* Copyright (c) 2010 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.ws.server;
\r
20 import javax.servlet.http.HttpServletRequest;
\r
21 import javax.xml.ws.WebServiceContext;
\r
22 import javax.xml.ws.handler.MessageContext;
\r
24 import org.apache.log4j.Level;
\r
25 import org.apache.log4j.Logger;
\r
27 import compbio.util.Timer;
\r
28 import compbio.util.Util;
\r
29 import compbio.ws.client.Services;
\r
31 public class WSLogger {
\r
33 private static final String SPACER = " ";
\r
35 private final String wsname;
\r
36 private final WebServiceContext wsContext;
\r
37 private final Logger wsAccessLog;
\r
39 private WSLogger(Services wsname, WebServiceContext context) {
\r
40 this.wsname = wsname.toString();
\r
41 this.wsContext = context;
\r
42 this.wsAccessLog = Logger.getLogger(wsname.toString() + "-stats");
\r
43 assert context != null : "WSContext was not injected";
\r
44 assert wsAccessLog != null : "wsAccessLog is not obtained";
\r
47 public static final WSLogger getStatLogger(Services service,
\r
48 WebServiceContext wsContext) {
\r
49 assert service != null;
\r
50 return new WSLogger(service, wsContext);
\r
54 * [Request_thread timeofRequest] configured via log4j configuration
\r
55 * RequestSource web_service timeToProcessRequest methodname {taskid}
\r
62 private void log(Timer timer, String activity, Level level, String jobId) {
\r
63 assert !Util.isEmpty(jobId) || !Util.isEmpty(activity); // At least one
\r
64 // of the two must be defined
\r
65 if (Util.isEmpty(jobId)) {
\r
66 jobId = ""; // prevent is to be printed as null
\r
68 assert !Util.isEmpty(activity) : "if jobId is defined, then activity must be defined too";
\r
71 wsAccessLog.log(level, getRemoteAddress() + SPACER + wsname + SPACER
\r
72 + timer.getTotalTime() + SPACER + activity + SPACER + jobId);
\r
75 public void logAll(Timer timer, String activity) {
\r
76 assert !Util.isEmpty(activity);
\r
77 log(timer, activity, Level.TRACE, "");
\r
80 public void logFine(Timer timer, String activity) {
\r
81 assert !Util.isEmpty(activity);
\r
82 log(timer, activity, Level.DEBUG, "");
\r
85 public void log(Timer timer, String activity) {
\r
86 assert !Util.isEmpty(activity);
\r
87 log(timer, activity, Level.INFO, "");
\r
90 public void log(Timer timer, String activity, String jobId) {
\r
91 assert !Util.isEmpty(activity);
\r
92 log(timer, activity, Level.INFO, jobId);
\r
97 * private static String getDataTimeForLog() { return
\r
98 * DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
\r
99 * DateFormat.SHORT).format(System.currentTimeMillis()); }
\r
102 * public static String getRemoteAddress(WebServiceContext wsContext) {
\r
103 * assert wsContext != null : "WS context injection failed!"; MessageContext
\r
104 * msContext = wsContext.getMessageContext(); HttpServletRequest request =
\r
105 * (HttpServletRequest) msContext .get(MessageContext.SERVLET_REQUEST);
\r
106 * return request == null ? "127.0.0.1" : request.getRemoteAddr(); }
\r
109 public String getRemoteAddress() {
\r
110 assert wsContext != null : "WS context injection failed!";
\r
111 MessageContext msContext = wsContext.getMessageContext();
\r
112 HttpServletRequest request = (HttpServletRequest) msContext
\r
113 .get(MessageContext.SERVLET_REQUEST);
\r
114 return request == null ? "127.0.0.1" : request.getRemoteAddr();
\r