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

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

HIGHLIGHTS:

CHANGELOG:
Make ProcedureParam? hold only ValueParams?.

Use id instead of names when naming gui components internally.

Basic procedure calling in GUI.

The actual procedure call is currently only backed
by the ObjectInstance?.

Add UnimplementedException?.

Improve naming of various gui elements.

Allow easy navigating in FEST Swing testing.

Add optional explicit order attribute to FramsClassAnnotation?.

That's because java reflection does return declared members
in any specific order. That ordering is needed only for
classes that have no representation in framsticks and need
a deterministic ordering of params.

Add ControlOwner? interface.

Add test for procedure calling in Browser.

First version of ParamAnnotation? for procedures.

Development of ProcedureParam?.

Add draft version of ProcedureParam? implementation in ReflectionAccess?.

Allow viewing FramsClasses? in gui Browser.

Extract ResourceBuilder? from ModelBuilder?.

Remove internalId from Param.

It was currently completely not utilised. Whether it is still needed
after introduction of ParamAnnotation? is arguable.

Add remaining param attributes to ParamAnnotation?.

Change AutoBuilder? semantics.

AutoBuilder? returns list of objects that are to be appended
with methods @AutoAppendAnnotation?.

This allows to omit explicit addition of ModelPackage? to instance
if the instance uses ModelBuilder? (registration of ModelPackage? comes
from schema).

Fix params ordering problem in auto created FramsClasses?.

Improve ObjectInstance?.

Several fixes to ModelBuilder?.

Improve test for ObjectInstance? in Browser.

Make initialization of robot static.

With robot recreated for second browser test, the test hanged
deep in AWT.

Add base convenience base test for Browser tests.

More tests to ObjectInstance?.

Rename Dispatcher.invokeLater() to dispatch().

Add assertDispatch.

It allows assertions in other threads, than TestNGInvoker.
Assertions are gathered after each method invocation and rethrown.

Use timeOut annotation attribute for tests involving some waiting.

Remove firstTask method (merge with joinableStart).

Clean up leftovers.

Remove unused FavouritesXMLFactory (the reading part is already
completely done with generic XmlLoader?, and writing part will be done
based on the same approach if needed).
Move UserFavourite? to the com.framsticks.gui.configuration package.

Remove GenotypeBrowser? as to specific.

This functionality will be available in ObjectInstance?.

Add interface ParamsPackage?.

Package containing registration of Java classes meant to use with
ReflectionAccess? may be in Instance using configuration.

Minor changes.

Make Group immutable.

Add AutoBuilder? interface extending Builder - only those would
be used to automatically build from XML.

Fix groups in FramsClass?.

Minor naming cleanup in Registry.

Add ModelComponent? interface.

All class creating the Model are implementing that interface.

Extract Model.build into ModelBuilder?.

ModelBuilder? will be compatible with other builders
and allow using it from configuration.

Fix NeuroConnection?.

Add synchronous get operation for dispatchers.

Rename JoinableMonitor? to Monitor.

Add ObjectInstance?.

This class is mainly for demonstration
and testing purposes.

Improve FramsServer? runner.

  • improve ExternalProcess? runner,
  • runner can kill the server but also react properly, when the server exists on it's own,
  • set default path to search for framsticks server installation,
  • add LoggingOutputListener?.
File size: 4.5 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.AutoAppendAnnotation;
18import com.framsticks.params.annotations.FramsClassAnnotation;
19import com.framsticks.params.annotations.ParamAnnotation;
20import com.framsticks.util.FramsticksException;
21import com.framsticks.util.Misc;
22import com.framsticks.util.dispatching.AbstractJoinable;
23import com.framsticks.util.dispatching.Dispatching;
24import com.framsticks.util.dispatching.Joinable;
25import com.framsticks.util.dispatching.JoinableParent;
26import com.framsticks.util.dispatching.JoinableState;
27import com.framsticks.util.dispatching.RunAt;
28import com.framsticks.util.dispatching.Thread;
29import com.framsticks.util.io.Encoding;
30
31@FramsClassAnnotation
32public class ExternalProcess extends AbstractJoinable implements JoinableParent, Entity {
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>() {
133
134                        @Override
135                        public void run() {
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.