KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > context > PortletContextAwareProcessor


1 /*
2  * Copyright 2002-2005 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.context;
18
19 import javax.portlet.PortletConfig;
20 import javax.portlet.PortletContext;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.springframework.beans.BeansException;
26 import org.springframework.beans.factory.config.BeanPostProcessor;
27
28 /**
29  * BeanPostProcessor implementation that passes the PortletContext to
30  * beans that implement the PortletContextAware interface.
31  *
32  * <p>Portlet application contexts will automatically register this with their
33  * underlying bean factory. Applications do not use this directly.
34  *
35  * @author Juergen Hoeller
36  * @author John A. Lewis
37  * @since 2.0
38  * @see org.springframework.web.portlet.context.PortletContextAware
39  * @see org.springframework.web.portlet.context.XmlPortletApplicationContext#postProcessBeanFactory
40  */

41 public class PortletContextAwareProcessor implements BeanPostProcessor {
42
43     protected final Log logger = LogFactory.getLog(getClass());
44
45     private PortletContext portletContext;
46
47     private PortletConfig portletConfig;
48
49
50     /**
51      * Create a new PortletContextAwareProcessor for the given context.
52      */

53     public PortletContextAwareProcessor(PortletContext portletContext) {
54         this(portletContext, null);
55     }
56     
57     /**
58      * Create a new PortletContextAwareProcessor for the given config.
59      */

60     public PortletContextAwareProcessor(PortletConfig portletConfig) {
61         this(null, portletConfig);
62     }
63
64     /**
65      * Create a new PortletContextAwareProcessor for the given context and config.
66      */

67     public PortletContextAwareProcessor(PortletContext portletContext, PortletConfig portletConfig) {
68         this.portletContext = portletContext;
69         this.portletConfig = portletConfig;
70         if (portletContext == null && portletConfig != null) {
71             this.portletContext = portletConfig.getPortletContext();
72         }
73     }
74
75
76     public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
77         if (bean instanceof PortletContextAware) {
78             if (this.portletContext == null) {
79                 throw new IllegalStateException JavaDoc("Cannot satisfy PortletContextAware for bean '" +
80                         beanName + "' without PortletContext");
81             }
82             if (logger.isDebugEnabled()) {
83                 logger.debug("Invoking setPortletContext on PortletContextAware bean '" + beanName + "'");
84             }
85             ((PortletContextAware) bean).setPortletContext(this.portletContext);
86         }
87         if (bean instanceof PortletConfigAware) {
88             if (this.portletConfig == null) {
89                 throw new IllegalStateException JavaDoc("Cannot satisfy PortletConfigAware for bean '" +
90                         beanName + "' without PortletConfig");
91             }
92             if (logger.isDebugEnabled()) {
93                 logger.debug("Invoking setPortletConfig on PortletConfigAware bean '" + beanName + "'");
94             }
95             ((PortletConfigAware) bean).setPortletConfig(this.portletConfig);
96         }
97         return bean;
98     }
99
100     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc beanName) {
101         return bean;
102     }
103
104 }
105
Popular Tags