1 16 17 package org.apache.commons.launcher; 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 23 29 public class ParentListener extends Thread { 30 31 33 36 private File heartbeatFile = null; 37 38 40 48 public ParentListener(String path) throws IOException { 49 50 if (path == null) 51 throw new IOException (); 52 53 heartbeatFile = new File (path); 55 heartbeatFile.getCanonicalPath(); 56 57 } 58 59 61 70 public void run() { 71 72 String osname = System.getProperty("os.name").toLowerCase(); 73 74 if (osname.indexOf("windows") >= 0) { 77 78 if (osname.indexOf("nt") == -1 && osname.indexOf("2000") == -1 && osname.indexOf("xp") == -1) 82 return; 83 84 for ( ; ; ) { 89 if (heartbeatFile.delete()) 90 break; 91 yield(); 93 try { 94 sleep(5000); 95 } catch (Exception e) {} 96 } 97 98 } else { 99 100 InputStream is = System.in; 102 int bytesAvailable = 0; 103 int bytesRead = 0; 104 byte[] buf = new byte[1024]; 105 try { 106 while (true) { 107 synchronized (is) { 108 is.mark(buf.length); 111 bytesAvailable = is.available(); 114 if (bytesAvailable < buf.length) { 115 bytesRead = is.read(buf, 0, bytesAvailable + 1); 116 is.reset(); 118 if (bytesRead == -1) 119 break; 120 } else { 121 if (buf.length < Integer.MAX_VALUE / 2) 123 buf = new byte[buf.length * 2]; 124 } 125 } 126 yield(); 127 } 128 } catch (IOException ioe) {} 129 130 } 131 132 if (heartbeatFile != null) 134 heartbeatFile.delete(); 135 136 System.exit(0); 138 139 } 140 141 } 142 | Popular Tags |