001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.compat;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.URL;
022: import java.net.MalformedURLException;
023: import java.util.Vector;
024: import java.util.Locale;
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027: import java.text.MessageFormat;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * General-purpose utility to provide backward-compatibility and JDK
034: * independence. This allow use of JDK1.3 ( or higher ) facilities if
035: * available, while maintaining the code compatible with older VMs.
036: *
037: * The goal is to make backward-compatiblity reasonably easy.
038: *
039: * The base class supports JDK1.3 behavior.
040: *
041: * @author Tim Funk
042: */
043: public class JdkCompat {
044:
045: // ------------------------------------------------------- Static Variables
046:
047: /**
048: * class providing java2 support
049: */
050: static final String JAVA14_SUPPORT = "org.apache.tomcat.util.compat.Jdk14Compat";
051: /**
052: * Commons logger wrapper
053: */
054: static Log logger = LogFactory.getLog(JdkCompat.class);
055:
056: /** Return java version as a string
057: */
058: public static String getJavaVersion() {
059: return javaVersion;
060: }
061:
062: public static boolean isJava2() {
063: return java2;
064: }
065:
066: public static boolean isJava14() {
067: return java14;
068: }
069:
070: public static boolean isJava15() {
071: return java15;
072: }
073:
074: // -------------------- Implementation --------------------
075:
076: // from ant
077: public static final String JAVA_1_0 = "1.0";
078: public static final String JAVA_1_1 = "1.1";
079: public static final String JAVA_1_2 = "1.2";
080: public static final String JAVA_1_3 = "1.3";
081: public static final String JAVA_1_4 = "1.4";
082: public static final String JAVA_1_5 = "1.5";
083:
084: static String javaVersion;
085: static boolean java2 = false;
086: static boolean java14 = false;
087: static boolean java15 = false;
088: static JdkCompat jdkCompat;
089:
090: static {
091: init();
092: }
093:
094: private static void init() {
095: try {
096: javaVersion = JAVA_1_0;
097: Class.forName("java.lang.Void");
098: javaVersion = JAVA_1_1;
099: Class.forName("java.lang.ThreadLocal");
100: java2 = true;
101: javaVersion = JAVA_1_2;
102: Class.forName("java.lang.StrictMath");
103: javaVersion = JAVA_1_3;
104: Class.forName("java.lang.CharSequence");
105: javaVersion = JAVA_1_4;
106: java14 = true;
107: Class.forName("java.lang.Appendable");
108: javaVersion = JAVA_1_5;
109: java15 = true;
110: } catch (ClassNotFoundException cnfe) {
111: // swallow as we've hit the max class version that we have
112: }
113: if (java14) {
114: try {
115: Class c = Class.forName(JAVA14_SUPPORT);
116: jdkCompat = (JdkCompat) c.newInstance();
117: } catch (Exception ex) {
118: jdkCompat = new JdkCompat();
119: }
120: } else {
121: jdkCompat = new JdkCompat();
122: // Install jar handler if none installed
123: }
124: }
125:
126: // ----------------------------------------------------------- Constructors
127: /**
128: * Default no-arg constructor
129: */
130: protected JdkCompat() {
131: }
132:
133: // --------------------------------------------------------- Public Methods
134: /**
135: * Get a compatibiliy helper class.
136: */
137: public static JdkCompat getJdkCompat() {
138: return jdkCompat;
139: }
140:
141: /**
142: * Return the URI for the given file. Originally created for
143: * o.a.c.loader.WebappClassLoader
144: *
145: * @param File to wrap into URI
146: * @return A URI as a URL
147: */
148: public URL getURI(File file) throws MalformedURLException {
149:
150: File realFile = file;
151: try {
152: realFile = realFile.getCanonicalFile();
153: } catch (IOException e) {
154: // Ignore
155: }
156:
157: return realFile.toURL();
158: }
159:
160: /**
161: * Return the maximum amount of memory the JVM will attempt to use.
162: */
163: public long getMaxMemory() {
164: return (-1L);
165: }
166:
167: /**
168: * Print out a partial servlet stack trace (truncating at the last
169: * occurrence of javax.servlet.).
170: */
171: public String getPartialServletStackTrace(Throwable t) {
172: StringWriter stackTrace = new StringWriter();
173: t.printStackTrace(new PrintWriter(stackTrace));
174: String st = stackTrace.toString();
175: int i = st
176: .lastIndexOf("org.apache.catalina.core.ApplicationFilterChain.internalDoFilter");
177: if (i > -1) {
178: return st.substring(0, i - 4);
179: } else {
180: return st;
181: }
182: }
183:
184: /**
185: * Splits a string into it's components.
186: * @param path String to split
187: * @param pat Pattern to split at
188: * @return the components of the path
189: */
190: public String[] split(String path, String pat) {
191: Vector comps = new Vector();
192: int pos = path.indexOf(pat);
193: int start = 0;
194: while (pos >= 0) {
195: if (pos > start) {
196: String comp = path.substring(start, pos);
197: comps.add(comp);
198: }
199: start = pos + pat.length();
200: pos = path.indexOf(pat, start);
201: }
202: if (start < path.length()) {
203: comps.add(path.substring(start));
204: }
205: String[] result = new String[comps.size()];
206: for (int i = 0; i < comps.size(); i++) {
207: result[i] = (String) comps.elementAt(i);
208: }
209: return result;
210: }
211:
212: /**
213: * Chains the <tt>wrapped</tt> throwable to the <tt>wrapper</tt> throwable.
214: *
215: * @param wrapper The wrapper throwable
216: * @param wrapped The throwable to be wrapped
217: */
218: public void chainException(Throwable wrapper, Throwable wrapped) {
219: // do nothing
220: }
221:
222: }
|