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 java.io.PrintWriter;
\r
21 import java.io.StringWriter;
\r
22 import java.io.Writer;
\r
23 import java.util.Date;
\r
24 import java.util.Map;
\r
25 import java.util.Set;
\r
26 import java.util.concurrent.ConcurrentHashMap;
\r
28 import javax.annotation.Resource;
\r
29 import javax.jws.WebService;
\r
30 import javax.servlet.http.HttpServletRequest;
\r
31 import javax.xml.ws.WebServiceContext;
\r
32 import javax.xml.ws.handler.MessageContext;
\r
34 import org.apache.log4j.Logger;
\r
36 import compbio.data.msa.JABAService;
\r
37 import compbio.ws.client.Services;
\r
38 import compbio.ws.client.WSTester;
\r
41 * JABAWS services registry
\r
46 @WebService(endpointInterface = "compbio.data.msa.RegistryWS", targetNamespace = JABAService.V2_SERVICE_NAMESPACE, serviceName = "RegistryWS")
\r
47 public class RegistryWS implements compbio.data.msa.RegistryWS, JABAService {
\r
49 // Ask for resource injection
\r
51 WebServiceContext wsContext;
\r
53 private static Logger log = Logger.getLogger(RegistryWS.class);
\r
56 * Stores tested and passed (the test) services and their testing time
\r
58 private final static Map<Services, Date> operating = new ConcurrentHashMap<Services, Date>();
\r
61 * Indicate whether the services were tested at all
\r
63 private static boolean allTested = false;
\r
66 public Set<Services> getSupportedServices() {
\r
68 return operating.keySet();
\r
71 private void init() {
\r
72 // Do not allow tests to run concurrently
\r
74 synchronized (operating) {
\r
83 private boolean timeToTest() {
\r
88 if (getLongestUntestedServiceTime() > 3600 * 24) {
\r
95 * Return time in seconds for the test for the oldest unchecked service
\r
99 private int getLongestUntestedServiceTime() {
\r
100 int timePassed = 0;
\r
101 for (Services serv : operating.keySet()) {
\r
102 int lasttimepassed = getLastTested(serv);
\r
103 if (timePassed < lasttimepassed) {
\r
104 timePassed = lasttimepassed;
\r
111 public int getLastTested(Services service) {
\r
112 Date testedOn = getLastTestedOn(service);
\r
113 if (testedOn != null) {
\r
114 return (int) ((System.currentTimeMillis() - testedOn.getTime()) / 1000);
\r
120 * Can potentially return null if the service has not been tested yet.
\r
123 public Date getLastTestedOn(Services service) {
\r
124 if (operating.containsKey(service)) {
\r
125 return operating.get(service);
\r
131 * TODO improve reporting. stop testing service on unsupported runtime env
\r
135 public String testAllServices() {
\r
136 Writer testlog = new StringWriter();
\r
137 PrintWriter writer = new PrintWriter(testlog, true);
\r
138 WSTester tester = new WSTester(getServicePath(), writer);
\r
139 // This is done deliberately to prevent malicious user from overloading
\r
141 synchronized (operating) {
\r
142 for (Services service : Services.values()) {
\r
144 if (tester.checkService(service)) {
\r
145 operating.put(service, new Date());
\r
147 } catch (Exception e) {
\r
148 log.info(e, e.getCause());
\r
149 writer.println("Fails to connect to a web service: "
\r
150 + service + " With " + e.getLocalizedMessage()
\r
152 e.printStackTrace(writer);
\r
157 return testlog.toString();
\r
160 private String getServicePath() {
\r
161 assert wsContext != null : "WS context injection failed!";
\r
162 MessageContext msContext = wsContext.getMessageContext();
\r
163 HttpServletRequest request = (HttpServletRequest) msContext
\r
164 .get(MessageContext.SERVLET_REQUEST);
\r
166 StringBuffer server = request.getRequestURL();
\r
167 server = server.delete(server.lastIndexOf("/"), server.length());
\r
168 return server.toString();
\r
172 public String testService(Services service) {
\r
173 String server = getServicePath();
\r
174 Writer testlog = new StringWriter();
\r
175 PrintWriter writer = new PrintWriter(testlog, true);
\r
176 WSTester tester = new WSTester(server, writer);
\r
178 synchronized (operating) {
\r
179 boolean succeed = tester.checkService(service);
\r
181 operating.put(service, new Date());
\r
184 } catch (Exception e) {
\r
185 log.info(e, e.getCause());
\r
186 writer.println("Fails to connect to a web service: " + service
\r
187 + " With " + e.getLocalizedMessage() + "\nDetails: ");
\r
188 e.printStackTrace(writer);
\r
192 return testlog.toString();
\r
195 public boolean isOperating(Services service) {
\r
197 return operating.containsKey(service);
\r