Luiz-Otavio Zorzella wrote:
This patch fixes an incredibly inefficient, often-used encodeURL method. The merits of the patch should be self-evident --
1) it preserves the original goal of using the jdk 1.4's encode if available, while
2) it does not call 2 java encode method each time and
3) it does not do so much reflection each time around
Let me know of any concerns,
Zorzella
------------------------------------------------------------------------
Index: src/share/org/apache/struts/util/RequestUtils.java
===================================================================
RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
retrieving revision 1.90
diff -u -r1.90 RequestUtils.java
--- src/share/org/apache/struts/util/RequestUtils.java 26 Feb 2003 04:48:56 -0000 1.90
+++ src/share/org/apache/struts/util/RequestUtils.java 1 Mar 2003 23:23:00 -0000
@@ -1896,6 +1896,19 @@
return errors;
}
+ private static Method _encode = null;
+
+ static {
+ try {
+ // get version of encode method with two String args
+ Class[] args = new Class[] { String.class, String.class };
+ _encode = URLEncoder.class.getMethod("encode", args);
+ } catch (Exception e) {
+ log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
+ }
+ }
+
+ /**
* Use the new URLEncoder.encode() method from java 1.4 if available, else
* use the old deprecated version. This method uses reflection to find the appropriate
@@ -1904,27 +1917,15 @@
* @return String - the encoded url.
*/
public static String encodeURL(String url) {
- // default to old version
- String encodedURL = URLEncoder.encode(url);
- Class encoderClass = URLEncoder.class;
try {
- // get version of encode method with two String args
- Class[] args = new Class[] { String.class, String.class };
- Method encode = encoderClass.getMethod("encode", args);
-
- // encode url with new 1.4 method and UTF-8 encoding
- encodedURL = (String) encode.invoke(null, new Object[] { url, "UTF-8" });
-
- } catch (IllegalAccessException e) {
- log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
- } catch (InvocationTargetException e) {
- log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
- } catch (NoSuchMethodException e) {
+ if (_encode == null)
+ return (String) _encode.invoke(null, new Object[] { url, "UTF-8" });
+ } catch (Exception e) {
log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
}
-
- return encodedURL;
+ + return URLEncoder.encode(url);
}
}
------------------------------------------------------------------------
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Index: src/share/org/apache/struts/util/RequestUtils.java =================================================================== RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v retrieving revision 1.90 diff -u -r1.90 RequestUtils.java --- src/share/org/apache/struts/util/RequestUtils.java 26 Feb 2003 04:48:56 -0000 1.90 +++ src/share/org/apache/struts/util/RequestUtils.java 1 Mar 2003 23:37:19 -0000 @@ -1896,6 +1896,19 @@ return errors; } + private static Method _encode = null; + + static { + try { + // get version of encode method with two String args + Class[] args = new Class[] { String.class, String.class }; + _encode = URLEncoder.class.getMethod("encode", args); + } catch (Exception e) { + log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); + } + } + + /** * Use the new URLEncoder.encode() method from java 1.4 if available, else * use the old deprecated version. This method uses reflection to find the appropriate @@ -1904,27 +1917,15 @@ * @return String - the encoded url. */ public static String encodeURL(String url) { - // default to old version - String encodedURL = URLEncoder.encode(url); - Class encoderClass = URLEncoder.class; try { - // get version of encode method with two String args - Class[] args = new Class[] { String.class, String.class }; - Method encode = encoderClass.getMethod("encode", args); - - // encode url with new 1.4 method and UTF-8 encoding - encodedURL = (String) encode.invoke(null, new Object[] { url, "UTF-8" }); - - } catch (IllegalAccessException e) { - log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); - } catch (InvocationTargetException e) { - log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); - } catch (NoSuchMethodException e) { + if (_encode != null) + return (String) _encode.invoke(null, new Object[] { url, "UTF-8" }); + } catch (Exception e) { log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e); } - - return encodedURL; + + return URLEncoder.encode(url); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]