001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.tomcat.common;
017:
018: import java.util.Properties;
019:
020: /**
021: * @version $Rev$ $Date$
022: */
023: public enum TomcatVersion {
024:
025: v3, v40, v41, v50, v55, v6, UNKNOWN;
026:
027: private String serverNumber;
028: private String serverBuilt;
029:
030: private static TomcatVersion version;
031:
032: public String getServerNumber() {
033: return serverNumber;
034: }
035:
036: public String getServerBuilt() {
037: return serverBuilt;
038: }
039:
040: public boolean isTheVersion() {
041: return get() == this ;
042: }
043:
044: public static boolean hasAnnotationProcessingSupport() {
045: switch (get()) {
046: case v40:
047: case v41:
048: case v50:
049: case v55:
050: return false;
051: default:
052: return true;
053: }
054: }
055:
056: public static TomcatVersion get() {
057: if (version != null)
058: return version;
059:
060: try {
061: // tomcat.version and tomcat.built properties should be added
062: // to System by TomcatHook
063: String serverNumber = System.getProperty("tomcat.version");
064: String serverBuilt = System.getProperty("tomcat.built");
065:
066: // This fallback block will only work in Tomcat6 since the
067: // common class loader in previous versions can not see the
068: // catalina.jar which contains the ServerInfo.properties
069: if (serverNumber == null) {
070: Properties properties = new Properties();
071: ClassLoader classLoader = TomcatVersion.class
072: .getClassLoader();
073: properties
074: .load(classLoader
075: .getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"));
076:
077: serverNumber = properties.getProperty("server.number");
078: if (serverNumber == null) {
079: // Tomcat 5.0 and earlier only has server.info
080: String serverInfo = properties
081: .getProperty("server.info");
082: if (serverInfo != null) {
083: int slash = serverInfo.indexOf('/');
084: serverNumber = serverInfo.substring(slash + 1);
085: }
086: }
087:
088: serverBuilt = properties.getProperty("server.built");
089: }
090:
091: if (serverNumber.startsWith("3"))
092: version = v3;
093: else if (serverNumber.startsWith("4.0"))
094: version = v40;
095: else if (serverNumber.startsWith("4.1"))
096: version = v41;
097: else if (serverNumber.startsWith("5.0"))
098: version = v50;
099: else if (serverNumber.startsWith("5.5"))
100: version = v55;
101: else if (serverNumber.startsWith("6."))
102: version = v6;
103: else
104: version = UNKNOWN;
105:
106: version.serverNumber = serverNumber;
107: version.serverBuilt = serverBuilt;
108:
109: return version;
110: } catch (Exception e) {
111: return UNKNOWN;
112: }
113: }
114: }
|