Author: ate
Date: Wed Oct 10 15:13:04 2012
New Revision: 1396637

URL: http://svn.apache.org/viewvc?rev=1396637&view=rev
Log:
RAVE-694: rave-web-hmvc
- getting rid of ugly SpringMVC hack using reflection to access private parent 
member fields: now using delegation to an internal RequestMappingHandlerMapping 
which is recreated on every config reload

Modified:
    
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/RoutedRequestMappingHandlerMapping.java

Modified: 
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/RoutedRequestMappingHandlerMapping.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/RoutedRequestMappingHandlerMapping.java?rev=1396637&r1=1396636&r2=1396637&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/RoutedRequestMappingHandlerMapping.java
 (original)
+++ 
rave/sandbox/content-services/rave-web-hmvc/src/main/java/org/apache/rave/portal/web/mvc/RoutedRequestMappingHandlerMapping.java
 Wed Oct 10 15:13:04 2012
@@ -18,7 +18,6 @@
  */
 package org.apache.rave.portal.web.mvc;
 
-import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.Collections;
 import java.util.HashMap;
@@ -39,8 +38,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.util.ClassUtils;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
 import org.springframework.util.ReflectionUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -49,7 +46,6 @@ import org.springframework.web.method.Ha
 import org.springframework.web.servlet.HandlerExecutionChain;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.HandlerMapping;
-import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
 import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
 import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
 import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
@@ -64,9 +60,36 @@ public class RoutedRequestMappingHandler
 
     private static final Logger log = 
LoggerFactory.getLogger(RoutedRequestMappingHandlerMapping.class);
 
-    // temporary hacked open *references* to the real but private 
handlerMethods and urlMap maps in AbstractHandlerMethodMapping.java
-    private Map<RequestMappingInfo, HandlerMethod> handlerMethods;
-    private MultiValueMap<String, RequestMappingInfo> urlMap = new 
LinkedMultiValueMap<String, RequestMappingInfo>();
+    private static class InternalRequestMappingHandlerMapping extends 
RequestMappingHandlerMapping {
+
+        public 
InternalRequestMappingHandlerMapping(RoutedRequestMappingHandlerMapping holder) 
{
+            super();
+            setApplicationContext(holder.getApplicationContext());
+            setPathMatcher(holder.getPathMatcher());
+            setUrlPathHelper(holder.routedPathHelper);
+            setUseSuffixPatternMatch(holder.useSuffixPatternMatch());
+            setUseTrailingSlashMatch(holder.useSuffixPatternMatch());
+        }
+
+        @Override
+        public void registerHandlerMethod(Object handler, Method method, 
RequestMappingInfo info) {
+            super.registerHandlerMethod(handler, method, info);
+        }
+
+        @Override
+        protected void handleMatch(RequestMappingInfo info, String lookupPath, 
HttpServletRequest request) {
+            super.handleMatch(info, lookupPath, request);
+            request.setAttribute(RouteMapping.REQUEST_MAPPING_INFO, info);
+
+        }
+
+        @Override
+        public HandlerMethod lookupHandlerMethod(String lookupPath, 
HttpServletRequest request) throws Exception {
+            return super.lookupHandlerMethod(lookupPath, request);
+        }
+    }
+
+    private InternalRequestMappingHandlerMapping internalHandlerMapping;
 
     private RoutedPathHelper routedPathHelper = new RoutedPathHelper(false);
     private RoutedPathHelper handlerPathHelper;
@@ -82,45 +105,10 @@ public class RoutedRequestMappingHandler
 
     public RoutedRequestMappingHandlerMapping() {
         super();
-
-        // temporary dirty hack to access private handlerMethods and urlMap 
maps in AbstractHandlerMethodMapping.java
-        // see #getHandlerMethodsInternal() and #getUrlMapInternal() methods 
which should be provided on that class
-        try {
-            Field f1 = 
AbstractHandlerMethodMapping.class.getDeclaredField("handlerMethods");
-            f1.setAccessible(true);
-            Field f2 = 
AbstractHandlerMethodMapping.class.getDeclaredField("urlMap");
-            f2.setAccessible(true);
-            try {
-                handlerMethods = (Map<RequestMappingInfo, 
HandlerMethod>)f1.get(this);
-                urlMap = (MultiValueMap<String, 
RequestMappingInfo>)f2.get(this);
-            } catch (IllegalAccessException e) {
-                throw new RuntimeException("Unexpected", e);
-            }
-        } catch (NoSuchFieldException e) {
-            throw new RuntimeException("Unexpected", e);
-        }
-        // end temporary dirty hack
-
         // Use our own RoutedPathHelper
         super.setUrlPathHelper(routedPathHelper);
     }
 
-    /**
-     * Temporary method which should be made available on super class 
AbstractHandlerMethodMapping.java instead
-     * to allow access to the internal private handlerMethods map.
-     */
-    protected Map<RequestMappingInfo, HandlerMethod> 
getHandlerMethodsInternal() {
-        return this.handlerMethods;
-    }
-
-    /**
-     * Temporary method which should be made available on super class 
AbstractHandlerMethodMapping.java instead
-     * to allow access to the internal private urlMap map.
-     */
-    protected MultiValueMap<String, RequestMappingInfo> getUrlMapInternal() {
-        return this.urlMap;
-    }
-
     @Override
     protected boolean isHandler(Class<?> beanType) {
         return (AnnotationUtils.findAnnotation(beanType, Routed.class) != 
null);
@@ -154,18 +142,6 @@ public class RoutedRequestMappingHandler
         this.configManager = configManager;
     }
 
-    protected PagesConfigManager getConfigManager() {
-        return configManager;
-    }
-
-    protected PagesConfig getConfig() {
-        return config;
-    }
-
-    protected RouteMappings getRouteMappings() {
-        return routeMappings;
-    }
-
     protected Class getRoutedController(String controllerClassName) {
         Class controller = null;
         try {
@@ -282,7 +258,7 @@ public class RoutedRequestMappingHandler
         PagesConfig latestConfiguration = configManager.getConfig();
         if ((config == null && latestConfiguration != null) ||
                 (config != null && latestConfiguration == null) ||
-                (config != latestConfiguration)) {
+                (config.lastModified() < latestConfiguration.lastModified())) {
             synchronized (configManager) {
                 config = latestConfiguration;
                 processConfig();
@@ -291,10 +267,9 @@ public class RoutedRequestMappingHandler
     }
 
     protected void processConfig() {
-        // reset
-        getHandlerMethodsInternal().clear();
-        getUrlMapInternal().clear();
-        getRouteMappings().clear();
+
+        internalHandlerMapping = new 
InternalRequestMappingHandlerMapping(this);
+        routeMappings.clear();
 
         if (config != null) {
             for (Route route : config.getRoutes()) {
@@ -338,7 +313,7 @@ public class RoutedRequestMappingHandler
                     HandlerMethodConfig hmc = handlers.get(route.getHandler());
                     if (hmc != null) {
                         RequestMappingInfo info = 
createRouteMappingInfo(route.getPath()).combine(hmc.getRequestMappingInfo());
-                        
registerHandlerMethod(hmc.getHandlerMethod().getBean(), 
hmc.getHandlerMethod().getMethod(), info);
+                        
internalHandlerMapping.registerHandlerMethod(hmc.getHandlerMethod().getBean(), 
hmc.getHandlerMethod().getMethod(), info);
                         routeMappings.getRouteMappings().put(info, new 
RouteHandlerMethodMapping(route, hmc));
 
                         if (!StringUtils.isEmpty(routeReferenceId)) {
@@ -357,7 +332,7 @@ public class RoutedRequestMappingHandler
                     RequestMappingInfo routeMappingInfo = 
createRouteMappingInfo(route.getPath());
                     for (HandlerMethodConfig hmc : handlers.values()) {
                         RequestMappingInfo info = 
routeMappingInfo.combine(hmc.getRequestMappingInfo());
-                        
registerHandlerMethod(hmc.getHandlerMethod().getBean(), 
hmc.getHandlerMethod().getMethod(), info);
+                        
internalHandlerMapping.registerHandlerMethod(hmc.getHandlerMethod().getBean(), 
hmc.getHandlerMethod().getMethod(), info);
                         routeMappings.getRouteMappings().put(info, new 
RouteHandlerMethodMapping(route, hmc));
                     }
                 }
@@ -393,7 +368,7 @@ public class RoutedRequestMappingHandler
         if (pfc != null) {
             HandlerMethodConfig hmc = pfc.getHandlerMethodConfig();
             RequestMappingInfo info = createRouteMappingInfo(route.getPath());
-            registerHandlerMethod(hmc.getHandlerMethod().getBean(), 
hmc.getHandlerMethod().getMethod(), info);
+            
internalHandlerMapping.registerHandlerMethod(hmc.getHandlerMethod().getBean(), 
hmc.getHandlerMethod().getMethod(), info);
             routeMappings.getRouteMappings().put(info, new 
RoutePageMapping(route, pfc));
 
             Map<String, Route> routesMap = 
routeMappings.getRoutesMap().get(pagePath);
@@ -468,13 +443,6 @@ public class RoutedRequestMappingHandler
     }
 
     @Override
-    protected void handleMatch(RequestMappingInfo info, String lookupPath, 
HttpServletRequest request) {
-        super.handleMatch(info, lookupPath, request);
-        request.setAttribute(RouteMapping.REQUEST_MAPPING_INFO, info);
-
-    }
-
-    @Override
     protected HandlerExecutionChain getHandlerExecutionChain(Object handler, 
HttpServletRequest request) {
         HandlerExecutionChain chain = super.getHandlerExecutionChain(handler, 
request);
 
@@ -513,7 +481,24 @@ public class RoutedRequestMappingHandler
             mountPath = routedPathHelper.isAlwaysUseFullPath() ? "" : 
routedPathHelper.getServletPath(request);
         }
 
-        HandlerMethod handlerMethod = super.getHandlerInternal(request);
+        String lookupPath = 
getUrlPathHelper().getLookupPathForRequest(request);
+        if (logger.isDebugEnabled()) {
+            logger.debug("Looking up handler method for path " + lookupPath);
+        }
+
+        HandlerMethod handlerMethod = 
internalHandlerMapping.lookupHandlerMethod(lookupPath, request);
+
+        if (logger.isDebugEnabled()) {
+            if (handlerMethod != null) {
+                logger.debug("Returning handler method [" + handlerMethod + 
"]");
+            }
+            else {
+                logger.debug("Did not find handler method for [" + lookupPath 
+ "]");
+            }
+        }
+        if (handlerMethod != null) {
+            handlerMethod = handlerMethod.createWithResolvedBean();
+        }
 
         if (handlerMethod != null) {
             RequestMappingInfo info = 
(RequestMappingInfo)request.getAttribute(RouteMapping.REQUEST_MAPPING_INFO);


Reply via email to