?? jetspeedportletfactoryservice.java
字號(hào):
/*
* 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.services.portletfactory;
//jetspeed stuff
import org.apache.jetspeed.portal.Portlet;
import org.apache.jetspeed.portal.PortletConfig;
import org.apache.jetspeed.portal.PortletException;
import org.apache.jetspeed.portal.BasePortletConfig;
import org.apache.jetspeed.portal.security.portlets.PortletWrapper;
import org.apache.jetspeed.om.SecurityReference;
import org.apache.jetspeed.om.profile.Entry;
import org.apache.jetspeed.om.profile.Parameter;
import org.apache.jetspeed.om.profile.MetaInfo;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.PortalToolkit;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.portletcache.PortletCache;
import org.apache.jetspeed.services.portletcache.Cacheable;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.om.profile.Profile;
import org.apache.jetspeed.util.MetaData;
import org.apache.jetspeed.services.rundata.JetspeedRunDataService;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.resources.ResourceService;
import org.apache.turbine.services.rundata.RunDataService;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.ServletConfig;
/**
* Simple implementation of the PortalFactoryService.
*
* @author <a href="mailto:raphael@apache.org">Rapha雔 Luta</a>
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $Id: JetspeedPortletFactoryService.java,v 1.23 2004/02/23 03:36:42 jford Exp $
*/
public class JetspeedPortletFactoryService extends TurbineBaseService
implements PortletFactoryService
{
/**
* Static initialization of the logger for this class
*/
private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortletFactoryService.class.getName());
/** The default control to use when none is specified */
private boolean enableCache = false;
/** The JetspeedRunData Service. */
private JetspeedRunDataService runDataService = null;
/**
* This is the early initialization method called by the
* Turbine <code>Service</code> framework
*/
public void init( ServletConfig conf ) throws InitializationException
{
ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance())
.getResources(PortletFactoryService.SERVICE_NAME);
this.enableCache = serviceConf.getBoolean("enable.cache",true);
// get the runData service
this.runDataService =
(JetspeedRunDataService)TurbineServices.getInstance()
.getService(RunDataService.SERVICE_NAME);
setInit(true);
}
/**
* Given a PSML Entry return an instanciated Portlet.
*
* @param entry a PSML Entry describing a portlet
* @param id the PSML entry's portlet id
* @return an instanciated portlet corresponding to this entry
*/
public Portlet getPortlet( Entry entry ) throws PortletException
{
PortletEntry regEntry = (PortletEntry)Registry.getEntry(Registry.PORTLET,
entry.getParent() );
if (regEntry == null)
{
throw new PortletException("PortletFactory: unknown portlet entry in Registry: "+entry.getParent());
}
if (PortletEntry.TYPE_ABSTRACT.equals(regEntry.getType()))
{
throw new PortletException("PortletFactory: can't instanciate abstract registry entry: "+regEntry.getName());
}
PortletConfig pc = getPortletConfig(regEntry, entry.getId());
// Set portlet config with values from PSML Entry
pc.getInitParameters().putAll(getParameters(entry));
pc.setPortletSkin( PortalToolkit.getSkin( entry.getSkin() ) );
pc.setSecurityRef( getSecurityReference(entry, regEntry));
return getPortlet( getClassname(regEntry), pc, entry.getId() );
}
/**
* Given a Portlet registry entry name, instanciate it
*
* @param name the name of a portlet in the registry
* @return an instanciated portlet corresponding to this entry
*/
public Portlet getPortlet( String name, String id ) throws PortletException
{
PortletEntry regEntry = (PortletEntry)Registry.getEntry(Registry.PORTLET, name );
if (regEntry == null)
{
throw new PortletException("PortletFactory: unknown portlet entry in Registry: "+name);
}
if (PortletEntry.TYPE_ABSTRACT.equals(regEntry.getType()))
{
throw new PortletException("PortletFactory: can't instanciate abstract registry entry: "+name);
}
PortletConfig pc = getPortletConfig(regEntry, id);
return getPortlet( getClassname(regEntry), pc, null );
}
/**
* Instanciates or retrieve from memory cache a portlet corresponding to the
* passed parameters
*
* @param classname the classname of the portlet to instanciate
* @param pc the PortletConfig object to be associated with this object
* @param id the PSML entry's portlet id
* @return the Portlet created or retrieve from cache
*/
protected Portlet getPortlet( String classname, PortletConfig pc, String id )
throws PortletException
{
//record the begining of the portlet creation
long begin = System.currentTimeMillis();
Portlet portlet = null;
Class portletClass = null;
String handle = null;
try
{
portletClass = Class.forName(classname);
}
catch (Exception e)
{
throw new PortletException( "PortletFactory: Unable to load class " + classname );
}
if (enableCache)
{
try
{
// try to invoke a static getHandle() for this class
Class[] signatureParams = { Object.class };
Object[] methodParams = { pc };
handle = (String)portletClass.getMethod("getHandle",signatureParams)
.invoke(null,methodParams);
// make sure the handle is differenciated by class
handle = String.valueOf(classname.hashCode())+handle;
}
catch (NoSuchMethodException e)
{
// ignore, this simply means the portlet is not cacheable
}
catch (Exception e)
{
// invocation failed or security exception, in both case
// log the error and treat the class as non cacheable
logger.error("PortletFactory: failed to get cache handle",e);
}
}
try {
if (enableCache && (handle != null))
{
portlet = (Portlet)PortletCache.getCacheable( handle );
//portlet in cache but expired, remove it from cache
if ((portlet!=null) && ((Cacheable)portlet).getExpire().isExpired() )
{
logger.info( "The portlet (" + handle + ") is expired" );
PortletCache.removeCacheable(handle);
if ( logger.isDebugEnabled() )
{
logger.debug( "After removal of object(" + handle + ")." );
}
portlet = null;
}
}
// we found a portlet in the cache
if ( (portlet != null)
&& ( portlet instanceof Cacheable )
&& (! ((Cacheable)portlet).getExpire().isExpired()) )
{
// update the config for the portlet to the current one
// Note: this is what was used to find the cached portlet.
// Note: the init params may have changed in the psml since caching,
// this will update the portlet to use them.
portlet.setPortletConfig( pc );
portlet.setID( id );
portlet.setName( pc.getName() );
//FIXME: we now avoid to override metainfo when nothing is set
//in the markup, so that cached portlets can keep their metainfo
//This may lead to an incorrect metainfo retrieved if the first
//instance of the portlet, which is put in the cache, has some
//special metainfo defined in the markup
MetaData meta = pc.getMetainfo();
if ( meta != null)
{
if (! MetaData.DEFAULT_TITLE.equals( meta.getTitle() ) )
{
portlet.setTitle( meta.getTitle() );
}
if (! MetaData.DEFAULT_DESCRIPTION.equals( meta.getDescription() ) )
{
portlet.setDescription( meta.getDescription() );
}
}
//FIXME: Notice here we are putting the portlet without wrapper
//in the cache, and we must wrap it on return.
//Security implications: the portletcache should not be
//publicly accessible.
//Alternative: we could wrap the portlet before putting
//it in the cache.
//now compute the time it took to instantate and log it...
// time in millis, sugested by Thomas Schaeck (schaeck@de.ibm.com)
long milliseconds = ( System.currentTimeMillis() - begin );
if (logger.isDebugEnabled())
logger.debug( "PortletFactory.getPortlet(): found in cache in "
+ milliseconds + " ms - handle: " + handle );
return PortletWrapper.wrap( portlet );
}
// if not found in the cache, instanciate a new Portlet
portlet = (Portlet)portletClass.newInstance();
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -