KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > compat > JdkCompat


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tomcat.util.compat;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Vector JavaDoc;
26
27
28 /**
29  * General-purpose utility to provide backward-compatibility and JDK
30  * independence. This allow use of JDK1.3 ( or higher ) facilities if
31  * available, while maintaining the code compatible with older VMs.
32  *
33  * The goal is to make backward-compatiblity reasonably easy.
34  *
35  * The base class supports JDK1.3 behavior.
36  *
37  * @author Tim Funk
38  */

39 public class JdkCompat {
40
41     // ------------------------------------------------------- Static Variables
42

43     /**
44      * class providing java2 support
45      */

46     static final String JavaDoc JAVA14_SUPPORT =
47         "org.apache.tomcat.util.compat.Jdk14Compat";
48
49     /** Return java version as a string
50      */

51     public static String JavaDoc getJavaVersion() {
52         return javaVersion;
53     }
54
55     public static boolean isJava2() {
56         return java2;
57     }
58    
59     public static boolean isJava14() {
60         return java14;
61     }
62
63     public static boolean isJava15() {
64         return java15;
65     }
66
67     // -------------------- Implementation --------------------
68

69     // from ant
70
public static final String JavaDoc JAVA_1_0 = "1.0";
71     public static final String JavaDoc JAVA_1_1 = "1.1";
72     public static final String JavaDoc JAVA_1_2 = "1.2";
73     public static final String JavaDoc JAVA_1_3 = "1.3";
74     public static final String JavaDoc JAVA_1_4 = "1.4";
75     public static final String JavaDoc JAVA_1_5 = "1.5";
76
77     static String JavaDoc javaVersion;
78     static boolean java2=false;
79     static boolean java14=false;
80     static boolean java15=false;
81     static JdkCompat jdkCompat;
82     
83     static {
84         init();
85     }
86
87     private static void init() {
88         try {
89             javaVersion = JAVA_1_0;
90             Class.forName("java.lang.Void");
91             javaVersion = JAVA_1_1;
92             Class.forName("java.lang.ThreadLocal");
93             java2=true;
94             javaVersion = JAVA_1_2;
95             Class.forName("java.lang.StrictMath");
96             javaVersion = JAVA_1_3;
97             Class.forName("java.lang.CharSequence");
98             javaVersion = JAVA_1_4;
99             java14=true;
100             Class.forName("java.lang.Appendable");
101             javaVersion = JAVA_1_5;
102             java15=true;
103         } catch (ClassNotFoundException JavaDoc cnfe) {
104             // swallow as we've hit the max class version that we have
105
}
106         if( java14 ) {
107             try {
108                 Class JavaDoc c=Class.forName(JAVA14_SUPPORT);
109                 jdkCompat=(JdkCompat)c.newInstance();
110             } catch( Exception JavaDoc ex ) {
111                 jdkCompat=new JdkCompat();
112             }
113         } else {
114             jdkCompat=new JdkCompat();
115             // Install jar handler if none installed
116
}
117     }
118
119     // ----------------------------------------------------------- Constructors
120
/**
121      * Default no-arg constructor
122      */

123     protected JdkCompat() {
124     }
125
126
127     // --------------------------------------------------------- Public Methods
128
/**
129      * Get a compatibiliy helper class.
130      */

131     public static JdkCompat getJdkCompat() {
132         return jdkCompat;
133     }
134
135     /**
136      * Return the URI for the given file. Originally created for
137      * o.a.c.loader.WebappClassLoader
138      *
139      * @param file The file to wrap into URI
140      * @return A URI as a URL
141      * @throws MalformedURLException Doh ;)
142      */

143     public URL JavaDoc getURI(File JavaDoc file)
144         throws MalformedURLException JavaDoc {
145
146         File JavaDoc realFile = file;
147         try {
148             realFile = realFile.getCanonicalFile();
149         } catch (IOException JavaDoc e) {
150             // Ignore
151
}
152
153         return realFile.toURL();
154     }
155
156
157     /**
158      * Return the maximum amount of memory the JVM will attempt to use.
159      */

160     public long getMaxMemory() {
161         return (-1L);
162     }
163
164
165     /**
166      * Print out a partial servlet stack trace (truncating at the last
167      * occurrence of javax.servlet.).
168      */

169     public String JavaDoc getPartialServletStackTrace(Throwable JavaDoc t) {
170         StringWriter JavaDoc stackTrace = new StringWriter JavaDoc();
171         t.printStackTrace(new PrintWriter JavaDoc(stackTrace));
172         String JavaDoc st = stackTrace.toString();
173         int i = st.lastIndexOf
174             ("org.apache.catalina.core.ApplicationFilterChain.internalDoFilter");
175         if (i > -1) {
176             return st.substring(0, i - 4);
177         } else {
178             return st;
179         }
180     }
181
182     /**
183      * Splits a string into it's components.
184      * @param path String to split
185      * @param pat Pattern to split at
186      * @return the components of the path
187      */

188     public String JavaDoc [] split(String JavaDoc path, String JavaDoc pat) {
189         Vector JavaDoc comps = new Vector JavaDoc();
190         int pos = path.indexOf(pat);
191         int start = 0;
192         while( pos >= 0 ) {
193             if(pos > start ) {
194                 String JavaDoc comp = path.substring(start,pos);
195                 comps.add(comp);
196             }
197             start = pos + pat.length();
198             pos = path.indexOf(pat,start);
199         }
200         if( start < path.length()) {
201             comps.add(path.substring(start));
202         }
203         String JavaDoc [] result = new String JavaDoc[comps.size()];
204         for(int i=0; i < comps.size(); i++) {
205             result[i] = (String JavaDoc)comps.elementAt(i);
206         }
207         return result;
208     }
209
210
211     /**
212      * Chains the <tt>wrapped</tt> throwable to the <tt>wrapper</tt> throwable.
213      *
214      * @param wrapper The wrapper throwable
215      * @param wrapped The throwable to be wrapped
216      */

217     public void chainException(Throwable JavaDoc wrapper, Throwable JavaDoc wrapped) {
218         // do nothing
219
}
220
221  }
222
Popular Tags