KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > VersionDetector


1 // Copyright (C) 2000-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet;
6
7 /**
8  * A class to determine the current Servlet API version number, and the
9  * current JDK version number. It looks at the available classes and
10  * variables to make the determination. The class can detect Servlet
11  * API versions up to 2.2, and JDK versions up to 1.3.
12  * <p>
13  * It can be used like this:
14  * <blockquote><pre>
15  * String servletVersion = VersionDetector.getServletVersion();
16  * &nbsp;
17  * String javaVersion = VersionDetector.getJavaVersion();
18  *
19  * @author <b>Jason Hunter</b>, Copyright &#169; 2000
20  * @version 1.2, 2001/04/11, added detection of JDK 1.4
21  * @version 1.1, 2000/09/22, added detection of Servlet API 2.3
22  * @version 1.0, 2000/02/08
23  */

24 public class VersionDetector {
25
26   static String JavaDoc servletVersion;
27   static String JavaDoc javaVersion;
28
29   /**
30    * Determines the Servlet API version number.
31    *
32    * @return a String representation of the servlet version
33    */

34   public static String JavaDoc getServletVersion() {
35     if (servletVersion != null) {
36       return servletVersion;
37     }
38
39     // Determine the servlet version by looking at available classes
40
// and variables
41
// javax.servlet.http.HttpSession was introduced in Servlet API 2.0
42
// javax.servlet.RequestDispatcher was introduced in Servlet API 2.1
43
// javax.servlet.http.HttpServletResponse.SC_EXPECTATION_FAILED was
44
// introduced in Servlet API 2.2
45
// javax.servlet.Filter is slated to be introduced in Servlet API 2.3
46
// Count up versions until a NoClassDefFoundError or NoSuchFieldException
47
// ends the try
48
String JavaDoc ver = null;
49     try {
50       ver = "1.0";
51       Class.forName("javax.servlet.http.HttpSession");
52       ver = "2.0";
53       Class.forName("javax.servlet.RequestDispatcher");
54       ver = "2.1";
55       Class.forName("javax.servlet.http.HttpServletResponse")
56                    .getDeclaredField("SC_EXPECTATION_FAILED");
57       ver = "2.2";
58       Class.forName("javax.servlet.Filter");
59       ver = "2.3";
60     }
61     catch (Throwable JavaDoc t) {
62     }
63     
64     servletVersion = ver;
65     return servletVersion;
66   }
67
68   /**
69    * Determines the JDK version number.
70    *
71    * @return a String representation of the JDK version
72    */

73   public static String JavaDoc getJavaVersion() {
74     if (javaVersion != null) {
75       return javaVersion;
76     }
77
78     // Determine the Java version by looking at available classes
79
// java.lang.Void was introduced in JDK 1.1
80
// java.lang.ThreadLocal was introduced in JDK 1.2
81
// java.lang.StrictMath was introduced in JDK 1.3
82
// java.net.URI is highly likely to be introduced in JDK 1.4
83
// Count up versions until a NoClassDefFoundError ends the try
84
String JavaDoc ver = null;
85     try {
86       ver = "1.0";
87       Class.forName("java.lang.Void");
88       ver = "1.1";
89       Class.forName("java.lang.ThreadLocal");
90       ver = "1.2";
91       Class.forName("java.lang.StrictMath");
92       ver = "1.3";
93       Class.forName("java.net.URI");
94       ver = "1.4";
95     }
96     catch (Throwable JavaDoc t) {
97     }
98
99     javaVersion = ver;
100     return javaVersion;
101   }
102 }
103
Popular Tags