tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject svn commit: r1150151 - in /tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5: internal/services/ComponentInstantiatorSourceImpl.java services/ComponentEventHandler.java services/transform/TransformationSupport.java
Date Sat, 23 Jul 2011 16:58:00 GMT
Author: hlship
Date: Sat Jul 23 16:57:59 2011
New Revision: 1150151

URL: http://svn.apache.org/viewvc?rev=1150151&view=rev
Log:
TAP5-1508: Add method addEventHandler() to TransformationSupport

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentEventHandler.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/TransformationSupport.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java?rev=1150151&r1=1150150&r2=1150151&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java
Sat Jul 23 16:57:59 2011
@@ -14,9 +14,6 @@
 
 package org.apache.tapestry5.internal.services;
 
-import java.util.Map;
-import java.util.Set;
-
 import org.apache.tapestry5.ComponentResources;
 import org.apache.tapestry5.SymbolConstants;
 import org.apache.tapestry5.internal.InternalComponentResources;
@@ -42,30 +39,20 @@ import org.apache.tapestry5.ioc.services
 import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
 import org.apache.tapestry5.model.ComponentModel;
 import org.apache.tapestry5.model.MutableComponentModel;
-import org.apache.tapestry5.plastic.ClassInstantiator;
-import org.apache.tapestry5.plastic.InstructionBuilder;
-import org.apache.tapestry5.plastic.InstructionBuilderCallback;
-import org.apache.tapestry5.plastic.MethodDescription;
-import org.apache.tapestry5.plastic.PlasticClass;
-import org.apache.tapestry5.plastic.PlasticClassEvent;
-import org.apache.tapestry5.plastic.PlasticClassListener;
-import org.apache.tapestry5.plastic.PlasticField;
-import org.apache.tapestry5.plastic.PlasticManager;
+import org.apache.tapestry5.plastic.*;
 import org.apache.tapestry5.plastic.PlasticManager.PlasticManagerBuilder;
-import org.apache.tapestry5.plastic.PlasticManagerDelegate;
-import org.apache.tapestry5.plastic.PlasticUtils;
-import org.apache.tapestry5.plastic.TransformationOption;
 import org.apache.tapestry5.runtime.Component;
+import org.apache.tapestry5.runtime.ComponentEvent;
 import org.apache.tapestry5.runtime.ComponentResourcesAware;
-import org.apache.tapestry5.services.ComponentClassResolver;
-import org.apache.tapestry5.services.InvalidationListener;
-import org.apache.tapestry5.services.UpdateListener;
-import org.apache.tapestry5.services.UpdateListenerHub;
+import org.apache.tapestry5.services.*;
 import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
 import org.apache.tapestry5.services.transform.ControlledPackageType;
 import org.apache.tapestry5.services.transform.TransformationSupport;
 import org.slf4j.Logger;
 
+import java.util.Map;
+import java.util.Set;
+
 /**
  * A wrapper around a {@link PlasticManager} that allows certain classes to be modified as
they are loaded.
  */
@@ -91,7 +78,7 @@ public final class ComponentInstantiator
     private final InternalComponentInvalidationEventHub invalidationHub;
 
     private final boolean productionMode;
-    
+
     private final ComponentClassResolver resolver;
 
     // These change whenever the invalidation event hub sends an invalidation notification
@@ -111,28 +98,28 @@ public final class ComponentInstantiator
 
     public ComponentInstantiatorSourceImpl(Logger logger,
 
-    LoggerSource loggerSource,
+                                           LoggerSource loggerSource,
+
+                                           @Builtin
+                                           PlasticProxyFactory proxyFactory,
 
-    @Builtin
-    PlasticProxyFactory proxyFactory,
+                                           @Primary
+                                           ComponentClassTransformWorker2 transformerChain,
 
-    @Primary
-    ComponentClassTransformWorker2 transformerChain,
+                                           InternalRequestGlobals internalRequestGlobals,
 
-    InternalRequestGlobals internalRequestGlobals,
+                                           ClasspathURLConverter classpathURLConverter,
 
-    ClasspathURLConverter classpathURLConverter,
+                                           OperationTracker tracker,
 
-    OperationTracker tracker,
+                                           Map<String, ControlledPackageType> configuration,
 
-    Map<String, ControlledPackageType> configuration,
+                                           @Symbol(SymbolConstants.PRODUCTION_MODE)
+                                           boolean productionMode,
 
-    @Symbol(SymbolConstants.PRODUCTION_MODE)
-    boolean productionMode,
-    
-    ComponentClassResolver resolver,
+                                           ComponentClassResolver resolver,
 
-    InternalComponentInvalidationEventHub invalidationHub)
+                                           InternalComponentInvalidationEventHub invalidationHub)
     {
         this.parent = proxyFactory.getClassLoader();
         this.transformerChain = transformerChain;
@@ -298,7 +285,7 @@ public final class ComponentInstantiator
 
                         if (isRoot
                                 && !(parentClassName.equals("java.lang.Object") ||
parentClassName
-                                        .equals("groovy.lang.GroovyObjectSupport")))
+                                .equals("groovy.lang.GroovyObjectSupport")))
                         {
                             String suggestedPackageName = buildSuggestedPackageName(className);
 
@@ -324,7 +311,7 @@ public final class ComponentInstantiator
 
                         boolean isPage = resolver.isPage(className);
 
-                        MutableComponentModel model = new MutableComponentModelImpl(className,
logger, baseResource,
+                        final MutableComponentModel model = new MutableComponentModelImpl(className,
logger, baseResource,
                                 parentModel, isPage);
 
                         transformerChain.transform(plasticClass, new TransformationSupport()
@@ -334,8 +321,7 @@ public final class ComponentInstantiator
                                 try
                                 {
                                     return PlasticInternalUtils.toClass(manager.getClassLoader(),
typeName);
-                                }
-                                catch (ClassNotFoundException ex)
+                                } catch (ClassNotFoundException ex)
                                 {
                                     throw new RuntimeException(String.format(
                                             "Unable to convert type '%s' to a Class: %s",
typeName,
@@ -347,6 +333,50 @@ public final class ComponentInstantiator
                             {
                                 return isRoot;
                             }
+
+                            public void addEventHandler(final String eventType, final int
minContextValues, final String operationDescription, final ComponentEventHandler handler)
+                            {
+                                assert InternalUtils.isNonBlank(eventType);
+                                assert minContextValues >= 0;
+                                assert handler != null;
+
+                                MethodAdvice advice = new MethodAdvice()
+                                {
+                                    public void advise(MethodInvocation invocation)
+                                    {
+                                        final ComponentEvent event = (ComponentEvent) invocation.getParameter(0);
+
+                                        boolean matches = event.matches(eventType, "", minContextValues);
+
+                                        if (matches)
+                                        {
+                                            final Component instance = (Component) invocation.getInstance();
+
+                                            tracker.invoke(operationDescription, new Invokable<Object>()
+                                            {
+                                                public Object invoke()
+                                                {
+                                                    handler.handleEvent(instance, event);
+
+                                                    return null;
+                                                }
+                                            });
+                                        }
+
+                                        // Order of operations is key here. This logic takes
precedence; base class event dispatch and event handler methods
+                                        // in the class come AFTER.
+
+                                        invocation.proceed();
+
+                                        if (matches)
+                                        {
+                                            invocation.setReturnValue(true);
+                                        }
+                                    }
+                                };
+
+                                plasticClass.introduceMethod(TransformConstants.DISPATCH_COMPONENT_EVENT_DESCRIPTION).addAdvice(advice);
+                            }
                         }, model);
 
                         classToModel.put(className, model);

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentEventHandler.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentEventHandler.java?rev=1150151&r1=1150150&r2=1150151&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentEventHandler.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentEventHandler.java
Sat Jul 23 16:57:59 2011
@@ -1,4 +1,4 @@
-// Copyright 2010 The Apache Software Foundation
+// Copyright 2010, 2011 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -18,12 +18,15 @@ import org.apache.tapestry5.runtime.Comp
 import org.apache.tapestry5.runtime.ComponentEvent;
 
 /**
- * Interface used with {@link ClassTransformation#addComponentEventHandler(String, int, String,
ComponentEventHandler)}.
- * 
+ * Interface used with  {@link org.apache.tapestry5.services.transform.TransformationSupport#addEventHandler(String,
int, ComponentEventHandler)}} (and, in the old
+ * API, {@link ClassTransformation#addComponentEventHandler(String, int, String, ComponentEventHandler)}).
+ *
  * @since 5.2.0
  */
 public interface ComponentEventHandler
 {
-    /** Handles the event. */
+    /**
+     * Handles the event.
+     */
     void handleEvent(Component instance, ComponentEvent event);
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/TransformationSupport.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/TransformationSupport.java?rev=1150151&r1=1150150&r2=1150151&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/TransformationSupport.java
(original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/transform/TransformationSupport.java
Sat Jul 23 16:57:59 2011
@@ -15,17 +15,17 @@
 package org.apache.tapestry5.services.transform;
 
 import org.apache.tapestry5.plastic.PlasticClass;
+import org.apache.tapestry5.services.ComponentEventHandler;
 
 /**
  * Additional utilities, beyond {@link PlasticClass}, needed when transforming.
- * 
+ *
  * @since 5.3
  */
 public interface TransformationSupport
 {
     /**
-     * @param typeName
-     *            Java type name (which may be a primitive type or array, or fully qualified
class name)
+     * @param typeName Java type name (which may be a primitive type or array, or fully qualified
class name)
      * @return corresponding Java Class
      */
     Class toClass(String typeName);
@@ -33,8 +33,20 @@ public interface TransformationSupport
     /**
      * Returns true if the class being transformed is a root class: it does not inherit
      * from another transformed class, but instead inherits from Object.
-     * 
+     *
      * @return true if root
      */
     boolean isRootTransformation();
+
+    /**
+     * Adds an event handler. Added event handlers execute <em>before</em> calls
to super-class event handlers,
+     * or calls to event handler methods.
+     *
+     * @param eventType            type of event
+     * @param minContextValues     number of context values required to activate the handler
+     * @param operationDescription Used with {@link org.apache.tapestry5.ioc.OperationTracker}
when invoking the handler
+     * @param handler              code to execute when the event matches
+     */
+    void addEventHandler(String eventType, int minContextValues, String operationDescription,
ComponentEventHandler handler);
+
 }



Mime
View raw message