File: WSTester.java

package info (click to toggle)
libjaba-client-java 0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 368 kB
  • ctags: 551
  • sloc: java: 3,332; makefile: 11
file content (366 lines) | stat: -rw-r--r-- 12,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Copyright (c) 2009 Peter Troshin
 *  
 *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     
 * 
 *  This library is free software; you can redistribute it and/or modify it under the terms of the
 *  Apache License version 2 as published by the Apache Software Foundation
 * 
 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache 
 *  License for more details.
 * 
 *  A copy of the license is in apache_license.txt. It is also available here:
 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 * Any republication or derived work distributed in source code form
 * must include this copyright and license notice.
 */

package compbio.ws.client;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.xml.ws.WebServiceException;

import compbio.data.msa.MsaWS;
import compbio.data.sequence.Alignment;
import compbio.data.sequence.FastaSequence;
import compbio.data.sequence.SequenceUtil;
import compbio.metadata.JobStatus;
import compbio.metadata.JobSubmissionException;
import compbio.metadata.Limit;
import compbio.metadata.LimitsManager;
import compbio.metadata.Preset;
import compbio.metadata.PresetManager;
import compbio.metadata.ResultNotAvailableException;
import compbio.metadata.RunnerConfig;
import compbio.metadata.UnsupportedRuntimeException;
import compbio.metadata.WrongParameterException;

/**
 * Class for testing web services
 * 
 * @author pvtroshin
 * 
 * @version 1.0 February 2010
 */
public class WSTester {

	/**
	 * Sequences to be used as input for all WS
	 */
	static final String fastaInput = ">Foobar\n"
			+ "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAVARLGRVRWTQQRHAEAAVLLQQASDAAPEHPGIALWLGHALEDAGQAEAAAAAYTRAHQLLPEEPYITAQLLNWRRRLCDWRALDVLSAQVRAAVAQGVGAVEPFAFLSEDASAAEQLACARTRAQAIAASVRPLAPTRVRSKGPLRVGFVSNGFGAHPTGLLTVALFEALQRRQPDLQMHLFATSGDDGSTLRTRLAQASTLHDVTALGHLATAKHIRHHGIDLLFDLRGWGGGGRPEVFALRPAPVQVNWLAYPGTSGAPWMDYVLGDAFALPPALEPFYSEHVLRLQGAFQPSDTSRVVAEPPSRTQCGLPEQGVVLCCFNNSYKLNPQSMARMLAVLREVPDSVLWLLSG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\n";

	static final List<FastaSequence> seqs = loadSeqs();

	private static final String FAILED = "FAILED";
	private static final String OK = "OK";

	/**
	 * Converting input to a form accepted by WS
	 * 
	 * @return List of FastaSequence records
	 */
	static List<FastaSequence> loadSeqs() {
		try {
			return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput
					.getBytes()));
		} catch (IOException ignored) {
			// Should not happen as a source is not a external stream
			ignored.printStackTrace();
		}
		return null;
	}

	/**
	 * Prints usage
	 */
	static void printUsage() {
		System.out.println("Usage: <Class or Jar file name> "
				+ Jws2Client.hostkey + Jws2Client.pseparator
				+ "host_and_context " + "<" + Jws2Client.servicekey
				+ Jws2Client.pseparator + "serviceName>");
		System.out.println();
		System.out
				.println(Jws2Client.hostkey
						+ Jws2Client.pseparator
						+ "<host_and_context> - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws");
		System.out
				.println(Jws2Client.servicekey
						+ Jws2Client.pseparator
						+ "<ServiceName> - optional if unspecified all services are tested otherwise one of "
						+ Arrays.toString(Services.values()));
		System.out.println();

	}

	/**
	 * Calls alignment with preset
	 * 
	 * @param <T>
	 * @param msaws
	 * @param presets
	 *            list of the Preset
	 * @throws UnsupportedRuntimeException
	 */
	static <T> boolean presetAlign(MsaWS<T> msaws, List<Preset<T>> presets)
			throws UnsupportedRuntimeException {
		boolean succeed = false;
		for (Preset<T> preset : presets) {
			System.out.print("Aligning with preset '" + preset.getName()
					+ "'... ");
			Alignment al = null;
			try {
				String taskId = msaws.presetAlign(seqs, preset);
				al = msaws.getResult(taskId);
				if (al != null) {
					System.out.println(OK);
				}
				succeed = true;
			} catch (UnsupportedRuntimeException e) {
				System.out.println(FAILED);
				// If executable is not supported than none of the presets are
				// going to work
				throw new UnsupportedRuntimeException(e);
			} catch (JobSubmissionException e) {
				// TODO custom message
				System.out.println(FAILED);
				System.out.println();
				e.printStackTrace();
				continue;
			} catch (WrongParameterException e) {
				// TODO custom message
				System.out.println(FAILED);
				System.out.println();
				e.printStackTrace();
				continue;
			} catch (ResultNotAvailableException e) {
				// TODO custom message
				System.out.println(FAILED);
				System.out.println();
				e.printStackTrace();
				continue;
			}
		}
		return succeed;
	}

	/**
	 * Call most of web services functions and check the output
	 * 
	 * @param <T>
	 *            web service type
	 * @param msaws
	 * @throws UnsupportedRuntimeException
	 *             is thrown if the connection to a web service was made, but
	 *             the web service is not functional. e.g. when native
	 *             executable does not exists for a server platform
	 */
	static <T> boolean checkService(MsaWS<T> msaws)
			throws UnsupportedRuntimeException {

		boolean succeed = testDefaultAlignment(msaws);

		// If exception above is thrown than the tests below is not run

		PresetManager<T> pmanager = msaws.getPresets();
		if (pmanager != null && pmanager.getPresets().size() > 0) {
			System.out.println("Testing alignment with presets:");
			List<Preset<T>> plist = pmanager.getPresets();
			succeed = !succeed ? presetAlign(msaws, plist) : succeed;
		}

		System.out.print("Querying presets...");

		if (pmanager != null && pmanager.getPresets().size() > 0) {
			System.out.println(OK);
		} else {
			System.out.println("UNSUPPORTED");
		}

		System.out.print("Querying Parameters...");
		RunnerConfig<T> options = msaws.getRunnerOptions();
		if (options != null && options.getArguments().size() > 0) {
			System.out.println(OK);
		} else {
			System.out.println(FAILED);
			succeed = false;
		}

		System.out.print("Querying Limits...");
		LimitsManager<T> limits = msaws.getLimits();
		if (limits != null && limits.getLimits().size() > 0) {
			System.out.println(OK);
		} else {
			System.out.println("UNSUPPORTED");
		}

		System.out.print("Querying Local Engine Limits...");
		Limit<T> localLimit = msaws
				.getLimit(PresetManager.LOCAL_ENGINE_LIMIT_PRESET);
		if (localLimit != null) {
			System.out.println(OK);
		} else {
			System.out.println("UNSUPPORTED");
		}
		return succeed;
	}

	/**
	 * Align using default settings
	 * 
	 * @param <T>
	 * @param msaws
	 * @throws UnsupportedRuntimeException
	 */
	static <T> boolean testDefaultAlignment(MsaWS<T> msaws)
			throws UnsupportedRuntimeException {
		System.out.print("Testing alignment with default parameters:");
		Alignment al = null;
		boolean succeed = false;
		try {
			String taskId = msaws.align(seqs);
			System.out.print("\nQuerying job status...");
			JobStatus status = msaws.getJobStatus(taskId);
			while (status != JobStatus.FINISHED) {
				Thread.sleep(1000);
				status = msaws.getJobStatus(taskId);
			}
			System.out.println(OK);
			System.out.print("Retrieving results...");
			al = msaws.getResult(taskId);
			succeed = true;
		} catch (ResultNotAvailableException e) {
			System.out.println(FAILED);
			e.printStackTrace();
		} catch (JobSubmissionException e) {
			System.out.println(FAILED);
			System.out.println();
			e.printStackTrace();
		} catch (InterruptedException e) {
			System.out.println(FAILED);
			System.out.println();
			e.printStackTrace();
		}
		if (al != null) {
			System.out.println(OK);
		}
		return succeed;
	}

	/**
	 * Connect to a WS using the host and the service name
	 * 
	 * @param <T>
	 * @param host
	 * @param service
	 * @return
	 */
	static <T> MsaWS<T> connect(String host, Services service) {
		MsaWS<T> msaws = null;
		try {
			System.out.print("Connecting to service " + service + " on " + host
					+ " ... ");
			msaws = Jws2Client.connect(host, service);
			System.out.println(OK);
		} catch (WebServiceException e) {
			System.out.println(FAILED);
		}
		return msaws;
	}

	/**
	 * Test JWS2 web services
	 * 
	 * @param <T>
	 *            web service type
	 * @param args
	 *            -h=<Your web application server host name, port and JWS2
	 *            context path>
	 * 
	 *            -s=<ServiceName> which is optional. If service name is not
	 *            provided then all known JWS2 web services are tested
	 * @throws IOException
	 */
	public static <T> void main(String[] args) throws IOException {

		if (args == null || args.length < 1) {
			printUsage();
			System.exit(0);
		}
		String host = Jws2Client.getHost(args);
		String serviceName = Jws2Client.getServiceName(args);
		if (!Jws2Client.validURL(host)) {
			System.out
					.println("<host_and_context> parameter is not provided or is incorrect!");
			System.exit(1);
		}
		boolean succeed = false;
		MsaWS<T> msaws = null;
		if (serviceName != null) {
			Services service = Services.getService(serviceName);
			if (service == null) {
				System.out.println("Service '" + serviceName
						+ "' is not supported. Valid values are: "
						+ Arrays.toString(Services.values()));
				System.out.println();
				printUsage();
				System.exit(1);
			}

			msaws = connect(host, service);
			if (msaws == null) {
				System.exit(1);
			}
			try {
				succeed = checkService(msaws);
			} catch (UnsupportedRuntimeException ignored) {
				System.exit(1);
			} finally {
				((Closeable) msaws).close();
			}
			reportResults(service, succeed);
			System.exit(0);
		}

		System.out
				.println("<ServiceName> is not provided checking all known services...");

		for (Services serv : Services.values()) {
			System.out.println();
			msaws = connect(host, serv);
			if (msaws == null) {
				continue;
			}
			try {
				succeed = checkService(msaws);
			} catch (UnsupportedRuntimeException ignored) {
				System.out.println("Service " + serv + " IS NOT FUNCTIONAL");
				continue;
			} finally {
				((Closeable) msaws).close();
			}
			reportResults(serv, succeed);
		}

	}

	private static void reportResults(Services serv, boolean succeed) {
		if (succeed) {
			System.out.println("Check is completed. The Service " + serv
					+ " IS WORKING");
		} else {
			System.out.println("Check is completed. The Service " + serv
					+ " HAS SOME PROBLEMS");
		}
	}
}