KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import javax.portlet.PortletRequest;
24
25 import org.springframework.beans.BeansException;
26 import org.springframework.util.Assert;
27
28 /**
29  * Abstract base class for HandlerMapping implementations that rely on a map
30  * which caches handler objects per lookup key. Supports arbitrary lookup keys,
31  * and automatically resolves handler bean names into handler bean instances.
32  *
33  * @author Juergen Hoeller
34  * @since 2.0
35  * @see #getLookupKey(javax.portlet.PortletRequest)
36  * @see #registerHandler(Object, Object)
37  */

38 public abstract class AbstractMapBasedHandlerMapping extends AbstractHandlerMapping {
39
40     private boolean lazyInitHandlers = false;
41
42     private final Map JavaDoc handlerMap = new HashMap JavaDoc();
43
44
45     /**
46      * Set whether to lazily initialize handlers. Only applicable to
47      * singleton handlers, as prototypes are always lazily initialized.
48      * Default is false, as eager initialization allows for more efficiency
49      * through referencing the handler objects directly.
50      * <p>If you want to allow your handlers to be lazily initialized,
51      * make them "lazy-init" and set this flag to true. Just making them
52      * "lazy-init" will not work, as they are initialized through the
53      * references from the handler mapping in this case.
54      */

55     public void setLazyInitHandlers(boolean lazyInitHandlers) {
56         this.lazyInitHandlers = lazyInitHandlers;
57     }
58
59
60     /**
61      * Determines a handler for the computed lookup key for the given request.
62      * @see #getLookupKey
63      */

64     protected Object JavaDoc getHandlerInternal(PortletRequest request) throws Exception JavaDoc {
65         Object JavaDoc lookupKey = getLookupKey(request);
66         Object JavaDoc 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     /**
74      * Build a lookup key for the given request.
75      * @param request current portlet request
76      * @return the lookup key (never <code>null</code>)
77      * @throws Exception if key computation failed
78      */

79     protected abstract Object JavaDoc getLookupKey(PortletRequest request) throws Exception JavaDoc;
80
81
82     /**
83      * Register all handlers specified in the Portlet mode map for the corresponding modes.
84      * @param handlerMap Map with lookup keys as keys and handler beans or bean names as values
85      * @throws BeansException if the handler couldn't be registered
86      */

87     protected void registerHandlers(Map JavaDoc handlerMap) throws BeansException {
88         Assert.notNull(handlerMap, "Handler Map must not be null");
89         for (Iterator JavaDoc it = handlerMap.entrySet().iterator(); it.hasNext();) {
90             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
91             registerHandler(entry.getKey(), entry.getValue());
92         }
93     }
94
95     /**
96      * Register the given handler instance for the given parameter value.
97      * @param lookupKey the key to map the handler onto
98      * @param handler the handler instance or handler bean name String
99      * (a bean name will automatically be resolved into the corrresponding handler bean)
100      * @throws BeansException if the handler couldn't be registered
101      * @throws IllegalStateException if there is a conflicting handler registered
102      */

103     protected void registerHandler(Object JavaDoc lookupKey, Object JavaDoc handler) throws BeansException, IllegalStateException JavaDoc {
104         Assert.notNull(lookupKey, "Lookup key must not be null");
105         Assert.notNull(handler, "Handler object must not be null");
106
107         // Check for duplicate mapping.
108
Object JavaDoc mappedHandler = this.handlerMap.get(lookupKey);
109         if (mappedHandler != null) {
110             throw new IllegalStateException JavaDoc("Cannot map handler [" + handler + "] to key [" + lookupKey +
111                     "]: There's already handler [" + mappedHandler + "] mapped.");
112         }
113
114         // Eagerly resolve handler if referencing singleton via name.
115
if (!this.lazyInitHandlers && handler instanceof String JavaDoc) {
116             String JavaDoc handlerName = (String JavaDoc) handler;
117             if (getApplicationContext().isSingleton(handlerName)) {
118                 handler = getApplicationContext().getBean(handlerName);
119             }
120         }
121
122         // Add the handler to the map.
123
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