Updated Branches: refs/heads/master 40b8e890b -> f7d3ad2d7
WICKET-4971: cache subscribed methods per class Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/f7d3ad2d Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/f7d3ad2d Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/f7d3ad2d Branch: refs/heads/master Commit: f7d3ad2d7a2788ca60bed39ca76c624e49671f48 Parents: 40b8e89 Author: Emond Papegaaij <[email protected]> Authored: Mon Jan 14 07:47:37 2013 +0100 Committer: Emond Papegaaij <[email protected]> Committed: Mon Jan 14 08:31:01 2013 +0100 ---------------------------------------------------------------------- .../AtmosphereEventSubscriptionCollector.java | 49 ++++++++++++--- 1 files changed, 39 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/f7d3ad2d/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereEventSubscriptionCollector.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereEventSubscriptionCollector.java b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereEventSubscriptionCollector.java index db5f50b..f3c749d 100644 --- a/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereEventSubscriptionCollector.java +++ b/wicket-experimental/wicket-atmosphere/src/main/java/org/apache/wicket/atmosphere/AtmosphereEventSubscriptionCollector.java @@ -17,6 +17,8 @@ package org.apache.wicket.atmosphere; import java.lang.reflect.Method; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import org.apache.wicket.Component; import org.apache.wicket.Page; @@ -25,6 +27,10 @@ import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.application.IComponentOnBeforeRenderListener; import org.apache.wicket.behavior.Behavior; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.collect.ImmutableList; + /** * Collects {@linkplain Subscribe event subscriptions} on components. Subscriptions are refreshed on * every render of component. If a page contains a component with a subscription, an @@ -35,6 +41,7 @@ import org.apache.wicket.behavior.Behavior; */ public class AtmosphereEventSubscriptionCollector implements IComponentOnBeforeRenderListener { + private Cache<Class<?>, Iterable<Method>> subscribedMethodsCache; private EventBus eventBus; /** @@ -45,29 +52,51 @@ public class AtmosphereEventSubscriptionCollector implements IComponentOnBeforeR public AtmosphereEventSubscriptionCollector(EventBus eventBus) { this.eventBus = eventBus; + subscribedMethodsCache = CacheBuilder.newBuilder().build(); } @Override public void onBeforeRender(Component component) { - for (Method curMethod : component.getClass().getMethods()) + for (Method curMethod : findSubscribedMethods(component.getClass())) + { + subscribeComponent(component, null, curMethod); + } + for (Behavior curBehavior : component.getBehaviors()) { - if (curMethod.isAnnotationPresent(Subscribe.class)) + for (Method curMethod : findSubscribedMethods(curBehavior.getClass())) { verifyMethodParameters(curMethod); - subscribeComponent(component, null, curMethod); + subscribeComponent(component, curBehavior, curMethod); } } - for (Behavior curBehavior : component.getBehaviors()) + } + + private Iterable<Method> findSubscribedMethods(final Class<?> clazz) + { + try { - for (Method curMethod : curBehavior.getClass().getMethods()) + return subscribedMethodsCache.get(clazz, new Callable<Iterable<Method>>() { - if (curMethod.isAnnotationPresent(Subscribe.class)) + @Override + public Iterable<Method> call() throws Exception { - verifyMethodParameters(curMethod); - subscribeComponent(component, curBehavior, curMethod); + ImmutableList.Builder<Method> ret = ImmutableList.builder(); + for (Method curMethod : clazz.getMethods()) + { + if (curMethod.isAnnotationPresent(Subscribe.class)) + { + verifyMethodParameters(curMethod); + ret.add(curMethod); + } + } + return ret.build(); } - } + }); + } + catch (ExecutionException e) + { + throw new WicketRuntimeException(e); } } @@ -77,7 +106,7 @@ public class AtmosphereEventSubscriptionCollector implements IComponentOnBeforeR if (params.length != 2 || !params[0].equals(AjaxRequestTarget.class)) throw new WicketRuntimeException("@Subscribe can only be used on " + "methods with 2 params, of which the first is AjaxRequestTarget. " + method + - " does conform to this signature."); + " does not conform to this signature."); } private void subscribeComponent(Component component, Behavior behavior, Method method)
