You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ta...@apache.org on 2004/08/16 20:27:54 UTC
cvs commit: jakarta-jetspeed-2/components/portlet-factory/src/java/org/apache/jetspeed/cache PortletCache.java
taylor 2004/08/16 11:27:54
Added: components/portlet-factory/src/java/org/apache/jetspeed/factory
JetspeedPortletFactory.java
components/portlet-factory/src/java/org/apache/jetspeed/cache
PortletCache.java
Log:
new component "portlet-factory"-- refactoring necessary for language support and finding resources in class loader
CVS: ----------------------------------------------------------------------
CVS: PR:
CVS: If this change addresses a PR in the problem report tracking
CVS: database, then enter the PR number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system, such as NCSA,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to Apache by someone else; i.e.,
CVS: they sent us a patch or a new module, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.
Revision Changes Path
1.1 jakarta-jetspeed-2/components/portlet-factory/src/java/org/apache/jetspeed/factory/JetspeedPortletFactory.java
Index: JetspeedPortletFactory.java
===================================================================
/*
* Copyright 2000-2001,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.factory;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.portlet.Portlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jetspeed.cache.PortletCache;
import org.apache.jetspeed.container.PortalAccessor;
import org.apache.pluto.om.portlet.PortletDefinition;
import org.apache.pluto.om.portlet.PortletDefinitionCtrl;
/**
* <p>
* JetspeedPortletFactory
* </p>
* <p>
*
* </p>
* @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
* @version $Id: JetspeedPortletFactory.java,v 1.1 2004/08/16 18:27:54 taylor Exp $
*
*/
public class JetspeedPortletFactory implements PortletFactory
{
private PortletCache portletCache;
private final ArrayList classLoaders;
private static final Log log = LogFactory.getLog(JetspeedPortletFactory.class);
/**
*
*/
public JetspeedPortletFactory(PortletCache portletCache)
{
super();
this.portletCache = portletCache;
classLoaders = new ArrayList();
}
/**
*
* <p>
* addClassLoader
* </p>
*
* Adds a ClassLoader to the search path, <code>classLoaders</code>, of the JetspeedPortletFactory.
*
* @param cl
*/
public void addClassLoader( ClassLoader cl )
{
synchronized(classLoaders)
{
if (!classLoaders.contains(cl))
{
classLoaders.add(cl);
}
}
}
/**
* <p>
* loadPortletClass
* </p>
* Loads a Portlet class by first checking Thread.currentThread().getContextClassLoader()
* then by checking all of the ClassLoaders in <code>classLoaders</code> until the
* class is located or returns <code>null</code> if the Portlet class could not be found.
*
* @param className
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Portlet loadPortletClass( String className ) throws InstantiationException, IllegalAccessException
{
Portlet portlet = null;
try
{
portlet = (Portlet)Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
}
catch (ClassNotFoundException e)
{
synchronized(classLoaders)
{
Iterator itr = classLoaders.iterator();
while(itr.hasNext() && portlet == null)
{
ClassLoader cl = (ClassLoader) itr.next();
try
{
portlet = (Portlet) cl.loadClass(className).newInstance();
}
catch (ClassNotFoundException e1)
{
// move along
}
}
}
}
return portlet;
}
/**
* Gets a portlet by either creating it or returning a handle to it from the portlet 'cache'
*
* @param portletDefinition The definition of the portlet
* @return Portlet
* @throws PortletException
*/
public Portlet getPortlet( ServletConfig servletConfig, PortletDefinition portletDefinition ) throws PortletException
{
Portlet portlet = null;
Class portletClass = null;
String handle = null;
String portletName = portletDefinition.getId().toString();
//String portletName = portletDefinition.getName();
String className = portletDefinition.getClassName();
try
{
portlet = portletCache.get(portletName);
if (null != portlet)
{
((PortletDefinitionCtrl) portletDefinition).setPortletClassLoader(portlet.getClass().getClassLoader());
return portlet;
}
portlet = loadPortletClass(className);
if(portlet == null)
{
throw new FileNotFoundException("Could not located portlet "+className+" in any classloader.");
}
((PortletDefinitionCtrl) portletDefinition).setPortletClassLoader(portlet.getClass().getClassLoader());
ServletContext servletContext = servletConfig.getServletContext();
PortletContext portletContext =
PortalAccessor.createPortletContext(servletContext,
portletDefinition.getPortletApplicationDefinition());
PortletConfig portletConfig = PortalAccessor.createPortletConfig(servletConfig, portletContext, portletDefinition);
portlet.init(portletConfig);
portletCache.add(portletName, portlet);
}
catch (Throwable e)
{
log.error("PortletFactory: Failed to load portlet "+className, e);
e.printStackTrace();
throw new PortletException( "PortletFactory: Failed to load portlet " + className +":"+e.toString(), e);
}
return portlet;
}
}
1.1 jakarta-jetspeed-2/components/portlet-factory/src/java/org/apache/jetspeed/cache/PortletCache.java
Index: PortletCache.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.cache;
import java.util.HashMap;
import java.util.Iterator;
import javax.portlet.Portlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pluto.om.portlet.PortletApplicationDefinition;
import org.apache.pluto.om.portlet.PortletDefinition;
/**
* Very Simple Portlet Cache to manage portlets in container
* Keeps only one object instance of a portlet
* The uniqueness of the portlet is determined by the portlet name
* There can be multiple java instances of the same portlet class
* when the portlet name (from the deployment descriptor/registry)
* differs per portlet description
*
* @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
* @version $Id: PortletCache.java,v 1.1 2004/08/16 18:27:54 taylor Exp $
*/
public class PortletCache
{
private final HashMap portlets;
private final Log log;
public PortletCache()
{
super();
portlets = new HashMap();
log = LogFactory.getLog(PortletCache.class);
}
/*
* Adds a portlet to the portlet cache. If it exists, replaces it
*
* @param portlet The portlet object to add to the cache
*/
public void add(String name, Portlet portlet)
{
synchronized(portlets)
{
portlets.put(name, portlet);
}
}
/*
* Gets a portlet from the portlet cache.
*
* @param portletName The name of the portlet from the registry
* @return The found portlet from the cache or null if not found
*/
public Portlet get(String portletName)
{
return (Portlet)portlets.get(portletName);
}
/*
* Removes a portlet from the portlet cache.
*
* @param portletClassName The full Java name of the portlet class
*/
public void remove(String portletName)
{
synchronized(portlets)
{
portlets.remove(portletName);
}
}
public void removeAll(PortletApplicationDefinition portletApplication)
{
Iterator itr = portletApplication.getPortletDefinitionList().iterator();
log.info("Removing all portlets from cache for PA: "+portletApplication.getId());
while(itr.hasNext())
{
PortletDefinition portlet = (PortletDefinition) itr.next();
log.info("Removing portlet "+portlet.getId());
remove(portlet.getId().toString());
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org