C:\Users\Morten\Documents\NetBeansProjects\A_Daemon\src\a_daemon\A_Daemon.java
1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 6 package a_daemon; 7 8 import java.io.BufferedReader; 9 import java.io.FileReader; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.io.InputStreamReader; 13 import java.text.SimpleDateFormat; 14 import java.util.Calendar; 15 import java.util.logging.Level; 16 import java.util.logging.Logger; 17 18 19 // 20 //https://joerglenhard.wordpress.com/2012/05/29/build-windows-service-from-java-application-with-procrun/ 21 // 22 23 /** 24 * 25 * @author Morten 26 */ 27 28 public class A_Daemon { 29 30 //private static final Logger log = Logger.getLogger(A_Deamon.class); 31 //private static org.apache.log4j.Logger log = Logger.getLogger(A_Deamon.class); 32 private static final Logger log = Logger.getLogger("ConsoleLogger"); 33 34 private static boolean stop = false; 35 private static boolean pause = false; 36 private static boolean restart = false; 37 private static boolean exception = false; 38 public static Process process = null; 39 public static ProcessBuilder process_builder; 40 public static StringBuilder sb = null; 41 public static String version = System.getProperty("java.version"); 42 public static String class_path = System.getProperty("java.class.path"); 43 public static String CurrentWorkingDirectory = System.getProperty("user.dir"); 44 public static String get_it[] = new String[8]; 45 public static int pointer = 0; 46 public static String read_in = null; 47 public static String read_line = null; 48 public static String JRE_HOME = null; 49 public static String JDK_HOME = null; 50 /** 51 * @param args the command line arguments 52 */ 53 54 // try{}catch(Exception in){System.out.println("Exception in"+in);} 55 56 /*****************************************************************************/ 57 /*****************************************************************************/ 58 59 // System.out.println(""); 60 61 /*****************************************************************************/ 62 /*****************************************************************************/ 63 64 65 /*===========================================================================*/ 66 public static void start(String[] args) { 67 /*===========================================================================*/ 68 System.out.println("System.out: public static void start(String[] args)" ); 69 log.log(Level.INFO, "Logger: public static void start(String[] args)"); 70 //----------------------------------------------------------------------------- 71 try { 72 String res; 73 System.out.println("First ProcessBuilder()...Begin"); 74 ProcessBuilder process_builder = 75 new ProcessBuilder(CurrentWorkingDirectory+"\\find_jdk.bat"); 76 Process process = process_builder.start(); 77 System.out.println("Processing..."); 78 StringBuilder string_builder = new StringBuilder(); 79 BufferedReader reader = new BufferedReader( 80 new InputStreamReader(process.getInputStream())); 81 //----------------------------------------------------------------------------- 82 String line; 83 while ((line = (String) reader.readLine()) != null) { 84 string_builder.append(line); } 85 //----------------------------------------------------------------------------- 86 int exitVal = process.waitFor(); 87 System.out.println("First Exit value = "+exitVal); 88 if (exitVal == 0) { 89 System.out.println("StringBuilder: "+string_builder); 90 } else { 91 System.out.println("First Abnormal temination"); 92 stop = true; 93 } 94 //----------------------------------------------------------------------------- 95 String jdk_string = string_builder.toString(); 96 // Result: JAVA_HOME = C:\\Program Files (x86)\\Java\\jdk1.7.0_72 97 JDK_HOME = jdk_string; 98 int pos; 99 pos = jdk_string.indexOf("C"); 100 System.out.println("pos = "+pos); 101 jdk_string = jdk_string.substring(pos); 102 System.out.println("jdk_string: "+jdk_string); 103 JDK_HOME = jdk_string; 104 System.out.println("JDK_HOME: " + JDK_HOME); 105 // Result: JDK_HOME: C:\\Program Files (x86)\\Java\\jdk1.7.0_72 106 //----------------------------------------------------------------------------- 107 }catch(Exception in){ 108 log.log(Level.INFO, "Logger: Exception in Start A_Deamon: " + in); 109 exception = true; 110 stop = true; 111 System.out.println("Exception in start Deamon " + in); 112 // System.exit(1234); 113 } 114 System.out.println("First ProcessBuilder()...END"); 115 //============================================================================= 116 System.out.println("\r\n"); 117 System.out.println("==================================================="); 118 System.out.println("\r\n"); 119 120 //https://www.mkyong.com/java/java-how-to-run-windows-bat-file/ 121 122 System.out.println(""); 123 //============================================================================= 124 while(!stop) { 125 System.out.println("\r\n"); 126 System.out.println("==================================================="); 127 System.out.println("while begin"); 128 System.out.println("\r\n"); 129 //----------------------------------------------------------------------------- 130 if(!exception){ 131 //----------------------------------------------------------------------------- 132 try{ 133 System.out.println(""); 134 System.out.println("Second ProcessBuilder()...Begin"); 135 Thread.sleep(2000); 136 System.out.println("\r\nGet CurrentWorkingDirectory: \r\n"+ 137 CurrentWorkingDirectory); 138 System.out.println(""); 139 //----------------------------------------------------------------------------- 140 //--------------------------- try --------------------------------------------- 141 try{ 142 System.out.println("JDK_HOME "+JDK_HOME+"\r\n"); 143 }catch(Exception in){ 144 System.out.println("Second Exception in JDK_HOME "+in);} 145 //--------------------------- catch ------------------------------------------- 146 //----------------------------------------------------------------------------- 147 process_builder = new ProcessBuilder(JDK_HOME+"\\bin\\java", "-jar", "AnothorStartStop.jar" ); 148 //----------------------------------------------------------------------------- 149 //----------------------------------------------------------------------------- 150 }catch(Exception in){ 151 System.out.println("Exception in ProcessBuilder A_Deamon: " + in); 152 exception = true; stop = true; } 153 //----------------------------------------------------------------------------- 154 // Second try 155 //----------------------------------------------------------------------------- 156 try{ 157 process_builder.redirectErrorStream(true); 158 Process p = process_builder.start(); 159 160 InputStream is = p.getInputStream(); 161 BufferedReader br = new BufferedReader(new InputStreamReader(is)); 162 String line = null; 163 for (line = br.readLine(); line != null; line = br.readLine()) { 164 System.out.println("A_Daemon "+ line ); // Or just ignore it 165 if(line.toLowerCase().indexOf("stop")!=-1){ 166 stop = true; 167 System.out.println("A_Daemon STOPED and Destroy"); 168 } 169 //line = new String(); 170 }//for... 171 //------------------------------------------------------------------------ 172 System.out.println("\r\n"); 173 System.out.println("while end"); 174 System.out.println("==================================================="); 175 System.out.println("\r\n"); 176 //----------------------------------------------------------------------------- 177 if(stop){ 178 System.out.println("A_Daemon STOPED and Destroy"); 179 p.destroyForcibly(); 180 } 181 //------------------------------------------------------------------------ 182 int exitVal = p.waitFor(); 183 p.waitFor(); 184 p.destroy(); 185 System.out.println("Second Exit value = "+exitVal); 186 if (exitVal == 0) { 187 } else { 188 System.out.println("Second Abnormal temination"); 189 stop = true; 190 } 191 System.out.println("Second ProcessBuilder()...End"); 192 //----------------------------------------------------------------------------- 193 try { 194 String get_current_time = GetCurrentTime(); 195 System.out.println("System.out: Thread.sleep(5000) "); 196 log.log(Level.INFO, "Logger: Thread.sleep(5000)"); 197 Thread.sleep(5000); 198 System.out.println("System.out: Continue"); 199 log.log(Level.INFO, "Logger: Continue"); 200 } catch (InterruptedException e) { 201 System.out.println("Exception " + e); 202 } 203 //----------------------------------------------------------------------------- 204 p.destroyForcibly(); 205 //----------------------------------------------------------------------------- 206 }catch(IOException in){ 207 log.info("IOException in ProcessBuilder A_Deamon: " + in); 208 System.out.println("IOException in ProcessBuilder A_Deamon: " + in); 209 exception = true; 210 stop = true; 211 }catch(Exception in){ 212 log.log(Level.INFO, "Exception in ProcessBuilder A_Deamon: " + in); 213 System.out.println("Exception in ProcessBuilder A_Deamon: " + in); 214 exception = true; 215 stop = true; } 216 //----------------------------------------------------------------------------- 217 }//if.. 218 //----------------------------------------------------------------------------- 219 }//while. 220 /*===========================================================================*/ 221 }//start 222 /*****************************************************************************/ 223 /*****************************************************************************/ 224 /*****************************************************************************/ 225 /*****************************************************************************/ 226 227 // System.out.println(""); 228 229 /*****************************************************************************/ 230 /*****************************************************************************/ 231 /*===========================================================================*/ 232 public static void stop(String[] args) { 233 /*===========================================================================*/ 234 System.out.println("stop"); 235 stop = true; 236 }//stop 237 /*****************************************************************************/ 238 /*****************************************************************************/ 239 /*****************************************************************************/ 240 241 // System.out.println(""); 242 243 /*****************************************************************************/ 244 /*****************************************************************************/ 245 /*===========================================================================*/ 246 /* public static void pause(String[] args) { 247 //----------------------------------------------------------------------------- 248 System.out.println("pause"); 249 // pause = true; 250 }//stop 251 /*****************************************************************************/ 252 /*****************************************************************************/ 253 /*****************************************************************************/ 254 255 // System.out.println(""); 256 257 /*****************************************************************************/ 258 /*****************************************************************************/ 259 /*===========================================================================*/ 260 /* public static void restart(String[] args) { 261 //----------------------------------------------------------------------------- 262 System.out.println("restart"); 263 // restart = true; 264 }//stop 265 /*****************************************************************************/ 266 /*****************************************************************************/ 267 /*****************************************************************************/ 268 269 // System.out.println(""); 270 271 /*****************************************************************************/ 272 /*****************************************************************************/ 273 274 // System.out.println(""); 275 276 /*****************************************************************************/ 277 /*****************************************************************************/ 278 279 //----------------------------------------------------------------------------- 280 281 /*===========================================================================*/ 282 public static void main(String[] args) { 283 284 System.out.println("Starting Main A_Daemon\r\n"); 285 CurrentWorkingDirectory = CurrentWorkingDirectory.concat("\\"); 286 System.out.println("Main CurrentWorkingDirectory = "+CurrentWorkingDirectory+"\r\n"); 287 log.log(Level.INFO, "Logger: CurrentWorkingDirectory = ", CurrentWorkingDirectory); 288 log.log(Level.INFO, "\r\n"); 289 290 try { 291 switch (args[0]) { 292 case "start": 293 System.out.println("System.out: Switch Start" ); 294 // log.log(Level.INFO, "Logger: Switch Start"); 295 start(args); 296 // break; 297 case "stop": 298 System.out.println("System.out: Switch Stop" ); 299 // log.log(Level.INFO, "Logger: Switch Stop"); 300 stop(args); 301 // break; 302 }//sw 303 System.runFinalization(); 304 System.exit(0); 305 }catch(Exception in){ 306 System.out.println("Exception in main() " + in ); 307 stop = true; 308 System.out.println("Stopping?\r\n"); 309 } 310 /*===========================================================================*/ 311 312 //----------------------------------------------------------------------------- 313 314 /*===========================================================================*/ 315 }//main 316 /*****************************************************************************/ 317 /*****************************************************************************/ 318 /*****************************************************************************/ 319 320 // System.out.println(""); 321 322 /*****************************************************************************/ 323 /*****************************************************************************/ 324 325 /*===========================================================================*/ 326 public static void file_open(){ 327 /*===========================================================================*/ 328 System.out.println("File Open Begin"); 329 //----------------------------------------------------------------------------- 330 JRE_HOME = new String(); 331 JDK_HOME = new String(); 332 pointer = 0; 333 //----------------------------------------------------------------------------- 334 try { 335 BufferedReader br = new BufferedReader(new FileReader("reg4.txt")); 336 //----------------------------------------------------------------------------- 337 while ((read_in = br.readLine()) != null) { 338 // System.out.println("Read " + read_in); 339 get_it[pointer] = read_in; 340 pointer++;} 341 //----------------------------------------------------------------------------- 342 pointer--;// Avoiding nullpointer 343 //----------------------------------------------------------------------------- 344 // System.out.println("Pointer " + pointer); 345 // System.out.println("Got it " + get_it[pointer]); 346 // Output: 347 // Got it "RuntimeLib"="C:\\Program Files\\Java\\jre7\\bin\\client\\jvm.dll" 348 //----------------------------------------------------------------------------- 349 }catch(Exception in){ 350 System.out.println("Exception in ReadLine() " + in);} 351 //----------------------------------------------------------------------------- 352 read_line = get_it[pointer]; 353 pointer = read_line.indexOf("="); 354 //----------------------------------------------------------------------------- 355 try { 356 JRE_HOME = read_line.substring(pointer+1, read_line.length()); 357 JRE_HOME = JRE_HOME.replaceAll("\\\\\\\\", "/"); 358 // System.out.println("JRE_HOME: " + JRE_HOME); 359 }catch(Exception in){System.out.println("Exception in " + in ); } 360 //----------------------------------------------------------------------------- 361 362 /*===========================================================================*/ 363 364 /*===========================================================================*/ 365 366 //----------------------------------------------------------------------------- 367 pointer = 0; 368 //----------------------------------------------------------------------------- 369 try { 370 BufferedReader br = new BufferedReader(new FileReader("reg2.txt")); 371 //----------------------------------------------------------------------------- 372 while ((read_in = br.readLine()) != null) { 373 // System.out.println("Read " + read_in); 374 get_it[pointer] = read_in; 375 pointer++;} 376 //----------------------------------------------------------------------------- 377 pointer--;// Avoiding nullpointer 378 //----------------------------------------------------------------------------- 379 // System.out.println("Pointer " + pointer); 380 // System.out.println("Got it " + get_it[pointer]); 381 // Output: 382 // Got it "RuntimeLib"="C:\\Program Files\\Java\\jre7\\bin\\client\\jvm.dll" 383 //----------------------------------------------------------------------------- 384 read_line = get_it[pointer]; 385 pointer = read_line.indexOf("="); 386 //----------------------------------------------------------------------------- 387 try { 388 JDK_HOME = read_line.substring(pointer+1, read_line.length()); 389 JDK_HOME = JDK_HOME.replaceAll("\"", ""); 390 // JDK_HOME = "\"" + JDK_HOME + "\\\\bin\\\\\""; 391 JDK_HOME = JDK_HOME + "\\\\bin\\\\"; 392 JDK_HOME = JDK_HOME.replaceAll("\\\\\\\\", "/"); 393 }catch(Exception in){System.out.println("Exception in JDK_HOME:" + in ); } 394 //----------------------------------------------------------------------------- 395 }catch(Exception in){ 396 System.out.println("Exception in ReadLine() " + in);} 397 //----------------------------------------------------------------------------- 398 System.out.println("File Open End"); 399 //----------------------------------------------------------------------------- 400 }// OK 401 /*****************************************************************************/ 402 /*****************************************************************************/ 403 /*****************************************************************************/ 404 405 // System.out.println(""); 406 407 /* ========================================================================= */ 408 private static String GetCurrentTime(){ 409 /* ========================================================================= */ 410 String current_time = new String(); 411 Calendar cal = Calendar.getInstance(); 412 SimpleDateFormat format1 = new SimpleDateFormat("kk.mm.ss"); 413 current_time = format1.format(cal.getTime()); 414 // System.out.println(current_time); 415 current_time.trim(); 416 /* ========================================================================= */ 417 return current_time; 418 /* ========================================================================= */ 419 }//GetCurrentTime 420 /* ========================================================================= */ 421 /* ========================================================================= */ 422 /*****************************************************************************/ 423 /*****************************************************************************/ 424 /*****************************************************************************/ 425 426 // System.out.println(""); 427 428 /*****************************************************************************/ 429 /*****************************************************************************/ 430 }//A_Deamon 431 /*****************************************************************************/ 432 /*****************************************************************************/ 433 /*****************************************************************************/ 434 435