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
005package 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 * &nbsp;
017 * String javaVersion = VersionDetector.getJavaVersion();
018 *
019 * @author <b>Jason Hunter</b>, Copyright &#169; 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 */
024public 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    }
061    catch (Throwable t) {
062    }
063    
064    servletVersion = ver;
065    return servletVersion;                                              
066  }
067
068  /**
069   * Determines the JDK version number.
070   *
071   * @return a String representation of the JDK version
072   */
073  public static String getJavaVersion() {
074    if (javaVersion != null) {
075      return javaVersion;
076    }
077
078    // Determine the Java version by looking at available classes
079    // java.lang.Void was introduced in JDK 1.1
080    // java.lang.ThreadLocal was introduced in JDK 1.2
081    // java.lang.StrictMath was introduced in JDK 1.3
082    // java.net.URI is highly likely to be introduced in JDK 1.4
083    // Count up versions until a NoClassDefFoundError ends the try
084    String ver = null;
085    try {
086      ver = "1.0";
087      Class.forName("java.lang.Void");
088      ver = "1.1";
089      Class.forName("java.lang.ThreadLocal");
090      ver = "1.2"; 
091      Class.forName("java.lang.StrictMath");
092      ver = "1.3"; 
093      Class.forName("java.net.URI");
094      ver = "1.4";
095    }
096    catch (Throwable t) {
097    }
098
099    javaVersion = ver;
100    return javaVersion;
101  }
102}