KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > resin > ResinRun


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

20 package org.apache.cactus.integration.ant.container.resin;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import org.apache.cactus.integration.ant.container.AbstractServerRun;
28
29 /**
30  * Starts/stop Resin by setting up a listener socket. Supports Resin 2.0.x,
31  * 2.1.x and 3.x.
32  *
33  * @version $Id: ResinRun.java,v 1.8 2004/04/18 12:21:50 vmassol Exp $
34  */

35 public class ResinRun extends AbstractServerRun
36 {
37     /**
38      * The started Resin server class. We use <code>Object</code> instead of
39      * the Resin class so that we don't need the Resin jars in the classpath
40      * to compile this class.
41      */

42     private Object JavaDoc resinServer;
43
44     /**
45      * @param theArgs the command line arguments
46      */

47     public ResinRun(String JavaDoc[] theArgs)
48     {
49         super(theArgs);
50     }
51
52     /**
53      * Entry point to start/stop the Resin server.
54      *
55      * @param theArgs the command line arguments
56      */

57     public static void main(String JavaDoc[] theArgs)
58     {
59         ResinRun resin = new ResinRun(theArgs);
60
61         resin.doRun();
62     }
63
64     /**
65      * Start the Resin server. We use reflection so that the Resin jars do not
66      * need to be in the classpath to compile this class.
67      *
68      * @see AbstractServerRun#doStartServer
69      */

70     protected final Thread JavaDoc doStartServer(String JavaDoc[] theArgs)
71     {
72         Thread JavaDoc runningThread = this;
73         
74         try
75         {
76             if (isResinVersion("2.0"))
77             {
78                 startResin20x(theArgs);
79             }
80             else if (isResinVersion("2.1"))
81             {
82                 startResin21x(theArgs);
83             }
84             else if (isResinVersion("3"))
85             {
86                 runningThread = startResin3x(theArgs);
87             }
88             else
89             {
90                 throw new RuntimeException JavaDoc("Unsupported Resin version ["
91                     + getResinVersion() + "]");
92             }
93         }
94         catch (Exception JavaDoc e)
95         {
96             e.printStackTrace();
97             throw new RuntimeException JavaDoc("Failed to start Resin server");
98         }
99
100         return runningThread;
101     }
102
103     /**
104      * Starts Resin 2.0.x
105      *
106      * @param theArgs the command line arguments for starting the server
107      * @throws Exception if an error happens when starting the server
108      */

109     private void startResin20x(String JavaDoc[] theArgs) throws Exception JavaDoc
110     {
111         Class JavaDoc resinClass =
112             Class.forName("com.caucho.server.http.ResinServer");
113         Constructor JavaDoc constructor = resinClass.getConstructor(
114             new Class JavaDoc[] {theArgs.getClass(), boolean.class});
115
116         this.resinServer = constructor.newInstance(
117             new Object JavaDoc[] {theArgs, Boolean.TRUE});
118     
119         Method JavaDoc initMethod = this.resinServer.getClass().getMethod("init",
120             new Class JavaDoc[] {boolean.class});
121
122         initMethod.invoke(this.resinServer, new Object JavaDoc[] {Boolean.TRUE});
123     }
124
125     /**
126      * Starts Resin 2.1.x
127      *
128      * @param theArgs the command line arguments for starting the server
129      * @throws Exception if an error happens when starting the server
130      */

131     private void startResin21x(String JavaDoc[] theArgs) throws Exception JavaDoc
132     {
133         Class JavaDoc resinClass =
134             Class.forName("com.caucho.server.http.ResinServer");
135         Constructor JavaDoc constructor = resinClass.getConstructor(
136             new Class JavaDoc[] {theArgs.getClass(), boolean.class});
137
138         this.resinServer = constructor.newInstance(
139             new Object JavaDoc[] {theArgs, Boolean.TRUE});
140         
141         Method JavaDoc initMethod = this.resinServer.getClass().getMethod("init",
142             new Class JavaDoc[] {ArrayList JavaDoc.class});
143
144         initMethod.invoke(this.resinServer, new Object JavaDoc[] {null});
145     }
146
147     /**
148      * Starts Resin 3.x
149      *
150      * @return the thread in which the server has been started
151      * @param theArgs the command line arguments for starting the server
152      * @throws Exception if an error happens when starting the server
153      */

154     private Thread JavaDoc startResin3x(final String JavaDoc[] theArgs) throws Exception JavaDoc
155     {
156         // Start the server in another thread so that it doesn't block
157
// the current thread. It seems that Resin 3.x is acting differently
158
// than Resin 2.x which was not blocking and thus which did not need
159
// to be started in a separate thread.
160
Thread JavaDoc startThread = new Thread JavaDoc()
161         {
162             public void run()
163             {
164                 try
165                 {
166                     Class JavaDoc resinClass =
167                         Class.forName("com.caucho.server.http.ResinServer");
168                     
169                     Method JavaDoc mainMethod = resinClass.getMethod("main",
170                         new Class JavaDoc[] {String JavaDoc[].class});
171                                 
172                     mainMethod.invoke(null, new Object JavaDoc[] {theArgs});
173                 }
174                 catch (Exception JavaDoc e)
175                 {
176                     e.printStackTrace();
177                     throw new RuntimeException JavaDoc(
178                         "Failed to start Resin 3.x. Error = ["
179                         + e.getMessage() + "]");
180                 }
181             }
182         };
183         startThread.start();
184         
185         return startThread;
186     }
187     
188     /**
189      * Stops the Resin server. We use reflection so that the Resin jars do not
190      * need to be in the classpath to compile this class.
191      *
192      * @see AbstractServerRun#doStopServer
193      */

194     protected final void doStopServer(String JavaDoc[] theArgs,
195         Thread JavaDoc theRunningServerThread)
196     {
197         try
198         {
199             if (isResinVersion("2.0"))
200             {
201                 stopResin20x(theArgs);
202             }
203             else if (isResinVersion("2.1"))
204             {
205                 stopResin20x(theArgs);
206             }
207             else if (isResinVersion("3"))
208             {
209                 stopResin3x(theArgs, theRunningServerThread);
210             }
211             else
212             {
213                 throw new RuntimeException JavaDoc("Unsupported Resin version ["
214                     + getResinVersion() + "]");
215             }
216         }
217         catch (Exception JavaDoc e)
218         {
219             e.printStackTrace();
220             throw new RuntimeException JavaDoc(
221                 "Failed to stop the running Resin server");
222         }
223     }
224     
225     /**
226      * Stops Resin 2.0.x and 2.1.x versions.
227      *
228      * @param theArgs the command line arguments for starting the server
229      * @throws Exception if an error happens when starting the server
230      */

231     private void stopResin20x(String JavaDoc[] theArgs) throws Exception JavaDoc
232     {
233         Method JavaDoc closeMethod = this.resinServer.getClass().getMethod(
234             "close", null);
235
236         closeMethod.invoke(this.resinServer, null);
237     }
238
239     /**
240      * Stops Resin 3.x.
241      *
242      * @param theArgs the command line arguments for starting the server
243      * @param theRunningServerThread the thread in which the server is running
244      * @throws Exception if an error happens when starting the server
245      */

246     private void stopResin3x(String JavaDoc[] theArgs,
247         Thread JavaDoc theRunningServerThread) throws Exception JavaDoc
248     {
249         // As we don't know how to properly stop a running Resin server,
250
// we simply try to kill the thread in which it is running.
251
// Not clean...
252
theRunningServerThread.stop();
253     }
254     
255     /**
256      * @return the Resin version
257      */

258     private String JavaDoc getResinVersion()
259     {
260         String JavaDoc version;
261         
262         try
263         {
264             Class JavaDoc versionClass = Class.forName("com.caucho.Version");
265             Field JavaDoc versionField = versionClass.getField("VERSION");
266             version = (String JavaDoc) versionField.get(null);
267         }
268         catch (Exception JavaDoc e)
269         {
270             throw new RuntimeException JavaDoc("Cannot get Resin version. Error = ["
271                 + e.getMessage() + "]");
272         }
273
274         return version;
275     }
276
277     /**
278      * @param theVersionPrefix the version prefix to test for
279      * @return true if the Resin version starts with versionPrefix
280      */

281     private boolean isResinVersion(String JavaDoc theVersionPrefix)
282     {
283         return getResinVersion().startsWith(theVersionPrefix);
284     }
285 }
286
Popular Tags