fb96de8adab902ac20b6af10466683a7c9f51872
[jabaws.git] / webservices / compbio / ws / server / WSUtil.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.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.HashSet;\r
23 import java.util.List;\r
24 import java.util.Set;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.data.sequence.FastaSequence;\r
29 import compbio.data.sequence.ScoreManager;\r
30 import compbio.engine.AsyncExecutor;\r
31 import compbio.engine.Configurator;\r
32 import compbio.engine.ProgressGetter;\r
33 import compbio.engine.client.ConfiguredExecutable;\r
34 import compbio.metadata.ChunkHolder;\r
35 import compbio.metadata.JobStatus;\r
36 import compbio.metadata.JobSubmissionException;\r
37 import compbio.metadata.Limit;\r
38 import compbio.metadata.LimitExceededException;\r
39 import compbio.metadata.Option;\r
40 import compbio.metadata.ResultNotAvailableException;\r
41 \r
42 public final class WSUtil {\r
43 \r
44         public static final void validateJobId(String jobId)\r
45                         throws InvalidParameterException {\r
46                 if (!compbio.engine.client.Util.isValidJobId(jobId)) {\r
47                         throw new InvalidParameterException(\r
48                                         "JobId is not provided or cannot be recognised! Given value: "\r
49                                                         + jobId);\r
50                 }\r
51         }\r
52 \r
53         public static final void validateFastaInput(List<FastaSequence> sequences)\r
54                         throws JobSubmissionException {\r
55                 if (sequences == null || sequences.isEmpty()) {\r
56                         throw new JobSubmissionException(\r
57                                         "List of fasta sequences required but not provided! ");\r
58                 }\r
59                 Set<String> names = new HashSet<String>();\r
60                 for (FastaSequence fs : sequences) {\r
61                         boolean unique = names.add(fs.getId());\r
62                         if (!unique) {\r
63                                 throw new JobSubmissionException(\r
64                                                 "Input sequences must have unique names! \n"\r
65                                                                 + "Sequence " + fs.getId() + " is a duplicate!");\r
66                         }\r
67                         if (fs.getLength() == 0) {\r
68                                 throw new JobSubmissionException(\r
69                                                 "Sequence must not be empty! Sequence: " + fs.getId()\r
70                                                                 + " was empty");\r
71                         }\r
72                 }\r
73         }\r
74 \r
75         public static JobStatus getJobStatus(String jobId) {\r
76                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
77                 return asyncEngine.getJobStatus(jobId);\r
78         }\r
79 \r
80         public static ChunkHolder pullFile(String file, long position) {\r
81                 return ProgressGetter.pull(file, position);\r
82         }\r
83 \r
84         public static byte getProgress(String jobId) {\r
85                 throw new UnsupportedOperationException();\r
86         }\r
87 \r
88         public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
89                 assert confClustal != null;\r
90                 return Configurator.getAsyncEngine(confClustal);\r
91         }\r
92 \r
93         public static boolean cancelJob(String jobId) {\r
94                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
95                 return asyncEngine.cancelJob(jobId);\r
96         }\r
97 \r
98         public static <T> String align(List<FastaSequence> sequences,\r
99                         ConfiguredExecutable<T> confExec, Logger logger,\r
100                         String callingMethod, Limit<T> limit)\r
101                         throws LimitExceededException, JobSubmissionException {\r
102 \r
103                 if (limit != null && limit.isExceeded(sequences)) {\r
104                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
105                 }\r
106                 compbio.runner.Util.writeInput(sequences, confExec);\r
107                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
108                 String jobId = engine.submitJob(confExec);\r
109                 return jobId;\r
110         }\r
111 \r
112         public static <T> String analize(List<FastaSequence> sequences,\r
113                         ConfiguredExecutable<T> confExec, Logger log, String method,\r
114                         Limit<T> limit) throws JobSubmissionException {\r
115                 if (limit != null && limit.isExceeded(sequences)) {\r
116                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
117                 }\r
118                 log.debug("Method: " + method + " with task: " + confExec.getTaskId());\r
119 \r
120                 compbio.runner.Util.writeInput(sequences, confExec);\r
121                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
122                 String jobId = engine.submitJob(confExec);\r
123 \r
124                 return jobId;\r
125         }\r
126 \r
127         /*\r
128          * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
129          */\r
130         public static final <T> List<String> getCommands(List<Option<T>> options,\r
131                         String keyValueSeparator) {\r
132                 List<String> oList = new ArrayList<String>();\r
133                 for (Option<T> o : options) {\r
134                         oList.add(o.toCommand(keyValueSeparator));\r
135                 }\r
136                 return oList;\r
137         }\r
138 \r
139         public static void validateAAConInput(List<FastaSequence> sequences)\r
140                         throws JobSubmissionException {\r
141                 validateFastaInput(sequences);\r
142                 int len = 0;\r
143                 for (FastaSequence fs : sequences) {\r
144                         if (len == 0) {\r
145                                 len = fs.getLength();\r
146                                 continue;\r
147                         }\r
148                         if (fs.getLength() != len) {\r
149                                 throw new JobSubmissionException(\r
150                                                 "All sequences must be of the same length. Please align "\r
151                                                                 + "the sequences prior to submission! The first sequence length is : "\r
152                                                                 + len + " but the sequence '" + fs.getId()\r
153                                                                 + "' length is " + fs.getLength());\r
154                         }\r
155                 }\r
156         }\r
157 \r
158         public static <T> ScoreManager getAnnotation(String jobId, Logger log)\r
159                         throws ResultNotAvailableException {\r
160                 WSUtil.validateJobId(jobId);\r
161                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
162                 ConfiguredExecutable<T> aacon = (ConfiguredExecutable<T>) asyncEngine\r
163                                 .getResults(jobId);\r
164                 ScoreManager mas = aacon.getResults();\r
165                 log.trace(jobId + " getConservation : " + mas);\r
166                 return mas;\r
167         }\r
168 \r
169         /*\r
170          * UNUSED\r
171          * \r
172          * @SuppressWarnings("unchecked") static <T> LimitsManager<T>\r
173          * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext\r
174          * wsContext) {\r
175          * \r
176          * String LIMIT_KEY = CACHE_KEY + clazz.getCanonicalName(); LimitsManager<T>\r
177          * limit = (LimitsManager<T>) getObjectFromApplContext( LIMIT_KEY,\r
178          * wsContext); if (limit == null) { synchronized (WSUtil.class) { limit =\r
179          * (LimitsManager<T>) getObjectFromApplContext(LIMIT_KEY, wsContext); if\r
180          * (limit == null) { limit = compbio.runner.Util\r
181          * .getLimits((Class<Executable<T>>) clazz);\r
182          * addObjectToApplContext(wsContext, LIMIT_KEY, limit); } } } return limit;\r
183          * }\r
184          * \r
185          * static void addObjectToApplContext(WebServiceContext wsContext, String\r
186          * objKey, Object obj) { assert !Util.isEmpty(objKey) :\r
187          * "Key for the object must not be empty! "; assert wsContext != null;\r
188          * \r
189          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
190          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); assert ctx !=\r
191          * null; log.debug("Adding object with key '" + objKey + "' and value '" +\r
192          * obj + "' to the application context"); ctx.setAttribute(objKey, obj); }\r
193          * static Object getObjectFromApplContext(String objKey, WebServiceContext\r
194          * wsContext) { assert !Util.isEmpty(objKey) :\r
195          * "Key for the object must not be empty! "; assert wsContext != null;\r
196          * \r
197          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
198          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); Object obj =\r
199          * ctx.getAttribute(objKey); log.trace("Retrieving object with key '" +\r
200          * objKey + "' and value '" + obj + "' from the application context");\r
201          * return obj; }\r
202          */\r
203 }\r