KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > client > HappyClient


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.PrintStream JavaDoc;
27
28 /**
29  * Client side equivalent of happyaxis
30  */

31 public class HappyClient {
32
33     PrintStream JavaDoc out;
34
35     public HappyClient(PrintStream JavaDoc out) {
36         this.out = out;
37     }
38
39     /**
40      * test for a class existing
41      * @param classname
42      * @return class iff present
43      */

44     Class JavaDoc classExists(String JavaDoc classname) {
45         try {
46             return Class.forName(classname);
47         } catch (ClassNotFoundException JavaDoc e) {
48             return null;
49         }
50     }
51
52     /**
53      * test for resource on the classpath
54      * @param resource
55      * @return true iff present
56      */

57     boolean resourceExists(String JavaDoc resource) {
58         boolean found;
59         InputStream JavaDoc instream = ClassUtils.getResourceAsStream(this.getClass(),resource);
60         found = instream != null;
61         if (instream != null) {
62             try {
63                 instream.close();
64             } catch (IOException JavaDoc e) {
65             }
66         }
67         return found;
68     }
69
70     /**
71      * probe for a class, print an error message is missing
72      * @param category text like "warning" or "error"
73      * @param classname class to look for
74      * @param jarFile where this class comes from
75      * @param errorText extra error text
76      * @param homePage where to d/l the library
77      * @return the number of missing classes
78      * @throws java.io.IOException
79      */

80     int probeClass(
81             String JavaDoc category,
82                    String JavaDoc classname,
83                    String JavaDoc jarFile,
84                    String JavaDoc description,
85                    String JavaDoc errorText,
86                    String JavaDoc homePage) throws IOException JavaDoc {
87         String JavaDoc url = "";
88         if (homePage != null) {
89             url=Messages.getMessage("happyClientHomepage",homePage);
90         }
91         String JavaDoc errorLine="";
92         if (errorText != null) {
93             errorLine=Messages.getMessage(errorText);
94         }
95         try {
96             Class JavaDoc clazz = classExists(classname);
97             if (clazz == null) {
98                 String JavaDoc 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 JavaDoc location = getLocation(clazz);
106                 String JavaDoc 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 JavaDoc 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     /**
128      * get the location of a class
129      * @param clazz
130      * @return the jar file or path where a class was found
131      */

132
133     String JavaDoc getLocation(
134             Class JavaDoc clazz) {
135         try {
136             java.net.URL JavaDoc url = clazz.getProtectionDomain().getCodeSource().getLocation();
137             String JavaDoc location = url.toString();
138             if (location.startsWith("jar")) {
139                 url = ((java.net.JarURLConnection JavaDoc) url.openConnection()).getJarFileURL();
140                 location = url.toString();
141             }
142
143             if (location.startsWith("file")) {
144                 java.io.File JavaDoc file = new java.io.File JavaDoc(url.getFile());
145                 return file.getAbsolutePath();
146             } else {
147                 return url.toString();
148             }
149         } catch (Throwable JavaDoc t) {
150         }
151         return Messages.getMessage("happyClientUnknownLocation");
152     }
153
154     /**
155      * a class we need if a class is missing
156      * @param classname class to look for
157      * @param jarFile where this class comes from
158      * @param errorText extra error text
159      * @param homePage where to d/l the library
160      * @throws java.io.IOException when needed
161      * @return the number of missing libraries (0 or 1)
162      */

163     int needClass(
164             String JavaDoc classname,
165                   String JavaDoc jarFile,
166                   String JavaDoc description,
167                   String JavaDoc errorText,
168                   String JavaDoc homePage) throws IOException JavaDoc {
169         return probeClass(
170                 Messages.getMessage("happyClientError"),
171                 classname,
172                 jarFile,
173                 description,
174                 errorText,
175                 homePage);
176     }
177
178     /**
179      * print warning message if a class is missing
180      * @param classname class to look for
181      * @param jarFile where this class comes from
182      * @param errorText extra error text
183      * @param homePage where to d/l the library
184      * @throws java.io.IOException when needed
185      * @return the number of missing libraries (0 or 1)
186      */

187     int wantClass(
188             String JavaDoc classname,
189                   String JavaDoc jarFile,
190                   String JavaDoc description,
191                   String JavaDoc errorText,
192                   String JavaDoc homePage) throws IOException JavaDoc {
193         return probeClass(
194                 Messages.getMessage("happyClientWarning"),
195                 classname,
196                 jarFile,
197                 description,
198                 errorText,
199                 homePage);
200     }
201
202     /**
203      * probe for a resource existing,
204      * @param resource
205      * @param errorText
206      * @throws Exception
207      */

208     int wantResource(
209             String JavaDoc resource,
210                      String JavaDoc errorText) throws Exception JavaDoc {
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     /**
223      * what parser are we using.
224      * @return the classname of the parser
225      */

226     private String JavaDoc getParserName() {
227         SAXParser JavaDoc saxParser = getSAXParser();
228         if (saxParser == null) {
229             return Messages.getMessage("happyClientNoParser");
230         }
231
232         // check to what is in the classname
233
String JavaDoc saxParserName = saxParser.getClass().getName();
234         return saxParserName;
235     }
236
237     /**
238      * Create a JAXP SAXParser
239      * @return parser or null for trouble
240      */

241     private SAXParser JavaDoc getSAXParser() {
242         SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
243         if (saxParserFactory == null) {
244             return null;
245         }
246         SAXParser JavaDoc saxParser = null;
247         try {
248             saxParser = saxParserFactory.newSAXParser();
249         } catch (Exception JavaDoc e) {
250         }
251         return saxParser;
252     }
253
254     /**
255      * get the location of the parser
256      * @return path or null for trouble in tracking it down
257      */

258
259     private String JavaDoc getParserLocation() {
260         SAXParser JavaDoc saxParser = getSAXParser();
261         if (saxParser == null) {
262             return null;
263         }
264         String JavaDoc location = getLocation(saxParser.getClass());
265         return location;
266     }
267
268     /**
269      * calculate the java version number by probing for classes;
270      * this tactic works across many jvm implementations; taken from Ant.
271      * @return JRE version as 10,11,12,13,14,...
272      */

273     public int getJavaVersionNumber() {
274         // Determine the Java version by looking at available classes
275
// java.lang.CharSequence was introduced in JDK 1.4
276
// java.lang.StrictMath was introduced in JDK 1.3
277
// java.lang.ThreadLocal was introduced in JDK 1.2
278
// java.lang.Void was introduced in JDK 1.1
279
// Count up version until a NoClassDefFoundError ends the try
280
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 JavaDoc t) {
291             // swallow as we've hit the max class version that
292
// we have
293
}
294         return javaVersionNumber;
295     }
296
297
298     private void title(String JavaDoc title) {
299         out.println();
300         String JavaDoc message=Messages.getMessage(title);
301         out.println(message);
302         //subtitle
303
for(int i=0;i< message.length();i++) {
304             out.print("=");
305         }
306         out.println();
307     }
308     /**
309      * Audit the client, print out status
310      * @param warningsAsErrors should any warning result in failure?
311      * @return true if we are happy
312      * @throws IOException
313      */

314     public boolean verifyClientIsHappy(boolean warningsAsErrors) throws IOException JavaDoc {
315         int needed = 0,wanted = 0;
316         out.println();
317         title("happyClientTitle");
318         title("happyClientNeeded");
319
320         /**
321          * the essentials, without these Axis is not going to work
322          */

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         //all refs to log4j are split to get past the package tester
348
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         //should we search for a javax.wsdl file here, to hint that it needs
355
//to go into an approved directory? because we dont seem to need to do that.
356
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         /*
397         * resources on the classpath path
398         */

399         int warningMessages=0;
400
401         String JavaDoc xmlParser = getParserName();
402         String JavaDoc 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         /* add more libraries here */
416
417         //print the summary information
418
boolean happy;
419         title("happyClientSummary");
420
421         //is everythng we need here
422
if (needed == 0) {
423             //yes, be happy
424
out.println(Messages.getMessage("happyClientCorePresent"));
425             happy=true;
426         } else {
427             happy=false;
428             //no, be very unhappy
429
out.println(Messages.getMessage("happyClientCoreMissing",
430                     Integer.toString(needed)));
431         }
432         //now look at wanted stuff
433
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     /**
456      * public happiness test. Exits with -1 if the client is unhappy.
457      * @param args a list of extra classes to look for
458      *
459      */

460     public static void main(String JavaDoc args[]) {
461         boolean isHappy = isClientHappy(args);
462         System.exit(isHappy?0:-1);
463     }
464
465     /**
466      * this is the implementation of the happiness test.
467      * @param args a list of extra classes to look for
468      * @return true iff we are happy: all needed ant all argument classes
469      * found
470      */

471     private static boolean isClientHappy(String JavaDoc[] 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 JavaDoc e) {
491             e.printStackTrace();
492             isHappy=false;
493         }
494         return isHappy;
495     }
496 }
497
Popular Tags