2880e8ff103fa91e7280a3b7739b0360984a63b1
[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, Future<ConfiguredExecutable<?>> future) {\r
45                 Future<ConfiguredExecutable<?>> replacedTask = submittedTasks.put(executable.getTaskId(), future);\r
46                 // just a percussion should never happened\r
47                 if (replacedTask != null) {\r
48                         log.fatal("Duplicated task id is detected by local engine for: " + executable);\r
49                         throw new RuntimeException("Duplicated task ID is detected: " + executable.getTaskId());\r
50                 }\r
51         }\r
52 \r
53         public static Future<ConfiguredExecutable<?>> getTask(String taskId) {\r
54                 return submittedTasks.get(taskId);\r
55         }\r
56 \r
57         public static void removeTask(ConfiguredExecutable<?> executable) {\r
58                 synchronized (submittedTasks) {\r
59                         assert executable != null;\r
60                         String taskId = executable.getTaskId();\r
61                         assert compbio.engine.client.Util.isValidJobId(taskId);\r
62                         submittedTasks.remove(taskId);\r
63                 }\r
64         }\r
65 \r
66         public static void removeTask(String key) {\r
67                 synchronized (submittedTasks) {\r
68                         if (compbio.engine.client.Util.isValidJobId(key)) {\r
69                                 submittedTasks.remove(key);\r
70                         } else {\r
71                                 log.error("Invalid key is given! " + key);\r
72                         }\r
73                 }\r
74         }\r
75 \r
76 }\r