f6da6a6eccaf65791a2c90ebaf0f7ba5a20aff34
[jabaws.git] / webservices / compbio / ws / server / _WSLogger.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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 @Deprecated\r
32 public class _WSLogger {\r
33 \r
34         private static final String SPACER = " ";\r
35 \r
36         private final String wsname;\r
37         private final WebServiceContext wsContext;\r
38         private final Logger wsAccessLog;\r
39 \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
46         }\r
47 \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
52         }\r
53 \r
54         /**\r
55          * [Request_thread timeofRequest] configured via log4j configuration\r
56          * RequestSource web_service timeToProcessRequest methodname {taskid}\r
57          * \r
58          * @param statLog\r
59          * @param wsContext\r
60          * @param jobId\r
61          * @param activity\r
62          */\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
68                 } else {\r
69                         assert !Util.isEmpty(activity) : "if jobId is defined, then activity must be defined too";\r
70                 }\r
71 \r
72                 wsAccessLog.log(level, getRemoteAddress() + SPACER + wsname + SPACER\r
73                                 + timer.getTotalTime() + SPACER + activity + SPACER + jobId);\r
74         }\r
75 \r
76         public void logAll(Timer timer, String activity) {\r
77                 assert !Util.isEmpty(activity);\r
78                 log(timer, activity, Level.TRACE, "");\r
79         }\r
80 \r
81         public void logFine(Timer timer, String activity) {\r
82                 assert !Util.isEmpty(activity);\r
83                 log(timer, activity, Level.DEBUG, "");\r
84         }\r
85 \r
86         public void log(Timer timer, String activity) {\r
87                 assert !Util.isEmpty(activity);\r
88                 log(timer, activity, Level.INFO, "");\r
89         }\r
90 \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
94         }\r
95 \r
96         /*\r
97          * \r
98          * private static String getDataTimeForLog() { return\r
99          * DateFormat.getDateTimeInstance(DateFormat.MEDIUM,\r
100          * DateFormat.SHORT).format(System.currentTimeMillis()); }\r
101          * \r
102          * \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
108          */\r
109 \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
116         }\r
117 \r
118 }\r