KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > mavenplugin > StartRemoteServer


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.deployment.mavenplugin;
19
20 import java.io.*;
21 import java.util.ArrayList JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24
25 /**
26  * @version $Rev: 149230 $ $Date: 2005-01-30 23:00:44 -0800 (Sun, 30 Jan 2005) $
27  */

28 public class StartRemoteServer {
29
30     private String JavaDoc geronimoTarget;
31     private String JavaDoc vmArgs = "";
32     private String JavaDoc configs;
33     private String JavaDoc debugPort;
34     private String JavaDoc output;
35     private String JavaDoc error;
36     private PrintStream out = System.out;
37     private PrintStream err = System.err;
38
39
40
41     public String JavaDoc getGeronimoTarget() {
42         return geronimoTarget;
43     }
44
45     public void setGeronimoTarget(String JavaDoc geronimoTarget) {
46         this.geronimoTarget = geronimoTarget;
47     }
48
49     public String JavaDoc getVmArgs() {
50         return vmArgs;
51     }
52
53     public void setVmArgs(String JavaDoc vmArgs) {
54         this.vmArgs = vmArgs;
55     }
56
57     public String JavaDoc getConfigs() {
58         return configs;
59     }
60
61     public void setConfigs(String JavaDoc configs) {
62         this.configs = configs;
63     }
64
65     public String JavaDoc getDebugPort() {
66         return debugPort;
67     }
68
69     public void setDebugPort(String JavaDoc debugPort) {
70         this.debugPort = debugPort;
71     }
72
73     public String JavaDoc getOutput() {
74         return output;
75     }
76
77     public void setOutput(String JavaDoc output) {
78         this.output = output;
79     }
80
81     public String JavaDoc getError() {
82         return error;
83     }
84
85     public void setError(String JavaDoc error) {
86         this.error = error;
87     }
88
89     private PrintStream getOut() {
90         try {
91             if (getOutput() != null) {
92                 out = new PrintStream(new FileOutputStream(getOutput()));
93             }
94         } catch (FileNotFoundException e) {
95             System.err.println(e.getMessage());
96         }
97
98         return out;
99     }
100
101     private PrintStream getErr() {
102         try {
103             if (getError() != null) {
104                 err = new PrintStream(new FileOutputStream(getError()));
105             } else if (getOutput() != null) {
106                 err = getOut();
107             }
108         } catch (FileNotFoundException e) {
109             System.err.println(e.getMessage());
110         }
111         return err;
112     }
113
114     public void execute() throws Exception JavaDoc {
115         ArrayList JavaDoc cmd = new ArrayList JavaDoc();
116         File root = new File(getGeronimoTarget());
117         File systemFile = new File(root, "bin/server.jar");
118         String JavaDoc s = java.io.File.separator;
119         String JavaDoc java = System.getProperty("java.home") + s + "bin" + s + "java";
120
121         cmd.add(java);
122
123         if (debugPort != null) {
124             cmd.add("-Xdebug");
125             cmd.add("-Xnoagent");
126             cmd.add("-Djava.compiler=NONE");
127             cmd.add("-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=" + debugPort);
128         }
129
130         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getVmArgs()); st.hasMoreTokens();) {
131             cmd.add(st.nextToken());
132         }
133
134         cmd.add("-ea");
135         cmd.add("-jar");
136         cmd.add(systemFile.getCanonicalPath());
137
138         if (getConfigs() != null) {
139             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(getConfigs()); st.hasMoreTokens();) {
140                 cmd.add(st.nextToken());
141             }
142         }
143         String JavaDoc[] command = (String JavaDoc[]) cmd.toArray(new String JavaDoc[0]);
144
145         Runtime JavaDoc runtime = Runtime.getRuntime();
146         Process JavaDoc server = runtime.exec(command);
147
148
149         // Pipe the processes STDOUT to ours
150
InputStream out = server.getInputStream();
151         Thread JavaDoc serverOut = new Thread JavaDoc(new Pipe(out, getOut()));
152         serverOut.setDaemon(true);
153         serverOut.start();
154
155         // Pipe the processes STDERR to ours
156
InputStream err = server.getErrorStream();
157         Thread JavaDoc serverErr = new Thread JavaDoc(new Pipe(err, getErr()));
158         serverErr.setDaemon(true);
159         serverErr.start();
160
161     }
162
163     private final class Pipe implements Runnable JavaDoc {
164
165         private final InputStream in;
166         private final OutputStream out;
167
168         public Pipe(InputStream in, OutputStream out) {
169             this.in = in;
170             this.out = out;
171         }
172
173         public void run() {
174             int i;
175             try {
176                 do {
177                     i = in.read();
178                     out.write(i);
179                 } while (i != -1);
180             } catch (IOException e) {
181                 e.printStackTrace();
182             }
183         }
184
185     }
186 }
187
Popular Tags