KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > GenericPortletBean


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 package org.springframework.web.portlet;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.portlet.GenericPortlet;
23 import javax.portlet.PortletConfig;
24 import javax.portlet.PortletContext;
25 import javax.portlet.PortletException;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.springframework.beans.BeanWrapper;
31 import org.springframework.beans.BeanWrapperImpl;
32 import org.springframework.beans.BeansException;
33 import org.springframework.beans.MutablePropertyValues;
34 import org.springframework.beans.PropertyValue;
35 import org.springframework.beans.PropertyValues;
36 import org.springframework.core.io.Resource;
37 import org.springframework.core.io.ResourceEditor;
38 import org.springframework.core.io.ResourceLoader;
39 import org.springframework.util.StringUtils;
40 import org.springframework.web.portlet.context.PortletContextResourceLoader;
41
42 /**
43  * Simple extension of <code>javax.portlet.GenericPortlet</code> that treats
44  * its config parameters as bean properties.
45  *
46  * <p>A very handy superclass for any type of portlet. Type conversion is automatic.
47  * It is also possible for subclasses to specify required properties.
48  *
49  * <p>This portlet leaves request handling to subclasses, inheriting the default
50  * behaviour of GenericPortlet (<code>doDispatch</code>, <code>processAction</code>, etc).
51  *
52  * <p>This portlet superclass has no dependency on a Spring application context,
53  * in contrast to the FrameworkPortlet class which loads its own context.
54  *
55  * @author William G. Thompson, Jr.
56  * @author John A. Lewis
57  * @author Juergen Hoeller
58  * @since 2.0
59  * @see #addRequiredProperty
60  * @see #initPortletBean
61  * @see #doDispatch
62  * @see #processAction
63  * @see FrameworkPortlet
64  */

65 public abstract class GenericPortletBean extends GenericPortlet {
66
67     /** Logger available to subclasses */
68     protected final Log logger = LogFactory.getLog(getClass());
69
70     /**
71      * Set of required properties (Strings) that must be supplied as
72      * config parameters to this portlet.
73      */

74     private final Set JavaDoc requiredProperties = new HashSet JavaDoc();
75
76     
77     /**
78      * Subclasses can invoke this method to specify that this property
79      * (which must match a JavaBean property they expose) is mandatory,
80      * and must be supplied as a config parameter. This method would
81      * normally be called from a subclass constructor.
82      * @param property name of the required property
83      */

84     protected final void addRequiredProperty(String JavaDoc property) {
85         this.requiredProperties.add(property);
86     }
87
88     /**
89      * Map config parameters onto bean properties of this portlet, and
90      * invoke subclass initialization.
91      * @throws PortletException if bean properties are invalid (or required
92      * properties are missing), or if subclass initialization fails.
93      */

94     public final void init() throws PortletException {
95         if (logger.isInfoEnabled()) {
96             logger.info("Initializing portlet '" + getPortletName() + "'");
97         }
98         
99         // Set bean properties from init parameters.
100
try {
101             PropertyValues pvs = new PortletConfigPropertyValues(getPortletConfig(), this.requiredProperties);
102             BeanWrapper bw = new BeanWrapperImpl(this);
103             ResourceLoader resourceLoader = new PortletContextResourceLoader(getPortletContext());
104             bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
105             initBeanWrapper(bw);
106             bw.setPropertyValues(pvs, true);
107         }
108         catch (BeansException ex) {
109             logger.error("Failed to set bean properties on portlet '" + getPortletName() + "'", ex);
110             throw ex;
111         }
112
113         // let subclasses do whatever initialization they like
114
initPortletBean();
115
116         if (logger.isInfoEnabled()) {
117             logger.info("Portlet '" + getPortletName() + "' configured successfully");
118         }
119     }
120     
121     /**
122      * Initialize the BeanWrapper for this GenericPortletBean,
123      * possibly with custom editors.
124      * @param bw the BeanWrapper to initialize
125      * @throws BeansException if thrown by BeanWrapper methods
126      * @see org.springframework.beans.BeanWrapper#registerCustomEditor
127      */

128     protected void initBeanWrapper(BeanWrapper bw) throws BeansException {
129     }
130
131
132     /**
133      * Overridden method that simply returns <code>null</code> when no
134      * PortletConfig set yet.
135      * @see #getPortletConfig()
136      */

137     public final String JavaDoc getPortletName() {
138         return (getPortletConfig() != null ? getPortletConfig().getPortletName() : null);
139     }
140
141     /**
142      * Overridden method that simply returns <code>null</code> when no
143      * PortletConfig set yet.
144      * @see #getPortletConfig()
145      */

146     public final PortletContext getPortletContext() {
147         return (getPortletConfig() != null ? getPortletConfig().getPortletContext() : null);
148     }
149
150
151     /**
152      * Subclasses may override this to perform custom initialization.
153      * All bean properties of this portlet will have been set before this
154      * method is invoked. This default implementation does nothing.
155      * @throws PortletException if subclass initialization fails
156      */

157     protected void initPortletBean() throws PortletException {
158     }
159
160
161     /**
162      * PropertyValues implementation created from PortletConfig init parameters.
163      */

164     private static class PortletConfigPropertyValues extends MutablePropertyValues {
165
166         /**
167          * Create new PortletConfigPropertyValues.
168          * @param config PortletConfig we'll use to take PropertyValues from
169          * @param requiredProperties set of property names we need, where
170          * we can't accept default values
171          * @throws PortletException if any required properties are missing
172          */

173         private PortletConfigPropertyValues(PortletConfig config, Set JavaDoc requiredProperties)
174             throws PortletException {
175                 
176             Set JavaDoc missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ?
177                     new HashSet JavaDoc(requiredProperties) : null;
178
179             Enumeration JavaDoc en = config.getInitParameterNames();
180             while (en.hasMoreElements()) {
181                 String JavaDoc property = (String JavaDoc) en.nextElement();
182                 Object JavaDoc value = config.getInitParameter(property);
183                 addPropertyValue(new PropertyValue(property, value));
184                 if (missingProps != null) {
185                     missingProps.remove(property);
186                 }
187             }
188
189             // fail if we are still missing properties
190
if (missingProps != null && missingProps.size() > 0) {
191                 throw new PortletException(
192                     "Initialization from PortletConfig for portlet '" + config.getPortletName() +
193                     "' failed; the following required properties were missing: " +
194                     StringUtils.collectionToDelimitedString(missingProps, ", "));
195             }
196         }
197     }
198
199 }
200
Popular Tags