1 4 5 package org.jfox.ioc.integration; 6 7 import java.io.File ; 8 import java.io.BufferedReader ; 9 import java.io.InputStreamReader ; 10 11 import org.jfox.ioc.common.AbstractService; 12 import org.jfox.ioc.ext.ActiveComponent; 13 14 17 18 public class IntegrateService extends AbstractService implements ActiveComponent { 19 20 private ProcessBuilder pbuilder = null; 21 private Process process = null; 22 23 private String workDirectory = "."; 24 private String startCmd = ""; 25 private String stopCmd = ""; 26 27 public String getWorkDirectory() { 28 return workDirectory; 29 } 30 31 public void setWorkDirectory(String workDirectory) { 32 this.workDirectory = context.getModuleDir() + "/" + workDirectory; 33 } 34 35 public String getStartCmd() { 36 return startCmd; 37 } 38 39 public void setStartCmd(String startCmd) { 40 this.startCmd = startCmd; 41 } 42 43 public String getStopCmd() { 44 return stopCmd; 45 } 46 47 public void setStopCmd(String stopCmd) { 48 this.stopCmd = stopCmd; 49 } 50 51 protected void doStart() throws Exception { 52 Thread thread = new Thread (this); 53 thread.setPriority(Thread.MIN_PRIORITY); 54 thread.start(); 55 } 56 57 protected void doStop() throws Exception { 58 logger.debug("stop command: cmd /c " + getStopCmd()); 59 if(stopCmd != null) { 60 Process proc = new ProcessBuilder ("cmd", "/c", getStopCmd()).directory(new File (getWorkDirectory())).start(); 61 BufferedReader br = new BufferedReader (new InputStreamReader (proc.getErrorStream())); 62 String line = null; 63 while((line = br.readLine()) != null) { 64 logger.debug(line); 65 } 66 } 67 } 69 70 protected void doInit() throws Exception { 71 } 72 73 protected void doDestroy() throws Exception { 74 } 75 76 public void run() { 77 try { 78 logger.debug("start command: cmd /c " + getStartCmd()); 79 pbuilder = new ProcessBuilder ("cmd", "/c", getStartCmd()); 80 pbuilder = pbuilder.directory(new File (getWorkDirectory())); 81 process = pbuilder.start(); 82 BufferedReader br = new BufferedReader (new InputStreamReader (process.getErrorStream())); 83 String line = null; 84 while((line = br.readLine()) != null) { 85 logger.debug(line); 86 } 87 } 88 catch(Exception e) { 89 logger.warn("start error.", e); 90 } 91 92 } 93 94 public static void main(String [] args) throws Exception { 95 IntegrateService iservice = new IntegrateService(); 96 iservice.setStartCmd("mysqld --standalone"); 97 iservice.setWorkDirectory("plugins/ebseries/mysql/bin"); 98 iservice.init(); 99 iservice.start(); 100 } 101 } 102 | Popular Tags |