1 16 17 package org.apache.axis.client; 18 19 import org.apache.axis.utils.Messages; 20 import org.apache.axis.utils.ClassUtils; 21 22 import javax.xml.parsers.SAXParser ; 23 import javax.xml.parsers.SAXParserFactory ; 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.io.PrintStream ; 27 28 31 public class HappyClient { 32 33 PrintStream out; 34 35 public HappyClient(PrintStream out) { 36 this.out = out; 37 } 38 39 44 Class classExists(String classname) { 45 try { 46 return Class.forName(classname); 47 } catch (ClassNotFoundException e) { 48 return null; 49 } 50 } 51 52 57 boolean resourceExists(String resource) { 58 boolean found; 59 InputStream instream = ClassUtils.getResourceAsStream(this.getClass(),resource); 60 found = instream != null; 61 if (instream != null) { 62 try { 63 instream.close(); 64 } catch (IOException e) { 65 } 66 } 67 return found; 68 } 69 70 80 int probeClass( 81 String category, 82 String classname, 83 String jarFile, 84 String description, 85 String errorText, 86 String homePage) throws IOException { 87 String url = ""; 88 if (homePage != null) { 89 url=Messages.getMessage("happyClientHomepage",homePage); 90 } 91 String errorLine=""; 92 if (errorText != null) { 93 errorLine=Messages.getMessage(errorText); 94 } 95 try { 96 Class clazz = classExists(classname); 97 if (clazz == null) { 98 String text; 99 text=Messages.getMessage("happyClientMissingClass", 100 category,classname,jarFile); 101 out.println(text); 102 out.println(url); 103 return 1; 104 } else { 105 String location = getLocation(clazz); 106 String text; 107 if (location == null) { 108 text=Messages.getMessage("happyClientFoundDescriptionClass", 109 description,classname); 110 } else { 111 text = Messages.getMessage("happyClientFoundDescriptionClassLocation", 112 description, classname,location); 113 } 114 out.println(text); 115 return 0; 116 } 117 } catch (NoClassDefFoundError ncdfe) { 118 out.println(Messages.getMessage("happyClientNoDependency", 119 category, classname, jarFile)); 120 out.println(errorLine); 121 out.println(url); 122 out.println(ncdfe.getMessage()); 123 return 1; 124 } 125 } 126 127 132 133 String getLocation( 134 Class clazz) { 135 try { 136 java.net.URL url = clazz.getProtectionDomain().getCodeSource().getLocation(); 137 String location = url.toString(); 138 if (location.startsWith("jar")) { 139 url = ((java.net.JarURLConnection ) url.openConnection()).getJarFileURL(); 140 location = url.toString(); 141 } 142 143 if (location.startsWith("file")) { 144 java.io.File file = new java.io.File (url.getFile()); 145 return file.getAbsolutePath(); 146 } else { 147 return url.toString(); 148 } 149 } catch (Throwable t) { 150 } 151 return Messages.getMessage("happyClientUnknownLocation"); 152 } 153 154 163 int needClass( 164 String classname, 165 String jarFile, 166 String description, 167 String errorText, 168 String homePage) throws IOException { 169 return probeClass( 170 Messages.getMessage("happyClientError"), 171 classname, 172 jarFile, 173 description, 174 errorText, 175 homePage); 176 } 177 178 187 int wantClass( 188 String classname, 189 String jarFile, 190 String description, 191 String errorText, 192 String homePage) throws IOException { 193 return probeClass( 194 Messages.getMessage("happyClientWarning"), 195 classname, 196 jarFile, 197 description, 198 errorText, 199 homePage); 200 } 201 202 208 int wantResource( 209 String resource, 210 String errorText) throws Exception { 211 if (!resourceExists(resource)) { 212 out.println(Messages.getMessage("happyClientNoResource",resource)); 213 out.println(errorText); 214 return 0; 215 } else { 216 out.println(Messages.getMessage("happyClientFoundResource", resource)); 217 return 1; 218 } 219 } 220 221 222 226 private String getParserName() { 227 SAXParser saxParser = getSAXParser(); 228 if (saxParser == null) { 229 return Messages.getMessage("happyClientNoParser"); 230 } 231 232 String saxParserName = saxParser.getClass().getName(); 234 return saxParserName; 235 } 236 237 241 private SAXParser getSAXParser() { 242 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 243 if (saxParserFactory == null) { 244 return null; 245 } 246 SAXParser saxParser = null; 247 try { 248 saxParser = saxParserFactory.newSAXParser(); 249 } catch (Exception e) { 250 } 251 return saxParser; 252 } 253 254 258 259 private String getParserLocation() { 260 SAXParser saxParser = getSAXParser(); 261 if (saxParser == null) { 262 return null; 263 } 264 String location = getLocation(saxParser.getClass()); 265 return location; 266 } 267 268 273 public int getJavaVersionNumber() { 274 int javaVersionNumber=10; 281 try { 282 Class.forName("java.lang.Void"); 283 javaVersionNumber++; 284 Class.forName("java.lang.ThreadLocal"); 285 javaVersionNumber++; 286 Class.forName("java.lang.StrictMath"); 287 javaVersionNumber++; 288 Class.forName("java.lang.CharSequence"); 289 javaVersionNumber++; 290 } catch (Throwable t) { 291 } 294 return javaVersionNumber; 295 } 296 297 298 private void title(String title) { 299 out.println(); 300 String message=Messages.getMessage(title); 301 out.println(message); 302 for(int i=0;i< message.length();i++) { 304 out.print("="); 305 } 306 out.println(); 307 } 308 314 public boolean verifyClientIsHappy(boolean warningsAsErrors) throws IOException { 315 int needed = 0,wanted = 0; 316 out.println(); 317 title("happyClientTitle"); 318 title("happyClientNeeded"); 319 320 323 needed = needClass("javax.xml.soap.SOAPMessage", 324 "saaj.jar", 325 "SAAJ", 326 "happyClientNoAxis", 327 "http://xml.apache.org/axis/"); 328 329 needed += needClass("javax.xml.rpc.Service", 330 "jaxrpc.jar", 331 "JAX-RPC", 332 "happyClientNoAxis", 333 "http://xml.apache.org/axis/"); 334 335 needed += needClass("org.apache.commons.discovery.Resource", 336 "commons-discovery.jar", 337 "Jakarta-Commons Discovery", 338 "happyClientNoAxis", 339 "http://jakarta.apache.org/commons/discovery.html"); 340 341 needed += needClass("org.apache.commons.logging.Log", 342 "commons-logging.jar", 343 "Jakarta-Commons Logging", 344 "happyClientNoAxis", 345 "http://jakarta.apache.org/commons/logging.html"); 346 347 needed += needClass("org.apache" + ".log" +"4j" +".Layout", 349 "log4"+"j-1.2.4.jar", 350 "Log4"+"j", 351 "happyClientNoLog4J", 352 "http://jakarta.apache.org/log"+"4j"); 353 354 needed += needClass("com.ibm.wsdl.factory.WSDLFactoryImpl", 357 "wsdl4j.jar", 358 "WSDL4Java", 359 "happyClientNoAxis", 360 null); 361 362 needed += needClass("javax.xml.parsers.SAXParserFactory", 363 "xerces.jar", 364 "JAXP", 365 "happyClientNoAxis", 366 "http://xml.apache.org/xerces-j/"); 367 368 369 title("happyClientOptional"); 370 371 wanted += wantClass("javax.mail.internet.MimeMessage", 372 "mail.jar", 373 "Mail", 374 "happyClientNoAttachments", 375 "http://java.sun.com/products/javamail/"); 376 377 wanted += wantClass("javax.activation.DataHandler", 378 "activation.jar", 379 "Activation", 380 "happyClientNoAttachments", 381 "http://java.sun.com/products/javabeans/glasgow/jaf.html"); 382 383 wanted += wantClass("org.apache.xml.security.Init", 384 "xmlsec.jar", 385 "XML Security", 386 "happyClientNoSecurity", 387 "http://xml.apache.org/security/"); 388 389 wanted += wantClass("javax.net.ssl.SSLSocketFactory", 390 Messages.getMessage("happyClientJSSEsources"), 391 "Java Secure Socket Extension", 392 "happyClientNoHTTPS", 393 "http://java.sun.com/products/jsse/"); 394 395 396 399 int warningMessages=0; 400 401 String xmlParser = getParserName(); 402 String xmlParserLocation = getParserLocation(); 403 out.println(Messages.getMessage("happyClientXMLinfo", 404 xmlParser,xmlParserLocation)); 405 if (xmlParser.indexOf("xerces") <= 0) { 406 warningMessages++; 407 out.println(); 408 out.println(Messages.getMessage("happyClientRecommendXerces")); 409 } 410 if (getJavaVersionNumber() < 13) { 411 warningMessages++; 412 out.println(); 413 out.println(Messages.getMessage("happyClientUnsupportedJVM")); 414 } 415 416 417 boolean happy; 419 title("happyClientSummary"); 420 421 if (needed == 0) { 423 out.println(Messages.getMessage("happyClientCorePresent")); 425 happy=true; 426 } else { 427 happy=false; 428 out.println(Messages.getMessage("happyClientCoreMissing", 430 Integer.toString(needed))); 431 } 432 if (wanted > 0) { 434 out.println(); 435 out.println(Messages.getMessage("happyClientOptionalMissing", 436 Integer.toString(wanted))); 437 out.println(Messages.getMessage("happyClientOptionalOK")); 438 if (warningsAsErrors) { 439 happy = false; 440 } 441 } else { 442 out.println(Messages.getMessage("happyClientOptionalPresent")); 443 } 444 if (warningMessages > 0) { 445 out.println(Messages.getMessage("happyClientWarningMessageCount", 446 Integer.toString(warningMessages))); 447 if (warningsAsErrors) { 448 happy = false; 449 } 450 } 451 452 return happy; 453 } 454 455 460 public static void main(String args[]) { 461 boolean isHappy = isClientHappy(args); 462 System.exit(isHappy?0:-1); 463 } 464 465 471 private static boolean isClientHappy(String [] args) { 472 HappyClient happy=new HappyClient(System.out); 473 boolean isHappy; 474 int missing=0; 475 try { 476 isHappy = happy.verifyClientIsHappy(false); 477 for(int i=0;i<args.length;i++) { 478 missing+=happy.probeClass( 479 "argument", 480 args[i], 481 null, 482 null, 483 null, 484 null 485 ); 486 } 487 if(missing>0) { 488 isHappy=false; 489 } 490 } catch (IOException e) { 491 e.printStackTrace(); 492 isHappy=false; 493 } 494 return isHappy; 495 } 496 } 497 | Popular Tags |