- Timestamp:
- 07/02/13 16:20:07 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/running/ExternalProcess.java
r88 r90 15 15 16 16 import com.framsticks.core.Entity; 17 import com.framsticks.params.annotations.AutoAppendAnnotation; 17 18 import com.framsticks.params.annotations.FramsClassAnnotation; 18 19 import com.framsticks.params.annotations.ParamAnnotation; 19 20 import com.framsticks.util.FramsticksException; 20 21 import com.framsticks.util.Misc; 22 import com.framsticks.util.dispatching.AbstractJoinable; 23 import com.framsticks.util.dispatching.Dispatching; 24 import com.framsticks.util.dispatching.Joinable; 25 import com.framsticks.util.dispatching.JoinableParent; 26 import com.framsticks.util.dispatching.JoinableState; 27 import com.framsticks.util.dispatching.RunAt; 21 28 import com.framsticks.util.dispatching.Thread; 22 29 import com.framsticks.util.io.Encoding; 23 30 24 31 @FramsClassAnnotation 25 public class ExternalProcess extends Thread<ExternalProcess> implementsEntity {32 public class ExternalProcess extends AbstractJoinable implements JoinableParent, Entity { 26 33 private static final Logger log = Logger.getLogger(ExternalProcess.class); 27 34 28 35 protected List<String> arguments = new ArrayList<>(); 29 36 protected Process process; 30 protected ProcessBuilder builder = new ProcessBuilder();31 protected Thread<ExternalProcess> readerThread ;37 protected final ProcessBuilder builder = new ProcessBuilder(); 38 protected Thread<ExternalProcess> readerThread = new Thread<ExternalProcess>(); 32 39 33 40 protected PrintWriter input; … … 37 44 protected final List<OutputListener> listeners = new LinkedList<>(); 38 45 46 @AutoAppendAnnotation 39 47 public void addListener(OutputListener listener) { 40 48 synchronized (listeners) { … … 50 58 setName("process"); 51 59 arguments.add(null); 60 builder.redirectErrorStream(true); 52 61 } 53 62 … … 68 77 } 69 78 70 @Override 71 protected void routine() { 79 protected void readerTask() { 72 80 73 81 String line; … … 91 99 } 92 100 log.debug("process ended " + this); 93 process = null;101 // process = null; 94 102 } catch (FramsticksException e) { 95 103 log.error("exception caught in process " + this, e); 96 104 } 97 105 interrupt(); 106 // finish(); 98 107 } 99 100 101 108 102 109 @ParamAnnotation … … 111 118 112 119 @Override 113 public void joinableStart() { 114 builder = new ProcessBuilder(); 120 protected void joinableStart() { 115 121 log.debug("running process with arguments: " + arguments); 116 122 builder.command(arguments); … … 119 125 input = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), Encoding.getDefaultCharset())); 120 126 output = new BufferedReader(new InputStreamReader(process.getInputStream(), Encoding.getDefaultCharset())); 127 121 128 } catch (IOException e) { 122 129 throw new FramsticksException().msg("failed to start process").cause(e); 123 130 } 124 super.joinableStart();125 131 132 readerThread.dispatch(new RunAt<ExternalProcess>() { 133 134 @Override 135 public void run() { 136 readerTask(); 137 } 138 139 }); 140 Dispatching.use(readerThread, this); 126 141 } 127 142 128 143 @Override 129 144 public String toString() { 130 return super.toString() + "[" + Misc.returnNotNull(getCommand(), "?") + "]";145 return getName() + "[" + Misc.returnNotNull(getCommand(), "?") + "]"; 131 146 } 132 147 … … 140 155 @Override 141 156 protected void joinableInterrupt() { 142 super.joinableInterrupt(); 143 finish(); 157 process.destroy(); 158 Dispatching.drop(readerThread, this); 159 // finish(); 144 160 } 145 161 162 @Override 163 @ParamAnnotation 164 public String getName() { 165 return readerThread.getName(); 166 } 167 168 /** 169 * @param name the name to set 170 */ 171 @ParamAnnotation 172 public void setName(String name) { 173 readerThread.setName(name); 174 } 175 176 @Override 177 protected void joinableFinish() { 178 179 } 180 181 @Override 182 protected void joinableJoin() throws InterruptedException { 183 Dispatching.join(readerThread); 184 } 185 186 @Override 187 public void childChangedState(Joinable joinable, JoinableState state) { 188 proceedToState(state); 189 } 190 191 192 146 193 }
Note: See TracChangeset
for help on using the changeset viewer.