1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine;
\r
21 import java.util.Queue;
\r
22 import java.util.concurrent.DelayQueue;
\r
23 import java.util.concurrent.TimeUnit;
\r
25 import org.apache.log4j.Logger;
\r
27 import compbio.engine.client.PathValidator;
\r
29 public final class PulledFileCache {
\r
31 private static final Logger log = Logger.getLogger(PulledFileCache.class);
\r
33 private static final Queue<FilePuller> CACHE = new DelayQueue<FilePuller>();
\r
35 public static FilePuller get(final String fileName) {
\r
36 assert PathValidator.isAbsolutePath(fileName);
\r
38 for (FilePuller fp : CACHE) {
\r
39 if (fp.getFile().equals(fileName)) {
\r
40 log.debug("Retrieving element from cache: " + fp);
\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
52 * @return true if the same filepooler is already in cache
\r
54 public static boolean put(final FilePuller fpuller) {
\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
62 return CACHE.add(fpuller);
\r
65 private static void sweep() {
\r
66 FilePuller puller = null;
\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
74 } while (puller != null);
\r
77 static int getSize() {
\r
78 return CACHE.size();
\r
81 static void clear() {
\r
85 static void print() {
\r
86 System.out.println(CACHE);
\r