1 package com.jsql.model.suspendable;
2
3 import com.jsql.model.InjectionModel;
4 import com.jsql.model.exception.JSqlException;
5 import com.jsql.util.LogLevelUtil;
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
8
9
10
11
12 public abstract class AbstractSuspendable {
13
14
15
16
17 private static final Logger LOGGER = LogManager.getRootLogger();
18
19
20
21
22 private boolean isStopped = false;
23
24
25
26
27 private boolean isPaused = false;
28
29 protected final InjectionModel injectionModel;
30
31 protected AbstractSuspendable(InjectionModel injectionModel) {
32 this.injectionModel = injectionModel;
33 }
34
35
36
37
38 public abstract String run(Object... args) throws JSqlException;
39
40
41
42
43
44
45
46 public synchronized boolean isSuspended() {
47
48 while (this.isPaused) {
49 try {
50 this.wait();
51 } catch (InterruptedException e) {
52 LOGGER.log(LogLevelUtil.IGNORE, e, e);
53 Thread.currentThread().interrupt();
54 }
55 }
56 return this.isStopped || this.injectionModel.isStoppedByUser();
57 }
58
59
60
61
62 public void stop() {
63 this.unpause();
64 this.isStopped = true;
65 }
66
67
68
69
70 public void pause() {
71 this.isPaused = true;
72 }
73
74
75
76
77 public void unpause() {
78 this.isPaused = false;
79 this.resume();
80 }
81
82
83
84
85
86 public boolean isPaused() {
87 return this.isPaused;
88 }
89
90
91
92
93 public synchronized void resume() {
94 this.notifyAll();
95 }
96 }