KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > Run


1 /*
2 Copyright (c) 2003-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29
30 package org.jibx.binding;
31
32 import java.io.BufferedReader JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.InputStreamReader JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 import org.jibx.runtime.JiBXException;
42
43 /**
44  * Bind-on-load class runner. This uses a binding loader to compile a binding,
45  * then loads and calls the main execution class for an application substituting
46  * the classes modified by the binding.
47  *
48  * @author Dennis M. Sosnoski
49  * @version 1.0
50  */

51  
52 public class Run
53 {
54     private static final String JavaDoc BINDING_LIST_RESOURCE = "jibx_bindings.txt";
55     private static final String JavaDoc DEFAULT_BINDING_RESOURCE = "jibx_binding.xml";
56     
57     private Run() {}
58     
59     /**
60      * Accumulate list of bindings from stream.
61      *
62      * @param is stream to be read for list of bindings (one per line)
63      * @param bindings accumulated collection of bindings
64      */

65     private static void addBindings(InputStream JavaDoc is, ArrayList JavaDoc bindings)
66         throws IOException JavaDoc {
67         BufferedReader JavaDoc rdr = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
68         String JavaDoc line;
69         while ((line = rdr.readLine()) != null) {
70             if (line.length() > 0) {
71                 bindings.add(line);
72             }
73         }
74     }
75     
76     /**
77      * Main method for bind-on-load handling.
78      *
79      * @param args command line arguments
80      */

81     public static void main(String JavaDoc[] args) {
82         if (args.length >= 1) {
83             try {
84             
85                 // first get binding definitions and target class information
86
ArrayList JavaDoc files = new ArrayList JavaDoc();
87                 ArrayList JavaDoc resources = new ArrayList JavaDoc();
88                 int index = 0;
89                 String JavaDoc target = null;
90                 while (index < args.length) {
91                     String JavaDoc arg = args[index++];
92                     if ("-b".equals(arg)) {
93                         if (index < args.length) {
94                             files.add(args[index++]);
95                         } else {
96                             System.err.println("Missing binding file and " +
97                                 "target class following '-b'");
98                         }
99                     } else if ("-l".equals(arg)) {
100                         if (index < args.length) {
101                             FileInputStream JavaDoc is =
102                                 new FileInputStream JavaDoc(args[index++]);
103                             addBindings(is, files);
104                             is.close();
105                         } else {
106                             System.err.println("Missing binding list file " +
107                                 "and target class following '-l'");
108                         }
109                     } else if ("-r".equals(arg)) {
110                         if (index < args.length) {
111                             resources.add(args[index++]);
112                         } else {
113                             System.err.println("Missing binding resource and " +
114                                 "target class following '-r'");
115                         }
116                     } else {
117                         target = arg;
118                         break;
119                     }
120                 }
121                 
122                 // make sure we have a target class name
123
if (target != null) {
124                     
125                     // save class name and create loader
126
Loader loader = new Loader();
127                 
128                     // check binding resources if no specified bindings
129
if (files.size() == 0 && resources.size() == 0) {
130                         InputStream JavaDoc is =
131                             loader.getResourceAsStream(BINDING_LIST_RESOURCE);
132                         if (is == null) {
133                             String JavaDoc name = target.replace('.', '/') + "_" +
134                                 BINDING_LIST_RESOURCE;
135                             is = loader.getResourceAsStream(name);
136                         }
137                         if (is != null) {
138                             addBindings(is, resources);
139                             is.close();
140                         } else {
141                             String JavaDoc name = DEFAULT_BINDING_RESOURCE;
142                             is = loader.getResourceAsStream(name);
143                             if (is == null) {
144                                 name = target.replace('.', '/') + "_" +
145                                     DEFAULT_BINDING_RESOURCE;
146                                 is = loader.getResourceAsStream(name);
147                             }
148                             if (is != null) {
149                                 resources.add(name);
150                                 is.close();
151                             }
152                             
153                         }
154                     }
155                     
156                     // make sure at least one binding has been specified
157
if (files.size() == 0 && resources.size() == 0) {
158                         System.err.println("No bindings found");
159                     } else {
160                 
161                         // compile all bindings
162
for (int i = 0; i < files.size(); i++) {
163                             loader.loadFileBinding((String JavaDoc)files.get(i));
164                         }
165                         for (int i = 0; i < resources.size(); i++) {
166                             String JavaDoc path = (String JavaDoc)resources.get(i);
167                             String JavaDoc fname = path;
168                             int split = fname.lastIndexOf('/');
169                             if (split >= 0) {
170                                 fname = fname.substring(split+1);
171                             }
172                             String JavaDoc bname = fname;
173                             split = bname.lastIndexOf('.');
174                             if (split >= 0) {
175                                 bname = bname.substring(0, split);
176                             }
177                             InputStream JavaDoc is = loader.getResourceAsStream(path);
178                             if (is == null) {
179                                 throw new IOException JavaDoc("Resource " + path +
180                                     " not found on classpath");
181                             }
182                             loader.loadBinding(fname,
183                                 Utility.convertName(bname), is, null);
184                         }
185                         loader.processBindings();
186                 
187                         // load the target class using custom class loader
188
Class JavaDoc clas = loader.loadClass(target);
189                     
190                         // invoke the "main" method of the application class
191
Class JavaDoc[] ptypes = new Class JavaDoc[] { args.getClass() };
192                         Method JavaDoc main = clas.getDeclaredMethod("main", ptypes);
193                         String JavaDoc[] pargs = new String JavaDoc[args.length-index];
194                         System.arraycopy(args, index, pargs, 0, pargs.length);
195                         Thread.currentThread().setContextClassLoader(loader);
196                         main.invoke(null, new Object JavaDoc[] { pargs });
197                         
198                     }
199                 }
200                 
201             } catch (ClassNotFoundException JavaDoc e) {
202                 e.printStackTrace();
203             } catch (NoSuchMethodException JavaDoc e) {
204                 e.printStackTrace();
205             } catch (IllegalArgumentException JavaDoc e) {
206                 e.printStackTrace();
207             } catch (IllegalAccessException JavaDoc e) {
208                 e.printStackTrace();
209             } catch (InvocationTargetException JavaDoc e) {
210                 e.printStackTrace();
211             } catch (JiBXException e) {
212                 e.printStackTrace();
213             } catch (IOException JavaDoc e) {
214                 e.printStackTrace();
215             }
216             
217         } else {
218             System.out.println
219                 ("Usage: org.jibx.binding.Run [-b binding-file][-l list-file]" +
220                     "[-r binding-resource] main-class args...");
221         }
222     }
223 }
Popular Tags