AAConWS further work and test cases
[jabaws.git] / webservices / compbio / ws / server / WSUtil.java
1 /* Copyright (c) 2009 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 java.security.InvalidParameterException;\r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 \r
24 import org.apache.log4j.Logger;\r
25 \r
26 import compbio.data.sequence.FastaSequence;\r
27 import compbio.engine.AsyncExecutor;\r
28 import compbio.engine.Configurator;\r
29 import compbio.engine.ProgressGetter;\r
30 import compbio.engine.client.ConfiguredExecutable;\r
31 import compbio.metadata.ChunkHolder;\r
32 import compbio.metadata.JobStatus;\r
33 import compbio.metadata.JobSubmissionException;\r
34 import compbio.metadata.Limit;\r
35 import compbio.metadata.LimitExceededException;\r
36 import compbio.metadata.Option;\r
37 import compbio.util.Timer;\r
38 \r
39 public final class WSUtil {\r
40 \r
41         private static Logger log = Logger.getLogger(WSUtil.class);\r
42 \r
43         public static final void validateJobId(String jobId)\r
44                         throws InvalidParameterException {\r
45                 if (!compbio.engine.client.Util.isValidJobId(jobId)) {\r
46                         throw new InvalidParameterException(\r
47                                         "JobId is not provided or cannot be recognised! Given value: "\r
48                                                         + jobId);\r
49                 }\r
50         }\r
51 \r
52         public static final void validateFastaInput(List<FastaSequence> sequences)\r
53                         throws InvalidParameterException {\r
54                 if (sequences == null || sequences.isEmpty()) {\r
55                         throw new InvalidParameterException(\r
56                                         "List of fasta sequences required but not provided! ");\r
57                 }\r
58         }\r
59 \r
60         public static JobStatus getJobStatus(String jobId) {\r
61                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
62                 return asyncEngine.getJobStatus(jobId);\r
63         }\r
64 \r
65         public static ChunkHolder pullFile(String file, long position) {\r
66                 return ProgressGetter.pull(file, position);\r
67         }\r
68 \r
69         public static byte getProgress(String jobId) {\r
70                 throw new UnsupportedOperationException();\r
71         }\r
72 \r
73         public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
74                 assert confClustal != null;\r
75                 return Configurator.getAsyncEngine(confClustal);\r
76         }\r
77 \r
78         public static boolean cancelJob(String jobId) {\r
79                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
80                 return asyncEngine.cancelJob(jobId);\r
81         }\r
82 \r
83         public static <T> String align(List<FastaSequence> sequences,\r
84                         ConfiguredExecutable<T> confExec, WSLogger logger,\r
85                         String callingMethod, Limit<T> limit)\r
86                         throws LimitExceededException, JobSubmissionException {\r
87                 Timer timer = Timer.getMilliSecondsTimer();\r
88                 if (limit != null && limit.isExceeded(sequences)) {\r
89                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
90                 }\r
91                 compbio.runner.Util.writeInput(sequences, confExec);\r
92                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
93                 String jobId = engine.submitJob(confExec);\r
94                 if (logger != null) {\r
95                         logger.log(timer, callingMethod, jobId);\r
96                 }\r
97                 return jobId;\r
98         }\r
99 \r
100         /*\r
101          * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
102          */\r
103         public static final <T> List<String> getCommands(List<Option<T>> options,\r
104                         String keyValueSeparator) {\r
105                 List<String> oList = new ArrayList<String>();\r
106                 for (Option<T> o : options) {\r
107                         oList.add(o.toCommand(keyValueSeparator));\r
108                 }\r
109                 return oList;\r
110         }\r
111 \r
112 }\r