Author: markt Date: Tue Sep 29 12:13:20 2015 New Revision: 1705842 URL: http://svn.apache.org/viewvc?rev=1705842&view=rev Log: Preparatory work for fixing https://bz.apache.org/bugzilla/show_bug.cgi?id=56777 Add support for "classpath:" URLs.
Added: tomcat/trunk/java/org/apache/catalina/webresources/ClasspathURLStreamHandler.java (with props) tomcat/trunk/test/org/apache/catalina/webresources/TestClasspathUrlStreamHandler.java (with props) tomcat/trunk/test/org/apache/tomcat/util/file/ Modified: tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/webresources/TomcatURLStreamHandlerFactory.java Added: tomcat/trunk/java/org/apache/catalina/webresources/ClasspathURLStreamHandler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/ClasspathURLStreamHandler.java?rev=1705842&view=auto ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/ClasspathURLStreamHandler.java (added) +++ tomcat/trunk/java/org/apache/catalina/webresources/ClasspathURLStreamHandler.java Tue Sep 29 12:13:20 2015 @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.webresources; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +import org.apache.tomcat.util.res.StringManager; + +public class ClasspathURLStreamHandler extends URLStreamHandler { + + private static final StringManager sm = + StringManager.getManager(ClasspathURLStreamHandler.class); + + + @Override + protected URLConnection openConnection(URL u) throws IOException { + String path = u.getPath(); + + // Thread context class loader first + URL classpathUrl = Thread.currentThread().getContextClassLoader().getResource(path); + if (classpathUrl == null) { + // This class's class loader if no joy with the tccl + classpathUrl = ClasspathURLStreamHandler.class.getResource(path); + } + + if (classpathUrl == null) { + throw new FileNotFoundException(sm.getString("classpathUrlStreamHandler.notFound", u)); + } + + return classpathUrl.openConnection(); + } +} Propchange: tomcat/trunk/java/org/apache/catalina/webresources/ClasspathURLStreamHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties?rev=1705842&r1=1705841&r2=1705842&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties Tue Sep 29 12:13:20 2015 @@ -25,6 +25,8 @@ cache.backgroundEvictFail=The background cache.objectMaxSizeTooBig=The value of [{0}]kB for objectMaxSize is larger than the limit of maxSize/20 so has been reduced to [{1}]kB cache.objectMaxSizeTooBigBytes=The value specified for the maximum object size to cache [{0}]kB is greater than Integer.MAX_VALUE bytes which is the maximum size that can be cached. The limit will be set to Integer.MAX_VALUE bytes. +classpathUrlStreamHandler.notFound=Unable to load the resource [{0}] using the thread context class loader or the current class's class loader + dirResourceSet.manifestFail=Failed to read manifest from [{0}] dirResourceSet.notDirectory=The directory specified by base and internal path [{0}]{1}[{2}] does not exist. dirResourceSet.writeExists=The target of the write already exists Modified: tomcat/trunk/java/org/apache/catalina/webresources/TomcatURLStreamHandlerFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/TomcatURLStreamHandlerFactory.java?rev=1705842&r1=1705841&r2=1705842&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/TomcatURLStreamHandlerFactory.java (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/TomcatURLStreamHandlerFactory.java Tue Sep 29 12:13:20 2015 @@ -26,6 +26,7 @@ import java.util.concurrent.CopyOnWriteA public class TomcatURLStreamHandlerFactory implements URLStreamHandlerFactory { private static final String WAR_PROTOCOL = "war"; + private static final String CLASSPTH_PROTOCOL = "classpath"; // Singleton instance private static volatile TomcatURLStreamHandlerFactory instance = null; @@ -150,6 +151,8 @@ public class TomcatURLStreamHandlerFacto // it. if (WAR_PROTOCOL.equals(protocol)) { return new WarURLStreamHandler(); + } else if (CLASSPTH_PROTOCOL.equals(protocol)) { + return new ClasspathURLStreamHandler(); } // Application handlers Added: tomcat/trunk/test/org/apache/catalina/webresources/TestClasspathUrlStreamHandler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestClasspathUrlStreamHandler.java?rev=1705842&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/TestClasspathUrlStreamHandler.java (added) +++ tomcat/trunk/test/org/apache/catalina/webresources/TestClasspathUrlStreamHandler.java Tue Sep 29 12:13:20 2015 @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.catalina.webresources; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Properties; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestClasspathUrlStreamHandler { + + @BeforeClass + public static void setup() { + TomcatURLStreamHandlerFactory.getInstance(); + } + + @Test + public void testClasspathURL01() throws IOException { + URL u = new URL("classpath:/org/apache/catalina/webresources/LocalStrings.properties"); + InputStream is = u.openStream(); + Properties p = new Properties(); + p.load(is); + String msg = (String) p.get("dirResourceSet.writeNpe"); + Assert.assertEquals("The input stream may not be null", msg); + } +} Propchange: tomcat/trunk/test/org/apache/catalina/webresources/TestClasspathUrlStreamHandler.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org