1 16 17 package org.springframework.web.portlet.handler; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import javax.portlet.PortletRequest; 24 25 import org.springframework.beans.BeansException; 26 import org.springframework.util.Assert; 27 28 38 public abstract class AbstractMapBasedHandlerMapping extends AbstractHandlerMapping { 39 40 private boolean lazyInitHandlers = false; 41 42 private final Map handlerMap = new HashMap (); 43 44 45 55 public void setLazyInitHandlers(boolean lazyInitHandlers) { 56 this.lazyInitHandlers = lazyInitHandlers; 57 } 58 59 60 64 protected Object getHandlerInternal(PortletRequest request) throws Exception { 65 Object lookupKey = getLookupKey(request); 66 Object handler = this.handlerMap.get(lookupKey); 67 if (handler != null && logger.isDebugEnabled()) { 68 logger.debug("Key [" + lookupKey + "] -> handler [" + handler + "]"); 69 } 70 return handler; 71 } 72 73 79 protected abstract Object getLookupKey(PortletRequest request) throws Exception ; 80 81 82 87 protected void registerHandlers(Map handlerMap) throws BeansException { 88 Assert.notNull(handlerMap, "Handler Map must not be null"); 89 for (Iterator it = handlerMap.entrySet().iterator(); it.hasNext();) { 90 Map.Entry entry = (Map.Entry ) it.next(); 91 registerHandler(entry.getKey(), entry.getValue()); 92 } 93 } 94 95 103 protected void registerHandler(Object lookupKey, Object handler) throws BeansException, IllegalStateException { 104 Assert.notNull(lookupKey, "Lookup key must not be null"); 105 Assert.notNull(handler, "Handler object must not be null"); 106 107 Object mappedHandler = this.handlerMap.get(lookupKey); 109 if (mappedHandler != null) { 110 throw new IllegalStateException ("Cannot map handler [" + handler + "] to key [" + lookupKey + 111 "]: There's already handler [" + mappedHandler + "] mapped."); 112 } 113 114 if (!this.lazyInitHandlers && handler instanceof String ) { 116 String handlerName = (String ) handler; 117 if (getApplicationContext().isSingleton(handlerName)) { 118 handler = getApplicationContext().getBean(handlerName); 119 } 120 } 121 122 this.handlerMap.put(lookupKey, handler); 124 if (logger.isDebugEnabled()) { 125 logger.debug("Mapped key [" + lookupKey + "] onto handler [" + handler + "]"); 126 } 127 } 128 129 } 130 | Popular Tags |