template engine has to use UberClassLoader

This commit is contained in:
Sebastian Sdorra
2014-08-19 21:08:41 +02:00
parent 301cc19290
commit babd1be9fc
3 changed files with 30 additions and 18 deletions

View File

@@ -46,6 +46,8 @@ import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.PluginLoader;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
@@ -84,11 +86,13 @@ public class MustacheTemplateEngine implements TemplateEngine
*
*
* @param context
* @param pluginLoader
*/
@Inject
public MustacheTemplateEngine(ServletContext context)
public MustacheTemplateEngine(ServletContext context,
PluginLoader pluginLoader)
{
factory = new ServletMustacheFactory(context);
factory = new ServletMustacheFactory(context, pluginLoader);
ThreadFactory threadFactory =
new ThreadFactoryBuilder().setNameFormat(THREAD_NAME).build();
@@ -168,11 +172,7 @@ public class MustacheTemplateEngine implements TemplateEngine
{
logger.warn("could not find mustache template at {}", templatePath);
}
catch (UncheckedExecutionException ex)
{
handleWrappedException(ex, templatePath);
}
catch (MustacheException ex)
catch (UncheckedExecutionException | MustacheException ex)
{
handleWrappedException(ex, templatePath);
}
@@ -231,5 +231,5 @@ public class MustacheTemplateEngine implements TemplateEngine
//~--- fields ---------------------------------------------------------------
/** Field description */
private ServletMustacheFactory factory;
private final ServletMustacheFactory factory;
}

View File

@@ -42,6 +42,8 @@ import com.google.common.base.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.PluginLoader;
//~--- JDK imports ------------------------------------------------------------
import java.io.BufferedReader;
@@ -71,10 +73,13 @@ public class ServletMustacheFactory extends DefaultMustacheFactory
*
*
* @param servletContext
* @param pluginLoader
*/
public ServletMustacheFactory(ServletContext servletContext)
public ServletMustacheFactory(ServletContext servletContext,
PluginLoader pluginLoader)
{
this.servletContext = servletContext;
this.pluginLoader = pluginLoader;
}
//~--- get methods ----------------------------------------------------------
@@ -111,14 +116,7 @@ public class ServletMustacheFactory extends DefaultMustacheFactory
resourceName = resourceName.substring(1);
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null)
{
classLoader = ServletMustacheFactory.class.getClassLoader();
}
is = classLoader.getResourceAsStream(resourceName);
is = pluginLoader.getUberClassLoader().getResourceAsStream(resourceName);
}
if (is != null)
@@ -146,6 +144,9 @@ public class ServletMustacheFactory extends DefaultMustacheFactory
//~--- fields ---------------------------------------------------------------
/** Field description */
private final PluginLoader pluginLoader;
/** Field description */
private ServletContext servletContext;
}

View File

@@ -33,6 +33,12 @@
package sonia.scm.template;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.plugin.PluginLoader;
import static org.mockito.Mockito.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.InputStream;
@@ -57,7 +63,12 @@ public class MustacheTemplateEngineTest extends TemplateEngineTestBase
@Override
public TemplateEngine createEngine(ServletContext context)
{
return new MustacheTemplateEngine(context);
PluginLoader loader = mock(PluginLoader.class);
when(loader.getUberClassLoader()).thenReturn(
Thread.currentThread().getContextClassLoader());
return new MustacheTemplateEngine(context, loader);
}
//~--- get methods ----------------------------------------------------------