1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.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
42 public final class WSUtil {
\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
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
59 Set<String> names = new HashSet<String>();
\r
60 for (FastaSequence fs : sequences) {
\r
61 boolean unique = names.add(fs.getId());
\r
63 throw new JobSubmissionException(
\r
64 "Input sequences must have unique names! \n"
\r
65 + "Sequence " + fs.getId() + " is a duplicate!");
\r
67 if (fs.getLength() == 0) {
\r
68 throw new JobSubmissionException(
\r
69 "Sequence must not be empty! Sequence: " + fs.getId()
\r
75 public static JobStatus getJobStatus(String jobId) {
\r
76 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
77 return asyncEngine.getJobStatus(jobId);
\r
80 public static ChunkHolder pullFile(String file, long position) {
\r
81 return ProgressGetter.pull(file, position);
\r
84 public static byte getProgress(String jobId) {
\r
85 throw new UnsupportedOperationException();
\r
88 public static AsyncExecutor getEngine(ConfiguredExecutable<?> confClustal) {
\r
89 assert confClustal != null;
\r
90 return Configurator.getAsyncEngine(confClustal);
\r
93 public static boolean cancelJob(String jobId) {
\r
94 AsyncExecutor asyncEngine = Configurator.getAsyncEngine(jobId);
\r
95 return asyncEngine.cancelJob(jobId);
\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
103 if (limit != null && limit.isExceeded(sequences)) {
\r
104 throw LimitExceededException.newLimitExceeded(limit, sequences);
\r
106 compbio.runner.Util.writeInput(sequences, confExec);
\r
107 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
108 String jobId = engine.submitJob(confExec);
\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
118 log.debug("Method: " + method + " with task: " + confExec.getTaskId());
\r
120 compbio.runner.Util.writeInput(sequences, confExec);
\r
121 AsyncExecutor engine = Configurator.getAsyncEngine(confExec);
\r
122 String jobId = engine.submitJob(confExec);
\r
128 * TODO Rewrite using purely CommandBuilder. This is breaking encapsulation
\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
139 public static void validateAAConInput(List<FastaSequence> sequences)
\r
140 throws JobSubmissionException {
\r
141 validateFastaInput(sequences);
\r
143 for (FastaSequence fs : sequences) {
\r
145 len = fs.getLength();
\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
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
172 * @SuppressWarnings("unchecked") static <T> LimitsManager<T>
\r
173 * getLimits(Class<? extends Executable<T>> clazz, WebServiceContext
\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
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
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
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