mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
make PluginLoader dependency of MustacheTemplateEngine optional
This commit is contained in:
@@ -37,27 +37,22 @@ package sonia.scm.template;
|
||||
|
||||
import com.github.mustachejava.Mustache;
|
||||
import com.github.mustachejava.MustacheException;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.Default;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -67,6 +62,14 @@ import javax.servlet.ServletContext;
|
||||
public class MustacheTemplateEngine implements TemplateEngine
|
||||
{
|
||||
|
||||
/**
|
||||
* Used to implement optional injection for the PluginLoader.
|
||||
* @see <a href="https://github.com/google/guice/wiki/FrequentlyAskedQuestions#how-can-i-inject-optional-parameters-into-a-constructor">Optional Injection</a>
|
||||
*/
|
||||
static class PluginLoaderHolder {
|
||||
@Inject(optional = true) PluginLoader pluginLoader;
|
||||
}
|
||||
|
||||
/** Field description */
|
||||
public static final TemplateType TYPE = new TemplateType("mustache",
|
||||
"Mustache", "mustache");
|
||||
@@ -87,13 +90,12 @@ public class MustacheTemplateEngine implements TemplateEngine
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param pluginLoader
|
||||
* @param pluginLoaderHolder
|
||||
*/
|
||||
@Inject
|
||||
public MustacheTemplateEngine(@Default ServletContext context,
|
||||
PluginLoader pluginLoader)
|
||||
public MustacheTemplateEngine(@Default ServletContext context, PluginLoaderHolder pluginLoaderHolder)
|
||||
{
|
||||
factory = new ServletMustacheFactory(context, pluginLoader);
|
||||
factory = new ServletMustacheFactory(context, createClassLoader(pluginLoaderHolder.pluginLoader));
|
||||
|
||||
ThreadFactory threadFactory =
|
||||
new ThreadFactoryBuilder().setNameFormat(THREAD_NAME).build();
|
||||
@@ -101,6 +103,13 @@ public class MustacheTemplateEngine implements TemplateEngine
|
||||
factory.setExecutorService(Executors.newCachedThreadPool(threadFactory));
|
||||
}
|
||||
|
||||
private ClassLoader createClassLoader(PluginLoader pluginLoader) {
|
||||
if (pluginLoader == null) {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
return pluginLoader.getUberClassLoader();
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -112,12 +121,9 @@ public class MustacheTemplateEngine implements TemplateEngine
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public Template getTemplate(String templateIdentifier, Reader reader)
|
||||
throws IOException
|
||||
{
|
||||
public Template getTemplate(String templateIdentifier, Reader reader) {
|
||||
if (logger.isTraceEnabled())
|
||||
{
|
||||
logger.trace("try to create mustache template from reader with id {}",
|
||||
|
||||
@@ -36,22 +36,17 @@ package sonia.scm.template;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.github.mustachejava.DefaultMustacheFactory;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -73,13 +68,12 @@ public class ServletMustacheFactory extends DefaultMustacheFactory
|
||||
*
|
||||
*
|
||||
* @param servletContext
|
||||
* @param pluginLoader
|
||||
* @param classLoader
|
||||
*/
|
||||
public ServletMustacheFactory(ServletContext servletContext,
|
||||
PluginLoader pluginLoader)
|
||||
public ServletMustacheFactory(ServletContext servletContext, ClassLoader classLoader)
|
||||
{
|
||||
this.servletContext = servletContext;
|
||||
this.pluginLoader = pluginLoader;
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
@@ -116,7 +110,7 @@ public class ServletMustacheFactory extends DefaultMustacheFactory
|
||||
resourceName = resourceName.substring(1);
|
||||
}
|
||||
|
||||
is = pluginLoader.getUberClassLoader().getResourceAsStream(resourceName);
|
||||
is = classLoader.getResourceAsStream(resourceName);
|
||||
}
|
||||
|
||||
if (is != null)
|
||||
@@ -144,9 +138,8 @@ public class ServletMustacheFactory extends DefaultMustacheFactory
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private final PluginLoader pluginLoader;
|
||||
|
||||
/** Field description */
|
||||
private ServletContext servletContext;
|
||||
|
||||
private ClassLoader classLoader;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user