20 package com.generalrobotix.ui.util;
22 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.PrintStream;
28 import java.util.ArrayList;
29 import java.util.List;
32 import java.util.concurrent.ConcurrentLinkedQueue;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.core.runtime.IProgressMonitor;
48 @SuppressWarnings(
"serial")
52 private java.util.List<
AProcess> process_ =
null;
53 private boolean isEnd_ =
false;
55 private StringBuffer outputBuffer_ =
null;
56 private ConcurrentLinkedQueue<String> lineQueue =
null;
57 private Thread thread_ =
null;
63 process_ =
new java.util.ArrayList<
AProcess>();
64 outputBuffer_ =
new StringBuffer();
65 lineQueue =
new ConcurrentLinkedQueue<String>();
66 GrxProcessManagerThis_ =
this;
71 if (GrxProcessManagerThis_ !=
null) {
74 GrxProcessManagerThis_.
process_.clear();
76 GrxProcessManagerThis_ =
null;
81 serverManager_ = serverManager;
82 IProgressMonitor monitor =
null;
83 setProcessList(monitor);
87 StringBuffer nsHost =
new StringBuffer(
"");
88 StringBuffer nsPort =
new StringBuffer(
"");
90 String nsOpt =
" -ORBInitRef NameService=corbaloc:iiop:" + nsHost +
":" + nsPort +
"/NameService";
94 if ( isRegistered(nameServerInfo_) ) {
95 if( !isRunning(nameServerInfo_) ){
96 unregister(nameServerInfo_.
id);
97 register(nameServerInfo_);
100 register(nameServerInfo_);
105 for(
int i=0;
i<localPi.
com.size();
i++){
107 localPi.
com.set(
i, localPi.
com.get(
i) +
" " + nsOpt);
109 localPi.
com.set(
i, localPi.
com.get(
i));
112 if ( isRegistered(localPi) ) {
113 if( !isRunning(localPi) ){
114 unregister(localPi.
id);
126 if (localProcess !=
null) {
133 if (
get(pi.
id) !=
null) {
141 if (
get(pi.id) !=
null) {
160 if (process_.remove(p)) {
173 for (
int i = 0;
i < process_.size();
i++) {
174 if (
get(
i).pi_.id.equals(
name)) {
182 if (0 <=
n &&
n < process_.size()) {
183 return process_.get(
n);
189 return process_.size();
193 for (
int i = 0;
i < process_.size();
i++) {
198 }
catch (Exception e) {
209 for (
int i = process_.size();
i > 0;
i--) {
219 if (!System.getProperty(
"os.name").equals(
"Linux")) {
224 pi.
com.add(
"/usr/bin/killall");
225 pi.
dir =
"/usr/bin/";
237 }
catch (Exception e) {
244 if (!System.getProperty(
"os.name").equals(
"Linux")) {
249 pi.
com.add(
"/bin/ps ax");
263 String[] rets = ret.split(
"\n");
265 for (
int i = 0;
i < rets.length;
i++) {
266 if (rets[
i].indexOf(str +
" ") != -1) {
267 ret += rets[
i] +
"\n";
271 }
catch (Exception e) {
277 public List<Integer>
getPID(String processName){
279 List<Integer> ret =
new ArrayList<Integer>();
280 String []commands={
"tasklist",
"/NH"};
281 String key =
new String(processName +
".exe");
282 Runtime r = Runtime.getRuntime();
283 if (System.getProperty(
"os.name").equals(
"Linux") ||
284 System.getProperty(
"os.name").equals(
"Mac OS X")) {
285 commands[0] =
new String (
"/bin/ps");
286 commands[1] =
new String (
"axh");
287 key =
new String(
"/" + processName);
292 Process p = r.exec(commands);
294 InputStream in = p.getInputStream();
295 p.getOutputStream().toString();
296 BufferedReader br =
new BufferedReader(
new InputStreamReader(in));
298 while ((line = br.readLine()) !=
null) {
299 if( line.indexOf(key) >= 0){
300 line = line.replaceAll(
"^[\\s]+",
"");
301 String[] splitLine = line.split(
"[\\s]+");
302 if( splitLine.length > splitNum ){
304 Integer pid = Integer.valueOf(splitLine[splitNum]);
308 }
catch (NumberFormatException ex){
309 ex.printStackTrace();
314 }
catch (Exception e) {
320 public void restart(IProgressMonitor monitor){
321 if(serverManager_ !=
null){
336 if(nameServer !=
null){
342 setProcessList(monitor);
347 public String
id =
null;
348 public List<String>
com =
new ArrayList<String>();
349 public List<String>
env =
new ArrayList<String>();
363 for (
int i = 0;
i <
com.size();
i++) {
366 if (
env.size() > 0) {
367 for (
int i = 0;
i <
env.size();
i++)
379 for(
int i = 0;
i <
com.size(); ++
i){
393 ret.
dir =
new String(
dir);
394 ret.
com =
new ArrayList<String>();
396 ret.
com.add(
new String(
i));
398 ret.
env =
new ArrayList<String>();
400 ret.
com.add(
new String(
i));
402 }
catch(CloneNotSupportedException ex){
403 ex.printStackTrace();
410 if( thread_ ==
null ){
411 thread_ =
new Thread() {
425 for (
int i = 0;
i <
size();
i++) {
431 if (sb ==
null || sb.length() == 0) {
434 String newLine = sb.toString();
435 if (outputBuffer_.length() > 50000) {
436 outputBuffer_.delete(0, newLine.length());
438 outputBuffer_.append(newLine);
451 lineQueue.offer(newLine);
455 Display display = Display.getDefault();
456 if (display !=
null && !display.isDisposed()) {
457 display.asyncExec(
new Runnable() {
459 String newLine =
null;
460 while ((newLine = lineQueue.poll()) !=
null) {
461 notifyObservers(
"append", newLine);
462 notifyObservers(
"setTopIndex");
469 }
catch (InterruptedException e) {
479 return outputBuffer_;
503 }
catch (InterruptedException ex) {
504 ex.printStackTrace();
512 private StringBuffer
com_ =
null;
519 private InputStream
is_ =
null;
520 private BufferedReader
br_ =
null;
521 private InputStream
es_ =
null;
522 private BufferedReader
bre_ =
null;
523 private PrintStream
ps_ =
null;
524 private StringBuffer
buf_ =
null;
537 }
catch (Exception e) {
541 buf_ =
new StringBuffer();
549 com_ =
new StringBuffer();
561 Thread thread =
new Thread() {
565 Display display = Display.getDefault();
566 if (display !=
null && !display.isDisposed()) {
567 display.asyncExec(
new Runnable() {
570 notifyObservers(
"append",
"[" +
pi_.
id +
":O] " +
"Process End\n");
571 notifyObservers(
"setTopIndex");
577 }
catch (InterruptedException e) {
604 String[] _com =
com_.toString().split(
" ");
605 String[] _opt = opt.split(
" ");
606 List<String>
com =
new ArrayList<String>();
607 for(
int i=0;
i<_com.length;
i++)
608 if(_com[
i].trim().
length()!=0 )
610 for(
int i=0;
i<_opt.length;
i++)
611 if(_opt[
i].trim().
length()!=0 )
613 ProcessBuilder pb =
new ProcessBuilder(
com);
614 if(
dir_ ==
null &&
new File(
com.get(0)).isAbsolute())
615 dir_ =
new File(
com.get(0)).getParentFile();
616 pb.directory(
dir_ );
617 Map<String, String> env = pb.environment();
619 for(
int i=0;
i<
env_.length;
i++){
622 env.put(
arg[0].trim(),
arg[1].trim());
624 Set<String> keySet = env.keySet();
625 String pathKey =
null;
626 for (String key : keySet) {
627 if (key.equalsIgnoreCase(
"Path")) {
631 String path = env.get(pathKey);
632 env.put(pathKey,
Activator.
getDefault().getPreferenceStore().getString(
"SERVER_DIR")+File.pathSeparator+path );
636 br_ =
new BufferedReader(
new InputStreamReader(
is_));
638 bre_ =
new BufferedReader(
new InputStreamReader(
es_));
646 }
catch (Exception e) {
664 String com0 = (
pi_.
com.get(0));
665 String path = com0.split(
" ")[0];
666 String
name =
new File(path).getName();
676 if (
pi_.
id.equals(nameServerInfo_.
id)) {
684 }
catch (Exception e) {
700 ServerObject serverObj = ServerObjectHelper.narrow(
obj);
701 serverObj.shutdown();
705 }
catch (Exception e) {
721 }
catch (IOException e) {
730 }
catch (IllegalThreadStateException e) {
732 }
catch (Exception e) {
742 String[] k = { key };
757 }
catch (Exception e1) {}
764 private String
checkKey(StringBuffer str, String[] key) {
766 for (
int i = 0;
i < key.length;
i++) {
767 if (str.indexOf(key[
i]) != -1) {
779 StringBuffer ret =
new StringBuffer();
782 char c = (char) br.read();
787 }
catch (Exception e) {
790 if (ret.length() == 0) {
798 StringBuffer
buf =
new StringBuffer();
799 for (
int i = 0;
i < 100;
i++) {
803 buf.append(
"[" +
pi_.
id +
":O] " + line1);
804 }
else if (line2 ==
null) {
808 buf.append(
"[" +
pi_.
id +
":E] " + line2);
819 String ret =
buf_.toString();
825 buf_.delete(0,
buf_.toString().length());
830 ps_.println(line +
"\n");
838 }
catch (InterruptedException e) {