28f6d1099ffccfc1d01d0894eace988208c572c8
[jabaws.git] / webservices / compbio / ws / server / WSLogger.java
1 /* Copyright (c) 2010 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
4  * \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
7  * \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
11  * \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
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.ws.server;\r
19 \r
20 import javax.servlet.http.HttpServletRequest;\r
21 import javax.xml.ws.WebServiceContext;\r
22 import javax.xml.ws.handler.MessageContext;\r
23 \r
24 import org.apache.log4j.Level;\r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.util.Timer;\r
28 import compbio.util.Util;\r
29 import compbio.ws.client.Services;\r
30 \r
31 public class WSLogger {\r
32 \r
33     private static final String SPACER = " ";\r
34 \r
35     private final String wsname;\r
36     private final WebServiceContext wsContext;\r
37     private final Logger wsAccessLog;\r
38 \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
45     }\r
46 \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
51     }\r
52 \r
53     /**\r
54      * [Request_thread timeofRequest] configured via log4j configuration\r
55      * RequestSource web_service timeToProcessRequest methodname {taskid}\r
56      * \r
57      * @param statLog\r
58      * @param wsContext\r
59      * @param jobId\r
60      * @param activity\r
61      */\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
67         } else {\r
68             assert !Util.isEmpty(activity) : "if jobId is defined, then activity must be defined too";\r
69         }\r
70 \r
71         wsAccessLog.log(level, getRemoteAddress() + SPACER + wsname + SPACER\r
72                 + timer.getTotalTime() + SPACER + activity + SPACER + jobId);\r
73     }\r
74 \r
75     public void logAll(Timer timer, String activity) {\r
76         assert !Util.isEmpty(activity);\r
77         log(timer, activity, Level.TRACE, "");\r
78     }\r
79 \r
80     public void logFine(Timer timer, String activity) {\r
81         assert !Util.isEmpty(activity);\r
82         log(timer, activity, Level.DEBUG, "");\r
83     }\r
84 \r
85     public void log(Timer timer, String activity) {\r
86         assert !Util.isEmpty(activity);\r
87         log(timer, activity, Level.INFO, "");\r
88     }\r
89 \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
93     }\r
94 \r
95     /*\r
96      * \r
97      * private static String getDataTimeForLog() { return\r
98      * DateFormat.getDateTimeInstance(DateFormat.MEDIUM,\r
99      * DateFormat.SHORT).format(System.currentTimeMillis()); }\r
100      * \r
101      * \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
107      */\r
108 \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
115     }\r
116 \r
117 }\r