1 16 17 package org.springframework.web.portlet.mvc; 18 19 import java.util.Enumeration ; 20 import java.util.Locale ; 21 import java.util.Properties ; 22 import java.util.ResourceBundle ; 23 24 import javax.portlet.ActionRequest; 25 import javax.portlet.ActionResponse; 26 import javax.portlet.Portlet; 27 import javax.portlet.PortletConfig; 28 import javax.portlet.PortletContext; 29 import javax.portlet.RenderRequest; 30 import javax.portlet.RenderResponse; 31 32 import org.springframework.beans.factory.BeanNameAware; 33 import org.springframework.beans.factory.DisposableBean; 34 import org.springframework.beans.factory.InitializingBean; 35 import org.springframework.web.portlet.ModelAndView; 36 import org.springframework.web.portlet.context.PortletConfigAware; 37 import org.springframework.web.portlet.context.PortletContextAware; 38 39 69 public class PortletWrappingController extends AbstractController 70 implements BeanNameAware, InitializingBean, DisposableBean, PortletContextAware, PortletConfigAware { 71 72 private boolean useSharedPortletConfig = true; 73 74 private PortletContext portletContext; 75 76 private PortletConfig portletConfig; 77 78 private Class portletClass; 79 80 private String portletName; 81 82 private Properties initParameters = new Properties (); 83 84 private String beanName; 85 86 private Portlet portletInstance; 87 88 89 97 public void setUseSharedPortletConfig(boolean useSharedPortletConfig) { 98 this.useSharedPortletConfig = useSharedPortletConfig; 99 } 100 101 public void setPortletContext(PortletContext portletContext) { 102 this.portletContext = portletContext; 103 } 104 105 public void setPortletConfig(PortletConfig portletConfig) { 106 this.portletConfig = portletConfig; 107 } 108 109 114 public void setPortletClass(Class portletClass) { 115 this.portletClass = portletClass; 116 } 117 118 122 public void setPortletName(String portletName) { 123 this.portletName = portletName; 124 } 125 126 130 public void setInitParameters(Properties initParameters) { 131 this.initParameters = initParameters; 132 } 133 134 public void setBeanName(String name) { 135 this.beanName = name; 136 } 137 138 139 public void afterPropertiesSet() throws Exception { 140 if (this.portletClass == null) { 141 throw new IllegalArgumentException ("portletClass is required"); 142 } 143 if (!Portlet.class.isAssignableFrom(this.portletClass)) { 144 throw new IllegalArgumentException ("portletClass [" + this.portletClass.getName() + 145 "] needs to implement interface [javax.portlet.Portlet]"); 146 } 147 if (this.portletName == null) { 148 this.portletName = this.beanName; 149 } 150 PortletConfig config = this.portletConfig; 151 if (config == null || !this.useSharedPortletConfig) { 152 config = new DelegatingPortletConfig(); 153 } 154 this.portletInstance = (Portlet) this.portletClass.newInstance(); 155 this.portletInstance.init(config); 156 } 157 158 159 protected void handleActionRequestInternal( 160 ActionRequest request, ActionResponse response) throws Exception { 161 162 this.portletInstance.processAction(request, response); 163 } 164 165 protected ModelAndView handleRenderRequestInternal( 166 RenderRequest request, RenderResponse response) throws Exception { 167 168 this.portletInstance.render(request, response); 169 return null; 170 } 171 172 173 public void destroy() { 174 this.portletInstance.destroy(); 175 } 176 177 178 184 private class DelegatingPortletConfig implements PortletConfig { 185 186 public String getPortletName() { 187 return portletName; 188 } 189 190 public PortletContext getPortletContext() { 191 return portletContext; 192 } 193 194 public String getInitParameter(String paramName) { 195 return initParameters.getProperty(paramName); 196 } 197 198 public Enumeration getInitParameterNames() { 199 return initParameters.keys(); 200 } 201 202 public ResourceBundle getResourceBundle(Locale locale) { 203 return (portletConfig != null ? portletConfig.getResourceBundle(locale) : null); 204 } 205 206 } 207 208 } 209 | Popular Tags |