{
ClassLoader paCl = classLoaderMap.get(paName);
if (paCl == null)
{
throw new UnavailableException("Portlet Application " + paName + " not available");
}
ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Class<?> clazz = paCl.loadClass(pd.getPortletClass());
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 (proxyUsed)
{
portlet = new JetspeedPortletProxyInstance(
pd.getPortletName(),
(Portlet) clazz.newInstance(),
this.autoSwitchEditDefaultsModeToEditMode,
this.autoSwitchConfigMode, this.customConfigModePortletUniqueName,
this.autoSwitchPreviewMode, this.customPreviewModePortletUniqueName
);
}
else
{
portlet = new JetspeedPortletInstance(pdName, (Portlet)clazz.newInstance());
}
}
finally
{
Thread.currentThread().setContextClassLoader(currentContextClassLoader);
}
}
catch (Exception e)
{
String msg = "Cannot create Portlet instance " + pd.getPortletClass() + " for Portlet Application " + paName;
log.error(msg, e);
throw new UnavailableException(msg);
}
JetspeedPortletContext portletContext = new JetspeedPortletContextImpl(servletContext,
pa,
ContainerInfo.getInfo(),
portalContext.getConfiguration(),
rdService,
servletContextProvider
);
JetspeedPortletConfig portletConfig = new JetspeedPortletConfigImpl(this, 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.getPortletClass()+" for Portlet Application "+paName, e1);
throw e1;
}
if (instanceCache == null)
{
instanceCache = Collections.synchronizedMap(new HashMap<String, PortletInstance>());
this.portletCache.put(paName, instanceCache);
}
instanceCache.put(pdName, portlet);
}
}
catch (PortletException pe)
{
throw pe;
}
catch (Throwable e)
{
log.error("PortletFactory: Failed to load portlet " + pd.getPortletClass(), e);
throw new UnavailableException("Failed to load portlet " + pd.getPortletClass() + ": " + e.toString());
}
return portlet;
}