KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30
31 /**
32  * A ServerManager.
33  *
34  * @author <a HREF="ryan.campbell@jboss.com">Ryan Campbell</a>
35  * @version $Revision: 56504 $
36  */

37 public class ServerManager
38 {
39    /**
40     * Wait for 45 secs
41     */

42    private static final int WAIT_TIME = 45;
43    private static final int START_TIME = 120;
44    
45    /** jboss root **/
46    private String JavaDoc jbossHome;
47
48    /** the jvm executable **/
49    private String JavaDoc jvm = "java";
50
51    /** the java home to use**/
52    private String JavaDoc javaHome;
53
54    private String JavaDoc udpGroup;
55    
56    /** list of all configured servers **/
57    private List JavaDoc<Server> servers = new ArrayList JavaDoc<Server>();
58
59    /**
60     * Create a new ServerManager. Make sure all servers are killed on shutdown.
61     *
62     */

63    public ServerManager()
64    {
65       Thread JavaDoc cleanupThread = new Thread JavaDoc()
66       {
67          public void run()
68          {
69             for (Iterator JavaDoc iter = servers.iterator(); iter.hasNext();)
70             {
71                Server server = (Server) iter.next();
72                if (server.isRunning())
73                {
74                   System.err.println("Found server \""+server.getName()+"\" still running; stopping it.");
75                   try
76                   {
77                      ServerController.stopServer(server, ServerManager.this);
78                   }
79                   catch (IOException JavaDoc e)
80                   {
81                      System.err.println("Failed to stop server(s) on shutdown.");
82                      e.printStackTrace(System.err);
83                   }
84                }
85             }
86          }
87       };
88
89       Runtime.getRuntime().addShutdownHook(cleanupThread);
90
91    }
92
93    /**
94     * Add a server
95     *
96     * @param newServer
97     */

98    public void addServer(Server newServer)
99    {
100       newServer.setManager(this);
101       servers.add(newServer);
102    }
103
104    /**
105     * Get the server by name. Can't use a hashmap because of
106     * Ant.
107     *
108     * @param name the name of the server
109     * @return the server
110     */

111    public Server getServer(String JavaDoc name)
112    {
113       for (Iterator JavaDoc iter = servers.iterator(); iter.hasNext();)
114       {
115          Server server = (Server) iter.next();
116          if (server.getName().equals(name))
117          {
118             return server;
119          }
120       }
121       throw new IllegalArgumentException JavaDoc("There is no server named: " + name);
122    }
123
124    /**
125     * Start the named server.
126     *
127     * @param serverName
128     * @throws IOException
129     */

130    public void startServer(String JavaDoc serverName) throws IOException JavaDoc
131    {
132       ServerController.startServer(getServer(serverName), this);
133    }
134
135    /**
136     * Stop the named server
137     *
138     * @param name
139     * @throws IOException
140     */

141    public void stopServer(String JavaDoc name) throws IOException JavaDoc
142    {
143       ServerController.stopServer(getServer(name), this);
144    }
145
146    /**
147     * Get the jvm.
148     *
149     * @return the jvm.
150     * @throws IOException
151     */

152    public String JavaDoc getJavaExecutable() throws IOException JavaDoc
153    {
154       return new File JavaDoc(javaHome + File.separator + "bin",jvm).getCanonicalPath();
155    }
156
157    /**
158     * Set the jvm.
159     *
160     * @param jvm The jvm to set.
161     */

162    public void setJvm(String JavaDoc jvm)
163    {
164       this.jvm = jvm;
165    }
166
167    /**
168     * Set the javaHome.
169     *
170     * @param javaHome The javaHome to set.
171     */

172    public void setJavaHome(String JavaDoc javaHome)
173    {
174       this.javaHome = javaHome;
175    }
176
177    /**
178     * @param jbossHome The jBossHome to set.
179     */

180    public void setJbossHome(String JavaDoc jbossHome)
181    {
182       this.jbossHome = jbossHome;
183    }
184
185    /**
186     * The classpath to run the server
187     *
188     * @return the classpath as a string
189     */

190    protected String JavaDoc getStartClasspath()
191    {
192       File JavaDoc runjar = new File JavaDoc(jbossHome + "/bin/run.jar");
193       File JavaDoc javaJar = new File JavaDoc(javaHome + "/lib/tools.jar");
194       return runjar.toString() + File.pathSeparator + javaJar.toString();
195    }
196
197    /**
198     * The classpath used to stop the server.
199     *
200     * @return the string to put on the classpath
201     */

202    protected String JavaDoc getStopClasspath()
203    {
204       File JavaDoc shutdownJar = new File JavaDoc(jbossHome + "/bin/shutdown.jar");
205       File JavaDoc clientJar = new File JavaDoc(jbossHome + "/client/jbossall-client.jar");
206       File JavaDoc logJar = new File JavaDoc(jbossHome + "/client/jboss-common.jar");
207       return shutdownJar.toString() + File.pathSeparator + clientJar.toString()+ File.pathSeparator
208            + logJar.toString();
209    }
210
211    /**
212     * Get the jboss home.
213     *
214     * @return the home directory as a string
215     */

216    public String JavaDoc getJBossHome()
217    {
218       return jbossHome;
219    }
220
221    /**
222     * How long to wait (in seconds) for a server to shutdown
223     *
224     * @return the shutdown timeout
225     */

226    public int getShutdownTimeout()
227    {
228       return WAIT_TIME;
229    }
230
231    /**
232     * How long to wait (in seconds) for a server to startup
233     *
234     * @return the startup timeout
235     */

236    public int getStartupTimeout()
237    {
238       return START_TIME;
239    }
240
241    /**
242     * Get the udpGroup.
243     *
244     * @return the udpGroup.
245     */

246    protected String JavaDoc getUdpGroup()
247    {
248       return udpGroup;
249    }
250
251    /**
252     * Set the udpGroup.
253     *
254     * @param udpGroup The udpGroup to set.
255     */

256    public void setUdpGroup(String JavaDoc udpGroup)
257    {
258       this.udpGroup = udpGroup;
259    }
260    
261 }
Popular Tags