KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > integration > IntegrateService


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4
5 package org.jfox.ioc.integration;
6
7 import java.io.File JavaDoc;
8 import java.io.BufferedReader JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10
11 import org.jfox.ioc.common.AbstractService;
12 import org.jfox.ioc.ext.ActiveComponent;
13
14 /**
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class IntegrateService extends AbstractService implements ActiveComponent {
19
20     private ProcessBuilder JavaDoc pbuilder = null;
21     private Process JavaDoc process = null;
22
23     private String JavaDoc workDirectory = ".";
24     private String JavaDoc startCmd = "";
25     private String JavaDoc stopCmd = "";
26
27     public String JavaDoc getWorkDirectory() {
28         return workDirectory;
29     }
30
31     public void setWorkDirectory(String JavaDoc workDirectory) {
32         this.workDirectory = context.getModuleDir() + "/" + workDirectory;
33     }
34
35     public String JavaDoc getStartCmd() {
36         return startCmd;
37     }
38
39     public void setStartCmd(String JavaDoc startCmd) {
40         this.startCmd = startCmd;
41     }
42
43     public String JavaDoc getStopCmd() {
44         return stopCmd;
45     }
46
47     public void setStopCmd(String JavaDoc stopCmd) {
48         this.stopCmd = stopCmd;
49     }
50
51     protected void doStart() throws Exception JavaDoc {
52         Thread JavaDoc thread = new Thread JavaDoc(this);
53         thread.setPriority(Thread.MIN_PRIORITY);
54         thread.start();
55     }
56
57     protected void doStop() throws Exception JavaDoc {
58         logger.debug("stop command: cmd /c " + getStopCmd());
59         if(stopCmd != null) {
60             Process JavaDoc proc = new ProcessBuilder JavaDoc("cmd", "/c", getStopCmd()).directory(new File JavaDoc(getWorkDirectory())).start();
61             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(proc.getErrorStream()));
62             String JavaDoc line = null;
63             while((line = br.readLine()) != null) {
64                 logger.debug(line);
65             }
66         }
67 // process.destroy();
68
}
69
70     protected void doInit() throws Exception JavaDoc {
71     }
72
73     protected void doDestroy() throws Exception JavaDoc {
74     }
75
76     public void run() {
77         try {
78             logger.debug("start command: cmd /c " + getStartCmd());
79             pbuilder = new ProcessBuilder JavaDoc("cmd", "/c", getStartCmd());
80             pbuilder = pbuilder.directory(new File JavaDoc(getWorkDirectory()));
81             process = pbuilder.start();
82             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(process.getErrorStream()));
83             String JavaDoc line = null;
84             while((line = br.readLine()) != null) {
85                 logger.debug(line);
86             }
87         }
88         catch(Exception JavaDoc e) {
89             logger.warn("start error.", e);
90         }
91
92     }
93
94     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
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