001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.portlet.handler;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import javax.portlet.PortletMode;
025: import javax.portlet.PortletRequest;
026:
027: import org.springframework.beans.BeansException;
028: import org.springframework.util.CollectionUtils;
029:
030: /**
031: * <p>Implementation of the HandlerMapping interface to map from
032: * the current PortletMode to request handler beans.</p>
033: *
034: * <p>The bean configuration for this mapping will look something like this:</p>
035: * <pre>
036: * <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
037: * <property name="portletModeMap">
038: * <map>
039: * <entry key="view"><ref bean="viewHandler"/></entry>
040: * <entry key="edit"><ref bean="editHandler"/></entry>
041: * <entry key="help"><ref bean="helpHandler"/></entry>
042: * </map>
043: * </property>
044: * </bean>
045: * </pre>
046: *
047: * @author William G. Thompson, Jr.
048: * @author John A. Lewis
049: * @since 2.0
050: */
051: public class PortletModeHandlerMapping extends
052: AbstractMapBasedHandlerMapping {
053:
054: private final Map portletModeMap = new HashMap();
055:
056: /**
057: * Set PortletMode to handler bean name mappings from a Properties object.
058: * @param mappings properties with PortletMode names as keys and bean names as values
059: */
060: public void setMappings(Properties mappings) {
061: this .portletModeMap.putAll(mappings);
062: }
063:
064: /**
065: * Set a Map with PortletModes as keys and handler beans as values.
066: * Convenient for population with bean references.
067: * @param portletModeMap map with PortletMode names as keys and beans or bean names as values
068: */
069: public void setPortletModeMap(Map portletModeMap) {
070: this .portletModeMap.putAll(portletModeMap);
071: }
072:
073: /**
074: * Calls the <code>registerHandlers</code> method in addition
075: * to the superclass's initialization.
076: * @see #registerHandlers
077: */
078: public void initApplicationContext() throws BeansException {
079: super .initApplicationContext();
080: registerHandlers(this .portletModeMap);
081: }
082:
083: /**
084: * Register all handlers specified in the Portlet mode map for the corresponding modes.
085: * @param portletModeMap Map with mode names as keys and handler beans or bean names as values
086: * @throws BeansException if the handler couldn't be registered
087: */
088: protected void registerHandlers(Map portletModeMap)
089: throws BeansException {
090: if (CollectionUtils.isEmpty(portletModeMap)) {
091: logger
092: .warn("Neither 'portletModeMap' nor 'mappings' set on PortletModeHandlerMapping");
093: } else {
094: for (Iterator it = portletModeMap.entrySet().iterator(); it
095: .hasNext();) {
096: Map.Entry entry = (Map.Entry) it.next();
097: String modeKey = (String) entry.getKey();
098: PortletMode mode = new PortletMode(modeKey);
099: Object handler = entry.getValue();
100: registerHandler(mode, handler);
101: }
102: }
103: }
104:
105: /**
106: * Uses the current PortletMode as lookup key.
107: */
108: protected Object getLookupKey(PortletRequest request)
109: throws Exception {
110: return request.getPortletMode();
111: }
112:
113: }
|