Next version of JABA
[jabaws.git] / engine / compbio / engine / ProgressGetter.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 package compbio.engine;\r
19 \r
20 import java.io.IOException;\r
21 import java.util.concurrent.TimeUnit;\r
22 \r
23 import org.apache.log4j.Logger;\r
24 \r
25 import compbio.metadata.ChunkHolder;\r
26 \r
27 public class ProgressGetter {\r
28 \r
29         private static final Logger log = Logger.getLogger(ProgressGetter.class);\r
30 \r
31         static final ChunkHolder pull(String file, long position, long delay,\r
32                         TimeUnit unit) {\r
33                 if (compbio.util.Util.isEmpty(file)) {\r
34                         throw new NullPointerException("File must be supplied!");\r
35                 }\r
36 \r
37                 FilePuller puller = PulledFileCache.get(file);\r
38                 if (puller == null) {\r
39                         puller = FilePuller.newFilePuller(file, 1024);\r
40                         // Use custom delay\r
41                         if (delay != 0) {\r
42                                 puller.setDelay(delay, unit);\r
43                         }\r
44                         PulledFileCache.put(puller);\r
45                 }\r
46                 if (puller.isFileCreated()) {\r
47                         try {\r
48                                 return puller.pull(position);\r
49                         } catch (IOException e) {\r
50                                 log.error(e.getLocalizedMessage(), e.getCause());\r
51                         }\r
52                 }\r
53                 return null;\r
54         }\r
55 \r
56         public final static ChunkHolder pull(String file, long position) {\r
57                 // Use default delay time\r
58                 if (position < 0) {\r
59                         throw new IllegalArgumentException(\r
60                                         "Position must be greater then 0");\r
61                 }\r
62                 return pull(file, position, 0, null);\r
63         }\r
64 \r
65         static byte getProgress(String progressFile, long delay, TimeUnit unit) {\r
66                 if (compbio.util.Util.isEmpty(progressFile)) {\r
67                         throw new NullPointerException("File must be supplied!");\r
68                 }\r
69 \r
70                 FilePuller puller = PulledFileCache.get(progressFile);\r
71                 if (puller == null) {\r
72                         puller = FilePuller.newProgressPuller(progressFile);\r
73                         // Use custom delay\r
74                         if (delay != 0) {\r
75                                 puller.setDelay(delay, unit);\r
76                         }\r
77                         PulledFileCache.put(puller);\r
78                 }\r
79                 if (puller.isFileCreated()) {\r
80                         try {\r
81                                 byte value = puller.getProgress();\r
82                                 return value;\r
83                         } catch (IOException e) {\r
84                                 log.error(e.getLocalizedMessage(), e.getCause());\r
85                         }\r
86                 }\r
87                 return 0;\r
88         }\r
89 \r
90         public final static byte getProgress(String progressFile) {\r
91                 return getProgress(progressFile, 0, null);\r
92         }\r
93 \r
94 }\r