📄 abstractportlet.java
字号:
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jetspeed.portal.portlets;
//jetspeed
import org.apache.jetspeed.capability.CapabilityMap;
import org.apache.jetspeed.capability.CapabilityMapFactory;
import org.apache.jetspeed.om.registry.MediaTypeEntry;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.portal.BasePortletConfig;
import org.apache.jetspeed.portal.expire.Expire;
import org.apache.jetspeed.portal.expire.ExpireFactory;
import org.apache.jetspeed.portal.Portlet;
import org.apache.jetspeed.portal.PortletConfig;
import org.apache.jetspeed.portal.PortletException;
import org.apache.jetspeed.portal.PortletState;
import org.apache.jetspeed.services.persistence.PersistenceManager;
import org.apache.jetspeed.services.persistence.PortalPersistenceException;
import org.apache.jetspeed.portal.PortletInstance;
import org.apache.jetspeed.services.portletcache.Cacheable;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.util.JetspeedException;
import org.apache.jetspeed.util.MetaData;
import org.apache.jetspeed.util.MimeType;
//ecs
import org.apache.jetspeed.util.JetspeedClearElement;
import org.apache.ecs.ConcreteElement;
//turbine stuff
import org.apache.turbine.services.cache.CachedObject;
import org.apache.turbine.services.cache.Refreshable;
import org.apache.turbine.util.RunData;
//java stuff
import java.util.Hashtable;
import java.util.Iterator;
/**
<p>
Should be used by most Portlets that wish to conform to default behavior
</p>
<p>
PERFORMANCE NOTE:
getContent returns a StringElement that was generated on setContent(). This is
used so that performance is increased since ECS does not have to work overtime
to generate output.
</p>
@author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>
@author <A HREF="mailto:raphael@apache.org">Rapha雔 Luta</A>
@author <A HREF="mailto:sgala@apache.org">Santiago Gala</A>
@author <A HREF="mailto:paulsp@apache.org">Paul Spencer</A>
@author <A HREF="mailto:morciuch@apache.org">Mark Orciuch</A>
@version $Id: AbstractPortlet.java,v 1.65 2004/03/29 21:38:42 taylor Exp $
*/
public abstract class AbstractPortlet implements Portlet, PortletState, Cacheable, Refreshable
{
/**
* Static initialization of the logger for this class
*/
private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(AbstractPortlet.class.getName());
private boolean cacheable = true;
private PortletConfig pc = null;
/**
Provide a required name for this Portlet
*/
private String name = null;
/**
Provide a Unique Portlet ID
*/
private String id = null;
/**
Cache handle for this object.
*/
private String handle = "";
/**
Expiration time of object in milliseconds since the standard base time
known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
*/
private Long expirationMillis = null;
/**
Holds instances of ConcreteElements (Portlet output/content)
based on its current CapabilityMap.
*/
protected Hashtable content = new Hashtable();
/**
The time this portlet was created.
*/
private long creationTime;
/**
* Handle to cached object
*/
private CachedObject cachedObject = null;
/**
*/
protected void clearContent() {
this.content.clear();
}
/**
*/
protected void setContent( ConcreteElement content ) {
this.setContent( content,
CapabilityMapFactory.getDefaultCapabilityMap() );
}
/**
*/
protected void setContent( String content ) {
this.setContent( new JetspeedClearElement( content ),
CapabilityMapFactory.getDefaultCapabilityMap() );
}
/**
*/
protected void setContent( ConcreteElement content,
CapabilityMap map )
throws IllegalArgumentException
{
CapabilityMap mymap = map;
if ( mymap == null ) {
mymap = CapabilityMapFactory.getDefaultCapabilityMap();
}
ConcreteElement buffer = new JetspeedClearElement( content.toString( ) );
this.content.put( mymap.toString(), buffer );
}
/*
* Implement methods required by Refreshable
*/
/**
* Usually called by caching system when portlet is marked as expired, but
* has not be idle longer then TimeToLive.
*
* This method should be implement in cachable portlets
*/
public void refresh() {
/*
* The following message is here to add in debugging. It is
* expected the any portlet type that is refreshable will
* implement this method.
*/
logger.debug( "AbstractPortlet - Refreshing " + this.getName() );
}
/*
* Implement methods required by Cacheable
*/
/**
* Is this portlet cacheable. It is the portlet's responsability to
* cache the content.
*
* @return <CODE>true</CODE> Cachable<BR>
* <CODE>false</CODE> Not cachable
*/
public boolean isCacheable() {
return this.cacheable;
}
/**
* Set cachable. This should only be called in the portlet's init().
*
* @param cacheable <CODE>true</CODE> Portlet is cachable<BR>
* <CODE>false</CODE> Portlet is NOT cachable
*/
public void setCacheable(boolean cacheable) {
this.cacheable = cacheable;
}
/**
* Used by a Cacheable object to determine when it should expire itself from the cache.
*
* @return Expire
*/
public Expire getExpire() {
try {
return ExpireFactory.getExpire( this, ExpireFactory.NO_EXPIRE );
} catch ( JetspeedException e ) {
logger.error("Exception", e);
return null;
}
}
/**
* <p>Used by the cache to get a unique reference on what you want to add
* and then retrieve in the future from the cache</p>
*
* <p>Most implementations should just call the CacheHandleManager with
* the given params within the implementation and just return this.</p>
*
* @return Cache handle (key)
*/
public final String getHandle() {
return this.handle;
}
/**
* Used by a Cacheable object to determine when it should
* expire itself from the cache.
*
* @param handle Cache Handle
*
* @deprecated cacheable classes should now implement a static getHandle(config) method
*/
public final void setHandle( String handle ) {
this.handle = handle;
}
/**
* Set the expiration time in milliseconds.
*
* @return Expiration time in milliseconds since epoch, or null if the expiration was not set.
*/
public Long getExpirationMillis() {
return this.expirationMillis;
}
/**
* Sets the cache expiration time. When the portlet is stale (expired),
* the refresh() will be called if the portlet has not been untouched
* longer then then it's TimeToLive.
*
* @param expirationMillis setExpirationMillis Expiration in milliseconds since epoch
*/
public void setExpirationMillis( long expirationMillis) {
this.expirationMillis = new Long(expirationMillis);
if (cachedObject != null) {
long expirationInterval = this.expirationMillis.longValue() - cachedObject.getCreated();
if (expirationInterval > 0) {
cachedObject.setExpires(expirationInterval);
} else {
cachedObject.setStale(true);
}
}
}
/**
* Builds a new cache handle for this cacheable class with the specified
* config object.
*
* @param config The configuration object to use for building the handle
*
* @return A cache handle
*/
public static Object getHandle(Object config)
{
//this implementation expects a PortletConfig object as its
// configuration
PortletConfig pc = null;
if (!(config instanceof PortletConfig))
{
return null;
}
// By default, only take into account the init parameters
pc = (PortletConfig)config;
StringBuffer handle = new StringBuffer(256);
if (pc.getURL()!=null && pc.isCachedOnURL())
{
handle.append(String.valueOf(pc.getURL().hashCode()));
}
Iterator i = pc.getInitParameterNames();
while(i.hasNext())
{
String name = (String)i.next();
String value = pc.getInitParameter(name);
if (value!=null)
{
handle.append("|").append(name).append("-").append(value);
}
}
return handle.toString();
}
/**
* Set this portlet's cached object.
*
* @param cachedObject Cached Object associated to this portlet
*/
public void setCachedObject(CachedObject cachedObject) {
this.cachedObject = cachedObject;
}
/*
* Implement methods required by Portlet
*/
/**
* Get the portlet's name
*
* @return Name of the portlet
*/
public String getName()
{
if ( name == null )
{
if (getPortletConfig()!=null)
{
if (getPortletConfig().getName()!=null)
{
return getPortletConfig().getName();
}
else
{
return this.getClass().getName();
}
}
}
return name;
}
/**
* Set the name of the portlet
*
* @param name Name of the portlet
*/
public void setName( String name ) {
this.name = name;
}
/**
* Get the config of this servlet
*
* @return PortletConfig Portlet
*/
public PortletConfig getPortletConfig() {
return this.pc;
}
/**
* Set's the configuration of this servlet.
*/
public void setPortletConfig( PortletConfig pc ) {
this.pc = pc;
}
/**
* @param rundata The RunData object for the current request
*/
public ConcreteElement getContent( RunData rundata ) {
return getContent( rundata, null , true );
}
public ConcreteElement getContent( RunData rundata, CapabilityMap map ) {
CapabilityMap mymap = map;
if ( mymap == null ) mymap = CapabilityMapFactory.getCapabilityMap( rundata );
return (ConcreteElement)content.get( mymap.toString() );
}
/**
* @param rundata The RunData object for the current request
*/
public ConcreteElement getContent( RunData rundata,
CapabilityMap map,
boolean allowRecurse ) {
CapabilityMap mymap = map;
if ( mymap == null ) mymap = CapabilityMapFactory.getCapabilityMap( rundata );
ConcreteElement element = (ConcreteElement)content.get( mymap.toString() );
if ( element == null ) {
if ( allowRecurse ) {
try {
// init will put content under default cmap
init( );
element = getContent( rundata, mymap, false );
if( element != null ) {
// now we put it under our cmap
this.setContent( element, mymap );
}
} catch (Exception e) {
element = new JetspeedClearElement("Error when retrieving Portlet contents");
if( logger.isDebugEnabled() ) {
logger.debug( "Error when retrieving Portlet contents", e );
}
}
} else {
if( element == null ) {
//FIXME: Let's asume that the contents under "default" map are good
mymap = CapabilityMapFactory.getDefaultCapabilityMap();
element = (ConcreteElement)content.get( mymap.toString() );
if( element == null ) {
element = new JetspeedClearElement("Unknown Problem getting Contents");
}
}
}
}
return element;
}
/**
* Provide a description within PML if the user has specified one.
*
* @return a null entry if the user hasn't defined anything
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -