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 private static final Logger LOGGER = LogManager.getRootLogger();
15
16
17
18
19 private boolean isStopped = false;
20
21
22
23
24 private boolean isPaused = false;
25
26 protected final InjectionModel injectionModel;
27
28 protected AbstractSuspendable(InjectionModel injectionModel) {
29 this.injectionModel = injectionModel;
30 }
31
32
33
34
35 public abstract String run(Input input) throws JSqlException;
36
37 public String run() throws JSqlException {
38 return this.run(null);
39 }
40
41
42
43
44
45
46
47 public synchronized boolean isSuspended() {
48
49 while (this.isPaused) {
50 try {
51 this.wait();
52 } catch (InterruptedException e) {
53 LOGGER.log(LogLevelUtil.IGNORE, e, e);
54 Thread.currentThread().interrupt();
55 }
56 }
57 return this.isStopped || this.injectionModel.isStoppedByUser();
58 }
59
60
61
62
63 public void stop() {
64 this.unpause();
65 this.isStopped = true;
66 }
67
68
69
70
71 public void pause() {
72 this.isPaused = true;
73 }
74
75
76
77
78 public void unpause() {
79 this.isPaused = false;
80 this.resume();
81 }
82
83
84
85
86
87 public boolean isPaused() {
88 return this.isPaused;
89 }
90
91
92
93
94 public synchronized void resume() {
95 this.notifyAll();
96 }
97 }