KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > tomcat > session > VersionHelper


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 public class VersionHelper {
17   private static final Object JavaDoc[] NO_ARGS = new Object JavaDoc[] {};
18   private static final Class JavaDoc[] NO_ARGS_SIGNATURE = new Class JavaDoc[] {};
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 JavaDoc m = new HashMap JavaDoc();
25
26   static {
27     String JavaDoc serverInfo = ServerInfo.getServerInfo();
28     serverInfo = (serverInfo == null) ? "null" : serverInfo;
29
30     int lastSlash = serverInfo.lastIndexOf("/");
31     if (lastSlash < 0 || serverInfo.endsWith("/")) { throw new AssertionError JavaDoc("Cannot determine tomcat version from "
32                                                                               + serverInfo); }
33
34     String JavaDoc 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 JavaDoc("Cannot determine tomcat version from " + serverInfo);
44     }
45   }
46
47   private VersionHelper() {
48     //
49
}
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 JavaDoc[] { Container.class },
57                                    new Object JavaDoc[] { container });
58   }
59
60   private static final Object JavaDoc createObject(Version version, String JavaDoc clazz) {
61     return createObject(version, clazz, NO_ARGS_SIGNATURE, NO_ARGS);
62   }
63
64   private static final Object JavaDoc createObject(Version version, String JavaDoc clazz, Class JavaDoc[] signature, Object JavaDoc[] params) {
65     String JavaDoc className = "com.tc." + version.getPackageName() + "." + clazz;
66
67     try {
68       Class JavaDoc c = Class.forName(className);
69       Constructor JavaDoc cstr = c.getDeclaredConstructor(signature);
70       return cstr.newInstance(params);
71     } catch (Exception JavaDoc e) {
72       Error JavaDoc err = new AssertionError JavaDoc("Error creating instance of " + className);
73       err.initCause(e);
74       throw err;
75     }
76   }
77
78   private static class Version {
79     private final String JavaDoc version;
80
81     Version(String JavaDoc version) {
82       this.version = version;
83     }
84
85     public String JavaDoc getPackageName() {
86       return "tomcat" + version;
87     }
88
89     public String JavaDoc getVersion() {
90       return version;
91     }
92
93     public String JavaDoc toString() {
94       return version;
95     }
96   }
97
98 }
99
Popular Tags