You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by jk...@apache.org on 2007/04/16 06:47:52 UTC

svn commit: r529131 - in /tapestry/tapestry4/trunk/tapestry-framework/src: java/org/apache/tapestry/enhance/ java/org/apache/tapestry/util/ test/org/apache/tapestry/enhance/ test/org/apache/tapestry/util/

Author: jkuhnert
Date: Sun Apr 15 21:47:51 2007
New Revision: 529131

URL: http://svn.apache.org/viewvc?view=rev&rev=529131
Log:
-) Fixes TAPESTRY-1410. Generics weren't detecting void return type. 

-) Enhanced ScriptUtils to use a pooled pattern cache so that synchronized could be eliminated - validated slightly faster runtime via invocationCount = 400 / threadPoolSize = 5 but took test params out after finishing to not drag regular test times down.

Modified:
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/MethodSignatureImpl.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/RegexpPoolObjectFactory.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/ScriptUtils.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/ScriptUtilsTest.java

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java?view=diff&rev=529131&r1=529130&r2=529131
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java Sun Apr 15 21:47:51 2007
@@ -59,7 +59,7 @@
                         return resolvedType;
                 }
 
-                return null;
+                return Void.TYPE;
             }
         }
 

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/MethodSignatureImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/MethodSignatureImpl.java?view=diff&rev=529131&r1=529130&r2=529131
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/MethodSignatureImpl.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/MethodSignatureImpl.java Sun Apr 15 21:47:51 2007
@@ -13,10 +13,10 @@
 // limitations under the License.
 package org.apache.tapestry.enhance;
 
-import java.lang.reflect.Method;
-
 import org.apache.hivemind.service.ClassFabUtils;
 
+import java.lang.reflect.Method;
+
 
 /**
  * JDK 1.4 based version of {@link MethodSignature}. 
@@ -71,7 +71,6 @@
     {
         if (_hashCode == -1)
         {
-
             _hashCode = _returnType.hashCode();
 
             _hashCode = 31 * _hashCode + _name.hashCode();

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/RegexpPoolObjectFactory.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/RegexpPoolObjectFactory.java?view=diff&rev=529131&r1=529130&r2=529131
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/RegexpPoolObjectFactory.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/RegexpPoolObjectFactory.java Sun Apr 15 21:47:51 2007
@@ -16,6 +16,8 @@
 import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
 import org.apache.hivemind.util.Defense;
 
+import java.util.regex.Pattern;
+
 
 /**
  * Implementation of {@link BaseKeyedPoolableObjectFactory} for regexp patterns
@@ -24,8 +26,6 @@
  */
 public class RegexpPoolObjectFactory extends BaseKeyedPoolableObjectFactory
 {
-    //private PatternCompiler _patternCompiler;
-    
     /**
      * {@inheritDoc}
      */
@@ -37,7 +37,7 @@
         
         String regexp = (String)pattern;
 
-        return java.util.regex.Pattern.compile(regexp, java.util.regex.Pattern.COMMENTS | java.util.regex.Pattern.DOTALL);
+        return java.util.regex.Pattern.compile(regexp, java.util.regex.Pattern.COMMENTS | java.util.regex.Pattern.DOTALL | Pattern.MULTILINE);
     }
 
 }

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/ScriptUtils.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/ScriptUtils.java?view=diff&rev=529131&r1=529130&r2=529131
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/ScriptUtils.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/ScriptUtils.java Sun Apr 15 21:47:51 2007
@@ -14,6 +14,9 @@
 package org.apache.tapestry.util;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.pool.KeyedPoolableObjectFactory;
+import org.apache.commons.pool.impl.GenericKeyedObjectPool;
+import org.apache.hivemind.ApplicationRuntimeException;
 
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -32,16 +35,31 @@
      * XML character data end.
      */
     public static final String END_COMMENT = "\n//]]>\n</script>\n";
+
     /**
      * Regexp represenging javascript matches.
      */
-    public static final Pattern SCRIPT_PATTERN =
-        Pattern.compile("(?:<script.*?>)(.*?)(?:<\\/script>)",
-            Pattern.COMMENTS | Pattern.DOTALL | Pattern.MULTILINE);
-    
+    public static final String SCRIPT_PATTERN = "(?:<script.*?>)(.*?)(?:<\\/script>)";
+
+    private static final KeyedPoolableObjectFactory _factory = new RegexpPoolObjectFactory();
+
+    private static final GenericKeyedObjectPool _pool;
+
+    private static final int MAX_ACTIVE = 100;
+
+    private static final long SLEEP_TIME = 1000 * 60 * 4;
+
+    static {
+        _pool = new GenericKeyedObjectPool(_factory, MAX_ACTIVE, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, -1);
+
+        _pool.setMaxIdle(MAX_ACTIVE / 2);
+        _pool.setMinEvictableIdleTimeMillis(MAX_ACTIVE);
+        _pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME);
+    }
+
     /* defeat instantiation */
     private ScriptUtils() { }
-    
+
     /**
      * Takes any <script>contents..</script> tags found in the specified
      * input string and replaces their contents into one large <script></script>
@@ -49,53 +67,69 @@
      * with the addition of {@link #BEGIN_COMMENT} inserted before the logic block and
      * {@link #END_COMMENT} inserted after the logic block.
      *
-     * @param input 
+     * @param input
      *          The string to replace tags on
      * @return The properly formatted string, if any formatting needed to occur.
      */
-    public static synchronized String ensureValidScriptTags(String input) {
+    public static String ensureValidScriptTags(String input) {
+
         if (input == null)
             return null;
-        
-        Matcher matcher = SCRIPT_PATTERN.matcher(input);
-        StringBuffer buffer = new StringBuffer(input.length());
-        
-        boolean matched = false;
-        int end = 0;
-        while (matcher.find()) {
-            
-            matched = true;
-            String str = matcher.group(1);
-            int pos = matcher.start() - end;
-            end = matcher.end();
-            
-            if (str == null || str.trim().equals(""))
-                matcher.appendReplacement(buffer, "");
+
+
+        Pattern compiled = null;
+
+        try {
+
+            compiled = (Pattern)_pool.borrowObject(SCRIPT_PATTERN);
+
+            Matcher matcher = compiled.matcher(input);
+            StringBuffer buffer = new StringBuffer(input.length());
+
+            boolean matched = false;
+            int end = 0;
+            while (matcher.find()) {
+
+                matched = true;
+                String str = matcher.group(1);
+                int pos = matcher.start() - end;
+                end = matcher.end();
+
+                if (str == null || str.trim().equals(""))
+                    matcher.appendReplacement(buffer, "");
+                else {
+                    // gather the text from the beggining to the match into a new buffer
+                    StringBuffer matchLocal = new StringBuffer();
+                    matcher.appendReplacement(matchLocal, BEGIN_COMMENT + "$1" + END_COMMENT);
+
+                    // the first part is always script-less, no need to remove comments from it.
+                    String curr =  matchLocal.toString();
+                    String prefix = curr.substring(0, pos);
+                    String suffix = curr.substring(pos);
+
+                    // the second part is in a script, so remove comments.
+                    suffix = StringUtils.replace(suffix, "<!--", "");
+                    suffix = StringUtils.replace(suffix, "// -->", "");
+                    buffer.append(prefix).append(suffix);
+                }
+            }
+
+            if (!matched)
+                buffer.append(input);
             else {
-                // gather the text from the beggining to the match into a new buffer
-                StringBuffer matchLocal = new StringBuffer();
-                matcher.appendReplacement(matchLocal, BEGIN_COMMENT + "$1" + END_COMMENT);
-                
-                // the first part is always script-less, no need to remove comments from it.
-                String curr =  matchLocal.toString();
-                String prefix = curr.substring(0, pos);
-                String suffix = curr.substring(pos);
-                
-                // the second part is in a script, so remove comments.
-                suffix = StringUtils.replace(suffix, "<!--", "");
-                suffix = StringUtils.replace(suffix, "// -->", "");
-                buffer.append(prefix).append(suffix);
+                //copies non matched character input, ie content after the last script.
+                matcher.appendTail(buffer);
             }
-        }
-        
-        if (!matched)
-            buffer.append(input);
-        else {
-            //copies non matched character input, ie content after the last script.
-            matcher.appendTail(buffer);
-        }
 
-        return buffer.toString();
+            return buffer.toString();
+
+        } catch (Exception e) {
+
+            throw new ApplicationRuntimeException(e);
+        } finally {
+
+            try { _pool.returnObject(SCRIPT_PATTERN, compiled); } catch (Throwable t) { }
+        }
     }
     
     /**

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java?view=diff&rev=529131&r1=529130&r2=529131
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java Sun Apr 15 21:47:51 2007
@@ -112,6 +112,27 @@
         assert base.equals(child);
     }
 
+    public void test_Generic_Method_Hash() {
+
+        Class testClass=MyTest.class;
+
+        Method[] methods = testClass.getMethods();
+
+        for (Method method : methods) {
+            
+            MethodSignatureImpl msi = new GenericsMethodSignatureImpl(testClass, method);
+            msi.hashCode();
+        }
+    }
+
+    public static abstract class BaseTest<T>{ }
+    
+    public static abstract class MyTest<T,E>extends BaseTest<T> {
+
+        public abstract E getRelativeObject();
+        public abstract void setRelativeObject(E e);
+    }
+
     public void test_Find_Type()
     {
         Class clazz=TestGeneric.class;

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/ScriptUtilsTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/ScriptUtilsTest.java?view=diff&rev=529131&r1=529130&r2=529131
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/ScriptUtilsTest.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/ScriptUtilsTest.java Sun Apr 15 21:47:51 2007
@@ -22,7 +22,6 @@
 /**
  * Tests functionality of {@link ScriptUtils}.
  * 
- * @author jkuhnert
  */
 @Test
 public class ScriptUtilsTest extends BaseComponentTestCase
@@ -110,9 +109,8 @@
     /**
      * Tests finding {@link #JAVASCRIPT_NOCOMMENT} with 
      * regular expressions.
-     * @testng.test groups = "functest", "util"
      */
-    public void testFindScript()
+    public void test_Find_Script()
     {
         Perl5Util util = new Perl5Util();
         String expr = "/(?:<script.*?>)((\\n|.)*?)(?:<\\/script>)/";
@@ -129,9 +127,8 @@
     /**
      * Tests finding {@link #JAVASCRIPT_NOCOMMENT} with 
      * regular expressions.
-     * @testng.test groups = "functest", "util"
      */
-    public void testFindMultipleScripts()
+    public void test_Find_Multiple_Scripts()
     {
         Perl5Util util = new Perl5Util();
         String expr = "/(?:<script.*?>)((\\n|.)*?)(?:<\\/script>)/";
@@ -150,9 +147,8 @@
      * Calls {@link ScriptUtils#ensureValidScriptTags(String)} with 
      * {@link #JAVASCRIPT_NOCOMMENT} and tests that it returns a validly
      * marked up content block capable of being embedded in an xml document.
-     * @testng.test groups = "functest" , "util"
      */
-    public void testEnsureValidScriptTags()
+    public void test_Ensure_Valid_Script_Tags()
     {
         assertEquals(ScriptUtils.ensureValidScriptTags(JAVASCRIPT_NOCOMMENT),
                 ScriptUtils.BEGIN_COMMENT
@@ -165,14 +161,12 @@
         assertEquals(ScriptUtils.ensureValidScriptTags("<html>This is html</html>"),
                 "<html>This is html</html>");
     }
-    
-    /**
-     * @testng.test groups = "functest" , "util"
-     */
-    public void testEnsureValidScriptTagsWithHtmlComments()
+
+    public void test_Ensure_Valid_Script_Tags_With_Html_Comments()
     {  
         String data = "<!-- some comments1 -->" + TEST_INPUT1 + "<b>test</b><!-- some comments2 -->";
         data += " <!-- some comments3 -->" + TEST_INPUT1 + "<b>test</b><!-- some comments4 -->";
+        
         String result = ScriptUtils.ensureValidScriptTags(data);
         
         assertTrue(result.indexOf("<!-- some comments1 -->") >= 0);
@@ -184,9 +178,8 @@
     /**
      * Tests that the complete string is returned, with 
      * any js in it "fixed".
-     * @testng.test groups = "functest", "util"
      */
-    public void testCompleteReturn()
+    public void test_Complete_Return()
     {
         assertEquals(ScriptUtils.ensureValidScriptTags(TEST_INPUT1),
                 ScriptUtils.BEGIN_COMMENT