Change the way to deal with Limits to simplify wrapper writing and enable couping...
[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 InvalidParameterException {\r
56                 if (sequences == null || sequences.isEmpty()) {\r
57                         throw new InvalidParameterException(\r
58                                         "List of fasta sequences required but not provided! ");\r
59                 }\r
60         }\r
61 \r
62         public static JobStatus getJobStatus(String jobId) {\r
63                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
64                 return asyncEngine.getJobStatus(jobId);\r
65         }\r
66 \r
67         public static ChunkHolder pullFile(String file, long position) {\r
68                 return ProgressGetter.pull(file, position);\r
69         }\r
70 \r
71         public static byte getProgress(String jobId) {\r
72                 throw new UnsupportedOperationException();\r
73         }\r
74 \r
75         public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {\r
76                 assert confClustal != null;\r
77                 return Configurator.getAsyncEngine(confClustal);\r
78         }\r
79 \r
80         public static boolean cancelJob(String jobId) {\r
81                 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);\r
82                 return asyncEngine.cancelJob(jobId);\r
83         }\r
84 \r
85         public static <T> String align(List<FastaSequence> sequences,\r
86                         ConfiguredExecutable<T> confExec, WSLogger logger,\r
87                         String callingMethod, Limit<T> limit)\r
88                         throws LimitExceededException, JobSubmissionException {\r
89                 Timer timer = Timer.getMilliSecondsTimer();\r
90                 if (limit != null && limit.isExceeded(sequences)) {\r
91                         throw LimitExceededException.newLimitExceeded(limit, sequences);\r
92                 }\r
93                 compbio.runner.Util.writeInput(sequences, confExec);\r
94                 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);\r
95                 String jobId = engine.submitJob(confExec);\r
96                 if (logger != null) {\r
97                         logger.log(timer, callingMethod, jobId);\r
98                 }\r
99                 return jobId;\r
100         }\r
101 \r
102         /*\r
103          * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation\r
104          */\r
105         public static final <T> List<String> getCommands(List<Option<T>> options,\r
106                         String keyValueSeparator) {\r
107                 List<String> oList = new ArrayList<String>();\r
108                 for (Option<T> o : options) {\r
109                         oList.add(o.toCommand(keyValueSeparator));\r
110                 }\r
111                 return oList;\r
112         }\r
113 \r
114         /*\r
115          * UNUSED\r
116          * \r
117          * @SuppressWarnings("unchecked") static <T> LimitsManager<T>\r
118          * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext\r
119          * wsContext) {\r
120          * \r
121          * String LIMIT_KEY = CACHE_KEY + clazz.getCanonicalName(); LimitsManager<T>\r
122          * limit = (LimitsManager<T>) getObjectFromApplContext( LIMIT_KEY,\r
123          * wsContext); if (limit == null) { synchronized (WSUtil.class) { limit =\r
124          * (LimitsManager<T>) getObjectFromApplContext(LIMIT_KEY, wsContext); if\r
125          * (limit == null) { limit = compbio.runner.Util\r
126          * .getLimits((Class<Executable<T>>) clazz);\r
127          * addObjectToApplContext(wsContext, LIMIT_KEY, limit); } } } return limit;\r
128          * }\r
129          * \r
130          * static void addObjectToApplContext(WebServiceContext wsContext, String\r
131          * objKey, Object obj) { assert !Util.isEmpty(objKey) :\r
132          * "Key for the object must not be empty! "; assert wsContext != null;\r
133          * \r
134          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
135          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); assert ctx !=\r
136          * null; log.debug("Adding object with key '" + objKey + "' and value '" +\r
137          * obj + "' to the application context"); ctx.setAttribute(objKey, obj); }\r
138          * static Object getObjectFromApplContext(String objKey, WebServiceContext\r
139          * wsContext) { assert !Util.isEmpty(objKey) :\r
140          * "Key for the object must not be empty! "; assert wsContext != null;\r
141          * \r
142          * ServletContext ctx = ((javax.servlet.ServletContext) wsContext\r
143          * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); Object obj =\r
144          * ctx.getAttribute(objKey); log.trace("Retrieving object with key '" +\r
145          * objKey + "' and value '" + obj + "' from the application context");\r
146          * return obj; }\r
147          */\r
148 }\r