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

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

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 4.6 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.params.annotations.AutoAppendAnnotation;
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.AbstractJoinable;
22import com.framsticks.util.dispatching.Dispatching;
23import com.framsticks.util.dispatching.Joinable;
24import com.framsticks.util.dispatching.JoinableParent;
25import com.framsticks.util.dispatching.JoinableState;
26import com.framsticks.util.dispatching.RunAt;
27import com.framsticks.util.dispatching.Thread;
28import com.framsticks.util.dispatching.ThrowExceptionHandler;
29import com.framsticks.util.io.Encoding;
30
31@FramsClassAnnotation
32public class ExternalProcess extends AbstractJoinable implements JoinableParent {
33        private static final Logger log = Logger.getLogger(ExternalProcess.class);
34
35        protected List<String> arguments = new ArrayList<>();
36        protected Process process;
37        protected final ProcessBuilder builder = new ProcessBuilder();
38        protected Thread<ExternalProcess> readerThread = new Thread<ExternalProcess>();
39
40        protected PrintWriter input;
41        protected BufferedReader output;
42        protected Integer exitCode;
43
44        protected final List<OutputListener> listeners = new LinkedList<>();
45
46        @AutoAppendAnnotation
47        public void addListener(OutputListener listener) {
48                synchronized (listeners) {
49                        listeners.add(listener);
50                }
51        }
52
53        /**
54         *
55         */
56        public ExternalProcess() {
57                super();
58                setName("process");
59                arguments.add(null);
60                builder.redirectErrorStream(true);
61        }
62
63        /**
64         * @return the command
65         */
66        @ParamAnnotation
67        public String getCommand() {
68                return arguments.get(0);
69        }
70
71        /**
72         * @param command the command to set
73         */
74        @ParamAnnotation
75        public void setCommand(String command) {
76                arguments.set(0, command);
77        }
78
79        protected void readerTask() {
80
81                String line;
82                try {
83                        try {
84                                while ((line = output.readLine()) != null) {
85                                        log.trace("read line: " + line);
86                                        synchronized (listeners) {
87                                                for (OutputListener l : listeners) {
88                                                        l.onLineRead(line);
89                                                }
90                                        }
91                                }
92                        } catch (IOException e) {
93                                throw new FramsticksException().msg("failed to read line from output of process").cause(e);
94                        }
95                        try {
96                                exitCode = process.waitFor();
97                        } catch (InterruptedException e) {
98                                throw new FramsticksException().msg("failed to wait for process").cause(e);
99                        }
100                        log.debug("process ended " + this);
101                        // process = null;
102                } catch (FramsticksException e) {
103                        log.error("exception caught in process " + this, e);
104                }
105                interrupt();
106                // finish();
107        }
108
109        @ParamAnnotation
110        public void setDirectory(String directory) {
111                builder.directory(new File(directory));
112        }
113
114        @ParamAnnotation
115        public String getDirectory() {
116                return builder.directory().getName();
117        }
118
119        @Override
120        protected void joinableStart() {
121                log.debug("running process with arguments: " + arguments);
122                builder.command(arguments);
123                try {
124                        process = builder.start();
125                        input = new PrintWriter(new OutputStreamWriter(process.getOutputStream(), Encoding.getDefaultCharset()));
126                        output = new BufferedReader(new InputStreamReader(process.getInputStream(), Encoding.getDefaultCharset()));
127
128                } catch (IOException e) {
129                        throw new FramsticksException().msg("failed to start process").cause(e);
130                }
131
132                readerThread.dispatch(new RunAt<ExternalProcess>(ThrowExceptionHandler.getInstance()) {
133
134                        @Override
135                        protected void runAt() {
136                                readerTask();
137                        }
138
139                });
140                Dispatching.use(readerThread, this);
141        }
142
143        @Override
144        public String toString() {
145                return getName() + "[" + Misc.returnNotNull(getCommand(), "?") + "]";
146        }
147
148        /**
149         * @return the input
150         */
151        public PrintWriter getInput() {
152                return input;
153        }
154
155        @Override
156        protected void joinableInterrupt() {
157                process.destroy();
158                Dispatching.drop(readerThread, this);
159                // finish();
160        }
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
193}
Note: See TracBrowser for help on using the repository browser.