01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.context;
18:
19: import javax.portlet.PortletConfig;
20: import javax.portlet.PortletContext;
21:
22: import org.springframework.beans.BeansException;
23: import org.springframework.beans.factory.config.BeanPostProcessor;
24:
25: /**
26: * {@link org.springframework.beans.factory.config.BeanPostProcessor}
27: * implementation that passes the PortletContext to beans that implement
28: * the {@link PortletContextAware} interface.
29: *
30: * <p>Portlet application contexts will automatically register this with their
31: * underlying bean factory. Applications do not use this directly.
32: *
33: * @author Juergen Hoeller
34: * @author John A. Lewis
35: * @since 2.0
36: * @see org.springframework.web.portlet.context.PortletContextAware
37: * @see org.springframework.web.portlet.context.XmlPortletApplicationContext#postProcessBeanFactory
38: */
39: public class PortletContextAwareProcessor implements BeanPostProcessor {
40:
41: private PortletContext portletContext;
42:
43: private PortletConfig portletConfig;
44:
45: /**
46: * Create a new PortletContextAwareProcessor for the given context.
47: */
48: public PortletContextAwareProcessor(PortletContext portletContext) {
49: this (portletContext, null);
50: }
51:
52: /**
53: * Create a new PortletContextAwareProcessor for the given config.
54: */
55: public PortletContextAwareProcessor(PortletConfig portletConfig) {
56: this (null, portletConfig);
57: }
58:
59: /**
60: * Create a new PortletContextAwareProcessor for the given context and config.
61: */
62: public PortletContextAwareProcessor(PortletContext portletContext,
63: PortletConfig portletConfig) {
64: this .portletContext = portletContext;
65: this .portletConfig = portletConfig;
66: if (portletContext == null && portletConfig != null) {
67: this .portletContext = portletConfig.getPortletContext();
68: }
69: }
70:
71: public Object postProcessBeforeInitialization(Object bean,
72: String beanName) throws BeansException {
73: if (this .portletContext != null
74: && bean instanceof PortletContextAware) {
75: ((PortletContextAware) bean)
76: .setPortletContext(this .portletContext);
77: }
78: if (this .portletConfig != null
79: && bean instanceof PortletConfigAware) {
80: ((PortletConfigAware) bean)
81: .setPortletConfig(this .portletConfig);
82: }
83: return bean;
84: }
85:
86: public Object postProcessAfterInitialization(Object bean,
87: String beanName) {
88: return bean;
89: }
90:
91: }
|