1 9 package org.jboss.portal.portlet; 10 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import javax.portlet.Portlet; 17 import javax.portlet.PortletConfig; 18 import javax.portlet.PortletException; 19 20 import org.jboss.portal.portlet.impl.PortletConfigImpl; 21 import org.jboss.portal.portlet.metadata.ParameterMetaData; 22 import org.jboss.portal.portlet.metadata.PortletMetaData; 23 import org.jboss.portal.portlet.metadata.SecurityRoleRefMetaData; 24 import org.jboss.portal.portlet.plugins.language.ResourceBundles; 25 import org.jboss.portal.server.Component; 26 import org.jboss.portal.server.metadata.ServerObjectMetaData; 27 28 34 public class PortletContainer 35 extends Component 36 { 37 38 39 protected PortletMetaData portletMD; 40 41 42 protected String className; 43 44 45 protected PortletConfig config; 46 47 48 protected Portlet portlet; 49 50 51 protected Map securityRoleRefsMap; 52 53 public PortletContainer(PortletMetaData portletMD) 54 { 55 super(portletMD.getName()); 56 this.portletMD = portletMD; 57 } 58 59 public ServerObjectMetaData getMetaData() 60 { 61 return portletMD; 62 } 63 64 public void start() throws Exception , PortletInitializationException 65 { 66 super.start(); 68 69 if (portletMD.getClassName() == null) 71 { 72 throw new Exception ("No class name specified"); 73 } 74 if (portletMD.getClassName() == null) 75 { 76 throw new Exception ("No class name specified"); 77 } 78 79 this.className = portletMD.getClassName(); 81 82 Map securityRoleRefsMap = new HashMap (); 84 for (Iterator i = portletMD.getSecurityRoleRefs().iterator();i.hasNext();) 85 { 86 SecurityRoleRefMetaData securityRoleRefMD = (SecurityRoleRefMetaData)i.next(); 87 securityRoleRefsMap.put(securityRoleRefMD.getRoleName(), securityRoleRefMD.getRoleLink()); 88 } 89 90 Map initParameters = new HashMap (); 92 for (Iterator i = portletMD.getParameters().values().iterator();i.hasNext();) 93 { 94 ParameterMetaData parameterMD = (ParameterMetaData)i.next(); 95 initParameters.put(parameterMD.getName(), parameterMD.getValue()); 96 } 97 98 ResourceBundles resourceBundles = (ResourceBundles)getPlugin(PortletConstants.PLUGIN_LANG); 100 PortletConfig config = new PortletConfigImpl( 101 name, 102 ((PortletApplication)application).getPortletContext(), 103 initParameters, 104 resourceBundles); 105 106 Integer expirationCache = null; 108 try 109 { 110 String value = portletMD.getExpirationCache(); 111 if (value != null && value.length() > 0) 112 { 113 expirationCache = Integer.valueOf(value); 114 } 115 } 116 catch (NumberFormatException e) 117 { 118 log.error("Invalid expiration cache value " + portletMD.getExpirationCache()); 119 } 120 121 try 123 { 124 log.debug("Loading portlet class " + className); 125 Class portletClass = application.getClassLoader().loadClass(className); 126 log.debug("Creating portlet object " + className); 127 Portlet portlet = (Portlet)portletClass.newInstance(); 128 log.debug("Created portlet object " + className); 129 initPortlet(portlet, config); 130 log.debug("Initialized portlet object " + className); 131 132 this.portlet = portlet; 134 this.config = config; 135 this.securityRoleRefsMap = Collections.unmodifiableMap(securityRoleRefsMap); 136 this.expirationCache = expirationCache; 137 138 valve.open(); 140 } 141 catch (IllegalAccessException e) 142 { 143 throw new PortletInitializationException("Portlet class not accessible " + className, e); 144 } 145 catch (ClassNotFoundException e) 146 { 147 throw new PortletInitializationException("Portlet class not found " + className, e); 148 } 149 catch (InstantiationException e) 150 { 151 throw new PortletInitializationException("Portlet class cannot be instantiated " + className, e); 152 } 153 catch (PortletException e) 154 { 155 throw new PortletInitializationException("The portlet threw a portlet exception during init", e); 156 } 157 catch (RuntimeException e) 158 { 159 throw new PortletInitializationException("The portlet threw a runtime exception during init", e); 160 } 161 catch (Error e) 162 { 163 throw new PortletInitializationException("The portlet threw an error during init", e); 164 } 165 } 166 167 public void stop() 168 { 169 boolean done = valve.closing(60000); 171 if (!done) 172 { 173 log.warn("The valve is still holding invocations, continue anyway"); 174 } 175 176 valve.closed(); 178 179 destroyPortlet(portlet); 181 182 className = null; 184 portlet = null; 185 securityRoleRefsMap = null; 186 config = null; 187 expirationCache = null; 188 } 189 190 public Portlet getPortlet() 191 { 192 return portlet; 193 } 194 195 public Map getSecurityRoleRefsMap() 196 { 197 return securityRoleRefsMap; 198 } 199 200 public String getClassName() 201 { 202 return className; 203 } 204 205 public PortletConfig getConfig() 206 { 207 return config; 208 } 209 210 213 private void initPortlet(Portlet portlet, PortletConfig config) throws PortletException 214 { 215 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); 216 try 217 { 218 ClassLoader newLoader = application.getClassLoader(); 220 Thread.currentThread().setContextClassLoader(newLoader); 221 portlet.init(config); 222 } 223 finally 224 { 225 Thread.currentThread().setContextClassLoader(oldLoader); 226 } 227 } 228 229 232 private void destroyPortlet(Portlet portlet) 233 { 234 ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); 235 try 236 { 237 ClassLoader newLoader = application.getClassLoader(); 239 Thread.currentThread().setContextClassLoader(newLoader); 240 if (portlet != null) 241 { 242 portlet.destroy(); 243 } 244 else 245 { 246 log.debug("Cannot call destroy, portlet was null"); 247 } 248 } 249 catch (RuntimeException e) 250 { 251 log.error("The portlet threw a runtime exception", e); 252 } 253 finally 254 { 255 Thread.currentThread().setContextClassLoader(oldLoader); 256 } 257 } 258 } 259 | Popular Tags |