1 45 package org.openejb.alt.config; 46 47 import java.io.InputStream ; 48 import java.io.File ; 49 import java.net.URL ; 50 import java.net.URLClassLoader ; 51 import java.net.MalformedURLException ; 52 import java.util.Properties ; 53 import java.util.Vector ; 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 68 public class EjbValidator { 69 private static final String 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 sets = new Vector (); 80 81 82 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 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 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 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 241 242 243 private static void printVersion() { 244 247 Properties versionInfo = new Properties (); 248 249 try { 250 JarUtils.setHandlerSystemProperty(); 251 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 252 } catch (java.io.IOException 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 header = "OpenEJB EJB Validation Tool "; 261 try { 262 JarUtils.setHandlerSystemProperty(); 263 Properties versionInfo = new Properties (); 264 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 265 header += versionInfo.get( "version" ); 266 } catch (java.io.IOException e) { 267 } 268 269 System.out.println( header ); 270 271 try { 273 InputStream 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 e) { 281 } 282 } 283 284 private static void printExamples() { 285 String header = "OpenEJB EJB Validation Tool "; 286 try { 287 JarUtils.setHandlerSystemProperty(); 288 Properties versionInfo = new Properties (); 289 versionInfo.load( new URL ( "resource:/openejb-version.properties" ).openConnection().getInputStream() ); 290 header += versionInfo.get( "version" ); 291 } catch (java.io.IOException e) { 292 } 293 294 System.out.println( header ); 295 296 try { 298 InputStream 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 e) { 306 } 307 } 308 309 310 public static void main(String args[]) { 311 try{ 312 File directory = SystemInstance.get().getHome().getDirectory("lib"); 313 SystemInstance.get().getClassPath().addJarsToPath(directory); 314 File directory1 = SystemInstance.get().getHome().getDirectory("dist"); 315 SystemInstance.get().getClassPath().addJarsToPath(directory1); 316 } catch (Exception e){ 317 } 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 for (; i < args.length; i++){ 349 try{ 350 EjbJarUtils ejbJarUtils = new EjbJarUtils(args[i]); 351 String jarLocation = ejbJarUtils.getJarLocation(); 352 ClassLoader classLoader = null; 353 try { 354 File jarFile = new File (jarLocation); 355 URL [] classpath = new URL []{jarFile.toURL()}; 356 classLoader = new URLClassLoader (classpath, EjbValidator.class.getClassLoader()); 357 } catch (MalformedURLException 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 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 e ) { 378 System.out.println(e.getMessage()); 379 } 381 } 382 383 } | Popular Tags |