KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > handler > PortletModeHandlerMapping


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.portlet.handler;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.portlet.PortletMode;
25 import javax.portlet.PortletRequest;
26
27 import org.springframework.beans.BeansException;
28 import org.springframework.util.CollectionUtils;
29
30 /**
31  * <p>Implementation of the HandlerMapping interface to map from
32  * the current PortletMode to request handler beans.</p>
33  *
34  * <p>The bean configuration for this mapping will look something like this:</p>
35  * <pre>
36  * &lt;bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"&gt;
37  * &lt;property name="portletModeMap"&gt;
38  * &lt;map&gt;
39  * &lt;entry key="view"&gt;&lt;ref bean="viewHandler"/&gt;&lt;/entry&gt;
40  * &lt;entry key="edit"&gt;&lt;ref bean="editHandler"/&gt;&lt;/entry&gt;
41  * &lt;entry key="help"&gt;&lt;ref bean="helpHandler"/&gt;&lt;/entry&gt;
42  * &lt;/map&gt;
43  * &lt;/property&gt;
44  * &lt;/bean&gt;
45  * </pre>
46  *
47  * @author William G. Thompson, Jr.
48  * @author John A. Lewis
49  * @since 2.0
50  */

51 public class PortletModeHandlerMapping extends AbstractMapBasedHandlerMapping {
52
53     private final Map JavaDoc portletModeMap = new HashMap JavaDoc();
54
55
56     /**
57      * Set PortletMode to handler bean name mappings from a Properties object.
58      * @param mappings properties with PortletMode names as keys and bean names as values
59      */

60     public void setMappings(Properties JavaDoc mappings) {
61         this.portletModeMap.putAll(mappings);
62     }
63
64     /**
65      * Set a Map with PortletModes as keys and handler beans as values.
66      * Convenient for population with bean references.
67      * @param portletModeMap map with PortletMode names as keys and beans or bean names as values
68      */

69     public void setPortletModeMap(Map JavaDoc portletModeMap) {
70         this.portletModeMap.putAll(portletModeMap);
71     }
72
73
74     /**
75      * Calls the <code>registerHandlers</code> method in addition
76      * to the superclass's initialization.
77      * @see #registerHandlers
78      */

79     public void initApplicationContext() throws BeansException {
80         super.initApplicationContext();
81         registerHandlers(this.portletModeMap);
82     }
83
84     /**
85      * Register all handlers specified in the Portlet mode map for the corresponding modes.
86      * @param portletModeMap Map with mode names as keys and handler beans or bean names as values
87      * @throws BeansException if the handler couldn't be registered
88      */

89     protected void registerHandlers(Map JavaDoc portletModeMap) throws BeansException {
90         if (CollectionUtils.isEmpty(portletModeMap)) {
91             logger.warn("Neither 'portletModeMap' nor 'mappings' set on PortletModeHandlerMapping");
92         }
93         else {
94             for (Iterator JavaDoc it = portletModeMap.entrySet().iterator(); it.hasNext();) {
95                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
96                 String JavaDoc modeKey = (String JavaDoc) entry.getKey();
97                 PortletMode mode = new PortletMode(modeKey);
98                 Object JavaDoc handler = entry.getValue();
99                 registerHandler(mode, handler);
100             }
101         }
102     }
103
104
105     /**
106      * Uses the current PortletMode as lookup key.
107      */

108     protected Object JavaDoc getLookupKey(PortletRequest request) throws Exception JavaDoc {
109         return request.getPortletMode();
110     }
111
112 }
113
Popular Tags