KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > mvc > PortletWrappingController


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.mvc;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.Portlet;
27 import javax.portlet.PortletConfig;
28 import javax.portlet.PortletContext;
29 import javax.portlet.RenderRequest;
30 import javax.portlet.RenderResponse;
31
32 import org.springframework.beans.factory.BeanNameAware;
33 import org.springframework.beans.factory.DisposableBean;
34 import org.springframework.beans.factory.InitializingBean;
35 import org.springframework.web.portlet.ModelAndView;
36 import org.springframework.web.portlet.context.PortletConfigAware;
37 import org.springframework.web.portlet.context.PortletContextAware;
38
39 /**
40  * {@link Controller} implementation that wraps a portlet instance which it manages
41  * internally. Such a wrapped portlet is not known outside of this controller;
42  * its entire lifecycle is covered here.
43  *
44  * <p>Useful to invoke an existing portlet via Spring's dispatching infrastructure,
45  * for example to apply Spring
46  * {@link org.springframework.web.portlet.HandlerInterceptor HandlerInterceptors}
47  * to its requests.
48  *
49  * <p><b>Example:</b>
50  *
51  * <pre>&lt;bean id="wrappingController" class="org.springframework.web.portlet.mvc.PortletWrappingController"&gt;
52  * &lt;property name="portletClass"&gt;
53  * &lt;value&gt;org.springframework.web.portlet.sample.HelloWorldPortlet&lt;/value&gt;
54  * &lt;/property&gt;
55  * &lt;property name="portletName"&gt;
56  * &lt;value&gt;hello-world&lt;/value&gt;
57  * &lt;/property&gt;
58  * &lt;property name="initParameters"&gt;
59  * &lt;props&gt;
60  * &lt;prop key="config"&gt;/WEB-INF/hello-world-portlet-config.xml&lt;/prop&gt;
61  * &lt;/props&gt;
62  * &lt;/property&gt;
63  * &lt;/bean&gt;</pre>
64  *
65  * @author Juergen Hoeller
66  * @author John A. Lewis
67  * @since 2.0
68  */

69 public class PortletWrappingController extends AbstractController
70     implements BeanNameAware, InitializingBean, DisposableBean, PortletContextAware, PortletConfigAware {
71
72     private boolean useSharedPortletConfig = true;
73
74     private PortletContext portletContext;
75
76     private PortletConfig portletConfig;
77
78     private Class JavaDoc portletClass;
79
80     private String JavaDoc portletName;
81
82     private Properties JavaDoc initParameters = new Properties JavaDoc();
83
84     private String JavaDoc beanName;
85
86     private Portlet portletInstance;
87
88
89     /**
90      * Set whether to use the shared PortletConfig object passed in
91      * through <code>setPortletConfig</code>, if available.
92      * <p>Default is "true". Turn this setting to "false" to pass in
93      * a mock PortletConfig object with the bean name as portlet name,
94      * holding the current PortletContext.
95      * @see #setPortletConfig
96      */

97     public void setUseSharedPortletConfig(boolean useSharedPortletConfig) {
98         this.useSharedPortletConfig = useSharedPortletConfig;
99     }
100
101     public void setPortletContext(PortletContext portletContext) {
102         this.portletContext = portletContext;
103     }
104
105     public void setPortletConfig(PortletConfig portletConfig) {
106         this.portletConfig = portletConfig;
107     }
108
109     /**
110      * Set the class of the Portlet to wrap.
111      * Needs to implement <code>javax.portlet.Portlet</code>.
112      * @see javax.portlet.Portlet
113      */

114     public void setPortletClass(Class JavaDoc portletClass) {
115         this.portletClass = portletClass;
116     }
117
118     /**
119      * Set the name of the Portlet to wrap.
120      * Default is the bean name of this controller.
121      */

122     public void setPortletName(String JavaDoc portletName) {
123         this.portletName = portletName;
124     }
125
126     /**
127      * Specify init parameters for the portlet to wrap,
128      * as name-value pairs.
129      */

130     public void setInitParameters(Properties JavaDoc initParameters) {
131         this.initParameters = initParameters;
132     }
133
134     public void setBeanName(String JavaDoc name) {
135         this.beanName = name;
136     }
137
138
139     public void afterPropertiesSet() throws Exception JavaDoc {
140         if (this.portletClass == null) {
141             throw new IllegalArgumentException JavaDoc("portletClass is required");
142         }
143         if (!Portlet.class.isAssignableFrom(this.portletClass)) {
144             throw new IllegalArgumentException JavaDoc("portletClass [" + this.portletClass.getName() +
145                 "] needs to implement interface [javax.portlet.Portlet]");
146         }
147         if (this.portletName == null) {
148             this.portletName = this.beanName;
149         }
150         PortletConfig config = this.portletConfig;
151         if (config == null || !this.useSharedPortletConfig) {
152             config = new DelegatingPortletConfig();
153         }
154         this.portletInstance = (Portlet) this.portletClass.newInstance();
155         this.portletInstance.init(config);
156     }
157
158
159     protected void handleActionRequestInternal(
160             ActionRequest request, ActionResponse response) throws Exception JavaDoc {
161
162         this.portletInstance.processAction(request, response);
163     }
164
165     protected ModelAndView handleRenderRequestInternal(
166             RenderRequest request, RenderResponse response) throws Exception JavaDoc {
167
168         this.portletInstance.render(request, response);
169         return null;
170     }
171
172
173     public void destroy() {
174         this.portletInstance.destroy();
175     }
176
177
178     /**
179      * Internal implementation of the PortletConfig interface, to be passed
180      * to the wrapped portlet.
181      * <p>Delegates to {@link PortletWrappingController} fields
182      * and methods to provide init parameters and other environment info.
183      */

184     private class DelegatingPortletConfig implements PortletConfig {
185
186         public String JavaDoc getPortletName() {
187             return portletName;
188         }
189
190         public PortletContext getPortletContext() {
191             return portletContext;
192         }
193
194         public String JavaDoc getInitParameter(String JavaDoc paramName) {
195             return initParameters.getProperty(paramName);
196         }
197
198         public Enumeration JavaDoc getInitParameterNames() {
199             return initParameters.keys();
200         }
201         
202         public ResourceBundle JavaDoc getResourceBundle(Locale JavaDoc locale) {
203             return (portletConfig != null ? portletConfig.getResourceBundle(locale) : null);
204         }
205
206     }
207
208 }
209
Popular Tags