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