1 16 17 package org.springframework.web.portlet; 18 19 import java.io.IOException ; 20 import java.security.Principal ; 21 import java.util.Map ; 22 23 import javax.portlet.ActionRequest; 24 import javax.portlet.ActionResponse; 25 import javax.portlet.PortletException; 26 import javax.portlet.PortletRequest; 27 import javax.portlet.PortletResponse; 28 import javax.portlet.RenderRequest; 29 import javax.portlet.RenderResponse; 30 31 import org.springframework.beans.BeanUtils; 32 import org.springframework.beans.BeansException; 33 import org.springframework.context.ApplicationContext; 34 import org.springframework.context.ApplicationContextException; 35 import org.springframework.context.ConfigurableApplicationContext; 36 import org.springframework.util.StringUtils; 37 import org.springframework.web.portlet.context.ConfigurablePortletApplicationContext; 38 import org.springframework.web.portlet.context.PortletApplicationContextUtils; 39 import org.springframework.web.portlet.context.PortletRequestHandledEvent; 40 import org.springframework.web.portlet.context.XmlPortletApplicationContext; 41 42 91 public abstract class FrameworkPortlet extends GenericPortletBean { 92 93 97 public static final Class DEFAULT_CONTEXT_CLASS = XmlPortletApplicationContext.class; 98 99 104 public static final String DEFAULT_NAMESPACE_SUFFIX = "-portlet"; 105 106 110 public static final String PORTLET_CONTEXT_PREFIX = FrameworkPortlet.class.getName() + ".CONTEXT."; 111 112 116 public static final String [] DEFAULT_USERINFO_ATTRIBUTE_NAMES = {"user.login.id", "user.name"}; 117 118 119 122 private Class contextClass = DEFAULT_CONTEXT_CLASS; 123 124 127 private String namespace; 128 129 132 private String contextConfigLocation; 133 134 137 private boolean publishContext = true; 138 139 142 private boolean publishEvents = true; 143 144 147 private String [] userinfoUsernameAttributes = DEFAULT_USERINFO_ATTRIBUTE_NAMES; 148 149 152 private ApplicationContext portletApplicationContext; 153 154 155 161 public void setContextClass(Class contextClass) { 162 this.contextClass = contextClass; 163 } 164 165 168 public Class getContextClass() { 169 return contextClass; 170 } 171 172 176 public void setNamespace(String namespace) { 177 this.namespace = namespace; 178 } 179 180 184 public String getNamespace() { 185 return (this.namespace != null) ? this.namespace : getPortletName() + DEFAULT_NAMESPACE_SUFFIX; 186 } 187 188 193 public void setContextConfigLocation(String contextConfigLocation) { 194 this.contextConfigLocation = contextConfigLocation; 195 } 196 197 200 public String getContextConfigLocation() { 201 return contextConfigLocation; 202 } 203 204 210 public void setPublishContext(boolean publishContext) { 211 this.publishContext = publishContext; 212 } 213 214 217 public boolean isPublishContext() { 218 return publishContext; 219 } 220 221 227 public void setPublishEvents(boolean publishEvents) { 228 this.publishEvents = publishEvents; 229 } 230 231 235 public boolean isPublishEvents() { 236 return publishEvents; 237 } 238 239 244 public void setUserinfoUsernameAttributes(String [] userinfoUsernameAttributes) { 245 this.userinfoUsernameAttributes = userinfoUsernameAttributes; 246 } 247 248 253 public String [] getUserinfoUsernameAttributes() { 254 return userinfoUsernameAttributes; 255 } 256 257 258 262 protected final void initPortletBean() throws PortletException, BeansException { 263 long startTime = System.currentTimeMillis(); 264 if (logger.isInfoEnabled()) { 265 logger.info("FrameworkPortlet '" + getPortletName() + "': initialization started"); 266 } 267 268 try { 269 this.portletApplicationContext = initPortletApplicationContext(); 270 initFrameworkPortlet(); 271 } 272 catch (PortletException ex) { 273 logger.error("Context initialization failed", ex); 274 throw ex; 275 } 276 catch (BeansException ex) { 277 logger.error("Context initialization failed", ex); 278 throw ex; 279 } 280 281 if (logger.isInfoEnabled()) { 282 long elapsedTime = System.currentTimeMillis() - startTime; 283 logger.info("FrameworkPortlet '" + getPortletName() + "': initialization completed in " + elapsedTime + " ms"); 284 } 285 } 286 287 295 protected ApplicationContext initPortletApplicationContext() throws BeansException { 296 getPortletContext().log("Loading PortletApplicationContext for Spring FrameworkPortlet '" + getPortletName() + "'"); 297 298 ApplicationContext parent = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext()); 299 ApplicationContext pac = createPortletApplicationContext(parent); 300 if (logger.isInfoEnabled()) { 301 logger.info("Using context class '" + pac.getClass().getName() + "' for portlet '" + 302 getPortletName() + "'"); 303 } 304 305 if (isPublishContext()) { 306 String attName = getPortletContextAttributeName(); 308 getPortletContext().setAttribute(attName, pac); 309 if (logger.isDebugEnabled()) { 310 logger.debug("Published PortletApplicationContext of portlet '" + getPortletName() + 311 "' as PortletContext attribute with name [" + attName + "]"); 312 } 313 } 314 return pac; 315 } 316 317 328 protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) 329 throws BeansException { 330 331 if (logger.isDebugEnabled()) { 332 logger.debug("Portlet with name '" + getPortletName() + 333 "' will try to create custom PortletApplicationContext context of class '" + 334 getContextClass().getName() + "'" + ", using parent context [" + parent + "]"); 335 } 336 if (!ConfigurablePortletApplicationContext.class.isAssignableFrom(getContextClass())) { 337 throw new ApplicationContextException("Fatal initialization error in portlet with name '" + getPortletName() + 338 "': custom PortletApplicationContext class [" + getContextClass().getName() + 339 "] is not of type ConfigurablePortletApplicationContext"); 340 } 341 342 ConfigurablePortletApplicationContext pac = 343 (ConfigurablePortletApplicationContext) BeanUtils.instantiateClass(getContextClass()); 344 pac.setParent(parent); 345 pac.setPortletContext(getPortletContext()); 346 pac.setPortletConfig(getPortletConfig()); 347 pac.setNamespace(getNamespace()); 348 if (getContextConfigLocation() != null) { 349 pac.setConfigLocations(StringUtils.tokenizeToStringArray(getContextConfigLocation(), 350 ConfigurablePortletApplicationContext.CONFIG_LOCATION_DELIMITERS)); 351 } 352 pac.refresh(); 353 return pac; 354 } 355 356 362 public String getPortletContextAttributeName() { 363 return PORTLET_CONTEXT_PREFIX + getPortletName(); 364 } 365 366 369 public final ApplicationContext getPortletApplicationContext() { 370 return portletApplicationContext; 371 } 372 373 381 protected void initFrameworkPortlet() throws PortletException, BeansException { 382 } 383 384 385 388 protected final void doDispatch(RenderRequest request, RenderResponse response) 389 throws PortletException, IOException { 390 391 processRequest(request, response); 392 } 393 394 397 public final void processAction(ActionRequest request, ActionResponse response) 398 throws PortletException, IOException { 399 400 processRequest(request, response); 401 } 402 403 410 protected final void processRequest(PortletRequest request, PortletResponse response) 411 throws PortletException, IOException { 412 413 long startTime = System.currentTimeMillis(); 414 Throwable failureCause = null; 415 416 try { 417 if (request instanceof ActionRequest) { 418 doActionService((ActionRequest) request, (ActionResponse) response); 419 } 420 else { 421 doRenderService((RenderRequest) request, (RenderResponse) response); 422 } 423 } 424 catch (PortletException ex) { 425 failureCause = ex; 426 throw ex; 427 } 428 catch (IOException ex) { 429 failureCause = ex; 430 throw ex; 431 } 432 catch (Throwable ex) { 433 failureCause = ex; 434 throw new PortletException("Request processing failed", ex); 435 } 436 437 finally { 438 if (failureCause != null) { 439 logger.error("Could not complete request", failureCause); 440 } 441 else { 442 logger.debug("Successfully completed request"); 443 } 444 if (isPublishEvents()) { 445 long processingTime = System.currentTimeMillis() - startTime; 447 this.portletApplicationContext.publishEvent( 448 new PortletRequestHandledEvent(this, 449 getPortletConfig().getPortletName(), request.getPortletMode().toString(), 450 (request instanceof ActionRequest ? "action" : "render"), 451 request.getRequestedSessionId(), getUsernameForRequest(request), 452 processingTime, failureCause)); 453 } 454 } 455 } 456 457 469 protected String getUsernameForRequest(PortletRequest request) { 470 Principal userPrincipal = request.getUserPrincipal(); 472 if (userPrincipal != null) { 473 return userPrincipal.getName(); 474 } 475 476 String userName = request.getRemoteUser(); 478 if (userName != null) { 479 return userName; 480 } 481 482 Map userInfo = (Map ) request.getAttribute(PortletRequest.USER_INFO); 484 if (userInfo != null) { 485 for (int i = 0, n = this.userinfoUsernameAttributes.length; i < n; i++) { 486 userName = (String ) userInfo.get(this.userinfoUsernameAttributes[i]); 487 if (userName != null) { 488 return userName; 489 } 490 } 491 } 492 493 return null; 495 } 496 497 508 protected abstract void doRenderService(RenderRequest request, RenderResponse response) 509 throws Exception ; 510 511 522 protected abstract void doActionService(ActionRequest request, ActionResponse response) 523 throws Exception ; 524 525 526 530 public void destroy() { 531 getPortletContext().log("Closing PortletApplicationContext of Spring FrameworkPortlet '" + getPortletName() + "'"); 533 if (this.portletApplicationContext instanceof ConfigurableApplicationContext) { 534 ((ConfigurableApplicationContext) this.portletApplicationContext).close(); 535 } 536 } 537 538 } 539 | Popular Tags |