1 16 17 package org.springframework.web.portlet.handler; 18 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.Locale ; 22 import java.util.ResourceBundle ; 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 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 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 postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 102 return bean; 103 } 104 105 public Object postProcessAfterInitialization(Object bean, String 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 bean, String beanName) throws BeansException { 122 if (bean instanceof Portlet) { 123 ((Portlet) bean).destroy(); 124 } 125 } 126 127 128 132 private static class DelegatingPortletConfig implements PortletConfig { 133 134 private final String portletName; 135 136 private final PortletContext portletContext; 137 138 private final PortletConfig portletConfig; 139 140 public DelegatingPortletConfig(String portletName, PortletContext portletContext, PortletConfig portletConfig) { 141 this.portletName = portletName; 142 this.portletContext = portletContext; 143 this.portletConfig = portletConfig; 144 } 145 146 public String getPortletName() { 147 return portletName; 148 } 149 150 public PortletContext getPortletContext() { 151 return portletContext; 152 } 153 154 public String getInitParameter(String paramName) { 155 return null; 156 } 157 158 public Enumeration getInitParameterNames() { 159 return Collections.enumeration(Collections.EMPTY_SET); 160 } 161 162 public ResourceBundle getResourceBundle(Locale locale) { 163 return portletConfig == null ? null : portletConfig.getResourceBundle(locale); 164 } 165 } 166 167 } 168 | Popular Tags |