001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.kernel.harvest.harvester;
025:
026: //=============================================================================
027:
028: class Executor extends Thread {
029: //---------------------------------------------------------------------------
030: //---
031: //--- Constructor
032: //---
033: //---------------------------------------------------------------------------
034:
035: public Executor(AbstractHarvester ah) {
036: terminate = false;
037: status = WAITING;
038: harvester = ah;
039: timeout = -1;
040: }
041:
042: //---------------------------------------------------------------------------
043: //---
044: //--- API methods
045: //---
046: //---------------------------------------------------------------------------
047:
048: public void setTimeout(int minutes) {
049: timeout = minutes;
050: }
051:
052: //---------------------------------------------------------------------------
053:
054: public void terminate() {
055: terminate = true;
056: interrupt();
057: harvester = null;
058: }
059:
060: //---------------------------------------------------------------------------
061:
062: public boolean isRunning() {
063: return status == RUNNING;
064: }
065:
066: //---------------------------------------------------------------------------
067: //---
068: //--- Executor's main loop
069: //---
070: //---------------------------------------------------------------------------
071:
072: public void run() {
073: while (!terminate) {
074: if (timeout == -1)
075: await(1);
076: else {
077: await(timeout);
078:
079: if (!terminate && harvester != null) {
080: status = RUNNING;
081: harvester.harvest();
082: status = WAITING;
083: }
084: }
085: }
086: }
087:
088: //---------------------------------------------------------------------------
089: //---
090: //--- Private methods
091: //---
092: //---------------------------------------------------------------------------
093:
094: private boolean await(int minutes) {
095: try {
096: sleep(minutes * 60 * 1000);
097: return false;
098: } catch (InterruptedException e) {
099: return true;
100: }
101: }
102:
103: //---------------------------------------------------------------------------
104: //---
105: //--- Variables
106: //---
107: //---------------------------------------------------------------------------
108:
109: private static final int WAITING = 0;
110: private static final int RUNNING = 1;
111:
112: //---------------------------------------------------------------------------
113:
114: private boolean terminate;
115: private int status;
116: private int timeout;
117:
118: private AbstractHarvester harvester;
119: }
120:
121: //=============================================================================
|