source: java/main/src/main/java/com/framsticks/running/ExternalProcess.java @ 88

Last change on this file since 88 was 88, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 3.4 KB
Line 
1package com.framsticks.running;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStreamReader;
7import java.io.OutputStreamWriter;
8import java.io.PrintWriter;
9import java.util.ArrayList;
10import java.util.LinkedList;
11import java.util.List;
12
13
14import org.apache.log4j.Logger;
15
16import com.framsticks.core.Entity;
17import com.framsticks.params.annotations.FramsClassAnnotation;
18import com.framsticks.params.annotations.ParamAnnotation;
19import com.framsticks.util.FramsticksException;
20import com.framsticks.util.Misc;
21import com.framsticks.util.dispatching.Thread;
22import com.framsticks.util.io.Encoding;
23
24@FramsClassAnnotation
25public class ExternalProcess extends Thread<ExternalProcess> implements Entity {
26        private static final Logger log = Logger.getLogger(ExternalProcess.class);
27
28        protected List<String> arguments = new ArrayList<>();
29        protected Process process;
30        protected ProcessBuilder builder = new ProcessBuilder();
31        protected Thread<ExternalProcess> readerThread;
32
33        protected PrintWriter input;
34        protected BufferedReader output;
35        protected Integer exitCode;
36
37        protected final List<OutputListener> listeners = new LinkedList<>();
38
39        public void addListener(OutputListener listener) {
40                synchronized (listeners) {
41                        listeners.add(listener);
42                }
43        }
44
45        /**
46         *
47         */
48        public ExternalProcess() {
49                super();
50                setName("process");
51                arguments.add(null);
52        }
53
54        /**
55         * @return the command
56         */
57        @ParamAnnotation
58        public String getCommand() {
59                return arguments.get(0);
60        }
61
62        /**
63         * @param command the command to set
64         */
65        @ParamAnnotation
66        public void setCommand(String command) {
67                arguments.set(0, command);
68        }
69
70        @Override
71        protected void routine() {
72
73                String line;
74                try {
75                        try {
76                                while ((line = output.readLine()) != null) {
77                                        log.trace("read line: " + line);
78                                        synchronized (listeners) {
79                                                for (OutputListener l : listeners) {
80                                                        l.onLineRead(line);
81                                                }
82                                        }
83                                }
84                        } catch (IOException e) {
85                                throw new FramsticksException().msg("failed to read line from output of process").cause(e);
86                        }
87                        try {
88                                exitCode = process.waitFor();
89                        } catch (InterruptedException e) {
90                                throw new FramsticksException().msg("failed to wait for process").cause(e);
91                        }
92                        log.debug("process ended " + this);
93                        process = null;
94                } catch (FramsticksException e) {
95                        log.error("exception caught in process " + this, e);
96                }
97                interrupt();
98        }
99
100
101
102        @ParamAnnotation
103        public void setDirectory(String directory) {
104                builder.directory(new File(directory));
105        }
106
107        @ParamAnnotation
108        public String getDirectory() {
109                return builder.directory().getName();
110        }
111
112        @Override
113        public void joinableStart() {
114                builder = new ProcessBuilder();
115                log.debug("running process with arguments: " + arguments);
116                builder.command(arguments);
117                try {
118                        process = builder.start();
119                        input = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), Encoding.getDefaultCharset()));
120                        output = new BufferedReader(new InputStreamReader(process.getInputStream(), Encoding.getDefaultCharset()));
121                } catch (IOException e) {
122                        throw new FramsticksException().msg("failed to start process").cause(e);
123                }
124                super.joinableStart();
125
126        }
127
128        @Override
129        public String toString() {
130                return super.toString() + "[" + Misc.returnNotNull(getCommand(), "?") + "]";
131        }
132
133        /**
134         * @return the input
135         */
136        public PrintWriter getInput() {
137                return input;
138        }
139
140        @Override
141        protected void joinableInterrupt() {
142                super.joinableInterrupt();
143                finish();
144        }
145
146}
Note: See TracBrowser for help on using the repository browser.