1 16 package org.springframework.web.portlet; 17 18 import java.util.Enumeration ; 19 import java.util.HashSet ; 20 import java.util.Set ; 21 22 import javax.portlet.GenericPortlet; 23 import javax.portlet.PortletConfig; 24 import javax.portlet.PortletContext; 25 import javax.portlet.PortletException; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.springframework.beans.BeanWrapper; 31 import org.springframework.beans.BeanWrapperImpl; 32 import org.springframework.beans.BeansException; 33 import org.springframework.beans.MutablePropertyValues; 34 import org.springframework.beans.PropertyValue; 35 import org.springframework.beans.PropertyValues; 36 import org.springframework.core.io.Resource; 37 import org.springframework.core.io.ResourceEditor; 38 import org.springframework.core.io.ResourceLoader; 39 import org.springframework.util.StringUtils; 40 import org.springframework.web.portlet.context.PortletContextResourceLoader; 41 42 65 public abstract class GenericPortletBean extends GenericPortlet { 66 67 68 protected final Log logger = LogFactory.getLog(getClass()); 69 70 74 private final Set requiredProperties = new HashSet (); 75 76 77 84 protected final void addRequiredProperty(String property) { 85 this.requiredProperties.add(property); 86 } 87 88 94 public final void init() throws PortletException { 95 if (logger.isInfoEnabled()) { 96 logger.info("Initializing portlet '" + getPortletName() + "'"); 97 } 98 99 try { 101 PropertyValues pvs = new PortletConfigPropertyValues(getPortletConfig(), this.requiredProperties); 102 BeanWrapper bw = new BeanWrapperImpl(this); 103 ResourceLoader resourceLoader = new PortletContextResourceLoader(getPortletContext()); 104 bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader)); 105 initBeanWrapper(bw); 106 bw.setPropertyValues(pvs, true); 107 } 108 catch (BeansException ex) { 109 logger.error("Failed to set bean properties on portlet '" + getPortletName() + "'", ex); 110 throw ex; 111 } 112 113 initPortletBean(); 115 116 if (logger.isInfoEnabled()) { 117 logger.info("Portlet '" + getPortletName() + "' configured successfully"); 118 } 119 } 120 121 128 protected void initBeanWrapper(BeanWrapper bw) throws BeansException { 129 } 130 131 132 137 public final String getPortletName() { 138 return (getPortletConfig() != null ? getPortletConfig().getPortletName() : null); 139 } 140 141 146 public final PortletContext getPortletContext() { 147 return (getPortletConfig() != null ? getPortletConfig().getPortletContext() : null); 148 } 149 150 151 157 protected void initPortletBean() throws PortletException { 158 } 159 160 161 164 private static class PortletConfigPropertyValues extends MutablePropertyValues { 165 166 173 private PortletConfigPropertyValues(PortletConfig config, Set requiredProperties) 174 throws PortletException { 175 176 Set missingProps = (requiredProperties != null && !requiredProperties.isEmpty()) ? 177 new HashSet (requiredProperties) : null; 178 179 Enumeration en = config.getInitParameterNames(); 180 while (en.hasMoreElements()) { 181 String property = (String ) en.nextElement(); 182 Object value = config.getInitParameter(property); 183 addPropertyValue(new PropertyValue(property, value)); 184 if (missingProps != null) { 185 missingProps.remove(property); 186 } 187 } 188 189 if (missingProps != null && missingProps.size() > 0) { 191 throw new PortletException( 192 "Initialization from PortletConfig for portlet '" + config.getPortletName() + 193 "' failed; the following required properties were missing: " + 194 StringUtils.collectionToDelimitedString(missingProps, ", ")); 195 } 196 } 197 } 198 199 } 200 | Popular Tags |