KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > apr > TomcatStarter


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

16  
17 package org.apache.jk.apr;
18
19 import java.lang.reflect.Method JavaDoc;
20
21 // Hack for Catalina 4.1 who hungs the calling thread.
22
// Also avoids delays in apache initialization ( tomcat can take a while )
23

24 /**
25  * Start some tomcat.
26  *
27  */

28 public class TomcatStarter implements Runnable JavaDoc {
29     Class JavaDoc c;
30     String JavaDoc args[];
31     AprImpl apr = new AprImpl();
32     
33     public static String JavaDoc mainClasses[]={ "org.apache.tomcat.startup.Main",
34                                          "org.apache.catalina.startup.BootstrapService",
35                                          "org.apache.catalina.startup.Bootstrap"};
36
37     // If someone has time - we can also guess the classpath and do other
38
// fancy guessings.
39

40     public static void main( String JavaDoc args[] ) {
41         System.err.println("TomcatStarter: main()");
42         int nClasses = 0;
43         
44         try {
45             AprImpl.jniMode();
46             // Find the class
47
Class JavaDoc c=null;
48             for( int i=0; i<mainClasses.length; i++ ) {
49                 try {
50                     System.err.println("Try " + mainClasses[i]);
51                     c=Class.forName( mainClasses[i] );
52                 } catch( ClassNotFoundException JavaDoc ex ) {
53                     continue;
54                 }
55                 if( c!= null ) {
56                     ++nClasses;
57                     Thread JavaDoc startThread=new Thread JavaDoc( new TomcatStarter(c, args));
58                     c=null;
59                     startThread.start();
60                     break;
61                 }
62             }
63             if (nClasses==0)
64                 System.err.println("No class found ");
65
66         } catch (Throwable JavaDoc t ) {
67             t.printStackTrace(System.err);
68         }
69     }
70
71     public TomcatStarter( Class JavaDoc c, String JavaDoc args[] ) {
72         this.c=c;
73         this.args=args;
74     }
75     
76     public void run() {
77         System.err.println("Starting " + c.getName());
78         try {
79             Class JavaDoc argClass=args.getClass();
80             Method JavaDoc m=c.getMethod( "main", new Class JavaDoc[] {argClass} );
81             m.invoke( c, new Object JavaDoc[] { args } );
82             System.out.println("TomcatStarter: Done");
83             if (apr.isLoaded())
84                 apr.jkSetAttribute(0, 0, "channel:jni", "done");
85             if (args[0].equals("stop")) {
86                   Thread.sleep(5000);
87                   Runtime.getRuntime().exit(0);
88             }
89         } catch( Throwable JavaDoc t ) {
90             t.printStackTrace(System.err);
91         }
92     }
93 }
94
Popular Tags