1 48 49 package com.caucho.portal.generic; 50 51 import javax.portlet.*; 52 import javax.servlet.ServletConfig ; 53 import javax.servlet.ServletException ; 54 import javax.servlet.http.HttpServlet ; 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 import java.io.IOException ; 58 import java.lang.reflect.Constructor ; 59 import java.lang.reflect.Modifier ; 60 import java.util.*; 61 import java.util.logging.Logger ; 62 63 137 public class PortletServlet 138 extends HttpServlet 139 implements Window, PortletConfig 140 { 141 static protected final Logger log = 142 Logger.getLogger(PortletServlet.class.getName()); 143 144 private Portal _portal; 145 private HttpPortletContext _portletContext; 146 147 private Portlet _portlet; 148 private String _portletName; 149 private String _namespace = ""; 150 private Map<String , String > _initParamMap; 151 private int _expirationCache; 152 private boolean _isPrivate; 153 private Renderer _renderer; 154 private int _bufferSize; 155 private Set<Locale> _supportedLocales; 156 private GenericPortletPreferences _defaultPreferences; 157 private ResourceBundleFactory _resourceBundleFactory; 158 159 162 public void setPortal(Portal portal) 163 { 164 if (_portal != null) 165 throw new IllegalArgumentException ("`portal' already set"); 166 167 _portal = portal; 168 } 169 170 174 public void setPortalClass(String className) 175 { 176 setPortal( (Portal) newInstance(Portal.class, className) ); 177 } 178 179 184 public void setPortalRef(String attributeName) 185 { 186 Portal portal = (Portal) getServletContext().getAttribute(attributeName); 187 188 if (portal == null) 189 throw new IllegalArgumentException ( 190 "Portal not found with ServletContext attribute name `" 191 + attributeName + "'"); 192 193 setPortal(portal); 194 } 195 196 203 public void setNamespace(String namespace) 204 { 205 _namespace = namespace; 206 } 207 208 213 public void setPortlet(Portlet portlet) 214 { 215 if (_portlet != null) 216 throw new IllegalArgumentException ("`portlet' is already set"); 217 218 _portlet = portlet; 219 } 220 221 222 226 public void setPortletClass(String className) 227 { 228 setPortlet( (Portlet) newInstance(Portlet.class, className) ); 229 } 230 231 234 public void setPortletName(String portletName) 235 { 236 _portletName = portletName; 237 } 238 239 245 public void addInitParam(String name, String value) 246 { 247 if (_initParamMap == null) 248 _initParamMap = new LinkedHashMap<String , String >(); 249 250 _initParamMap.put(name, value); 251 } 252 253 public void addInitParam(NameValuePair nameValuePair) 254 { 255 addInitParam(nameValuePair.getName(), nameValuePair.getValue()); 256 } 257 258 261 public void setPortletPreferences(GenericPortletPreferences defaultPreferences) 262 { 263 _defaultPreferences = defaultPreferences; 264 } 265 266 274 public void setExpirationCache(int expirationCache) 275 { 276 _expirationCache = expirationCache; 277 } 278 279 290 public void setPrivate(boolean isPrivate) 291 { 292 _isPrivate = isPrivate; 293 } 294 295 299 void addSupportedLocale(String locale) 300 { 301 String language = ""; 302 String country = ""; 303 String variant = ""; 304 305 String [] split = locale.split("_", 3); 306 int len = split.length; 307 308 if (len == 0) { 309 split = locale.split("-", 3); 310 len = split.length; 311 } 312 313 if (len == 0) 314 throw new IllegalArgumentException (locale); 315 316 language = split[0]; 317 if (len > 0) 318 country = split[1]; 319 if (len > 1) 320 country = split[2]; 321 322 if (_supportedLocales == null) 323 _supportedLocales = new LinkedHashSet<Locale>(); 324 325 _supportedLocales.add(new Locale(language, country, variant)); 326 } 327 328 333 void addSupportedLocales(String locales) 334 { 335 String [] split = locales.split("\\s*,\\s*"); 336 for (int i = 0; i < split.length; i++) 337 addSupportedLocale(split[i]); 338 } 339 340 344 public void setResourceBundle(String name) 345 { 346 ResourceBundleFactory resourceBundleFactory = 347 new ResourceBundleFactory(); 348 349 resourceBundleFactory.setName(name); 350 351 setResourceBundleFactory(resourceBundleFactory); 352 } 353 354 public void setResourceBundleFactory(ResourceBundleFactory factory) 355 { 356 if (_resourceBundleFactory != null) 357 throw new IllegalArgumentException ("resource-bundle-factory already set"); 358 } 359 360 364 public void setRenderer(Renderer renderer) 365 { 366 _renderer = renderer; 367 } 368 369 373 public void setRendererClass(String className) 374 { 375 setRenderer( (Renderer) newInstance(Renderer.class, className) ); 376 } 377 378 381 public void setBufferSize(int bufferSize) 382 { 383 _bufferSize = bufferSize; 384 } 385 386 public void init(ServletConfig servletConfig) 387 throws ServletException 388 { 389 super.init(servletConfig); 390 391 String p; 392 393 p = super.getInitParameter("portal-class"); 394 if (p != null) 395 setPortalClass( p ); 396 397 p = super.getInitParameter("portal-ref"); 398 if (p != null) 399 setPortalRef(p); 400 401 if (_portal == null) 402 _portal = new GenericPortal(); 403 404 p = super.getInitParameter("portlet-class"); 405 if (p != null) 406 setPortletClass( p ); 407 408 if (_portlet == null) 409 throw new ServletException ("`portlet' is required"); 410 411 p = super.getInitParameter("portlet-name"); 412 if (p != null) 413 setPortletName(p); 414 415 if (_portletName == null) 416 _portletName = servletConfig.getServletName(); 417 418 p = super.getInitParameter("expiration-cache"); 419 if (p != null) 420 setExpirationCache(Integer.parseInt(p)); 421 422 p = super.getInitParameter("private"); 423 if (p != null) 424 setPrivate(Boolean.valueOf(p).booleanValue()); 425 426 p = super.getInitParameter("buffer-size"); 427 if (p != null) 428 setBufferSize(Integer.parseInt(p)); 429 430 p = super.getInitParameter("supported-locales"); 431 if (p != null) 432 addSupportedLocales( p ); 433 434 p = super.getInitParameter("resource-bundle"); 435 if (p != null) 436 setResourceBundle( p ); 437 438 p = super.getInitParameter("renderer-class"); 439 if (p != null) 440 setRendererClass( p ); 441 442 _portletContext = new HttpPortletContext(getServletContext()); 443 444 try { 445 _portlet.init(this); 446 } 447 catch (PortletException ex) { 448 throw new ServletException (ex); 449 } 450 } 451 452 protected Object newInstance(Class targetClass, String className) 453 throws IllegalArgumentException 454 { 455 Class cl = null; 456 457 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 458 459 try { 460 cl = Class.forName(className, false, loader); 461 } catch (ClassNotFoundException e) { 462 } 463 464 if (cl == null) 465 throw new IllegalArgumentException ( 466 "`" + className + "' is not a known class"); 467 468 if (!targetClass.isAssignableFrom(cl)) 469 throw new IllegalArgumentException ( 470 "'" + className + "' must implement " + targetClass.getName()); 471 472 if (Modifier.isAbstract(cl.getModifiers())) 473 throw new IllegalArgumentException ( 474 "'" + className + "' must not be abstract."); 475 476 if (!Modifier.isPublic(cl.getModifiers())) 477 throw new IllegalArgumentException ( 478 "'" + className + "' must be public."); 479 480 Constructor []constructors = cl.getDeclaredConstructors(); 481 482 Constructor zeroArg = null; 483 for (int i = 0; i < constructors.length; i++) { 484 if (constructors[i].getParameterTypes().length == 0) { 485 zeroArg = constructors[i]; 486 break; 487 } 488 } 489 490 if (zeroArg == null || !Modifier.isPublic(zeroArg.getModifiers())) 491 throw new IllegalArgumentException ( 492 "'" + className + "' must have a public zero arg constructor"); 493 494 Object obj = null; 495 496 try { 497 obj = cl.newInstance(); 498 } 499 catch (Exception ex) { 500 throw new IllegalArgumentException ( 501 "error instantiating `" + className + "': " + ex.toString(), ex); 502 } 503 504 return obj; 505 } 506 507 public PortletConfig getPortletConfig() 508 { 509 return this; 510 } 511 512 public String getInitParameter(String name) 513 { 514 if (_initParamMap == null) 515 return super.getInitParameter(name); 516 else 517 return _initParamMap.get(name); 518 } 519 520 public Enumeration getInitParameterNames() 521 { 522 if (_initParamMap == null) 523 return super.getInitParameterNames(); 524 else 525 return Collections.enumeration(_initParamMap.keySet()); 526 } 527 528 531 public int getExpirationCache() 532 { 533 return _expirationCache; 534 } 535 536 539 public boolean isPrivate() 540 { 541 return _isPrivate; 542 } 543 544 549 public boolean isWindowStateAllowed(PortletRequest request, 550 WindowState windowState) 551 { 552 return true; 555 } 556 557 562 public boolean isPortletModeAllowed(PortletRequest request, 563 PortletMode portletMode) 564 { 565 return true; 567 } 568 569 575 public Set<String > getSupportedContentTypes(PortletMode portletMode) 576 { 577 return null; 580 } 581 582 588 public Set<Locale> getSupportedLocales() 589 { 590 return _supportedLocales; 591 } 592 593 598 public PortletPreferences getDefaultPreferences() 599 { 600 return _defaultPreferences; 601 } 602 603 606 public ArrayList<PreferencesValidator> getPreferencesValidators() 607 { 608 if (_defaultPreferences != null) 609 return _defaultPreferences.getPreferencesValidators(); 610 else 611 return null; 612 } 613 614 619 public Map<String , String > getRoleRefMap() 620 { 621 return null; 622 } 624 625 630 public ArrayList<Constraint> getConstraints() 631 { 632 return null; 633 } 635 636 639 public Renderer getRenderer() 640 { 641 return _renderer; 642 } 643 644 649 public PortletMode handlePortletModeFailure( PortletRequest request, 650 PortletMode notAllowed ) 651 { 652 return PortletMode.VIEW; 653 } 654 655 656 661 public WindowState handleWindowStateFailure( PortletRequest request, 662 WindowState notAllowed ) 663 { 664 return WindowState.NORMAL; 665 } 666 667 672 public void handleConstraintFailure(RenderRequest request, 673 RenderResponse response, 674 ConstraintFailureEvent event) 675 { 676 } 677 678 679 684 public void handleException(RenderRequest request, 685 RenderResponse response, 686 ExceptionEvent event) 687 { 688 } 690 691 694 public String getPortletName() 695 { 696 return _portletName; 697 } 698 699 702 public PortletContext getPortletContext() 703 { 704 return _portletContext; 705 } 706 707 710 public ResourceBundle getResourceBundle(Locale locale) 711 { 712 if (_resourceBundleFactory == null) 713 _resourceBundleFactory = new ResourceBundleFactory(); 714 715 return _resourceBundleFactory.getResourceBundle(locale); 716 } 717 718 721 public int getBufferSize() 722 { 723 return _bufferSize; 724 } 725 726 protected void doGet(HttpServletRequest req, HttpServletResponse res) 727 throws ServletException , IOException 728 { 729 doRequest(req, res); 730 } 731 732 protected void doPost(HttpServletRequest req, HttpServletResponse res) 733 throws ServletException , IOException 734 { 735 doRequest(req, res); 736 } 737 738 protected void doRequest(HttpServletRequest httpRequest, 739 HttpServletResponse httpResponse) 740 throws ServletException , IOException 741 { 742 HttpPortletConnection connection = new HttpPortletConnection(); 743 connection.start(_portal, _portletContext, httpRequest, httpResponse, true); 744 745 try { 746 Action action = connection.getAction(this, _namespace); 747 748 if (action != null) { 749 try { 750 if (action.isTarget()) { 751 action.processAction(_portlet); 752 } 753 } 754 finally { 755 action.finish(); 756 } 757 } 758 759 Render render = connection.getRender(this, _namespace); 760 761 if (render != null) { 762 try { 763 render.render(_portlet); 764 } 765 finally { 766 render.finish(); 767 } 768 } 769 770 connection.checkForFailure(); 771 } 772 catch (PortletException ex) { 773 throw new ServletException (ex); 774 } 775 finally { 776 connection.finish(); 777 } 778 } 779 780 public void destroy() 781 { 782 if (_portlet != null) 783 _portlet.destroy(); 784 } 785 786 } 787 788 | Popular Tags |