1 16 17 18 19 import java.io.*; 20 import java.net.*; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.Enumeration ; 24 25 import org.apache.commons.collections.ExtendedProperties; 26 import java.io.IOException ; 27 import java.util.Iterator ; 28 29 32 public class AloneService { 33 34 private ExtendedProperties prop = null; 35 private Process proc[] = null; 36 private ServiceDaemonReadThread readout[] = null; 37 private ServiceDaemonReadThread readerr[] = null; 38 39 protected void finalize() { 40 System.err.println("ServiceDaemon: instance "+this.hashCode()+ 41 " garbage collected"); 42 } 43 44 47 public void init(String [] arguments) 48 throws Exception { 49 50 System.setErr(new PrintStream(new FileOutputStream(new File("/ServiceDaemon.err"),true))); 51 System.err.println("ServiceDaemon: instance "+this.hashCode()+ 52 " init"); 53 54 55 prop = new ExtendedProperties("startfile"); 56 57 58 int i=0; 59 for (Iterator e = prop.getKeys(); e.hasNext() ;) { 60 e.next(); 61 i++; 62 } 63 System.err.println("ServiceDaemon: init for " + i + " processes"); 64 proc = new Process [i]; 65 readout = new ServiceDaemonReadThread[i]; 66 readerr = new ServiceDaemonReadThread[i]; 67 for (i=0;i<proc.length;i++) { 68 proc[i] = null; 69 readout[i] = null; 70 readerr[i] = null; 71 } 72 73 System.err.println("ServiceDaemon: init done "); 74 75 } 76 77 public void start() { 78 79 System.err.println("ServiceDaemon: starting"); 80 81 82 int i=0; 83 for (Iterator e = prop.getKeys(); e.hasNext() ;) { 84 String name = (String ) e.next(); 85 System.err.println("ServiceDaemon: starting: " + name + " : " + prop.getString(name)); 86 try { 87 proc[i] = Runtime.getRuntime().exec(prop.getString(name)); 88 } catch(Exception ex) { 89 System.err.println("Exception: " + ex); 90 } 91 92 readerr[i] = 93 new ServiceDaemonReadThread(proc[i].getErrorStream()); 94 readout[i] = 95 new ServiceDaemonReadThread(proc[i].getInputStream()); 96 readerr[i].start(); 97 readout[i].start(); 98 i++; 99 } 100 } 101 102 public void stop() 103 throws IOException , InterruptedException { 104 105 System.err.println("ServiceDaemon: stopping"); 106 107 for (int i=0;i<proc.length;i++) { 108 if ( proc[i]==null) 109 continue; 110 proc[i].destroy(); 111 try { 112 proc[i].waitFor(); 113 } catch(InterruptedException ex) { 114 System.err.println("ServiceDaemon: exception while stopping:" + 115 ex); 116 } 117 } 118 119 System.err.println("ServiceDaemon: stopped"); 120 } 121 122 public void destroy() { 123 System.err.println("ServiceDaemon: instance "+this.hashCode()+ 124 " destroy"); 125 } 126 127 } 128 | Popular Tags |