Skip to content

Commit

Permalink
DispatchType now caches the DispatcherTypeProvider list on a per requ…
Browse files Browse the repository at this point in the history
…est basis
  • Loading branch information
chkal committed Jul 14, 2014
1 parent c0880d2 commit c960607
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DispatcherTypeProvider> 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
Expand All @@ -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;
Expand All @@ -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<DispatcherTypeProvider> getDispatcherTypeProviders(HttpServletRewrite event)
{
List<DispatcherTypeProvider> providers = (List<DispatcherTypeProvider>)
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}
Expand Down

0 comments on commit c960607

Please sign in to comment.