implementations that rely on a map which caches handler objects per lookup key.
Supports arbitrary lookup keys, and automatically resolves handler bean names
into handler bean instances.
| Method from org.springframework.web.portlet.handler.AbstractMapBasedHandlerMapping Detail: |
protected Object getHandlerInternal(PortletRequest request) throws Exception {
Object lookupKey = getLookupKey(request);
Object handler = this.handlerMap.get(lookupKey);
if (handler != null && logger.isDebugEnabled()) {
logger.debug("Key [" + lookupKey + "] - > handler [" + handler + "]");
}
if (handler instanceof Map) {
Map predicateMap = (Map) handler;
List predicates = new LinkedList(predicateMap.keySet());
Collections.sort(predicates);
for (Iterator it = predicates.iterator(); it.hasNext();) {
PortletRequestMappingPredicate predicate = (PortletRequestMappingPredicate) it.next();
if (predicate.match(request)) {
return predicateMap.get(predicate);
}
}
return null;
}
return handler;
}
Determines a handler for the computed lookup key for the given request. |
abstract protected Object getLookupKey(PortletRequest request) throws Exception
Build a lookup key for the given request. |
protected void registerHandler(Object lookupKey,
Object handler) throws IllegalStateException, BeansException {
registerHandler(lookupKey, handler, null);
}
Register the given handler instance for the given parameter value. |
protected void registerHandler(Object lookupKey,
Object handler,
AbstractMapBasedHandlerMapping.PortletRequestMappingPredicate predicate) throws IllegalStateException, BeansException {
Assert.notNull(lookupKey, "Lookup key must not be null");
Assert.notNull(handler, "Handler object must not be null");
Object resolvedHandler = handler;
// Eagerly resolve handler if referencing singleton via name.
if (!this.lazyInitHandlers && handler instanceof String) {
String handlerName = (String) handler;
if (getApplicationContext().isSingleton(handlerName)) {
resolvedHandler = getApplicationContext().getBean(handlerName);
}
}
// Check for duplicate mapping.
Object mappedHandler = this.handlerMap.get(lookupKey);
if (mappedHandler != null && !(mappedHandler instanceof Map)) {
if (mappedHandler != resolvedHandler) {
throw new IllegalStateException("Cannot map handler [" + handler + "] to key [" + lookupKey +
"]: There's already handler [" + mappedHandler + "] mapped.");
}
}
else {
if (predicate != null) {
// Add the handler to the predicate map.
Map predicateMap = (Map) mappedHandler;
if (predicateMap == null) {
predicateMap = new LinkedHashMap();
this.handlerMap.put(lookupKey, predicateMap);
}
predicateMap.put(predicate, resolvedHandler);
}
else {
// Add the single handler to the map.
this.handlerMap.put(lookupKey, resolvedHandler);
}
if (logger.isDebugEnabled()) {
logger.debug("Mapped key [" + lookupKey + "] onto handler [" + resolvedHandler + "]");
}
}
}
Register the given handler instance for the given parameter value. |
protected void registerHandlers(Map handlerMap) throws BeansException {
Assert.notNull(handlerMap, "Handler Map must not be null");
for (Iterator it = handlerMap.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
registerHandler(entry.getKey(), entry.getValue());
}
}
Register all handlers specified in the Portlet mode map for the corresponding modes. |
public void setLazyInitHandlers(boolean lazyInitHandlers) {
this.lazyInitHandlers = lazyInitHandlers;
}
Set whether to lazily initialize handlers. Only applicable to
singleton handlers, as prototypes are always lazily initialized.
Default is false, as eager initialization allows for more efficiency
through referencing the handler objects directly.
If you want to allow your handlers to be lazily initialized,
make them "lazy-init" and set this flag to true. Just making them
"lazy-init" will not work, as they are initialized through the
references from the handler mapping in this case. |