1 5 package com.tc.tomcat.session; 6 7 import org.apache.catalina.Container; 8 import org.apache.catalina.Pipeline; 9 import org.apache.catalina.Valve; 10 import org.apache.catalina.util.ServerInfo; 11 12 import java.lang.reflect.Constructor ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 public class VersionHelper { 17 private static final Object [] NO_ARGS = new Object [] {}; 18 private static final Class [] NO_ARGS_SIGNATURE = new Class [] {}; 19 20 private static final Version TOMCAT_50 = new Version("50"); 21 private static final Version TOMCAT_55 = new Version("55"); 22 private static final Version CURRENT; 23 24 Map m = new HashMap (); 25 26 static { 27 String serverInfo = ServerInfo.getServerInfo(); 28 serverInfo = (serverInfo == null) ? "null" : serverInfo; 29 30 int lastSlash = serverInfo.lastIndexOf("/"); 31 if (lastSlash < 0 || serverInfo.endsWith("/")) { throw new AssertionError ("Cannot determine tomcat version from " 32 + serverInfo); } 33 34 String ver = serverInfo.substring(lastSlash + 1); 35 36 if (ver.startsWith("5.0")) { 37 CURRENT = TOMCAT_50; 38 } else if (ver.startsWith("5.5")) { 39 CURRENT = TOMCAT_55; 40 } else if (ver.startsWith("6.0")) { 41 CURRENT = TOMCAT_55; 42 } else { 43 throw new AssertionError ("Cannot determine tomcat version from " + serverInfo); 44 } 45 } 46 47 private VersionHelper() { 48 } 50 51 public static Valve createSessionValve() { 52 return (Valve) createObject(CURRENT, "session.SessionValve" + CURRENT.getVersion()); 53 } 54 55 public static Pipeline createTerracottaPipeline(Container container) { 56 return (Pipeline) createObject(CURRENT, "TerracottaPipeline", new Class [] { Container.class }, 57 new Object [] { container }); 58 } 59 60 private static final Object createObject(Version version, String clazz) { 61 return createObject(version, clazz, NO_ARGS_SIGNATURE, NO_ARGS); 62 } 63 64 private static final Object createObject(Version version, String clazz, Class [] signature, Object [] params) { 65 String className = "com.tc." + version.getPackageName() + "." + clazz; 66 67 try { 68 Class c = Class.forName(className); 69 Constructor cstr = c.getDeclaredConstructor(signature); 70 return cstr.newInstance(params); 71 } catch (Exception e) { 72 Error err = new AssertionError ("Error creating instance of " + className); 73 err.initCause(e); 74 throw err; 75 } 76 } 77 78 private static class Version { 79 private final String version; 80 81 Version(String version) { 82 this.version = version; 83 } 84 85 public String getPackageName() { 86 return "tomcat" + version; 87 } 88 89 public String getVersion() { 90 return version; 91 } 92 93 public String toString() { 94 return version; 95 } 96 } 97 98 } 99 | Popular Tags |