|
Lines 17-22
Link Here
|
| 17 |
import java.io.FileInputStream; |
17 |
import java.io.FileInputStream; |
| 18 |
import java.io.IOException; |
18 |
import java.io.IOException; |
| 19 |
import java.io.InputStream; |
19 |
import java.io.InputStream; |
|
|
20 |
import java.text.MessageFormat; |
| 20 |
import java.util.ArrayList; |
21 |
import java.util.ArrayList; |
| 21 |
import java.util.Arrays; |
22 |
import java.util.Arrays; |
| 22 |
import java.util.HashSet; |
23 |
import java.util.HashSet; |
|
Lines 24-36
Link Here
|
| 24 |
import java.util.List; |
25 |
import java.util.List; |
| 25 |
import java.util.Queue; |
26 |
import java.util.Queue; |
| 26 |
import java.util.Set; |
27 |
import java.util.Set; |
|
|
28 |
import java.util.concurrent.Callable; |
| 29 |
import java.util.concurrent.ExecutorService; |
| 30 |
import java.util.concurrent.Executors; |
| 31 |
import java.util.concurrent.Future; |
| 27 |
import java.util.regex.Pattern; |
32 |
import java.util.regex.Pattern; |
| 28 |
|
33 |
|
| 29 |
import org.eclipse.ptp.proxy.event.IProxyEvent; |
34 |
import org.eclipse.ptp.proxy.event.IProxyEvent; |
|
|
35 |
import org.eclipse.ptp.proxy.event.IProxyMessageEvent.Level; |
| 30 |
import org.eclipse.ptp.rm.proxy.core.attributes.AttributeDefinition; |
36 |
import org.eclipse.ptp.rm.proxy.core.attributes.AttributeDefinition; |
| 31 |
import org.eclipse.ptp.rm.proxy.core.element.ElementManager; |
37 |
import org.eclipse.ptp.rm.proxy.core.element.ElementManager; |
| 32 |
import org.eclipse.ptp.rm.proxy.core.element.IElement; |
38 |
import org.eclipse.ptp.rm.proxy.core.element.IElement; |
| 33 |
import org.eclipse.ptp.rm.proxy.core.event.IEventFactory; |
39 |
import org.eclipse.ptp.rm.proxy.core.event.IEventFactory; |
|
|
40 |
import org.eclipse.ptp.rm.proxy.core.messages.Messages; |
| 34 |
import org.eclipse.ptp.rm.proxy.core.parser.IParser; |
41 |
import org.eclipse.ptp.rm.proxy.core.parser.IParser; |
| 35 |
import org.eclipse.ptp.utils.core.RangeSet; |
42 |
import org.eclipse.ptp.utils.core.RangeSet; |
| 36 |
|
43 |
|
|
Lines 56-61
Link Here
|
| 56 |
*/ |
63 |
*/ |
| 57 |
public class Controller { |
64 |
public class Controller { |
| 58 |
|
65 |
|
|
|
66 |
/** |
| 67 |
* @since 1.1 |
| 68 |
*/ |
| 69 |
public static interface ErrorHandler { |
| 70 |
void handle(Level level, String msg); |
| 71 |
} |
| 72 |
|
| 59 |
class FilterData { |
73 |
class FilterData { |
| 60 |
String key; |
74 |
String key; |
| 61 |
Pattern pattern; |
75 |
Pattern pattern; |
|
Lines 67-72
Link Here
|
| 67 |
} |
81 |
} |
| 68 |
|
82 |
|
| 69 |
private Queue<String> debugFiles = null; |
83 |
private Queue<String> debugFiles = null; |
|
|
84 |
private ErrorHandler errorHandler = null; |
| 70 |
private final String command; |
85 |
private final String command; |
| 71 |
private final IEventFactory eventFactory; |
86 |
private final IEventFactory eventFactory; |
| 72 |
private final IParser parser; |
87 |
private final IParser parser; |
|
Lines 77-82
Link Here
|
| 77 |
private FilterData filter = null; |
92 |
private FilterData filter = null; |
| 78 |
|
93 |
|
| 79 |
private final AttributeDefinition attrDef; |
94 |
private final AttributeDefinition attrDef; |
|
|
95 |
private static ExecutorService fPool = Executors.newCachedThreadPool(); |
| 80 |
|
96 |
|
| 81 |
/** |
97 |
/** |
| 82 |
* Instantiates a new controller. |
98 |
* Instantiates a new controller. |
|
Lines 153-183
Link Here
|
| 153 |
* @throws Execption |
169 |
* @throws Execption |
| 154 |
*/ |
170 |
*/ |
| 155 |
public Set<IElement> parse() throws Exception { |
171 |
public Set<IElement> parse() throws Exception { |
| 156 |
InputStream is = null; |
|
|
| 157 |
if (debugFiles != null) { |
172 |
if (debugFiles != null) { |
| 158 |
String file = debugFiles.poll(); |
173 |
String file = debugFiles.poll(); |
| 159 |
is = readFile(file); |
174 |
InputStream is = new BufferedInputStream(new FileInputStream(new File(file))); |
| 160 |
debugFiles.add(file); |
175 |
debugFiles.add(file); |
| 161 |
} else { |
|
|
| 162 |
is = readProgramOutput(command); |
| 163 |
} |
| 164 |
/* |
| 165 |
* throws Exception to allow any Exception from different parsers |
| 166 |
*/ |
| 167 |
return parser.parse(attrDef, is); |
| 168 |
} |
| 169 |
|
| 170 |
private InputStream readFile(String path) throws IOException { |
| 171 |
return new BufferedInputStream(new FileInputStream(new File(path))); |
| 172 |
} |
| 173 |
|
| 174 |
private InputStream readProgramOutput(String string) throws IOException { |
| 175 |
String[] args = string.split(" "); //$NON-NLS-1$ |
| 176 |
Process p = Runtime.getRuntime().exec(args); // TODO read stderr for |
| 177 |
// potential errors |
| 178 |
|
176 |
|
| 179 |
// read the standard output of the command |
177 |
/* |
| 180 |
return new BufferedInputStream(p.getInputStream()); |
178 |
* throws Exception to allow any Exception from different parsers |
|
|
179 |
*/ |
| 180 |
try { |
| 181 |
return parser.parse(attrDef, is); |
| 182 |
} finally { |
| 183 |
is.close(); |
| 184 |
} |
| 185 |
} |
| 186 |
Process p = Runtime.getRuntime().exec(command); |
| 187 |
final InputStream is = new BufferedInputStream(p.getInputStream()); |
| 188 |
final InputStream err = new BufferedInputStream(p.getInputStream()); |
| 189 |
Callable<Set<IElement>> parseThread = new Callable<Set<IElement>>() { |
| 190 |
public Set<IElement> call() throws Exception { |
| 191 |
Set<IElement> ret = parser.parse(attrDef, is); |
| 192 |
try { |
| 193 |
while (is.read(new byte[1024]) > -1) { |
| 194 |
} /* read any additional unparsed output to prevent dead-lock */ |
| 195 |
} catch (IOException e) { |
| 196 |
// ignore |
| 197 |
} |
| 198 |
return ret; |
| 199 |
} |
| 200 |
}; |
| 201 |
Future<Set<IElement>> ret = fPool.submit(parseThread); |
| 202 |
byte buf[] = new byte[1024]; |
| 203 |
int length; |
| 204 |
while ((length = err.read(buf)) > -1) { |
| 205 |
if (errorHandler != null) |
| 206 |
errorHandler.handle(Level.WARNING, new String(buf, 0, length)); |
| 207 |
} |
| 208 |
p.waitFor(); |
| 209 |
int exitValue = p.exitValue(); |
| 210 |
if (exitValue != 0 && errorHandler != null) { |
| 211 |
errorHandler.handle(Level.ERROR, MessageFormat.format(Messages.getString("Controller.0"), command, exitValue)); //$NON-NLS-1$ |
| 212 |
} |
| 213 |
return ret.get(); |
| 181 |
} |
214 |
} |
| 182 |
|
215 |
|
| 183 |
/** |
216 |
/** |
|
Lines 204-209
Link Here
|
| 204 |
} |
237 |
} |
| 205 |
|
238 |
|
| 206 |
/** |
239 |
/** |
|
|
240 |
* @since 1.1 |
| 241 |
*/ |
| 242 |
public void setErrorHandler(ErrorHandler errorHandler) { |
| 243 |
this.errorHandler = errorHandler; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 207 |
* Acctivate filters for the output to be sent to the client |
247 |
* Acctivate filters for the output to be sent to the client |
| 208 |
* |
248 |
* |
| 209 |
* @param key |
249 |
* @param key |