50a75c2c422aa0d1c06677691b45976087458cb9
[jabaws.git] / engine / compbio / engine / PulledFileCache.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.Queue;\r
22 import java.util.concurrent.DelayQueue;\r
23 import java.util.concurrent.TimeUnit;\r
24 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.engine.client.PathValidator;\r
28 \r
29 public final class PulledFileCache {\r
30 \r
31         private static final Logger log = Logger.getLogger(PulledFileCache.class);\r
32 \r
33         private static final Queue<FilePuller> CACHE = new DelayQueue<FilePuller>();\r
34 \r
35         public static FilePuller get(final String fileName) {\r
36                 assert PathValidator.isAbsolutePath(fileName);\r
37 \r
38                 for (FilePuller fp : CACHE) {\r
39                         if (fp.getFile().equals(fileName)) {\r
40                                 log.debug("Retrieving element from cache: " + fp);\r
41                                 return fp;\r
42                         }\r
43                 }\r
44                 return null;\r
45         }\r
46 \r
47         /**\r
48          * This method allows duplicates to be added. This is a responsibility of\r
49          * the called to ensure this will not happen!\r
50          * \r
51          * @param fpuller\r
52          * @return\r
53          */\r
54         public static boolean put(final FilePuller fpuller) {\r
55                 sweep();\r
56                 log.debug("Adding element to cache: " + fpuller);\r
57                 if (fpuller.getDelay(TimeUnit.NANOSECONDS) < 0) {\r
58                         throw new IllegalArgumentException(\r
59                                         "Could not cache expired FilePullers! FilePuler: "\r
60                                                         + fpuller);\r
61                 }\r
62                 return CACHE.add(fpuller);\r
63         }\r
64 \r
65         private static void sweep() {\r
66                 FilePuller puller = null;\r
67                 do {\r
68                         // All elements with delay expired gets removed from cache here\r
69                         puller = CACHE.poll();\r
70                         if (puller != null) {\r
71                                 puller.disconnect();\r
72                                 log.debug("Removing element from cache: " + puller);\r
73                         }\r
74                 } while (puller != null);\r
75         }\r
76 \r
77         static int getSize() {\r
78                 return CACHE.size();\r
79         }\r
80 \r
81         static void clear() {\r
82                 CACHE.clear();\r
83         }\r
84 \r
85         static void print() {\r
86                 System.out.println(CACHE);\r
87         }\r
88 }\r