1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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
32 public class _WSLogger {
\r
34 private static final String SPACER = " ";
\r
36 private final String wsname;
\r
37 private final WebServiceContext wsContext;
\r
38 private final Logger wsAccessLog;
\r
40 private _WSLogger(Services wsname, WebServiceContext context) {
\r
41 this.wsname = wsname.toString();
\r
42 this.wsContext = context;
\r
43 this.wsAccessLog = Logger.getLogger(wsname.toString() + "-stats");
\r
44 assert context != null : "WSContext was not injected";
\r
45 assert wsAccessLog != null : "wsAccessLog is not obtained";
\r
48 public static final _WSLogger getStatLogger(Services service,
\r
49 WebServiceContext wsContext) {
\r
50 assert service != null;
\r
51 return new _WSLogger(service, wsContext);
\r
55 * [Request_thread timeofRequest] configured via log4j configuration
\r
56 * RequestSource web_service timeToProcessRequest methodname {taskid}
\r
63 private void log(Timer timer, String activity, Level level, String jobId) {
\r
64 assert !Util.isEmpty(jobId) || !Util.isEmpty(activity); // At least one
\r
65 // of the two must be defined
\r
66 if (Util.isEmpty(jobId)) {
\r
67 jobId = ""; // prevent is to be printed as null
\r
69 assert !Util.isEmpty(activity) : "if jobId is defined, then activity must be defined too";
\r
72 wsAccessLog.log(level, getRemoteAddress() + SPACER + wsname + SPACER
\r
73 + timer.getTotalTime() + SPACER + activity + SPACER + jobId);
\r
76 public void logAll(Timer timer, String activity) {
\r
77 assert !Util.isEmpty(activity);
\r
78 log(timer, activity, Level.TRACE, "");
\r
81 public void logFine(Timer timer, String activity) {
\r
82 assert !Util.isEmpty(activity);
\r
83 log(timer, activity, Level.DEBUG, "");
\r
86 public void log(Timer timer, String activity) {
\r
87 assert !Util.isEmpty(activity);
\r
88 log(timer, activity, Level.INFO, "");
\r
91 public void log(Timer timer, String activity, String jobId) {
\r
92 assert !Util.isEmpty(activity);
\r
93 log(timer, activity, Level.INFO, jobId);
\r
98 * private static String getDataTimeForLog() { return
\r
99 * DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
\r
100 * DateFormat.SHORT).format(System.currentTimeMillis()); }
\r
103 * public static String getRemoteAddress(WebServiceContext wsContext) {
\r
104 * assert wsContext != null : "WS context injection failed!"; MessageContext
\r
105 * msContext = wsContext.getMessageContext(); HttpServletRequest request =
\r
106 * (HttpServletRequest) msContext .get(MessageContext.SERVLET_REQUEST);
\r
107 * return request == null ? "127.0.0.1" : request.getRemoteAddr(); }
\r
110 public String getRemoteAddress() {
\r
111 assert wsContext != null : "WS context injection failed!";
\r
112 MessageContext msContext = wsContext.getMessageContext();
\r
113 HttpServletRequest request = (HttpServletRequest) msContext
\r
114 .get(MessageContext.SERVLET_REQUEST);
\r
115 return request == null ? "127.0.0.1" : request.getRemoteAddr();
\r