From c960607c4f3a4d099aea4d0a0dc1548b3e29feca Mon Sep 17 00:00:00 2001 From: Christian Kaltepoth Date: Mon, 14 Jul 2014 08:01:48 +0200 Subject: [PATCH] DispatchType now caches the DispatcherTypeProvider list on a per request basis --- .../rewrite/servlet/config/DispatchType.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/DispatchType.java b/config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/DispatchType.java index 405b19f44..b5826fad0 100644 --- a/config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/DispatchType.java +++ b/config-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/DispatchType.java @@ -37,18 +37,13 @@ */ public class DispatchType extends HttpCondition { + private final static String PROVIDER_KEY = DispatchType.class.getName() + "_PROVIDERS"; + private final DispatcherType type; - private final List providers; private DispatchType(final DispatcherType type) { this.type = type; - - // for performance reasons only create the list of providers once - providers = Iterators.asList( - ServiceLoader.loadTypesafe(DispatcherTypeProvider.class).iterator()); - Collections.sort(providers, new WeightedComparator()); - } @Override @@ -66,7 +61,7 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont */ private DispatcherType getDispatcherType(final HttpServletRewrite event) { - for (DispatcherTypeProvider provider : providers) { + for (DispatcherTypeProvider provider : getDispatcherTypeProviders(event)) { DispatcherType dispatcherType = provider.getDispatcherType(event.getRequest(), event.getServletContext()); if (dispatcherType != null) { return dispatcherType; @@ -75,6 +70,22 @@ private DispatcherType getDispatcherType(final HttpServletRewrite event) throw new IllegalStateException("Unable to determine dispatcher type of current request"); } + /** + * Simple caching mechanism for the providers on a per request basis + */ + @SuppressWarnings("unchecked") + private List getDispatcherTypeProviders(HttpServletRewrite event) + { + List providers = (List) + event.getRequest().getAttribute(PROVIDER_KEY); + if (providers == null) { + providers = Iterators.asList(ServiceLoader.loadTypesafe(DispatcherTypeProvider.class).iterator()); + Collections.sort(providers, new WeightedComparator()); + event.getRequest().setAttribute(PROVIDER_KEY, providers); + } + return providers; + } + /** * Create a {@link DispatchType} condition that ensures the current {@link HttpServletRewrite} is a * {@link DispatcherType#FORWARD}