*/
public PortletInstance getPortletInstance( ServletContext servletContext, PortletDefinition pd ) throws PortletException
{
PortletInstance portlet = null;
String pdId = pd.getId().toString();
PortletApplication pa = (PortletApplication)pd.getPortletApplicationDefinition();
try
{
synchronized (portletCache)
{
portlet = (PortletInstance)portletCache.get(pdId);
if (null != portlet)
{
return portlet;
}
ClassLoader paCl = (ClassLoader)classLoaderMap.get(pa.getId());
if ( paCl == null )
{
throw new UnavailableException("Portlet Application "+pa.getName()+" not available");
}
ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Class clazz = paCl.loadClass(pd.getClassName());
try
{
Thread.currentThread().setContextClassLoader(paCl);
// wrap new Portlet inside PortletInstance which ensures the destroy
// method will wait for all its invocation threads to complete
// and thereby releasing all its ClassLoader locks as needed for local portlets.
if (this.portletProxyUsed && !PortletObjectProxy.isPortletObjectProxied())
{
portlet = new JetspeedPortletProxyInstance(pd.getName(), (Portlet)clazz.newInstance(), this.autoSwitchEditDefaultsModeToEditMode, this.autoSwitchConfigMode, this.customConfigModePortletUniqueName);
}
else
{
portlet = new JetspeedPortletInstance(pd.getName(), (Portlet)clazz.newInstance());
}
}
finally
{
Thread.currentThread().setContextClassLoader(currentContextClassLoader);
}
}
catch (Exception e)
{
String msg = "Cannot create Portlet instance "+pd.getClassName()+" for Portlet Application "+pa.getName();
log.error(msg,e);
throw new UnavailableException(msg);
}
PortletContext portletContext = PortalAccessor.createPortletContext(servletContext, pa);
PortletConfig portletConfig = PortalAccessor.createPortletConfig(portletContext, pd);
try
{
try
{
Thread.currentThread().setContextClassLoader(paCl);
portlet.init(portletConfig);
}
finally
{
Thread.currentThread().setContextClassLoader(currentContextClassLoader);
}
}
catch (PortletException e1)
{
log.error("Failed to initialize Portlet "+pd.getClassName()+" for Portlet Application "+pa.getName(), e1);
throw e1;
}
portletCache.put(pdId, portlet);
}
}