001/*
002 * Copyright 2002-2012 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 *      https://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
017package org.springframework.web.portlet.context;
018
019import javax.portlet.PortletConfig;
020import javax.portlet.PortletContext;
021
022import org.springframework.beans.BeansException;
023import org.springframework.beans.factory.config.BeanPostProcessor;
024
025/**
026 * {@link org.springframework.beans.factory.config.BeanPostProcessor}
027 * implementation that passes the PortletContext to beans that implement
028 * the {@link PortletContextAware} interface.
029 *
030 * <p>Portlet application contexts will automatically register this with their
031 * underlying bean factory. Applications do not use this directly.
032 *
033 * @author Juergen Hoeller
034 * @author John A. Lewis
035 * @since 2.0
036 * @see org.springframework.web.portlet.context.PortletContextAware
037 * @see org.springframework.web.portlet.context.XmlPortletApplicationContext#postProcessBeanFactory
038 */
039public class PortletContextAwareProcessor implements BeanPostProcessor {
040
041        private PortletContext portletContext;
042
043        private PortletConfig portletConfig;
044
045
046        /**
047         * Create a new PortletContextAwareProcessor for the given context.
048         */
049        public PortletContextAwareProcessor(PortletContext portletContext) {
050                this(portletContext, null);
051        }
052
053        /**
054         * Create a new PortletContextAwareProcessor for the given config.
055         */
056        public PortletContextAwareProcessor(PortletConfig portletConfig) {
057                this(null, portletConfig);
058        }
059
060        /**
061         * Create a new PortletContextAwareProcessor for the given context and config.
062         */
063        public PortletContextAwareProcessor(PortletContext portletContext, PortletConfig portletConfig) {
064                this.portletContext = portletContext;
065                this.portletConfig = portletConfig;
066                if (portletContext == null && portletConfig != null) {
067                        this.portletContext = portletConfig.getPortletContext();
068                }
069        }
070
071
072        @Override
073        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
074                if (this.portletContext != null && bean instanceof PortletContextAware) {
075                        ((PortletContextAware) bean).setPortletContext(this.portletContext);
076                }
077                if (this.portletConfig != null && bean instanceof PortletConfigAware) {
078                        ((PortletConfigAware) bean).setPortletConfig(this.portletConfig);
079                }
080                return bean;
081        }
082
083        @Override
084        public Object postProcessAfterInitialization(Object bean, String beanName) {
085                return bean;
086        }
087
088}