Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull reqest #14 from philwebb/SWF-1500

* SWF-1500:
  Modify <faces:resources> to add portlet variant
  • Loading branch information...
commit 73c34611a7ed57ab1b61e214c09a92c17a4e68d6 2 parents c5471f4 + d904786
authored
109  spring-faces/src/main/java/org/springframework/faces/config/ResourcesBeanDefinitionParser.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2004-2008 the original author or authors.
  2
+ * Copyright 2004-2012 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
13 13
  * See the License for the specific language governing permissions and
14 14
  * limitations under the License.
15 15
  */
  16
+
16 17
 package org.springframework.faces.config;
17 18
 
18 19
 import java.util.Map;
@@ -22,6 +23,7 @@
22 23
 import org.springframework.beans.factory.support.RootBeanDefinition;
23 24
 import org.springframework.beans.factory.xml.BeanDefinitionParser;
24 25
 import org.springframework.beans.factory.xml.ParserContext;
  26
+import org.springframework.faces.webflow.JsfRuntimeInformation;
25 27
 import org.springframework.util.StringUtils;
26 28
 import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
27 29
 import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
@@ -31,51 +33,98 @@
31 33
  * Parser for the resources tag.
32 34
  * 
33 35
  * @author Rossen Stoyanchev
  36
+ * @author Phillip Webb
34 37
  * @since 2.2.0
35 38
  */
36 39
 public class ResourcesBeanDefinitionParser implements BeanDefinitionParser {
37 40
 
38  
-	static final String RESOURCE_HANDLER_BEAN_NAME = "jsfResourceRequestHandler";
39  
-	static final String RESOURCE_HANDLER_CLASS_NAME = "org.springframework.faces.webflow.JsfResourceRequestHandler";
  41
+	static final String SERVLET_RESOURCE_HANDLER_BEAN_NAME = "jsfResourceRequestHandler";
  42
+
  43
+	static final String PORTLET_RESOURCE_HANDLER_BEAN_NAME = "jsfPortletResourceRequestHandler";
40 44
 
41 45
 	public BeanDefinition parse(Element element, ParserContext parserContext) {
42  
-		Object source = parserContext.extractSource(element);
43  
-		registerHandlerAdapterIfNecessary(parserContext, source);
44  
-		registerResourceHandler(parserContext, source);
45  
-		registerHandlerMappings(element, parserContext, source);
  46
+		new ServletRegistrar(element, parserContext).register();
  47
+		if (JsfRuntimeInformation.isSpringPortletPresent()) {
  48
+			new PortletRegistrar(element, parserContext).register();
  49
+		}
46 50
 		return null;
47 51
 	}
48 52
 
49  
-	private void registerHandlerMappings(Element element, ParserContext parserContext, Object source) {
50  
-		Map<String, String> urlMap = new ManagedMap<String, String>();
51  
-		urlMap.put("/javax.faces.resource/**", RESOURCE_HANDLER_BEAN_NAME);
  53
+	private static abstract class Registrar {
52 54
 
53  
-		RootBeanDefinition beanDefinition = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
54  
-		beanDefinition.setSource(source);
55  
-		beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
56  
-		beanDefinition.getPropertyValues().add("urlMap", urlMap);
  55
+		protected final Element element;
57 56
 
58  
-		String order = element.getAttribute("order");
59  
-		beanDefinition.getPropertyValues().add("order", StringUtils.hasText(order) ? order : 0);
60  
-		parserContext.getReaderContext().registerWithGeneratedName(beanDefinition);
  57
+		protected final ParserContext parserContext;
  58
+
  59
+		protected final Object source;
  60
+
  61
+		public Registrar(Element element, ParserContext parserContext) {
  62
+			this.element = element;
  63
+			this.parserContext = parserContext;
  64
+			this.source = parserContext.extractSource(element);
  65
+		}
  66
+
  67
+		public abstract void register();
61 68
 	}
62 69
 
63  
-	private void registerResourceHandler(ParserContext parserContext, Object source) {
64  
-		RootBeanDefinition beanDefinition = new RootBeanDefinition(RESOURCE_HANDLER_CLASS_NAME);
65  
-		beanDefinition.setSource(source);
66  
-		beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
67  
-		parserContext.getRegistry().registerBeanDefinition(RESOURCE_HANDLER_BEAN_NAME, beanDefinition);
  70
+	private static class ServletRegistrar extends Registrar {
  71
+
  72
+		public ServletRegistrar(Element element, ParserContext parserContext) {
  73
+			super(element, parserContext);
  74
+		}
  75
+
  76
+		@Override
  77
+		public void register() {
  78
+			registerHandlerAdapterIfNecessary();
  79
+			registerResourceHandler();
  80
+			registerHandlerMappings();
  81
+		}
  82
+
  83
+		private void registerHandlerAdapterIfNecessary() {
  84
+			if (parserContext.getRegistry().containsBeanDefinition("org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter")) {
  85
+				return;
  86
+			}
  87
+			RootBeanDefinition beanDefinition = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
  88
+			beanDefinition.setSource(source);
  89
+			beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  90
+			parserContext.getReaderContext().registerWithGeneratedName(beanDefinition);
  91
+		}
  92
+
  93
+		private void registerResourceHandler() {
  94
+			RootBeanDefinition beanDefinition = new RootBeanDefinition("org.springframework.faces.webflow.JsfResourceRequestHandler");
  95
+			beanDefinition.setSource(source);
  96
+			beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  97
+			parserContext.getRegistry().registerBeanDefinition(SERVLET_RESOURCE_HANDLER_BEAN_NAME, beanDefinition);
  98
+		}
  99
+
  100
+		private void registerHandlerMappings() {
  101
+			Map<String, String> urlMap = new ManagedMap<String, String>();
  102
+			urlMap.put("/javax.faces.resource/**", SERVLET_RESOURCE_HANDLER_BEAN_NAME);
  103
+
  104
+			RootBeanDefinition beanDefinition = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
  105
+			beanDefinition.setSource(source);
  106
+			beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  107
+			beanDefinition.getPropertyValues().add("urlMap", urlMap);
  108
+
  109
+			String order = element.getAttribute("order");
  110
+			beanDefinition.getPropertyValues().add("order", StringUtils.hasText(order) ? order : 0);
  111
+			parserContext.getReaderContext().registerWithGeneratedName(beanDefinition);
  112
+		}
68 113
 	}
69 114
 
70  
-	private void registerHandlerAdapterIfNecessary(ParserContext parserContext, Object source) {
71  
-		if (parserContext.getRegistry().containsBeanDefinition(
72  
-				"org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter")) {
73  
-			return;
  115
+	private static class PortletRegistrar extends Registrar {
  116
+
  117
+		public PortletRegistrar(Element element, ParserContext parserContext) {
  118
+			super(element, parserContext);
  119
+		}
  120
+
  121
+		@Override
  122
+		public void register() {
  123
+			RootBeanDefinition beanDefinition = new RootBeanDefinition("org.springframework.faces.webflow.context.portlet.JsfResourceRequestHandler");
  124
+			beanDefinition.setSource(source);
  125
+			beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
  126
+			parserContext.getRegistry().registerBeanDefinition(PORTLET_RESOURCE_HANDLER_BEAN_NAME, beanDefinition);
74 127
 		}
75  
-		RootBeanDefinition beanDefinition = new RootBeanDefinition(HttpRequestHandlerAdapter.class);
76  
-		beanDefinition.setSource(source);
77  
-		beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
78  
-		parserContext.getReaderContext().registerWithGeneratedName(beanDefinition);
79 128
 	}
80 129
 
81 130
 }
17  spring-faces/src/main/java/org/springframework/faces/webflow/JsfRuntimeInformation.java
@@ -42,7 +42,13 @@
42 42
 
43 43
 	private static final int jsfVersion;
44 44
 
45  
-	private static final boolean myFacesPresent = ClassUtils.isPresent("org.apache.myfaces.webapp.MyFacesServlet", JsfUtils.class.getClassLoader());
  45
+	private static final ClassLoader CLASS_LOADER = JsfUtils.class.getClassLoader();
  46
+
  47
+	private static final boolean myFacesPresent = ClassUtils.isPresent("org.apache.myfaces.webapp.MyFacesServlet", CLASS_LOADER);
  48
+
  49
+	private static boolean portletPresent = ClassUtils.isPresent("javax.portlet.Portlet", CLASS_LOADER);
  50
+	
  51
+	private static boolean springPortletPresent = ClassUtils.isPresent("org.springframework.web.portlet.DispatcherPortlet", CLASS_LOADER);
46 52
 
47 53
 	static {
48 54
 		if (ReflectionUtils.findMethod(FacesContext.class, "isPostback") != null) {
@@ -83,6 +89,15 @@ public static boolean isMyFacesPresent() {
83 89
 	}
84 90
 
85 91
 	/**
  92
+	 * Determines if the container has support for portlets and if Spring MVC portlet support is available
  93
+	 * 
  94
+	 * @return <tt>true</tt> if a portlet environment is detected
  95
+	 */
  96
+	public static boolean isSpringPortletPresent() {
  97
+		return portletPresent && springPortletPresent;
  98
+	}
  99
+
  100
+	/**
86 101
 	 * Determine if the specified {@link FacesContext} is from a portlet request.
87 102
 	 * 
88 103
 	 * @param context the faces context
11  spring-faces/src/test/java/org/springframework/faces/config/ResourcesBeanDefinitionParserTests.java
@@ -24,16 +24,21 @@ public void testConfigureDefaults() {
24 24
 		Map<String, ?> map = this.context.getBeansOfType(HttpRequestHandlerAdapter.class);
25 25
 		assertEquals(1, map.values().size());
26 26
 
27  
-		Object resourceHandler = this.context.getBean(ResourcesBeanDefinitionParser.RESOURCE_HANDLER_BEAN_NAME);
  27
+		Object resourceHandler = this.context.getBean(ResourcesBeanDefinitionParser.SERVLET_RESOURCE_HANDLER_BEAN_NAME);
28 28
 		assertNotNull(resourceHandler);
29 29
 		assertTrue(resourceHandler instanceof JsfResourceRequestHandler);
30 30
 
31 31
 		map = this.context.getBeansOfType(SimpleUrlHandlerMapping.class);
32 32
 		assertEquals(1, map.values().size());
33 33
 		SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) map.values().iterator().next();
34  
-		assertEquals(ResourcesBeanDefinitionParser.RESOURCE_HANDLER_BEAN_NAME,
  34
+		assertEquals(ResourcesBeanDefinitionParser.SERVLET_RESOURCE_HANDLER_BEAN_NAME,
35 35
 				handlerMapping.getUrlMap().get("/javax.faces.resource/**"));
36 36
 		assertEquals(0, handlerMapping.getOrder());
37 37
 	}
38  
-
  38
+	
  39
+	public void testConfigurePortlet() {
  40
+		Object resourceHandler = this.context.getBean(ResourcesBeanDefinitionParser.PORTLET_RESOURCE_HANDLER_BEAN_NAME);
  41
+		assertNotNull(resourceHandler);
  42
+		assertTrue(resourceHandler instanceof org.springframework.faces.webflow.context.portlet.JsfResourceRequestHandler);
  43
+	}
39 44
 }

0 notes on commit 73c3461

Please sign in to comment.
Something went wrong with that request. Please try again.