1 48 49 package com.caucho.portal.generic; 50 51 import javax.portlet.*; 52 import java.io.IOException ; 53 import java.lang.reflect.Constructor ; 54 import java.lang.reflect.Modifier ; 55 import java.util.*; 56 import java.util.logging.Level ; 57 import java.util.logging.Logger ; 58 59 abstract public class GenericWindow 60 implements Window, PortletConfig 61 { 62 static protected final Logger log = 63 Logger.getLogger(GenericWindow.class.getName()); 64 65 private String _namespace = ""; 66 private String _portletName; 67 private Map<String , String > _initParamMap; 68 private int _expirationCache; 69 private boolean _isPrivate; 70 private Renderer _renderer; 71 private int _bufferSize; 72 private Set<Locale> _supportedLocales; 73 private GenericPortletPreferences _defaultPreferences; 74 private ResourceBundleFactory _resourceBundleFactory; 75 private String _errorPage; 76 77 private PortletContext _portletContext; 78 79 public GenericWindow() 80 { 81 } 82 83 90 public void setNamespace(String namespace) 91 { 92 _namespace = namespace; 93 } 94 95 98 public void setPortletName(String portletName) 99 { 100 _portletName = portletName; 101 } 102 103 106 public void addInitParam(String name, String value) 107 { 108 if (_initParamMap == null) 109 _initParamMap = new LinkedHashMap<String , String >(); 110 111 _initParamMap.put(name, value); 112 } 113 114 public void addInitParam(NameValuePair nameValuePair) 115 { 116 addInitParam(nameValuePair.getName(), nameValuePair.getValue()); 117 } 118 119 122 public void setPortletPreferences(GenericPortletPreferences defaultPreferences) 123 { 124 _defaultPreferences = defaultPreferences; 125 } 126 127 132 public void setExpirationCache(int expirationCache) 133 { 134 _expirationCache = expirationCache; 135 } 136 137 145 public void setPrivate(boolean isPrivate) 146 { 147 _isPrivate = isPrivate; 148 } 149 150 154 void addSupportedLocale(String locale) 155 { 156 String language = ""; 157 String country = ""; 158 String variant = ""; 159 160 String [] split = locale.split("_", 3); 161 int len = split.length; 162 163 if (len == 0) { 164 split = locale.split("-", 3); 165 len = split.length; 166 } 167 168 if (len == 0) 169 throw new IllegalArgumentException (locale); 170 171 language = split[0]; 172 if (len > 0) 173 country = split[1]; 174 if (len > 1) 175 country = split[2]; 176 177 if (_supportedLocales == null) 178 _supportedLocales = new LinkedHashSet<Locale>(); 179 180 _supportedLocales.add(new Locale(language, country, variant)); 181 } 182 183 188 void addSupportedLocales(String locales) 189 { 190 String [] split = locales.split("\\s*,\\s*"); 191 for (int i = 0; i < split.length; i++) 192 addSupportedLocale(split[i]); 193 } 194 195 199 public void setResourceBundle(String name) 200 { 201 ResourceBundleFactory resourceBundleFactory = 202 new ResourceBundleFactory(); 203 204 resourceBundleFactory.setName(name); 205 206 setResourceBundleFactory(resourceBundleFactory); 207 } 208 209 public void setResourceBundleFactory(ResourceBundleFactory factory) 210 { 211 if (_resourceBundleFactory != null) 212 throw new IllegalArgumentException ("resource-bundle-factory already set"); 213 } 214 215 219 public void setRenderer(Renderer renderer) 220 { 221 _renderer = renderer; 222 } 223 224 228 public void setRendererClass(String className) 229 { 230 setRenderer( (Renderer) newInstance(Renderer.class, className) ); 231 } 232 233 239 public void setBufferSize(int bufferSize) 240 { 241 _bufferSize = bufferSize; 242 } 243 244 296 public void setErrorPage(String errorPage) 297 { 298 _errorPage = errorPage; 299 300 if (_bufferSize == 0) 301 _bufferSize = -1; 302 } 303 304 public void init(PortletContext portletContext) 305 throws PortletException 306 { 307 if (_portletContext != null) 308 throw new IllegalStateException ("portlet-context already set!"); 309 310 _portletContext = portletContext; 311 312 if (_portletName == null) 313 _portletName = _namespace == null ? "anonymous" : _namespace; 314 } 315 316 332 protected Object newInstance(Class targetClass, String className) 333 throws IllegalArgumentException 334 { 335 Class cl = null; 336 337 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 338 339 try { 340 cl = Class.forName(className, false, loader); 341 } catch (ClassNotFoundException e) { 342 } 343 344 if (cl == null) 345 throw new IllegalArgumentException ( 346 "`" + className + "' is not a known class"); 347 348 if (!targetClass.isAssignableFrom(cl)) 349 throw new IllegalArgumentException ( 350 "'" + className + "' must implement " + targetClass.getName()); 351 352 if (Modifier.isAbstract(cl.getModifiers())) 353 throw new IllegalArgumentException ( 354 "'" + className + "' must not be abstract."); 355 356 if (!Modifier.isPublic(cl.getModifiers())) 357 throw new IllegalArgumentException ( 358 "'" + className + "' must be public."); 359 360 Constructor []constructors = cl.getDeclaredConstructors(); 361 362 Constructor zeroArg = null; 363 for (int i = 0; i < constructors.length; i++) { 364 if (constructors[i].getParameterTypes().length == 0) { 365 zeroArg = constructors[i]; 366 break; 367 } 368 } 369 370 if (zeroArg == null || !Modifier.isPublic(zeroArg.getModifiers())) 371 throw new IllegalArgumentException ( 372 "'" + className + "' must have a public zero arg constructor"); 373 374 Object obj = null; 375 376 try { 377 obj = cl.newInstance(); 378 } 379 catch (Exception ex) { 380 throw new IllegalArgumentException ( 381 "error instantiating `" + className + "': " + ex.toString(), ex); 382 } 383 384 return obj; 385 } 386 387 protected String getNamespace() 388 { 389 return _namespace; 390 } 391 392 395 public PortletContext getPortletContext() 396 { 397 if (_portletContext == null) 398 throw new IllegalStateException ("missing init()?"); 399 400 return _portletContext; 401 } 402 403 406 public PortletConfig getPortletConfig() 407 { 408 return this; 409 } 410 411 414 public String getPortletName() 415 { 416 return _portletName; 417 } 418 419 public String getInitParameter(String name) 420 { 421 if (_initParamMap == null) 422 return null; 423 else 424 return _initParamMap.get(name); 425 } 426 427 public Enumeration getInitParameterNames() 428 { 429 if (_initParamMap == null) 430 return Collections.enumeration(Collections.EMPTY_LIST); 431 else 432 return Collections.enumeration(_initParamMap.keySet()); 433 } 434 435 438 public int getExpirationCache() 439 { 440 return _expirationCache; 441 } 442 443 446 public boolean isPrivate() 447 { 448 return _isPrivate; 449 } 450 451 456 public boolean isWindowStateAllowed(PortletRequest request, 457 WindowState windowState) 458 { 459 return true; 462 } 463 464 469 public boolean isPortletModeAllowed(PortletRequest request, 470 PortletMode portletMode) 471 { 472 return true; 474 } 475 476 482 public Set<String > getSupportedContentTypes(PortletMode portletMode) 483 { 484 return null; 487 } 488 489 495 public Set<Locale> getSupportedLocales() 496 { 497 return _supportedLocales; 498 } 499 500 505 public PortletPreferences getDefaultPreferences() 506 { 507 return _defaultPreferences; 508 } 509 510 513 public ArrayList<PreferencesValidator> getPreferencesValidators() 514 { 515 if (_defaultPreferences != null) 516 return _defaultPreferences.getPreferencesValidators(); 517 else 518 return null; 519 } 520 521 526 public Map<String , String > getRoleRefMap() 527 { 528 return null; 529 } 531 532 537 public ArrayList<Constraint> getConstraints() 538 { 539 return null; 540 } 542 543 546 public Renderer getRenderer() 547 { 548 return _renderer; 549 } 550 551 556 public PortletMode handlePortletModeFailure( PortletRequest request, 557 PortletMode notAllowed ) 558 { 559 return PortletMode.VIEW; 560 } 561 562 563 568 public WindowState handleWindowStateFailure( PortletRequest request, 569 WindowState notAllowed ) 570 { 571 return WindowState.NORMAL; 572 } 573 574 579 public void handleConstraintFailure( RenderRequest request, 580 RenderResponse response, 581 ConstraintFailureEvent event ) 582 { 583 if (_errorPage == null) 584 return; 585 586 Object existingConstraint = 587 request.getAttribute("com.caucho.portal.error.constraint"); 588 589 Object existingConstraintType = 590 request.getAttribute("com.caucho.portal.error.constraint_type"); 591 592 Object existingStatusCode = 593 request.getAttribute("com.caucho.portal.error.status_code"); 594 595 Constraint constraint = event.getConstraint(); 596 Class constraintType = constraint.getClass(); 597 Integer statusCode = new Integer (event.getStatusCode()); 598 599 request.setAttribute( "com.caucho.portal.error.constraint", 600 constraint ); 601 request.setAttribute( "com.caucho.portal.error.constraint_type", 602 constraintType); 603 request.setAttribute( "com.caucho.portal.error.status_code", 604 statusCode ); 605 606 try { 607 if (handleErrorPage(request, response)) 608 event.setHandled(false); 609 else 610 event.setHandled(true); 611 } 612 finally { 613 if (existingConstraint == null) { 614 request.removeAttribute( "com.caucho.portal.error.constraint" ); 615 request.removeAttribute( "com.caucho.portal.error.constraint_type" ); 616 request.removeAttribute( "com.caucho.portal.error.status_code" ); 617 } 618 else { 619 request.setAttribute( "com.caucho.portal.error.constraint", 620 existingConstraint ); 621 622 request.setAttribute( "com.caucho.portal.error.constraint_type", 623 existingConstraintType ); 624 625 request.setAttribute( "com.caucho.portal.error.status_code", 626 existingStatusCode ); 627 } 628 } 629 } 630 631 632 637 public void handleException( RenderRequest request, 638 RenderResponse response, 639 ExceptionEvent event ) 640 { 641 if (_errorPage == null) 642 return; 643 644 Object existingException = 645 request.getAttribute("com.caucho.portal.error.exception"); 646 647 Object existingExceptionType = 648 request.getAttribute("com.caucho.portal.error.exception_type"); 649 650 Object existingMessage = 651 request.getAttribute("com.caucho.portal.error.message"); 652 653 Exception exception = event.getException(); 654 Class exceptionType = exception.getClass(); 655 String message = exception.getMessage(); 656 657 request.setAttribute( "com.caucho.portal.error.exception", 658 exception ); 659 660 request.setAttribute( "com.caucho.portal.error.exception_type", 661 exceptionType ); 662 663 request.setAttribute( "com.caucho.portal.error.message", 664 message ); 665 666 try { 667 if (handleErrorPage(request, response)) 668 event.setHandled(false); 669 else 670 event.setHandled(true); 671 } 672 finally { 673 if (existingException == null) { 674 request.removeAttribute( "com.caucho.portal.error.exception" ); 675 request.removeAttribute( "com.caucho.portal.error.exception_type" ); 676 request.removeAttribute( "com.caucho.portal.error.message" ); 677 } 678 else { 679 request.setAttribute( "com.caucho.portal.error.exception", 680 existingException ); 681 682 request.setAttribute( "com.caucho.portal.error.exception_type", 683 existingExceptionType ); 684 685 request.setAttribute( "com.caucho.portal.error.message", 686 existingMessage ); 687 } 688 } 689 } 690 691 private boolean handleErrorPage( RenderRequest request, 692 RenderResponse response ) 693 { 694 try { 695 696 PortletRequestDispatcher dispatcher 697 = getPortletContext().getRequestDispatcher(_errorPage); 698 699 dispatcher.include(request, response); 700 } 701 catch (Exception ex) { 702 log.log(Level.WARNING, ex.toString(), ex); 703 return false; 704 } 705 706 return true; 707 } 708 709 712 public ResourceBundle getResourceBundle(Locale locale) 713 { 714 if (_resourceBundleFactory == null) 715 _resourceBundleFactory = new ResourceBundleFactory(); 716 717 return _resourceBundleFactory.getResourceBundle(locale); 718 } 719 720 723 public int getBufferSize() 724 { 725 return _bufferSize; 726 } 727 728 abstract public void processAction(PortletConnection connection) 729 throws PortletException, IOException ; 730 731 abstract public void render(PortletConnection connection) 732 throws PortletException, IOException ; 733 734 public void destroy() 735 { 736 } 737 } 738 739 | Popular Tags |