KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > EjbValidator


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: EjbValidator.java 2230 2005-10-11 03:14:03Z jcscoobyrs $
44  */

45 package org.openejb.alt.config;
46
47 import java.io.InputStream JavaDoc;
48 import java.io.File JavaDoc;
49 import java.net.URL JavaDoc;
50 import java.net.URLClassLoader JavaDoc;
51 import java.net.MalformedURLException JavaDoc;
52 import java.util.Properties JavaDoc;
53 import java.util.Vector JavaDoc;
54
55 import org.openejb.OpenEJBException;
56 import org.openejb.loader.SystemInstance;
57 import org.openejb.alt.config.ejb11.EjbJar;
58 import org.openejb.alt.config.rules.CheckClasses;
59 import org.openejb.alt.config.rules.CheckMethods;
60 import org.openejb.util.JarUtils;
61 import org.openejb.util.Messages;
62 import org.openejb.util.Logger;
63
64
65 /**
66  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
67  */

68 public class EjbValidator {
69     private static final String JavaDoc helpBase = "META-INF/org.openejb.cli/";
70
71     protected static final Messages _messages = new Messages( "org.openejb.util.resources" );
72
73     int LEVEL = 2;
74     boolean PRINT_DETAILS = false;
75     boolean PRINT_XML = false;
76     boolean PRINT_WARNINGS = true;
77     boolean PRINT_COUNT = false;
78
79     private Vector JavaDoc sets = new Vector JavaDoc();
80
81     /*------------------------------------------------------*/
82     /* Constructors */
83     /*------------------------------------------------------*/
84     public EjbValidator() throws OpenEJBException {
85         JarUtils.setHandlerSystemProperty();
86     }
87
88     public void addEjbSet(EjbSet set){
89         sets.add( set );
90     }
91
92     public EjbSet[] getEjbSets(){
93         EjbSet[] ejbSets = new EjbSet[sets.size()];
94         sets.copyInto(ejbSets);
95         return ejbSets;
96     }
97
98     public EjbSet validateJar(EjbJarUtils ejbJarUtils, ClassLoader JavaDoc classLoader){
99         EjbSet set = null;
100
101         try {
102             set = new EjbSet(ejbJarUtils.getJarLocation(), ejbJarUtils.getEjbJar(), ejbJarUtils.getBeans(), classLoader);
103             ValidationRule[] rules = getValidationRules();
104             for (int i=0; i < rules.length; i++){
105                 rules[i].validate( set );
106             }
107         } catch ( Throwable JavaDoc e ) {
108             e.printStackTrace(System.out);
109             ValidationError err = new ValidationError( "cannot.validate" );
110             err.setDetails( e.getMessage() );
111             set.addError( err );
112         }
113         return set;
114     }
115
116     protected ValidationRule[] getValidationRules(){
117         ValidationRule[] rules = new ValidationRule[]{
118             new CheckClasses(),
119             new CheckMethods()
120         };
121         return rules;
122     }
123
124     //Validate all classes are present
125
//Validate classes are the correct type
126
//Validate ejb references
127
//Validate resource references
128
//Validate security references
129

130
131     public void printResults(EjbSet set){
132         if (!set.hasErrors() && !set.hasFailures() && (!PRINT_WARNINGS || !set.hasWarnings())){
133             return;
134         }
135         System.out.println("------------------------------------------");
136         System.out.println("JAR "+ set.getJarPath() );
137         System.out.println(" ");
138
139         printValidationExceptions( set.getErrors() );
140         printValidationExceptions( set.getFailures() );
141
142         if (PRINT_WARNINGS) {
143             printValidationExceptions( set.getWarnings() );
144         }
145     }
146
147     protected void printValidationExceptions(ValidationException[] exceptions ) {
148         for (int i=0; i < exceptions.length; i++){
149             System.out.print(" ");
150             System.out.print(exceptions[i].getPrefix() );
151             System.out.print(" ... ");
152             if (!(exceptions[i] instanceof ValidationError)) {
153                 System.out.print(exceptions[i].getBean().getEjbName());
154                 System.out.print(": ");
155             }
156             if (LEVEL > 2) {
157                 System.out.println(exceptions[i].getMessage(1));
158                 System.out.println();
159                 System.out.print('\t');
160                 System.out.println(exceptions[i].getMessage(LEVEL));
161                 System.out.println();
162             } else {
163                 System.out.println(exceptions[i].getMessage(LEVEL));
164             }
165         }
166         if (PRINT_COUNT && exceptions.length > 0) {
167             System.out.println();
168             System.out.print(" "+exceptions.length+" ");
169             System.out.println(exceptions[0].getCategory() );
170             System.out.println();
171         }
172
173     }
174     public void printResultsXML(EjbSet set){
175         if (!set.hasErrors() && !set.hasFailures() && (!PRINT_WARNINGS || !set.hasWarnings())){
176             return;
177         }
178
179         System.out.println("<jar>");
180         System.out.print(" <path>");
181         System.out.print(set.getJarPath());
182         System.out.println("</path>");
183
184         printValidationExceptionsXML( set.getErrors() );
185         printValidationExceptionsXML( set.getFailures() );
186
187         if (PRINT_WARNINGS) {
188             printValidationExceptionsXML( set.getWarnings() );
189         }
190         System.out.println("</jar>");
191     }
192
193     protected void printValidationExceptionsXML(ValidationException[] exceptions ) {
194         for (int i=0; i < exceptions.length; i++){
195             System.out.print(" <");
196             System.out.print(exceptions[i].getPrefix() );
197             System.out.println(">");
198             if (!(exceptions[i] instanceof ValidationError)) {
199                 System.out.print(" <ejb-name>");
200                 System.out.print(exceptions[i].getBean().getEjbName());
201                 System.out.println("</ejb-name>");
202             }
203             System.out.print(" <summary>");
204             System.out.print(exceptions[i].getMessage(1));
205             System.out.println("</summary>");
206             System.out.println(" <description><![CDATA[");
207             System.out.println( exceptions[i].getMessage(3) );
208             System.out.println("]]></description>");
209             System.out.print(" </");
210             System.out.print(exceptions[i].getPrefix() );
211             System.out.println(">");
212         }
213     }
214
215     public void displayResults(EjbSet[] sets){
216         if (PRINT_XML) {
217             System.out.println("<results>");
218             for (int i=0; i < sets.length; i++){
219                 printResultsXML( sets[i] );
220             }
221             System.out.println("</results>");
222         } else {
223             for (int i=0; i < sets.length; i++){
224                 printResults( sets[i] );
225             }
226             for (int i=0; i < sets.length; i++){
227                 if (sets[i].hasErrors() || sets[i].hasFailures()){
228                     if (LEVEL < 3) {
229                         System.out.println();
230                         System.out.println("For more details, use the -vvv option");
231                     }
232                     i = sets.length;
233                 }
234             }
235         }
236
237     }
238
239     /*------------------------------------------------------*/
240     /* Static methods */
241     /*------------------------------------------------------*/
242
243     private static void printVersion() {
244         /*
245          * Output startup message
246          */

247         Properties JavaDoc versionInfo = new Properties JavaDoc();
248
249         try {
250             JarUtils.setHandlerSystemProperty();
251             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
252         } catch (java.io.IOException JavaDoc e) {
253         }
254
255         System.out.println( "OpenEJB EJB Validation Tool " + versionInfo.get( "version" ) +" build: "+versionInfo.get( "date" )+"-"+versionInfo.get( "time" ));
256         System.out.println( "" + versionInfo.get( "url" ) );
257     }
258
259     private static void printHelp() {
260         String JavaDoc header = "OpenEJB EJB Validation Tool ";
261         try {
262             JarUtils.setHandlerSystemProperty();
263             Properties JavaDoc versionInfo = new Properties JavaDoc();
264             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
265             header += versionInfo.get( "version" );
266         } catch (java.io.IOException JavaDoc e) {
267         }
268
269         System.out.println( header );
270
271         // Internationalize this
272
try {
273             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResource(helpBase + "validate.help").openConnection().getInputStream();
274
275             int b = in.read();
276             while (b != -1) {
277                 System.out.write( b );
278                 b = in.read();
279             }
280         } catch (java.io.IOException JavaDoc e) {
281         }
282     }
283
284     private static void printExamples() {
285         String JavaDoc header = "OpenEJB EJB Validation Tool ";
286         try {
287             JarUtils.setHandlerSystemProperty();
288             Properties JavaDoc versionInfo = new Properties JavaDoc();
289             versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
290             header += versionInfo.get( "version" );
291         } catch (java.io.IOException JavaDoc e) {
292         }
293
294         System.out.println( header );
295
296         // Internationalize this
297
try {
298             InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResource(helpBase + "validate.examples").openConnection().getInputStream();
299
300             int b = in.read();
301             while (b != -1) {
302                 System.out.write( b );
303                 b = in.read();
304             }
305         } catch (java.io.IOException JavaDoc e) {
306         }
307     }
308
309
310     public static void main(String JavaDoc args[]) {
311         try{
312             File JavaDoc directory = SystemInstance.get().getHome().getDirectory("lib");
313             SystemInstance.get().getClassPath().addJarsToPath(directory);
314             File JavaDoc directory1 = SystemInstance.get().getHome().getDirectory("dist");
315             SystemInstance.get().getClassPath().addJarsToPath(directory1);
316         } catch (Exception JavaDoc e){
317             // ignore it
318
}
319         Logger.initialize( System.getProperties() );
320         
321         try {
322             EjbValidator v = new EjbValidator();
323
324             if (args.length == 0) {
325                 printHelp();
326                 return;
327             }
328
329             for (int i=0; i < args.length; i++){
330                 if (args[i].equals("-v")){
331                     v.LEVEL = 1;
332                 } else if (args[i].equals("-vv")){
333                     v.LEVEL = 2;
334                 } else if (args[i].equals("-vvv")){
335                     v.LEVEL = 3;
336                 } else if (args[i].equals("-nowarn")){
337                     v.PRINT_WARNINGS = false;
338                 } else if (args[i].equals("-xml")){
339                     v.PRINT_XML = true;
340                 } else if (args[i].equals("--help")){
341                     printHelp();
342                 } else if (args[i].equals("-examples")){
343                     printExamples();
344                 } else if (args[i].equals("-version")){
345                     printVersion();
346                 } else {
347                     // We must have reached the jar list
348
for (; i < args.length; i++){
349                         try{
350                             EjbJarUtils ejbJarUtils = new EjbJarUtils(args[i]);
351                             String JavaDoc jarLocation = ejbJarUtils.getJarLocation();
352                             ClassLoader JavaDoc classLoader = null;
353                             try {
354                                 File JavaDoc jarFile = new File JavaDoc(jarLocation);
355                                 URL JavaDoc[] classpath = new URL JavaDoc[]{jarFile.toURL()};
356                                 classLoader = new URLClassLoader JavaDoc(classpath, EjbValidator.class.getClassLoader());
357                             } catch (MalformedURLException JavaDoc e) {
358                                 throw new OpenEJBException("Unable to create a classloader to load classes from '"+jarLocation+"'", e);
359                             }
360                             EjbSet set = v.validateJar( ejbJarUtils, classLoader);
361                             v.addEjbSet( set );
362                        } catch (Exception JavaDoc e){
363                            e.printStackTrace();
364                        }
365                     }
366                 }
367             }
368
369             EjbSet[] sets = v.getEjbSets();
370             v.displayResults(sets);
371
372             for (int i=0; i < sets.length; i++){
373                 if (sets[i].hasErrors() || sets[i].hasFailures()){
374                     System.exit(1);
375                 }
376             }
377         } catch ( Exception JavaDoc e ) {
378             System.out.println(e.getMessage());
379             //e.printStackTrace();
380
}
381     }
382
383 }
Popular Tags