KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
23
24 import javax.portlet.Portlet;
25 import javax.portlet.PortletConfig;
26 import javax.portlet.PortletContext;
27 import javax.portlet.PortletException;
28
29 import org.springframework.beans.BeansException;
30 import org.springframework.beans.factory.BeanInitializationException;
31 import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
32 import org.springframework.web.portlet.context.PortletConfigAware;
33 import org.springframework.web.portlet.context.PortletContextAware;
34
35 /**
36  * Bean post-processor that applies initialization and destruction callbacks
37  * to beans that implement the Portlet interface.
38  *
39  * <p>After initialization of the bean instance, the Portlet <code>init</code>
40  * method will be called with a PortletConfig that contains the bean name
41  * of the Portlet and the PortletContext that it is running in.
42  *
43  * <p>Before destruction of the bean instance, the Portlet <code>destroy</code>
44  * will be called.
45  *
46  * <p><b>Note that this post-processor does not support Portlet initialization
47  * parameters.</b> Bean instances that implement the Portlet interface are
48  * supposed to be configured like any other Spring bean, that is, through
49  * constructor arguments or bean properties.
50  *
51  * <p>For reuse of a Portlet implementation in a plain Portlet container and as
52  * a bean in a Spring context, consider deriving from Spring's GenericPortletBean
53  * base class that applies Portlet initialization parameters as bean properties,
54  * supporting both initialization styles.
55  *
56  * <p><b>Alternatively, consider wrapping a Portlet with Spring's
57  * PortletWrappingController.</b> This is particularly appropriate for
58  * existing Portlet classes, allowing to specify Portlet initialization
59  * parameters etc.
60  *
61  * @author Juergen Hoeller
62  * @author John A. Lewis
63  * @since 2.0
64  * @see javax.portlet.Portlet
65  * @see javax.portlet.PortletConfig
66  * @see SimplePortletHandlerAdapter
67  * @see org.springframework.web.portlet.GenericPortletBean
68  * @see org.springframework.web.portlet.mvc.PortletWrappingController
69  */

70 public class SimplePortletPostProcessor
71     implements DestructionAwareBeanPostProcessor, PortletContextAware, PortletConfigAware {
72
73     private boolean useSharedPortletConfig = true;
74
75     private PortletContext portletContext;
76
77     private PortletConfig portletConfig;
78
79
80     /**
81      * Set whether to use the shared PortletConfig object passed in
82      * through <code>setPortletConfig</code>, if available.
83      * <p>Default is "true". Turn this setting to "false" to pass in
84      * a mock PortletConfig object with the bean name as portlet name,
85      * holding the current PortletContext.
86      * @see #setPortletConfig
87      */

88     public void setUseSharedPortletConfig(boolean useSharedPortletConfig) {
89         this.useSharedPortletConfig = useSharedPortletConfig;
90     }
91
92     public void setPortletContext(PortletContext portletContext) {
93         this.portletContext = portletContext;
94     }
95
96     public void setPortletConfig(PortletConfig portletConfig) {
97         this.portletConfig = portletConfig;
98     }
99
100
101     public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
102         return bean;
103     }
104
105     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
106         if (bean instanceof Portlet) {
107             PortletConfig config = this.portletConfig;
108             if (config == null || !this.useSharedPortletConfig) {
109                 config = new DelegatingPortletConfig(beanName, this.portletContext, this.portletConfig);
110             }
111             try {
112                 ((Portlet) bean).init(config);
113             }
114             catch (PortletException ex) {
115                 throw new BeanInitializationException("Portlet.init threw exception", ex);
116             }
117         }
118         return bean;
119     }
120
121     public void postProcessBeforeDestruction(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
122         if (bean instanceof Portlet) {
123             ((Portlet) bean).destroy();
124         }
125     }
126
127
128     /**
129      * Internal implementation of the PortletConfig interface, to be passed
130      * to the wrapped servlet.
131      */

132     private static class DelegatingPortletConfig implements PortletConfig {
133
134         private final String JavaDoc portletName;
135
136         private final PortletContext portletContext;
137
138         private final PortletConfig portletConfig;
139
140         public DelegatingPortletConfig(String JavaDoc portletName, PortletContext portletContext, PortletConfig portletConfig) {
141             this.portletName = portletName;
142             this.portletContext = portletContext;
143             this.portletConfig = portletConfig;
144         }
145
146         public String JavaDoc getPortletName() {
147             return portletName;
148         }
149
150         public PortletContext getPortletContext() {
151             return portletContext;
152         }
153
154         public String JavaDoc getInitParameter(String JavaDoc paramName) {
155             return null;
156         }
157
158         public Enumeration JavaDoc getInitParameterNames() {
159             return Collections.enumeration(Collections.EMPTY_SET);
160         }
161
162         public ResourceBundle JavaDoc getResourceBundle(Locale JavaDoc locale) {
163             return portletConfig == null ? null : portletConfig.getResourceBundle(locale);
164         }
165     }
166
167 }
168
Popular Tags