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

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

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

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