KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > util > Bootstrapper


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.util;
20
21 import java.lang.reflect.Method JavaDoc;
22
23 import alt.jiapi.InstrumentationContext;
24
25 /**
26  * Bootstrapper which can be used to launch applications based on Jiapi.
27  * Bootstrapper is needed so that the classes are loaded to same name
28  * space. There's two phases during the bootstrap process:
29  *
30  * <ul>
31  * <li><b>1. bootstrap Jiapi application</b></li>
32  * During the 1. phase an application written on Jiapi framework is
33  * bootstrapped. It is loaded with a Jiapi compatible class loader
34  * (e.g. alt.jiapi.util.InstrumentingClassLoader) and called through
35  * Java reflection.
36  *
37  * <li><b>2. launch target application</b></li>
38  * The Jiapi application which was bootstrapped in phase 1. usually
39  * does something for a target application. Bootstrapper.launch is
40  * a utility method which can be used launch a target application so that
41  * the classes are loaded to a proper namespace.
42  * </ul>
43  *
44  * @author Mika Riekkinen
45  * @author Joni Suominen
46  * @version $Revision: 1.4 $ $Date: 2004/08/10 08:32:45 $
47  */

48 public class Bootstrapper {
49     public Bootstrapper(String JavaDoc className, String JavaDoc methodName,
50                         Class JavaDoc []parameterTypes, Object JavaDoc []args,
51                         ClassLoader JavaDoc classLoader) {
52         bootstrap(className, methodName, parameterTypes, args, classLoader);
53     }
54
55     /**
56      * This method is used to bootstrap an application. The command-line
57      * entry point uses this method to start a Jiapi application.
58      *
59      * @param className a fully qualified name of a class to bootstrap
60      * @param methodName name of a method to call (e.g. "main")
61      * @param parameterTypes parameter types of a method
62      * @param args arguments for a method
63      * @param classLoader to be used, the given classloader should
64      * implement setContext method
65      */

66     public void bootstrap(String JavaDoc className, String JavaDoc methodName,
67                           Class JavaDoc []parameterTypes, Object JavaDoc []args,
68                           ClassLoader JavaDoc classLoader) {
69         try {
70             Class JavaDoc clazz = classLoader.loadClass(className);
71             Method JavaDoc method = clazz.getMethod(methodName, parameterTypes);
72             Object JavaDoc obj = clazz.newInstance();
73             method.invoke(obj, new Object JavaDoc [] { args });
74         } catch (Exception JavaDoc e) {
75             e.printStackTrace();
76         }
77     }
78
79     /**
80      * A utility method which is usually used from Jiapi applications
81      * to launch a target application for instrumentation. It ensures
82      * that the classes are properly loaded to a same name space. The
83      * target classe's main(String args[]) method is called.
84      *
85      * @param className a fully qualified name of a class to bootstrap
86      * @param args arguments for a method
87      * @param classLoader which loaded the calling application
88      */

89     public static void launch(String JavaDoc className, Object JavaDoc []args,
90                               InstrumentationContext ctx,
91                               ClassLoader JavaDoc classLoader) {
92
93         Class JavaDoc []types = new Class JavaDoc[] { String JavaDoc[].class };
94         launch(className, "main", types, args, ctx, classLoader);
95     }
96
97     /**
98      * A utility method which is usually used from Jiapi applications
99      * to launch a target application for instrumentation. It ensures
100      * that the classes are properly loaded to a same name space.
101      *
102      * @param className a fully qualified name of a class to bootstrap
103      * @param methodName name of a method to call (e.g. "main")
104      * @param parameterTypes parameter types of a method
105      * @param args arguments for a method
106      * @param classLoader which loaded the calling application
107      */

108     public static void launch(String JavaDoc className, String JavaDoc methodName,
109                               Class JavaDoc []parameterTypes, Object JavaDoc []args,
110                               InstrumentationContext ctx,
111                               ClassLoader JavaDoc classLoader) {
112         try {
113             // Set the context for ClassLoader.
114
Method JavaDoc setContext = classLoader.getClass().getMethod("setContext",
115                                                                  new Class JavaDoc [] { ctx.getClass() });
116             setContext.invoke(classLoader, new Object JavaDoc [] { ctx });
117         }
118         catch (Exception JavaDoc e) {
119             e.printStackTrace();
120             System.err.println("\n" + e.getClass() + ": " + e.getMessage());
121             System.err.println("\nYou are propably trying to launch an application without properly bootstrapping it. A recommended way is to use utility alt.jiapi.util.Bootstrapper. At your shell prompt say:\njava alt.jiapi.util.Bootstrapper org.your.App [params]\nThat will launch your application so that the classes needed for instrumentation are loaded to a same namespace. There's also a script which can be used for bootstrapping:\njiapi.sh org.your.App [params]\n\n");
122             return;
123         }
124
125         try {
126             // Launch the target application:
127
Class JavaDoc clazz = classLoader.loadClass(className);
128             Method JavaDoc method = clazz.getMethod(methodName, parameterTypes);
129
130             // commented newInstance call, assume static call to main()
131
//Object obj = clazz.newInstance();
132

133             if (args == null) {
134                 args = new String JavaDoc[0];
135             }
136
137 // method.invoke(obj, new Object[] { args });
138
method.invoke(null, new Object JavaDoc[] { args });
139         } catch (Exception JavaDoc e) {
140             e.printStackTrace();
141         }
142     }
143
144     /**
145      * A command-line entry point.
146      */

147     public static void main(String JavaDoc args[]) {
148         // The first argument is class name, the rest are String args.
149
String JavaDoc [] _args = new String JavaDoc[args.length - 1];
150         System.arraycopy(args, 1, _args, 0, _args.length);
151         
152         ClassLoader JavaDoc classLoader = InstrumentingClassLoader.createClassLoader();
153         Class JavaDoc []types = new Class JavaDoc[] { _args.getClass() };
154         new Bootstrapper(args[0], "main", types, _args, classLoader);
155     }
156 }
157
Popular Tags