001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.tomcat.session;
006:
007: import org.apache.catalina.Container;
008: import org.apache.catalina.Pipeline;
009: import org.apache.catalina.Valve;
010: import org.apache.catalina.util.ServerInfo;
011:
012: import java.lang.reflect.Constructor;
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: public class VersionHelper {
017: private static final Object[] NO_ARGS = new Object[] {};
018: private static final Class[] NO_ARGS_SIGNATURE = new Class[] {};
019:
020: private static final Version TOMCAT_50 = new Version("50");
021: private static final Version TOMCAT_55 = new Version("55");
022: private static final Version CURRENT;
023:
024: Map m = new HashMap();
025:
026: static {
027: String serverInfo = ServerInfo.getServerInfo();
028: serverInfo = (serverInfo == null) ? "null" : serverInfo;
029:
030: int lastSlash = serverInfo.lastIndexOf("/");
031: if (lastSlash < 0 || serverInfo.endsWith("/")) {
032: throw new AssertionError(
033: "Cannot determine tomcat version from "
034: + serverInfo);
035: }
036:
037: String ver = serverInfo.substring(lastSlash + 1);
038:
039: if (ver.startsWith("5.0")) {
040: CURRENT = TOMCAT_50;
041: } else if (ver.startsWith("5.5")) {
042: CURRENT = TOMCAT_55;
043: } else if (ver.startsWith("6.0")) {
044: CURRENT = TOMCAT_55;
045: } else {
046: throw new AssertionError(
047: "Cannot determine tomcat version from "
048: + serverInfo);
049: }
050: }
051:
052: private VersionHelper() {
053: //
054: }
055:
056: public static Valve createSessionValve() {
057: return (Valve) createObject(CURRENT, "session.SessionValve"
058: + CURRENT.getVersion());
059: }
060:
061: public static Pipeline createTerracottaPipeline(Container container) {
062: return (Pipeline) createObject(CURRENT, "TerracottaPipeline",
063: new Class[] { Container.class },
064: new Object[] { container });
065: }
066:
067: private static final Object createObject(Version version,
068: String clazz) {
069: return createObject(version, clazz, NO_ARGS_SIGNATURE, NO_ARGS);
070: }
071:
072: private static final Object createObject(Version version,
073: String clazz, Class[] signature, Object[] params) {
074: String className = "com.tc." + version.getPackageName() + "."
075: + clazz;
076:
077: try {
078: Class c = Class.forName(className);
079: Constructor cstr = c.getDeclaredConstructor(signature);
080: return cstr.newInstance(params);
081: } catch (Exception e) {
082: Error err = new AssertionError(
083: "Error creating instance of " + className);
084: err.initCause(e);
085: throw err;
086: }
087: }
088:
089: private static class Version {
090: private final String version;
091:
092: Version(String version) {
093: this .version = version;
094: }
095:
096: public String getPackageName() {
097: return "tomcat" + version;
098: }
099:
100: public String getVersion() {
101: return version;
102: }
103:
104: public String toString() {
105: return version;
106: }
107: }
108:
109: }
|