001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.portlet.handler;
018:
019: import java.util.Collections;
020: import java.util.Enumeration;
021: import java.util.Locale;
022: import java.util.ResourceBundle;
023:
024: import javax.portlet.Portlet;
025: import javax.portlet.PortletConfig;
026: import javax.portlet.PortletContext;
027: import javax.portlet.PortletException;
028:
029: import org.springframework.beans.BeansException;
030: import org.springframework.beans.factory.BeanInitializationException;
031: import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
032: import org.springframework.web.portlet.context.PortletConfigAware;
033: import org.springframework.web.portlet.context.PortletContextAware;
034:
035: /**
036: * Bean post-processor that applies initialization and destruction callbacks
037: * to beans that implement the Portlet interface.
038: *
039: * <p>After initialization of the bean instance, the Portlet <code>init</code>
040: * method will be called with a PortletConfig that contains the bean name
041: * of the Portlet and the PortletContext that it is running in.
042: *
043: * <p>Before destruction of the bean instance, the Portlet <code>destroy</code>
044: * will be called.
045: *
046: * <p><b>Note that this post-processor does not support Portlet initialization
047: * parameters.</b> Bean instances that implement the Portlet interface are
048: * supposed to be configured like any other Spring bean, that is, through
049: * constructor arguments or bean properties.
050: *
051: * <p>For reuse of a Portlet implementation in a plain Portlet container and as
052: * a bean in a Spring context, consider deriving from Spring's GenericPortletBean
053: * base class that applies Portlet initialization parameters as bean properties,
054: * supporting both initialization styles.
055: *
056: * <p><b>Alternatively, consider wrapping a Portlet with Spring's
057: * PortletWrappingController.</b> This is particularly appropriate for
058: * existing Portlet classes, allowing to specify Portlet initialization
059: * parameters etc.
060: *
061: * @author Juergen Hoeller
062: * @author John A. Lewis
063: * @since 2.0
064: * @see javax.portlet.Portlet
065: * @see javax.portlet.PortletConfig
066: * @see SimplePortletHandlerAdapter
067: * @see org.springframework.web.portlet.GenericPortletBean
068: * @see org.springframework.web.portlet.mvc.PortletWrappingController
069: */
070: public class SimplePortletPostProcessor implements
071: DestructionAwareBeanPostProcessor, PortletContextAware,
072: PortletConfigAware {
073:
074: private boolean useSharedPortletConfig = true;
075:
076: private PortletContext portletContext;
077:
078: private PortletConfig portletConfig;
079:
080: /**
081: * Set whether to use the shared PortletConfig object passed in
082: * through <code>setPortletConfig</code>, if available.
083: * <p>Default is "true". Turn this setting to "false" to pass in
084: * a mock PortletConfig object with the bean name as portlet name,
085: * holding the current PortletContext.
086: * @see #setPortletConfig
087: */
088: public void setUseSharedPortletConfig(boolean useSharedPortletConfig) {
089: this .useSharedPortletConfig = useSharedPortletConfig;
090: }
091:
092: public void setPortletContext(PortletContext portletContext) {
093: this .portletContext = portletContext;
094: }
095:
096: public void setPortletConfig(PortletConfig portletConfig) {
097: this .portletConfig = portletConfig;
098: }
099:
100: public Object postProcessBeforeInitialization(Object bean,
101: String beanName) throws BeansException {
102: return bean;
103: }
104:
105: public Object postProcessAfterInitialization(Object bean,
106: String beanName) throws BeansException {
107: if (bean instanceof Portlet) {
108: PortletConfig config = this .portletConfig;
109: if (config == null || !this .useSharedPortletConfig) {
110: config = new DelegatingPortletConfig(beanName,
111: this .portletContext, this .portletConfig);
112: }
113: try {
114: ((Portlet) bean).init(config);
115: } catch (PortletException ex) {
116: throw new BeanInitializationException(
117: "Portlet.init threw exception", ex);
118: }
119: }
120: return bean;
121: }
122:
123: public void postProcessBeforeDestruction(Object bean,
124: String beanName) throws BeansException {
125: if (bean instanceof Portlet) {
126: ((Portlet) bean).destroy();
127: }
128: }
129:
130: /**
131: * Internal implementation of the PortletConfig interface, to be passed
132: * to the wrapped servlet.
133: */
134: private static class DelegatingPortletConfig implements
135: PortletConfig {
136:
137: private final String portletName;
138:
139: private final PortletContext portletContext;
140:
141: private final PortletConfig portletConfig;
142:
143: public DelegatingPortletConfig(String portletName,
144: PortletContext portletContext,
145: PortletConfig portletConfig) {
146: this .portletName = portletName;
147: this .portletContext = portletContext;
148: this .portletConfig = portletConfig;
149: }
150:
151: public String getPortletName() {
152: return portletName;
153: }
154:
155: public PortletContext getPortletContext() {
156: return portletContext;
157: }
158:
159: public String getInitParameter(String paramName) {
160: return null;
161: }
162:
163: public Enumeration getInitParameterNames() {
164: return Collections.enumeration(Collections.EMPTY_SET);
165: }
166:
167: public ResourceBundle getResourceBundle(Locale locale) {
168: return portletConfig == null ? null : portletConfig
169: .getResourceBundle(locale);
170: }
171: }
172:
173: }
|