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