1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.ws.server;
\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
26 import org.apache.log4j.Logger;
\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 import compbio.ws.client.Services;
\r
42 import compbio.ws.client.ServicesUtil;
\r
44 public final class WSUtil {
\r
46 public static final void validateJobId(String jobId)
\r
47 throws InvalidParameterException {
\r
48 if (!compbio.engine.client.Util.isValidJobId(jobId)) {
\r
49 throw new InvalidParameterException(
\r
50 "JobId is not provided or cannot be recognised! Given value: "
\r
55 public static final void validateFastaInput(List<FastaSequence> sequences)
\r
56 throws JobSubmissionException {
\r
57 if (sequences == null || sequences.isEmpty()) {
\r
58 throw new JobSubmissionException(
\r
59 "List of fasta sequences required but not provided! ");
\r
61 Set<String> names = new HashSet<String>();
\r
62 for (FastaSequence fs : sequences) {
\r
63 boolean unique = names.add(fs.getId());
\r
65 throw new JobSubmissionException(
\r
66 "Input sequences must have unique names! \n"
\r
67 + "Sequence " + fs.getId() + " is a duplicate!");
\r
69 if (fs.getLength() == 0) {
\r
70 throw new JobSubmissionException(
\r
71 "Sequence must not be empty! Sequence: " + fs.getId()
\r
77 public static JobStatus getJobStatus(String jobId) {
\r
78 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
79 return asyncEngine.getJobStatus(jobId);
\r
82 public static ChunkHolder pullFile(String file, long position) {
\r
83 return ProgressGetter.pull(file, position);
\r
86 public static byte getProgress(String jobId) {
\r
87 throw new UnsupportedOperationException();
\r
90 public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {
\r
91 assert confClustal != null;
\r
92 return Configurator.getAsyncEngine(confClustal);
\r
95 public static boolean cancelJob(String jobId) {
\r
96 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
97 return asyncEngine.cancelJob(jobId);
\r
100 public static <T> String align(List<FastaSequence> sequences,
\r
101 ConfiguredExecutable<T> confExec, Logger logger,
\r
102 String callingMethod, Limit<T> limit)
\r
103 throws LimitExceededException, JobSubmissionException {
\r
105 if (limit != null && limit.isExceeded(sequences)) {
\r
106 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
108 compbio.runner.Util.writeInput(sequences, confExec);
\r
109 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
110 String jobId = engine.submitJob(confExec);
\r
111 reportUsage(confExec, logger);
\r
115 // Hardcoded gapchar '-'
\r
117 public static <T> String fold(List<FastaSequence> sequences,
\r
118 ConfiguredExecutable<T> confExec, Logger logger,
\r
119 String callingMethod, Limit<T> limit)
\r
120 throws LimitExceededException, JobSubmissionException {
\r
122 if (limit != null && limit.isExceeded(sequences)) {
\r
123 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
125 compbio.runner.Util.writeClustalInput(sequences, confExec, '-');
\r
126 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
127 String jobId = engine.submitJob(confExec);
\r
128 reportUsage(confExec, logger);
\r
132 static <T> void reportUsage(ConfiguredExecutable<T> confExec, Logger logger) {
\r
133 if (GAUtils.IS_GA_ENABLED) {
\r
134 Services service = ServicesUtil.getServiceByRunner(confExec
\r
135 .getExecutable().getClass());
\r
136 GAUtils.reportUsage(service);
\r
137 logger.info("Reporting GA usage for " + service);
\r
141 public static <T> String analize(List<FastaSequence> sequences,
\r
142 ConfiguredExecutable<T> confExec, Logger log, String method,
\r
143 Limit<T> limit) throws JobSubmissionException {
\r
144 if (limit != null && limit.isExceeded(sequences)) {
\r
145 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
147 log.debug("Method: " + method + " with task: " + confExec.getTaskId());
\r
149 compbio.runner.Util.writeInput(sequences, confExec);
\r
150 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
151 String jobId = engine.submitJob(confExec);
\r
152 reportUsage(confExec, log);
\r
157 * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation
\r
159 public static final <T> List<String> getCommands(List<Option<T>> options,
\r
160 String keyValueSeparator) {
\r
161 List<String> oList = new ArrayList<String>();
\r
162 for (Option<T> o : options) {
\r
163 oList.add(o.toCommand(keyValueSeparator));
\r
168 public static void validateAAConInput(List<FastaSequence> sequences)
\r
169 throws JobSubmissionException {
\r
170 validateFastaInput(sequences);
\r
172 for (FastaSequence fs : sequences) {
\r
174 len = fs.getLength();
\r
177 if (fs.getLength() != len) {
\r
178 throw new JobSubmissionException(
\r
179 "All sequences must be of the same length. Please align "
\r
180 + "the sequences prior to submission! The first sequence length is : "
\r
181 + len + " but the sequence '" + fs.getId()
\r
182 + "' length is " + fs.getLength());
\r
187 public static <T> ScoreManager getAnnotation(String jobId, Logger log)
\r
188 throws ResultNotAvailableException {
\r
189 WSUtil.validateJobId(jobId);
\r
190 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
191 ConfiguredExecutable<T> aacon = (ConfiguredExecutable<T>) asyncEngine
\r
192 .getResults(jobId);
\r
193 ScoreManager mas = aacon.getResults();
\r
194 log.trace(jobId + " getConservation : " + mas);
\r
201 * @SuppressWarnings("unchecked") static <T> LimitsManager<T>
\r
202 * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext
\r
205 * String LIMIT_KEY = CACHE_KEY + clazz.getCanonicalName(); LimitsManager<T>
\r
206 * limit = (LimitsManager<T>) getObjectFromApplContext( LIMIT_KEY,
\r
207 * wsContext); if (limit == null) { synchronized (WSUtil.class) { limit =
\r
208 * (LimitsManager<T>) getObjectFromApplContext(LIMIT_KEY, wsContext); if
\r
209 * (limit == null) { limit = compbio.runner.Util
\r
210 * .getLimits((Class<Executable<T>>) clazz);
\r
211 * addObjectToApplContext(wsContext, LIMIT_KEY, limit); } } } return limit;
\r
214 * static void addObjectToApplContext(WebServiceContext wsContext, String
\r
215 * objKey, Object obj) { assert !Util.isEmpty(objKey) :
\r
216 * "Key for the object must not be empty! "; assert wsContext != null;
\r
218 * ServletContext ctx = ((javax.servlet.ServletContext) wsContext
\r
219 * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); assert ctx !=
\r
220 * null; log.debug("Adding object with key '" + objKey + "' and value '" +
\r
221 * obj + "' to the application context"); ctx.setAttribute(objKey, obj); }
\r
222 * static Object getObjectFromApplContext(String objKey, WebServiceContext
\r
223 * wsContext) { assert !Util.isEmpty(objKey) :
\r
224 * "Key for the object must not be empty! "; assert wsContext != null;
\r
226 * ServletContext ctx = ((javax.servlet.ServletContext) wsContext
\r
227 * .getMessageContext().get(MessageContext. SERVLET_CONTEXT)); Object obj =
\r
228 * ctx.getAttribute(objKey); log.trace("Retrieving object with key '" +
\r
229 * objKey + "' and value '" + obj + "' from the application context");
\r