6f33f24ba84069acf65aa37bbe2aa9d5d6b84d7a
[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 final String CACHE_KEY = "LIMITS_CACHE";\r
42 \r
43         private static Logger log = Logger.getLogger(WSUtil.class);\r
44 \r
45         public static final void validateJobId(String jobId)\r
46                         throws InvalidParameterException {\r
47                 if (!compbio.engine.client.Util.isValidJobId(jobId)) {\r
48                         throw new InvalidParameterException(\r
49                                         "JobId is not provided or cannot be recognised! Given value: "\r
50                                                         + jobId);\r
51                 }\r
52         }\r
53 \r
54         public static final void validateFastaInput(List<FastaSequence> sequences)\r
55                         throws JobSubmissionException {\r
56                 if (sequences == null || sequences.isEmpty()) {\r
57                         throw new JobSubmissionException(\r
58                                         "List of fasta sequences required but not provided! ");\r
59                 }\r
60                 for (FastaSequence fs : sequences) {\r
61                         if (fs.getLength() == 0) {\r
62                                 throw new JobSubmissionException(\r
63                                                 "Sequence must not be empty! Sequence: " + fs.getId()\r
64                                                                 + " was empty");\r
65                         }\r
66                 }\r
67         }\r
68 \r
69         public static JobStatus getJobStatus(String jobId) {\r
70                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
71                 return asyncEngine.getJobStatus(jobId);\r
72         }\r
73 \r
74         public static ChunkHolder pullFile(String file, long position) {\r
75                 return ProgressGetter.pull(file, position);\r
76         }\r
77 \r
78         public static byte getProgress(String jobId) {\r
79                 throw new UnsupportedOperationException();\r
80         }\r
81 \r
82         public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
83                 assert confClustal != null;\r
84                 return Configurator.getAsyncEngine(confClustal);\r
85         }\r
86 \r
87         public static boolean cancelJob(String jobId) {\r
88                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
89                 return asyncEngine.cancelJob(jobId);\r
90         }\r
91 \r
92         public static <T> String align(List<FastaSequence> sequences,\r
93                         ConfiguredExecutable<T> confExec, WSLogger logger,\r
94                         String callingMethod, Limit<T> limit)\r
95                         throws LimitExceededException, JobSubmissionException {\r
96                 Timer timer = Timer.getMilliSecondsTimer();\r
97                 if (limit != null && limit.isExceeded(sequences)) {\r
98                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
99                 }\r
100                 compbio.runner.Util.writeInput(sequences, confExec);\r
101                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
102                 String jobId = engine.submitJob(confExec);\r
103                 if (logger != null) {\r
104                         logger.log(timer, callingMethod, jobId);\r
105                 }\r
106                 return jobId;\r
107         }\r
108 \r
109         /*\r
110          * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
111          */\r
112         public static final <T> List<String> getCommands(List<Option<T>> options,\r
113                         String keyValueSeparator) {\r
114                 List<String> oList = new ArrayList<String>();\r
115                 for (Option<T> o : options) {\r
116                         oList.add(o.toCommand(keyValueSeparator));\r
117                 }\r
118                 return oList;\r
119         }\r
120 \r
121         public static void validateAAConInput(List<FastaSequence> sequences)\r
122                         throws JobSubmissionException {\r
123                 validateFastaInput(sequences);\r
124                 int len = 0;\r
125                 for (FastaSequence fs : sequences) {\r
126                         if (len == 0) {\r
127                                 len = fs.getLength();\r
128                                 continue;\r
129                         }\r
130                         if (fs.getLength() != len) {\r
131                                 throw new JobSubmissionException(\r
132                                                 "All sequences must be of the same length. Please align "\r
133                                                                 + "the sequences prior to submission! The first sequence length is : "\r
134                                                                 + len + " but the sequence '" + fs.getId()\r
135                                                                 + "' length is " + fs.getLength());\r
136                         }\r
137                 }\r
138         }\r
139 \r
140         /*\r
141          * UNUSED\r
142          * \r
143          * @SuppressWarnings("unchecked") static <T> LimitsManager<T>\r
144          * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext\r
145          * wsContext) {\r
146          * \r
147          * String LIMIT_KEY = CACHE_KEY + clazz.getCanonicalName(); LimitsManager<T>\r
148          * limit = (LimitsManager<T>) getObjectFromApplContext( LIMIT_KEY,\r
149          * wsContext); if (limit == null) { synchronized (WSUtil.class) { limit =\r
150          * (LimitsManager<T>) getObjectFromApplContext(LIMIT_KEY, wsContext); if\r
151          * (limit == null) { limit = compbio.runner.Util\r
152          * .getLimits((Class<Executable<T>>) clazz);\r
153          * addObjectToApplContext(wsContext, LIMIT_KEY, limit); } } } return limit;\r
154          * }\r
155          * \r
156          * static void addObjectToApplContext(WebServiceContext wsContext, String\r
157          * objKey, Object obj) { assert !Util.isEmpty(objKey) :\r
158          * "Key for the object must not be empty! "; assert wsContext != null;\r
159          * \r
160          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
161          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); assert ctx !=\r
162          * null; log.debug("Adding object with key '" + objKey + "' and value '" +\r
163          * obj + "' to the application context"); ctx.setAttribute(objKey, obj); }\r
164          * static Object getObjectFromApplContext(String objKey, WebServiceContext\r
165          * wsContext) { assert !Util.isEmpty(objKey) :\r
166          * "Key for the object must not be empty! "; assert wsContext != null;\r
167          * \r
168          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
169          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); Object obj =\r
170          * ctx.getAttribute(objKey); log.trace("Retrieving object with key '" +\r
171          * objKey + "' and value '" + obj + "' from the application context");\r
172          * return obj; }\r
173          */\r
174 }\r