Next version of JABA
[jabaws.git] / engine / compbio / engine / conf / DirectoryManager.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.conf;\r
20 \r
21 import org.apache.log4j.Logger;\r
22 \r
23 import compbio.engine.client.ConfExecutable;\r
24 import compbio.engine.client.Executable;\r
25 import compbio.util.Util;\r
26 \r
27 public class DirectoryManager {\r
28 \r
29         private static Logger log = Logger.getLogger(DirectoryManager.class);\r
30 \r
31         public static final String DELIM = "#";\r
32 \r
33         public static String getTaskDirectory(Class<?> clazz) {\r
34                 return clazz.getSimpleName() + DELIM + getNonRepeatableNumber();\r
35         }\r
36 \r
37         static long getNonRepeatableNumber() {\r
38                 // The random value is concatenated with time value not added to it and\r
39                 // then converted to long.\r
40                 // Top 3 high order bits are cut and replaced by random number\r
41                 // to make sure that the resulting number fits into long value\r
42                 String longNum = new Long(System.nanoTime()).toString();\r
43                 return Long.parseLong(Util.getRandomNumber(100, 999)\r
44                                 + longNum.substring(3));\r
45         }\r
46 \r
47         public static Class<Executable<?>> getClass(String taskId) {\r
48                 assert compbio.engine.client.Util.isValidJobId(taskId);\r
49                 String className = null;\r
50                 if (taskId.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX)) {\r
51                         className = taskId.substring(1, taskId.indexOf(DELIM));\r
52                 } else {\r
53                         className = taskId.substring(0, taskId.indexOf(DELIM));\r
54                 }\r
55                 try {\r
56                         return (Class<Executable<?>>) Class.forName(className);\r
57                 } catch (ClassNotFoundException e) {\r
58                         log.error("Could not parse taskId " + taskId + " Message "\r
59                                         + e.getLocalizedMessage(), e.getCause());\r
60                 }\r
61                 return null;\r
62         }\r
63 \r
64 }\r