Next version of JABA
[jabaws.git] / engine / compbio / engine / SubmissionManager.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 \r
19 package compbio.engine;\r
20 \r
21 import java.util.Map;\r
22 import java.util.concurrent.ConcurrentHashMap;\r
23 import java.util.concurrent.Future;\r
24 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.engine.client.ConfiguredExecutable;\r
28 \r
29 /**\r
30  * Submit jobs for execution\r
31  * \r
32  * @author pvtroshin\r
33  * \r
34  */\r
35 public class SubmissionManager {\r
36 \r
37         private static final Logger log = Logger.getLogger(SubmissionManager.class);\r
38         static final Map<String, Future<ConfiguredExecutable<?>>> submittedTasks = new ConcurrentHashMap<String, Future<ConfiguredExecutable<?>>>();\r
39 \r
40         private SubmissionManager() {\r
41                 // uninitializable\r
42         }\r
43 \r
44         public static void addTask(ConfiguredExecutable<?> executable,\r
45                         Future<ConfiguredExecutable<?>> future) {\r
46                 Future<ConfiguredExecutable<?>> replacedTask = submittedTasks.put(\r
47                                 executable.getTaskId(), future);\r
48                 // just a percussion should never happened\r
49                 if (replacedTask != null) {\r
50                         log.fatal("Duplicated task id is detected by local engine for: "\r
51                                         + executable);\r
52                         throw new RuntimeException("Duplicated task ID is detected: "\r
53                                         + executable.getTaskId());\r
54                 }\r
55         }\r
56 \r
57         public static Future<ConfiguredExecutable<?>> getTask(String taskId) {\r
58                 return submittedTasks.get(taskId);\r
59         }\r
60 \r
61         public static void removeTask(ConfiguredExecutable<?> executable) {\r
62                 synchronized (submittedTasks) {\r
63                         assert executable != null;\r
64                         String taskId = executable.getTaskId();\r
65                         assert compbio.engine.client.Util.isValidJobId(taskId);\r
66                         submittedTasks.remove(taskId);\r
67                 }\r
68         }\r
69 \r
70         public static void removeTask(String key) {\r
71                 synchronized (submittedTasks) {\r
72                         if (compbio.engine.client.Util.isValidJobId(key)) {\r
73                                 submittedTasks.remove(key);\r
74                         } else {\r
75                                 log.error("Invalid key is given! " + key);\r
76                         }\r
77                 }\r
78         }\r
79 \r
80 }\r