HappyClientpublic class HappyClient extends Object Client side equivalent of happyaxis |
Fields Summary |
---|
PrintStream | out |
Constructors Summary |
---|
public HappyClient(PrintStream out)
this.out = out;
|
Methods Summary |
---|
java.lang.Class | classExists(java.lang.String classname)test for a class existing
try {
return Class.forName(classname);
} catch (ClassNotFoundException e) {
return null;
}
| public int | getJavaVersionNumber()calculate the java version number by probing for classes;
this tactic works across many jvm implementations; taken from Ant.
// Determine the Java version by looking at available classes
// java.lang.CharSequence was introduced in JDK 1.4
// java.lang.StrictMath was introduced in JDK 1.3
// java.lang.ThreadLocal was introduced in JDK 1.2
// java.lang.Void was introduced in JDK 1.1
// Count up version until a NoClassDefFoundError ends the try
int javaVersionNumber=10;
try {
Class.forName("java.lang.Void");
javaVersionNumber++;
Class.forName("java.lang.ThreadLocal");
javaVersionNumber++;
Class.forName("java.lang.StrictMath");
javaVersionNumber++;
Class.forName("java.lang.CharSequence");
javaVersionNumber++;
} catch (Throwable t) {
// swallow as we've hit the max class version that
// we have
}
return javaVersionNumber;
| java.lang.String | getLocation(java.lang.Class clazz)get the location of a class
try {
java.net.URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
String location = url.toString();
if (location.startsWith("jar")) {
url = ((java.net.JarURLConnection) url.openConnection()).getJarFileURL();
location = url.toString();
}
if (location.startsWith("file")) {
java.io.File file = new java.io.File(url.getFile());
return file.getAbsolutePath();
} else {
return url.toString();
}
} catch (Throwable t) {
}
return Messages.getMessage("happyClientUnknownLocation");
| private java.lang.String | getParserLocation()get the location of the parser
SAXParser saxParser = getSAXParser();
if (saxParser == null) {
return null;
}
String location = getLocation(saxParser.getClass());
return location;
| private java.lang.String | getParserName()what parser are we using.
SAXParser saxParser = getSAXParser();
if (saxParser == null) {
return Messages.getMessage("happyClientNoParser");
}
// check to what is in the classname
String saxParserName = saxParser.getClass().getName();
return saxParserName;
| private javax.xml.parsers.SAXParser | getSAXParser()Create a JAXP SAXParser
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
if (saxParserFactory == null) {
return null;
}
SAXParser saxParser = null;
try {
saxParser = saxParserFactory.newSAXParser();
} catch (Exception e) {
}
return saxParser;
| private static boolean | isClientHappy(java.lang.String[] args)this is the implementation of the happiness test.
HappyClient happy=new HappyClient(System.out);
boolean isHappy;
int missing=0;
try {
isHappy = happy.verifyClientIsHappy(false);
for(int i=0;i<args.length;i++) {
missing+=happy.probeClass(
"argument",
args[i],
null,
null,
null,
null
);
}
if(missing>0) {
isHappy=false;
}
} catch (IOException e) {
e.printStackTrace();
isHappy=false;
}
return isHappy;
| public static void | main(java.lang.String[] args)public happiness test. Exits with -1 if the client is unhappy.
boolean isHappy = isClientHappy(args);
System.exit(isHappy?0:-1);
| int | needClass(java.lang.String classname, java.lang.String jarFile, java.lang.String description, java.lang.String errorText, java.lang.String homePage)a class we need if a class is missing
return probeClass(
Messages.getMessage("happyClientError"),
classname,
jarFile,
description,
errorText,
homePage);
| int | probeClass(java.lang.String category, java.lang.String classname, java.lang.String jarFile, java.lang.String description, java.lang.String errorText, java.lang.String homePage)probe for a class, print an error message is missing
String url = "";
if (homePage != null) {
url=Messages.getMessage("happyClientHomepage",homePage);
}
String errorLine="";
if (errorText != null) {
errorLine=Messages.getMessage(errorText);
}
try {
Class clazz = classExists(classname);
if (clazz == null) {
String text;
text=Messages.getMessage("happyClientMissingClass",
category,classname,jarFile);
out.println(text);
out.println(url);
return 1;
} else {
String location = getLocation(clazz);
String text;
if (location == null) {
text=Messages.getMessage("happyClientFoundDescriptionClass",
description,classname);
} else {
text = Messages.getMessage("happyClientFoundDescriptionClassLocation",
description, classname,location);
}
out.println(text);
return 0;
}
} catch (NoClassDefFoundError ncdfe) {
out.println(Messages.getMessage("happyClientNoDependency",
category, classname, jarFile));
out.println(errorLine);
out.println(url);
out.println(ncdfe.getMessage());
return 1;
}
| boolean | resourceExists(java.lang.String resource)test for resource on the classpath
boolean found;
InputStream instream = ClassUtils.getResourceAsStream(this.getClass(),resource);
found = instream != null;
if (instream != null) {
try {
instream.close();
} catch (IOException e) {
}
}
return found;
| private void | title(java.lang.String title)
out.println();
String message=Messages.getMessage(title);
out.println(message);
//subtitle
for(int i=0;i< message.length();i++) {
out.print("=");
}
out.println();
| public boolean | verifyClientIsHappy(boolean warningsAsErrors)Audit the client, print out status
int needed = 0,wanted = 0;
out.println();
title("happyClientTitle");
title("happyClientNeeded");
/**
* the essentials, without these Axis is not going to work
*/
needed = needClass("javax.xml.soap.SOAPMessage",
"saaj.jar",
"SAAJ",
"happyClientNoAxis",
"http://xml.apache.org/axis/");
needed += needClass("javax.xml.rpc.Service",
"jaxrpc.jar",
"JAX-RPC",
"happyClientNoAxis",
"http://xml.apache.org/axis/");
needed += needClass("org.apache.commons.discovery.Resource",
"commons-discovery.jar",
"Jakarta-Commons Discovery",
"happyClientNoAxis",
"http://jakarta.apache.org/commons/discovery.html");
needed += needClass("org.apache.commons.logging.Log",
"commons-logging.jar",
"Jakarta-Commons Logging",
"happyClientNoAxis",
"http://jakarta.apache.org/commons/logging.html");
//all refs to log4j are split to get past the package tester
needed += needClass("org.apache" + ".log" +"4j" +".Layout",
"log4"+"j-1.2.4.jar",
"Log4"+"j",
"happyClientNoLog4J",
"http://jakarta.apache.org/log"+"4j");
//should we search for a javax.wsdl file here, to hint that it needs
//to go into an approved directory? because we dont seem to need to do that.
needed += needClass("com.ibm.wsdl.factory.WSDLFactoryImpl",
"wsdl4j.jar",
"WSDL4Java",
"happyClientNoAxis",
null);
needed += needClass("javax.xml.parsers.SAXParserFactory",
"xerces.jar",
"JAXP",
"happyClientNoAxis",
"http://xml.apache.org/xerces-j/");
title("happyClientOptional");
wanted += wantClass("javax.mail.internet.MimeMessage",
"mail.jar",
"Mail",
"happyClientNoAttachments",
"http://java.sun.com/products/javamail/");
wanted += wantClass("javax.activation.DataHandler",
"activation.jar",
"Activation",
"happyClientNoAttachments",
"http://java.sun.com/products/javabeans/glasgow/jaf.html");
wanted += wantClass("org.apache.xml.security.Init",
"xmlsec.jar",
"XML Security",
"happyClientNoSecurity",
"http://xml.apache.org/security/");
wanted += wantClass("javax.net.ssl.SSLSocketFactory",
Messages.getMessage("happyClientJSSEsources"),
"Java Secure Socket Extension",
"happyClientNoHTTPS",
"http://java.sun.com/products/jsse/");
/*
* resources on the classpath path
*/
int warningMessages=0;
String xmlParser = getParserName();
String xmlParserLocation = getParserLocation();
out.println(Messages.getMessage("happyClientXMLinfo",
xmlParser,xmlParserLocation));
if (xmlParser.indexOf("xerces") <= 0) {
warningMessages++;
out.println();
out.println(Messages.getMessage("happyClientRecommendXerces"));
}
if (getJavaVersionNumber() < 13) {
warningMessages++;
out.println();
out.println(Messages.getMessage("happyClientUnsupportedJVM"));
}
/* add more libraries here */
//print the summary information
boolean happy;
title("happyClientSummary");
//is everythng we need here
if (needed == 0) {
//yes, be happy
out.println(Messages.getMessage("happyClientCorePresent"));
happy=true;
} else {
happy=false;
//no, be very unhappy
out.println(Messages.getMessage("happyClientCoreMissing",
Integer.toString(needed)));
}
//now look at wanted stuff
if (wanted > 0) {
out.println();
out.println(Messages.getMessage("happyClientOptionalMissing",
Integer.toString(wanted)));
out.println(Messages.getMessage("happyClientOptionalOK"));
if (warningsAsErrors) {
happy = false;
}
} else {
out.println(Messages.getMessage("happyClientOptionalPresent"));
}
if (warningMessages > 0) {
out.println(Messages.getMessage("happyClientWarningMessageCount",
Integer.toString(warningMessages)));
if (warningsAsErrors) {
happy = false;
}
}
return happy;
| int | wantClass(java.lang.String classname, java.lang.String jarFile, java.lang.String description, java.lang.String errorText, java.lang.String homePage)print warning message if a class is missing
return probeClass(
Messages.getMessage("happyClientWarning"),
classname,
jarFile,
description,
errorText,
homePage);
| int | wantResource(java.lang.String resource, java.lang.String errorText)probe for a resource existing,
if (!resourceExists(resource)) {
out.println(Messages.getMessage("happyClientNoResource",resource));
out.println(errorText);
return 0;
} else {
out.println(Messages.getMessage("happyClientFoundResource", resource));
return 1;
}
|
|