KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > server > Server


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.util.server;
23
24 import java.io.File JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * A Server.
34  *
35  * @author <a HREF="ryan.campbell@jboss.com">Ryan Campbell</a>
36  * @version $Revision: 57471 $
37  */

38 public class Server
39 {
40    /** the handle for the server **/
41    private String JavaDoc name;
42
43    /** the config to start **/
44    private String JavaDoc config;
45
46    /** the arguments to pass to jboss **/
47    private List JavaDoc<Argument> arguments = new ArrayList JavaDoc<Argument>();
48
49    /** the server's process, if running **/
50    private Process JavaDoc process;
51
52    /** the arguments for the jvm **/
53    private List JavaDoc<Argument> jvmArguments = new ArrayList JavaDoc<Argument>();
54
55    /** system properties for the jvm **/
56    private List JavaDoc<Property> sysProperties = new ArrayList JavaDoc<Property>();
57
58    /** the port used to determine if jboss started **/
59    private Integer JavaDoc httpPort = new Integer JavaDoc(8080);
60
61    /** where to find the rmi port **/
62    private Integer JavaDoc rmiPort = new Integer JavaDoc(1099);
63
64    /** the name or IP address to bind to **/
65    private String JavaDoc host = "localhost";
66
67    /** used for global config info **/
68    private ServerManager manager;
69
70    /** the output log **/
71    private PrintWriter JavaDoc outWriter;
72
73    /** the error log **/
74    private PrintWriter JavaDoc errorWriter;
75
76    /** Is there a servlet engine? **/
77    private boolean hasWebServer = true;
78
79    /**
80     * Get the name.
81     *
82     * @return the name.
83     */

84    public String JavaDoc getName()
85    {
86       return name;
87    }
88
89    /**
90     * Set the name.
91     *
92     * @param name The name to set.
93     */

94    public void setName(String JavaDoc name)
95    {
96       this.name = name;
97    }
98
99    /**
100     * Set the manager.
101     * @param manager
102     */

103    protected void setManager(ServerManager manager)
104    {
105       this.manager = manager;
106    }
107
108    /**
109     * Add an argument.
110     *
111     * @param arg
112     */

113    public void addArg(Argument arg)
114    {
115       arguments.add(arg);
116    }
117
118    /**
119     * Get the arguments as a string for the command line.
120     *
121     * @return the arguments as a string
122     */

123    public String JavaDoc getArgs()
124    {
125       StringBuffer JavaDoc args = new StringBuffer JavaDoc();
126       for (Iterator JavaDoc iter = arguments.iterator(); iter.hasNext();)
127       {
128          Argument argument = (Argument) iter.next();
129          args.append(argument.getValue() + " ");
130       }
131       return args.toString();
132    }
133
134    /**
135     * Add a jvm arg.
136     *
137     * @param arg
138     */

139    public void addJvmArg(Argument arg)
140    {
141       jvmArguments.add(arg);
142    }
143
144    /**
145     * Get the JVM args for the command line.
146     *
147     * @return the arguments as a string
148     */

149    public String JavaDoc getJvmArgs()
150    {
151       StringBuffer JavaDoc args = new StringBuffer JavaDoc();
152       for (Iterator JavaDoc iter = jvmArguments.iterator(); iter.hasNext();)
153       {
154          Argument argument = (Argument) iter.next();
155          args.append(argument.getValue() + " ");
156       }
157       return args.toString();
158    }
159
160    /**
161     * Add a system property.
162     *
163     * @param property
164     */

165    public void addSysProperty(Property property)
166    {
167       sysProperties.add(property);
168    }
169
170    /**
171     * Get the system properties for the command line.
172     *
173     * @return the properties as a string
174     */

175    public String JavaDoc getSysProperties()
176    {
177       StringBuffer JavaDoc args = new StringBuffer JavaDoc();
178       for (Iterator JavaDoc iter = sysProperties.iterator(); iter.hasNext();)
179       {
180          Property property = (Property) iter.next();
181          args.append("-D" + property.getKey() + "=" + property.getValue() + " ");
182       }
183       return args.toString();
184    }
185
186    /**
187     * The running process of this server.
188     * @param process
189     */

190    public void setProcess(Process JavaDoc process)
191    {
192       this.process = process;
193    }
194
195    /**
196     * Is the server actually running?
197     *
198     * @return whether the server is running
199     */

200    public boolean isRunning()
201    {
202       if (isStopped())
203       {
204          return false;
205       }
206       else
207       {
208          try
209          {
210             //exitValue() only returns if process has ended.
211
process.exitValue();
212             return false;
213          }
214          catch (IllegalThreadStateException JavaDoc e)
215          {
216             return true;
217          }
218       }
219    }
220
221    /**
222     * Has the server been intentionally stopped?
223     *
224     * @return whether the server is stopped
225     */

226    public boolean isStopped()
227    {
228       return process == null;
229    }
230
231    /**
232     * Get the process.
233     *
234     * @return the process
235     */

236    public Process JavaDoc getProcess()
237    {
238       return process;
239    }
240
241    /**
242     * Where is the HTTP service listening?
243     *
244     * @return whether the service is listening
245     * @throws MalformedURLException for a malformed url
246     */

247    public URL JavaDoc getHttpUrl() throws MalformedURLException JavaDoc
248    {
249       return new URL JavaDoc("http://" + host + ":" + httpPort);
250    }
251
252    /**
253     * The URl for the RMI listener.
254     *
255     * @return the rmi url
256     */

257    public String JavaDoc getRmiUrl()
258    {
259       return "jnp://" + host + ":" + rmiPort;
260    }
261
262    /**
263     * Get the config. Defaults to the server name.
264     *
265     * @return the config.
266     */

267    public String JavaDoc getConfig()
268    {
269       if (config != null)
270       {
271          return config;
272       }
273       else
274       {
275          return name;
276       }
277    }
278
279    /**
280     * Set the config.
281     *
282     * @param config The config to set.
283     */

284    public void setConfig(String JavaDoc config)
285    {
286       this.config = config;
287    }
288
289    /**
290     * Get the host.
291     *
292     * @return the host.
293     */

294    public String JavaDoc getHost()
295    {
296       return host;
297    }
298
299    /**
300     * Set the host.
301     *
302     * @param host The host to set.
303     */

304    public void setHost(String JavaDoc host)
305    {
306       this.host = host;
307    }
308
309    /**
310     * Set the httpPort.
311     *
312     * @param httpPort The httpPort to set.
313     */

314    public void setHttpPort(Integer JavaDoc httpPort)
315    {
316       this.httpPort = httpPort;
317    }
318
319    /**
320     * Set the rmiPort.
321     *
322     * @param rmiPort The rmiPort to set.
323     */

324    public void setRmiPort(Integer JavaDoc rmiPort)
325    {
326       this.rmiPort = rmiPort;
327    }
328
329    /**
330     * Get the rmiPort
331     *
332     * @return the rmi port
333     */

334    public Integer JavaDoc getRmiPort()
335    {
336       return rmiPort;
337    }
338    /**
339     * Where should the server's std err log go?
340     *
341     * @return the error log file
342     */

343    public File JavaDoc getErrorLog()
344    {
345       return new File JavaDoc(getLogDir(), "error.log");
346    }
347
348    /**
349     * Where should the servers's std out go?
350     *
351     * @return the output log file
352     */

353    public File JavaDoc getOutputLog()
354    {
355       return new File JavaDoc(getLogDir(), "output.log");
356    }
357
358    /**
359     * The server's log directory
360     *
361     * @return the log directory
362     */

363    private File JavaDoc getLogDir()
364    {
365       return new File JavaDoc(getConfDir(), "log");
366    }
367
368    /**
369     * The server's directory (ie, all, default)
370     *
371     * @return the configuration directory
372     */

373    private File JavaDoc getConfDir()
374    {
375       return new File JavaDoc(manager.getJBossHome(), "server/" + getConfig());
376    }
377
378    /**
379     * Set the output log's writer
380     *
381     * @param outlog the log writer
382     */

383    public void setOutWriter(PrintWriter JavaDoc outlog)
384    {
385       outWriter = outlog;
386    }
387
388    /**
389     * The writer for the output log.
390     *
391     * @return the output writer
392     */

393    public PrintWriter JavaDoc getOutWriter()
394    {
395       return outWriter;
396    }
397
398    /**
399     * The error log's writer.
400     *
401     * @return the log writer
402     */

403    public PrintWriter JavaDoc getErrorWriter()
404    {
405       return errorWriter;
406    }
407
408    /**
409     * Set the error writer.
410     * @param errorlog
411     */

412    public void setErrorWriter(PrintWriter JavaDoc errorlog)
413    {
414       errorWriter = errorlog;
415    }
416
417    /**
418     * Get the hasWebServer.
419     *
420     * @return the hasWebServer.
421     */

422    public boolean hasWebServer()
423    {
424       return hasWebServer;
425    }
426    /**
427     * Set the hasWebServer.
428     *
429     * @param hasWebServer The hasWebServer to set.
430     */

431    public void setHasWebServer(boolean hasWebServer)
432    {
433       this.hasWebServer = hasWebServer;
434    }
435
436 }
Popular Tags