001: // Copyright (C) 2000-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
002: // All rights reserved. Use of this class is limited.
003: // Please see the LICENSE for more information.
004:
005: package com.oreilly.servlet;
006:
007: /**
008: * A class to determine the current Servlet API version number, and the
009: * current JDK version number. It looks at the available classes and
010: * variables to make the determination. The class can detect Servlet
011: * API versions up to 2.2, and JDK versions up to 1.3.
012: * <p>
013: * It can be used like this:
014: * <blockquote><pre>
015: * String servletVersion = VersionDetector.getServletVersion();
016: *
017: * String javaVersion = VersionDetector.getJavaVersion();
018: *
019: * @author <b>Jason Hunter</b>, Copyright © 2000
020: * @version 1.2, 2001/04/11, added detection of JDK 1.4
021: * @version 1.1, 2000/09/22, added detection of Servlet API 2.3
022: * @version 1.0, 2000/02/08
023: */
024: public class VersionDetector {
025:
026: static String servletVersion;
027: static String javaVersion;
028:
029: /**
030: * Determines the Servlet API version number.
031: *
032: * @return a String representation of the servlet version
033: */
034: public static String getServletVersion() {
035: if (servletVersion != null) {
036: return servletVersion;
037: }
038:
039: // Determine the servlet version by looking at available classes
040: // and variables
041: // javax.servlet.http.HttpSession was introduced in Servlet API 2.0
042: // javax.servlet.RequestDispatcher was introduced in Servlet API 2.1
043: // javax.servlet.http.HttpServletResponse.SC_EXPECTATION_FAILED was
044: // introduced in Servlet API 2.2
045: // javax.servlet.Filter is slated to be introduced in Servlet API 2.3
046: // Count up versions until a NoClassDefFoundError or NoSuchFieldException
047: // ends the try
048: String ver = null;
049: try {
050: ver = "1.0";
051: Class.forName("javax.servlet.http.HttpSession");
052: ver = "2.0";
053: Class.forName("javax.servlet.RequestDispatcher");
054: ver = "2.1";
055: Class.forName("javax.servlet.http.HttpServletResponse")
056: .getDeclaredField("SC_EXPECTATION_FAILED");
057: ver = "2.2";
058: Class.forName("javax.servlet.Filter");
059: ver = "2.3";
060: } catch (Throwable t) {
061: }
062:
063: servletVersion = ver;
064: return servletVersion;
065: }
066:
067: /**
068: * Determines the JDK version number.
069: *
070: * @return a String representation of the JDK version
071: */
072: public static String getJavaVersion() {
073: if (javaVersion != null) {
074: return javaVersion;
075: }
076:
077: // Determine the Java version by looking at available classes
078: // java.lang.Void was introduced in JDK 1.1
079: // java.lang.ThreadLocal was introduced in JDK 1.2
080: // java.lang.StrictMath was introduced in JDK 1.3
081: // java.net.URI is highly likely to be introduced in JDK 1.4
082: // Count up versions until a NoClassDefFoundError ends the try
083: String ver = null;
084: try {
085: ver = "1.0";
086: Class.forName("java.lang.Void");
087: ver = "1.1";
088: Class.forName("java.lang.ThreadLocal");
089: ver = "1.2";
090: Class.forName("java.lang.StrictMath");
091: ver = "1.3";
092: Class.forName("java.net.URI");
093: ver = "1.4";
094: } catch (Throwable t) {
095: }
096:
097: javaVersion = ver;
098: return javaVersion;
099: }
100: }
|