Author: nbubna
Date: Tue Sep 2 18:25:39 2008
New Revision: 691455
URL: http://svn.apache.org/viewvc?rev=691455&view=rev
Log:
VELOCITY-585 re-implement this using reflection to avoid compilation errors in
JDK 1.4 (and add test case to confirm reflection works)
Added:
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
(with props)
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/URLResourceLoader.java
Modified:
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/URLResourceLoader.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/URLResourceLoader.java?rev=691455&r1=691454&r2=691455&view=diff
==============================================================================
---
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/URLResourceLoader.java
(original)
+++
velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/URLResourceLoader.java
Tue Sep 2 18:25:39 2008
@@ -21,11 +21,13 @@
import java.io.InputStream;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.commons.lang.StringUtils;
+import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
@@ -42,6 +44,7 @@
private String[] roots = null;
protected HashMap templateRoots = null;
private int timeout = -1;
+ private Method[] timeoutMethods;
/**
* @see
org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
@@ -62,7 +65,19 @@
timeout = configuration.getInt("timeout", -1);
if (timeout > 0)
{
- log.debug("URLResourceLoader : timeout set to "+timeout);
+ try
+ {
+ Class[] types = new Class[] { Integer.TYPE };
+ Method conn =
URLConnection.class.getMethod("setConnectTimeout", types);
+ Method read = URLConnection.class.getMethod("setReadTimeout",
types);
+ timeoutMethods = new Method[] { conn, read };
+ log.debug("URLResourceLoader : timeout set to "+timeout);
+ }
+ catch (NoSuchMethodException nsme)
+ {
+ log.debug("URLResourceLoader : Java 1.5+ is required to
customize timeout!", nsme);
+ timeout = -1;
+ }
}
// init the template paths map
@@ -96,11 +111,7 @@
{
URL u = new URL(roots[i] + name);
URLConnection conn = u.openConnection();
- if (timeout > 0)
- {
- conn.setConnectTimeout(timeout);
- conn.setReadTimeout(timeout);
- }
+ tryToSetTimeout(conn);
inputStream = conn.getInputStream();
if (inputStream != null)
@@ -178,11 +189,7 @@
// get a connection to the URL
URL u = new URL(root + name);
URLConnection conn = u.openConnection();
- if (timeout > 0)
- {
- conn.setConnectTimeout(timeout);
- conn.setReadTimeout(timeout);
- }
+ tryToSetTimeout(conn);
return conn.getLastModified();
}
catch (IOException ioe)
@@ -194,4 +201,32 @@
}
}
+ /**
+ * Returns the current, custom timeout setting. If negative, there is no
custom timeout.
+ * @since 1.6
+ */
+ public int getTimeout()
+ {
+ return timeout;
+ }
+
+ private void tryToSetTimeout(URLConnection conn)
+ {
+ if (timeout > 0)
+ {
+ Object[] arg = new Object[] { new Integer(timeout) };
+ try
+ {
+ timeoutMethods[0].invoke(conn, arg);
+ timeoutMethods[1].invoke(conn, arg);
+ }
+ catch (Exception e)
+ {
+ String msg = "Unexpected exception while setting connection
timeout for "+conn;
+ log.error(msg, e);
+ throw new VelocityException(msg, e);
+ }
+ }
+ }
+
}
Added:
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
URL:
http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java?rev=691455&view=auto
==============================================================================
---
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
(added)
+++
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
Tue Sep 2 18:25:39 2008
@@ -0,0 +1,81 @@
+package org.apache.velocity.test;
+
+/*
+ * 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.
+ */
+
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.resource.loader.URLResourceLoader;
+import org.apache.velocity.test.misc.TestLogChute;
+
+/**
+ * This class tests support for custom timeouts in URLResourceLoader.
+ */
+public class URLResourceLoaderTimeoutTestCase extends BaseEvalTestCase
+{
+ private static boolean isJava5plus;
+ static
+ {
+ try
+ {
+ Class.forName("java.lang.annotation.Annotation");
+ isJava5plus = true;
+ }
+ catch (ClassNotFoundException cnfe)
+ {
+ isJava5plus = false;
+ }
+ }
+ private TestLogChute logger = new TestLogChute();
+ private URLResourceLoader loader = new URLResourceLoader();
+ private int timeout = 2000;
+
+ public URLResourceLoaderTimeoutTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ engine.setProperty("resource.loader", "url");
+ engine.setProperty("url.resource.loader.instance", loader);
+ engine.setProperty("url.resource.loader.timeout", new
Integer(timeout));
+
+ // actual instance of logger
+ logger.on();
+ engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
+ engine.setProperty("runtime.log.logsystem.test.level", "debug");
+ engine.init();
+ }
+
+ public void testTimeout()
+ {
+ if (isJava5plus)
+ {
+ System.out.println("Testing a 1.5+ JDK: "+logger.getLog());
+ assertEquals(timeout, loader.getTimeout());
+ }
+ else
+ {
+ System.out.println("Testing a pre-1.5 JDK");
+ assertEquals(-1, loader.getTimeout());
+ }
+ }
+
+}
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
------------------------------------------------------------------------------
svn:executable = *
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
------------------------------------------------------------------------------
svn:keywords = Revision
Propchange:
velocity/engine/trunk/src/test/org/apache/velocity/test/URLResourceLoaderTimeoutTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain