Refactoring of all SequenceAnnotation web services
[jabaws.git] / webservices / compbio / ws / server / _WSLogger.java
diff --git a/webservices/compbio/ws/server/_WSLogger.java b/webservices/compbio/ws/server/_WSLogger.java
new file mode 100644 (file)
index 0000000..5b8ebd1
--- /dev/null
@@ -0,0 +1,118 @@
+/* Copyright (c) 2010 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+package compbio.ws.server;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.xml.ws.WebServiceContext;\r
+import javax.xml.ws.handler.MessageContext;\r
+\r
+import org.apache.log4j.Level;\r
+import org.apache.log4j.Logger;\r
+\r
+import compbio.util.Timer;\r
+import compbio.util.Util;\r
+import compbio.ws.client.Services;\r
+\r
+@Deprecated\r
+public class _WSLogger {\r
+\r
+       private static final String SPACER = " ";\r
+\r
+       private final String wsname;\r
+       private final WebServiceContext wsContext;\r
+       private final Logger wsAccessLog;\r
+\r
+       private _WSLogger(Services wsname, WebServiceContext context) {\r
+               this.wsname = wsname.toString();\r
+               this.wsContext = context;\r
+               this.wsAccessLog = Logger.getLogger(wsname.toString() + "-stats");\r
+               assert context != null : "WSContext was not injected";\r
+               assert wsAccessLog != null : "wsAccessLog is not obtained";\r
+       }\r
+\r
+       public static final _WSLogger getStatLogger(Services service,\r
+                       WebServiceContext wsContext) {\r
+               assert service != null;\r
+               return new _WSLogger(service, wsContext);\r
+       }\r
+\r
+       /**\r
+        * [Request_thread timeofRequest] configured via log4j configuration\r
+        * RequestSource web_service timeToProcessRequest methodname {taskid}\r
+        * \r
+        * @param statLog\r
+        * @param wsContext\r
+        * @param jobId\r
+        * @param activity\r
+        */\r
+       private void log(Timer timer, String activity, Level level, String jobId) {\r
+               assert !Util.isEmpty(jobId) || !Util.isEmpty(activity); // At least one\r
+               // of the two must be defined\r
+               if (Util.isEmpty(jobId)) {\r
+                       jobId = ""; // prevent is to be printed as null\r
+               } else {\r
+                       assert !Util.isEmpty(activity) : "if jobId is defined, then activity must be defined too";\r
+               }\r
+\r
+               wsAccessLog.log(level, getRemoteAddress() + SPACER + wsname + SPACER\r
+                               + timer.getTotalTime() + SPACER + activity + SPACER + jobId);\r
+       }\r
+\r
+       public void logAll(Timer timer, String activity) {\r
+               assert !Util.isEmpty(activity);\r
+               log(timer, activity, Level.TRACE, "");\r
+       }\r
+\r
+       public void logFine(Timer timer, String activity) {\r
+               assert !Util.isEmpty(activity);\r
+               log(timer, activity, Level.DEBUG, "");\r
+       }\r
+\r
+       public void log(Timer timer, String activity) {\r
+               assert !Util.isEmpty(activity);\r
+               log(timer, activity, Level.INFO, "");\r
+       }\r
+\r
+       public void log(Timer timer, String activity, String jobId) {\r
+               assert !Util.isEmpty(activity);\r
+               log(timer, activity, Level.INFO, jobId);\r
+       }\r
+\r
+       /*\r
+        * \r
+        * private static String getDataTimeForLog() { return\r
+        * DateFormat.getDateTimeInstance(DateFormat.MEDIUM,\r
+        * DateFormat.SHORT).format(System.currentTimeMillis()); }\r
+        * \r
+        * \r
+        * public static String getRemoteAddress(WebServiceContext wsContext) {\r
+        * assert wsContext != null : "WS context injection failed!"; MessageContext\r
+        * msContext = wsContext.getMessageContext(); HttpServletRequest request =\r
+        * (HttpServletRequest) msContext .get(MessageContext.SERVLET_REQUEST);\r
+        * return request == null ? "127.0.0.1" : request.getRemoteAddr(); }\r
+        */\r
+\r
+       public String getRemoteAddress() {\r
+               assert wsContext != null : "WS context injection failed!";\r
+               MessageContext msContext = wsContext.getMessageContext();\r
+               HttpServletRequest request = (HttpServletRequest) msContext\r
+                               .get(MessageContext.SERVLET_REQUEST);\r
+               return request == null ? "127.0.0.1" : request.getRemoteAddr();\r
+       }\r
+\r
+}\r