001: package de.anomic.plasma.dbImport;
002:
003: import java.util.HashMap;
004:
005: import de.anomic.plasma.plasmaSwitchboard;
006: import de.anomic.server.logging.serverLog;
007:
008: public abstract class AbstractImporter extends Thread implements
009: dbImporter {
010:
011: protected int jobID = -1;
012: protected String jobType;
013: protected serverLog log;
014: protected boolean stopped = false;
015: protected boolean paused = false;
016:
017: protected int cacheSize;
018: protected long preloadTime;
019:
020: protected long globalStart = System.currentTimeMillis();
021: protected long globalEnd;
022: protected long globalPauseLast;
023: protected long globalPauseDuration;
024: protected String error;
025:
026: protected plasmaSwitchboard sb;
027:
028: AbstractImporter(String theJobType, plasmaSwitchboard switchboard) {
029: super (switchboard.dbImportManager.runningJobs, "");
030: this .jobType = theJobType;
031: this .sb = switchboard;
032: }
033:
034: public String getError() {
035: return this .error;
036: }
037:
038: /**
039: * @see dbImporter#init(HashMap)
040: */
041: public void init(HashMap<String, String> initparams)
042: throws ImporterException {
043: // initializing the logger and setting a more verbose thread name
044: this .log = new serverLog("IMPORT_" + this .jobType + "_"
045: + this .jobID);
046: this .setName("IMPORT_" + this .jobType + "_" + this .jobID);
047: }
048:
049: public void startIt() {
050: this .start();
051: }
052:
053: public void stopIt() throws InterruptedException {
054: this .stopped = true;
055: this .continueIt();
056: this .join();
057: }
058:
059: public void pauseIt() {
060: synchronized (this ) {
061: this .globalPauseLast = System.currentTimeMillis();
062: this .paused = true;
063: }
064: }
065:
066: public void continueIt() {
067: synchronized (this ) {
068: if (this .paused) {
069: this .globalPauseDuration += System.currentTimeMillis()
070: - this .globalPauseLast;
071: this .paused = false;
072: this .notifyAll();
073: }
074: }
075: }
076:
077: public boolean isPaused() {
078: synchronized (this ) {
079: return this .paused;
080: }
081: }
082:
083: protected boolean isAborted() {
084: synchronized (this ) {
085: if (this .paused) {
086: try {
087: this .wait();
088: } catch (InterruptedException e) {
089: }
090: }
091: }
092:
093: return (this .stopped) || Thread.currentThread().isInterrupted();
094: }
095:
096: public boolean isStopped() {
097: return !this .isAlive();
098: }
099:
100: public int getJobID() {
101: return this .jobID;
102: }
103:
104: public void setJobID(int id) {
105: if (this .jobID != -1)
106: throw new IllegalStateException("job ID already assigned");
107: this .jobID = id;
108: }
109:
110: public long getTotalRuntime() {
111: return (this .globalEnd == 0) ? System.currentTimeMillis()
112: - (this .globalStart + this .globalPauseDuration)
113: : this .globalEnd
114: - (this .globalStart + this .globalPauseDuration);
115: }
116:
117: public long getElapsedTime() {
118: if (this .paused) {
119: this .globalPauseDuration += System.currentTimeMillis()
120: - this .globalPauseLast;
121: this .globalPauseLast = System.currentTimeMillis();
122: }
123: return isStopped() ? this .globalEnd
124: - (this .globalStart + this .globalPauseDuration)
125: : System.currentTimeMillis()
126: - (this .globalStart + this .globalPauseDuration);
127: }
128:
129: public String getJobType() {
130: return this .jobType;
131: }
132:
133: public abstract long getEstimatedTime();
134:
135: public abstract String getJobName();
136:
137: public abstract int getProcessingStatusPercent();
138:
139: }
|