1 16 17 package org.apache.jetspeed.portal.portlets; 18 19 import org.apache.jetspeed.capability.CapabilityMap; 21 import org.apache.jetspeed.capability.CapabilityMapFactory; 22 import org.apache.jetspeed.om.registry.MediaTypeEntry; 23 import org.apache.jetspeed.om.registry.PortletEntry; 24 import org.apache.jetspeed.portal.BasePortletConfig; 25 import org.apache.jetspeed.portal.expire.Expire; 26 import org.apache.jetspeed.portal.expire.ExpireFactory; 27 import org.apache.jetspeed.portal.Portlet; 28 import org.apache.jetspeed.portal.PortletConfig; 29 import org.apache.jetspeed.portal.PortletException; 30 import org.apache.jetspeed.portal.PortletState; 31 import org.apache.jetspeed.services.persistence.PersistenceManager; 32 import org.apache.jetspeed.services.persistence.PortalPersistenceException; 33 import org.apache.jetspeed.portal.PortletInstance; 34 import org.apache.jetspeed.services.portletcache.Cacheable; 35 import org.apache.jetspeed.services.Registry; 36 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 37 import org.apache.jetspeed.services.logging.JetspeedLogger; 38 import org.apache.jetspeed.util.JetspeedException; 39 import org.apache.jetspeed.util.MetaData; 40 import org.apache.jetspeed.util.MimeType; 41 42 import org.apache.jetspeed.util.JetspeedClearElement; 44 import org.apache.ecs.ConcreteElement; 45 46 import org.apache.turbine.services.cache.CachedObject; 48 import org.apache.turbine.services.cache.Refreshable; 49 import org.apache.turbine.util.RunData; 50 51 import java.util.Hashtable ; 53 import java.util.Iterator ; 54 74 public abstract class AbstractPortlet implements Portlet, PortletState, Cacheable, Refreshable 75 { 76 77 80 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(AbstractPortlet.class.getName()); 81 82 private boolean cacheable = true; 83 private PortletConfig pc = null; 84 85 88 private String name = null; 89 90 93 private String id = null; 94 95 98 private String handle = ""; 99 100 104 private Long expirationMillis = null; 105 106 110 protected Hashtable content = new Hashtable (); 111 112 115 private long creationTime; 116 117 120 private CachedObject cachedObject = null; 121 122 124 protected void clearContent() { 125 this.content.clear(); 126 } 127 128 130 protected void setContent( ConcreteElement content ) { 131 this.setContent( content, 132 CapabilityMapFactory.getDefaultCapabilityMap() ); 133 } 134 135 137 protected void setContent( String content ) { 138 this.setContent( new JetspeedClearElement( content ), 139 CapabilityMapFactory.getDefaultCapabilityMap() ); 140 } 141 142 144 protected void setContent( ConcreteElement content, 145 CapabilityMap map ) 146 throws IllegalArgumentException 147 { 148 CapabilityMap mymap = map; 149 if ( mymap == null ) { 150 mymap = CapabilityMapFactory.getDefaultCapabilityMap(); 151 } 152 153 ConcreteElement buffer = new JetspeedClearElement( content.toString( ) ); 154 this.content.put( mymap.toString(), buffer ); 155 } 156 157 158 161 162 168 public void refresh() { 169 174 logger.debug( "AbstractPortlet - Refreshing " + this.getName() ); 175 } 176 177 180 181 188 public boolean isCacheable() { 189 return this.cacheable; 190 } 191 192 198 public void setCacheable(boolean cacheable) { 199 this.cacheable = cacheable; 200 } 201 202 203 208 public Expire getExpire() { 209 try { 210 return ExpireFactory.getExpire( this, ExpireFactory.NO_EXPIRE ); 211 } catch ( JetspeedException e ) { 212 logger.error("Exception", e); 213 return null; 214 } 215 } 216 217 226 public final String getHandle() { 227 return this.handle; 228 } 229 230 238 public final void setHandle( String handle ) { 239 this.handle = handle; 240 } 241 242 247 public Long getExpirationMillis() { 248 return this.expirationMillis; 249 } 250 251 258 public void setExpirationMillis( long expirationMillis) { 259 this.expirationMillis = new Long (expirationMillis); 260 261 if (cachedObject != null) { 262 long expirationInterval = this.expirationMillis.longValue() - cachedObject.getCreated(); 263 if (expirationInterval > 0) { 264 cachedObject.setExpires(expirationInterval); 265 } else { 266 cachedObject.setStale(true); 267 } 268 } 269 } 270 271 279 public static Object getHandle(Object config) 280 { 281 PortletConfig pc = null; 284 285 if (!(config instanceof PortletConfig)) 286 { 287 return null; 288 289 } 290 291 pc = (PortletConfig)config; 293 StringBuffer handle = new StringBuffer (256); 294 295 if (pc.getURL()!=null && pc.isCachedOnURL()) 296 { 297 handle.append(String.valueOf(pc.getURL().hashCode())); 298 } 299 300 Iterator i = pc.getInitParameterNames(); 301 while(i.hasNext()) 302 { 303 String name = (String )i.next(); 304 String value = pc.getInitParameter(name); 305 306 if (value!=null) 307 { 308 handle.append("|").append(name).append("-").append(value); 309 } 310 } 311 312 return handle.toString(); 313 } 314 315 320 public void setCachedObject(CachedObject cachedObject) { 321 this.cachedObject = cachedObject; 322 } 323 324 327 328 333 public String getName() 334 { 335 336 if ( name == null ) 337 { 338 if (getPortletConfig()!=null) 339 { 340 if (getPortletConfig().getName()!=null) 341 { 342 return getPortletConfig().getName(); 343 } 344 else 345 { 346 return this.getClass().getName(); 347 } 348 } 349 } 350 351 return name; 352 353 } 354 355 360 public void setName( String name ) { 361 this.name = name; 362 } 363 364 369 public PortletConfig getPortletConfig() { 370 return this.pc; 371 } 372 373 376 public void setPortletConfig( PortletConfig pc ) { 377 this.pc = pc; 378 } 379 380 383 public ConcreteElement getContent( RunData rundata ) { 384 385 return getContent( rundata, null , true ); 386 } 387 388 public ConcreteElement getContent( RunData rundata, CapabilityMap map ) { 389 CapabilityMap mymap = map; 390 if ( mymap == null ) mymap = CapabilityMapFactory.getCapabilityMap( rundata ); 391 392 return (ConcreteElement)content.get( mymap.toString() ); 393 } 394 395 398 public ConcreteElement getContent( RunData rundata, 399 CapabilityMap map, 400 boolean allowRecurse ) { 401 402 CapabilityMap mymap = map; 403 if ( mymap == null ) mymap = CapabilityMapFactory.getCapabilityMap( rundata ); 404 405 ConcreteElement element = (ConcreteElement)content.get( mymap.toString() ); 406 407 if ( element == null ) { 408 if ( allowRecurse ) { 409 try { 410 init( ); 412 element = getContent( rundata, mymap, false ); 413 if( element != null ) { 414 this.setContent( element, mymap ); 416 } 417 } catch (Exception e) { 418 element = new JetspeedClearElement("Error when retrieving Portlet contents"); 419 if( logger.isDebugEnabled() ) { 420 logger.debug( "Error when retrieving Portlet contents", e ); 421 } 422 } 423 } else { 424 if( element == null ) { 425 mymap = CapabilityMapFactory.getDefaultCapabilityMap(); 427 element = (ConcreteElement)content.get( mymap.toString() ); 428 if( element == null ) { 429 element = new JetspeedClearElement("Unknown Problem getting Contents"); 430 } 431 } 432 } 433 } 434 435 return element; 436 437 } 438 439 444 public String getDescription() { 445 if (getPortletConfig()!=null) 446 if (getPortletConfig().getMetainfo()!=null) 447 return getPortletConfig().getMetainfo().getDescription(); 448 449 return null; 450 } 451 452 457 public String getDescription(String instanceDescription) 458 { 459 if (instanceDescription != null) 460 return instanceDescription; 461 return getDescription(); 462 } 463 464 466 public void setDescription( String description ) { 467 PortletConfig pc = getPortletConfig(); 468 if (pc==null) { 469 pc = new BasePortletConfig(); 470 setPortletConfig(pc); 471 } 472 473 MetaData meta = pc.getMetainfo(); 474 if (meta==null) { 475 meta = new MetaData(); 476 pc.setMetainfo(meta); 477 } 478 479 meta.setDescription(description); 480 } 481 482 484 489 public String getTitle() 490 { 491 if (getPortletConfig()!=null) 492 if (getPortletConfig().getMetainfo()!=null) 493 return getPortletConfig().getMetainfo().getTitle(); 494 495 return null; 496 } 497 498 503 public String getTitle(String instanceTitle) 504 { 505 if (instanceTitle != null) 506 return instanceTitle; 507 return getTitle(); 508 } 509 510 514 public void setTitle( String title ) { 515 PortletConfig pc = getPortletConfig(); 516 if (pc==null) { 517 pc = new BasePortletConfig(); 518 setPortletConfig(pc); 519 } 520 521 MetaData meta = pc.getMetainfo(); 522 if (meta==null) { 523 meta = new MetaData(); 524 pc.setMetainfo(meta); 525 } 526 527 meta.setTitle(title); 528 } 529 530 531 535 public String getImage() 536 { 537 if (getPortletConfig()!=null) 538 if (getPortletConfig().getMetainfo()!=null) 539 return getPortletConfig().getMetainfo().getImage(); 540 541 return null; 542 } 543 544 548 public String getImage(String instanceImage) 549 { 550 if (instanceImage != null) 551 return instanceImage; 552 return getImage(); 553 } 554 555 public void setImage( String image ) 556 { 557 PortletConfig pc = getPortletConfig(); 558 if (pc==null) { 559 pc = new BasePortletConfig(); 560 setPortletConfig(pc); 561 } 562 563 MetaData meta = pc.getMetainfo(); 564 if (meta==null) { 565 meta = new MetaData(); 566 pc.setMetainfo(meta); 567 } 568 569 meta.setImage(image); 570 } 571 572 578 public boolean getAllowEdit( RunData rundata ) 579 { 580 return allowCustomize(rundata); 581 } 582 583 591 public boolean getAllowView( RunData rundata ) 592 { 593 return allowView( rundata ); 594 } 595 596 602 public boolean getAllowMaximize( RunData rundata ) 603 { 604 return allowMaximize( rundata ); 605 } 606 607 610 public void init( ) throws PortletException 611 { 612 clearContent(); 614 } 615 616 618 public long getCreationTime() { 619 return this.creationTime; 620 } 621 622 624 public void setCreationTime( long creationTime ) { 625 this.creationTime = creationTime; 626 } 627 628 630 public boolean supportsType( MimeType mimeType ) 631 { 632 PortletEntry entry = (PortletEntry)Registry.getEntry(Registry.PORTLET, getName() ); 633 String baseType = mimeType.toString(); 634 if (entry!=null) 635 { 636 Iterator i = entry.listMediaTypes(); 637 638 while(i.hasNext()) 639 { 640 String name = (String )i.next(); 641 MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, name); 642 if (media != null) 643 { 644 if (baseType.equals(media.getMimeType())) return true; 645 } 646 } 647 } 648 649 return MimeType.HTML.equals( mimeType ); 650 } 651 652 655 656 662 public boolean allowClose( RunData rundata ) 663 { 664 return !isClosed( rundata ); 667 } 668 669 674 public boolean isClosed(RunData rundata) 675 { 676 return this.getAttribute("_display", "normal", rundata ).equals("closed"); 677 } 678 679 685 public void setClosed(boolean close, RunData rundata) 686 { 687 if( allowClose( rundata ) ) 688 { 689 this.setAttribute("_display", close ? "closed" : "normal", rundata ); 690 } 691 } 692 693 699 public boolean allowInfo( RunData rundata ) 700 { 701 return true; 704 } 705 706 712 public boolean allowCustomize( RunData rundata ) 713 { 714 return true; 717 } 718 719 725 public boolean allowMaximize( RunData rundata ) 726 { 727 return true; 730 } 731 732 738 public boolean allowMinimize( RunData rundata ) 739 { 740 return true; 743 } 744 745 751 public boolean allowView( RunData rundata ) 752 { 753 return true; 756 } 757 758 764 public boolean allowPrintFriendly( RunData rundata ) 765 { 766 return true; 769 } 770 771 776 public boolean isMinimized(RunData rundata) 777 { 778 return this.getAttribute("_display", "normal", rundata ).equals("minimized"); 779 } 780 781 787 public void setMinimized( boolean minimize, RunData rundata ) 788 { 789 if( allowMinimize( rundata ) ) 790 { 791 this.setAttribute("_display", minimize ? "minimized" : "normal", rundata ); 792 } 793 } 794 795 801 public boolean isShowTitleBar(RunData rundata) 802 { 803 if (getPortletConfig()!=null) 804 { 805 return Boolean.valueOf(getPortletConfig().getInitParameter("_showtitlebar","true")).booleanValue(); 807 } 808 return this.getAttribute("_showtitlebar", "true", rundata ).equals("true"); 809 } 810 812 820 public String getAttribute( String attrName, String attrDefValue, RunData rundata ) 821 { 822 String attrValue = null ; 823 824 PortletInstance instance = PersistenceManager.getInstance(this, rundata); 825 attrValue = instance.getAttribute(attrName, attrDefValue); 826 827 return attrValue; 828 } 829 830 837 public void setAttribute( String attrName, String attrValue, RunData rundata ) 838 { 839 try 840 { 841 PortletInstance instance = PersistenceManager.getInstance(this, rundata); 842 instance.setAttribute(attrName, attrValue); 843 PersistenceManager.store(instance); 844 } 845 catch (PortalPersistenceException e) 846 { 847 logger.error("Exception while setting attribute "+attrName+" for portlet "+getName(), e); 848 } 849 } 850 851 857 public PortletInstance getInstance(RunData rundata) 858 { 859 return PersistenceManager.getInstance(this, rundata); 860 } 861 862 public String getID() 866 { 867 return id; 868 } 869 870 public void setID(String id) 871 { 872 this.id = id; 873 } 874 875 878 public boolean providesCustomization() 879 { 880 return false; 881 } 882 883 } 884 | Popular Tags |