Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
/target
/bin
/.gradle
/.idea
/silken.iml
/build
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ pre-compile.
contain/reference ``$CLASSPATH`` and ``$WEBROOT``. **Default**:
*$CLASSPATH:$WEBROOT/templates:$WEBROOT/WEB-INF/templates*

```moduleProvider``` - Provide a custom iterable of Guice modules used in generation. ***Default***: *none*

##Injecting Configuration and Resolvers at Runtime (Advanced)

In addition to using Servlet Init Parameters, configuration can be modified in
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.papercut.silken</groupId>
<artifactId>silken</artifactId>
<packaging>jar</packaging>
<version>2013-03-05</version>
<version>2015-01-13</version>
<name>silken</name>
<url>https://github.com/codedance/silken</url>
<description>Silken - a nicer tasting Soy Tofu (Google Closure Templates)</description>
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/papercut/silken/Config.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.papercut.silken;

import java.util.List;

import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* This config class holds various global configuration. Multiple layers have a reference to this configuration
* store/POJO.
Expand Down Expand Up @@ -36,6 +36,8 @@ public class Config {

private boolean showStackTracesInErrors;

private ModuleProvider moduleProvider;

public Config() {
sharedNameSpaces = ImmutableList.of("shared");
disableCaching = false;
Expand Down Expand Up @@ -136,4 +138,11 @@ public synchronized void setShowStackTracesInErrors(boolean showStackTracesInErr
this.showStackTracesInErrors = showStackTracesInErrors;
}

public synchronized ModuleProvider getModuleProvider() {
return moduleProvider;
}

public synchronized void setModuleProvider(final ModuleProvider moduleProvider) {
this.moduleProvider = moduleProvider;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/papercut/silken/ModuleProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.papercut.silken;

import com.google.inject.Module;

import java.util.Map;

/**
* Implement this interface to provide custom modules for soy generation.
*
* @author arlo
*/
public interface ModuleProvider {

/**
* @return Return an iterable of modules
*/
Iterable<Module> getModules();

}
23 changes: 15 additions & 8 deletions src/main/java/com/papercut/silken/NamespaceSet.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.papercut.silken;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.template.soy.SoyFileSet;
import com.google.template.soy.data.SoyMapData;
import com.google.template.soy.jssrc.SoyJsSrcOptions;
Expand All @@ -19,6 +15,12 @@
import com.google.template.soy.tofu.SoyTofu.Renderer;
import com.google.template.soy.xliffmsgplugin.XliffMsgPlugin;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Stores fileset and cached tofu for a given namespace.
*
Expand Down Expand Up @@ -168,8 +170,13 @@ private SoyFileSet generateSoyFileSet(String suffix) {
final FileSetResolver fileSetResolver = config.getFileSetResolver();
final String searchPath = config.getSearchPath();

SoyFileSet.Builder builder = new SoyFileSet.Builder();

final SoyFileSet.Builder builder;
if (config.getModuleProvider() != null) {
final Injector injector = Guice.createInjector(config.getModuleProvider().getModules());
builder = injector.getInstance(SoyFileSet.Builder.class);
} else {
builder = new SoyFileSet.Builder();
}
boolean hasSetGlobals = false;

// Use global compile time provider if set.
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/com/papercut/silken/SilkenServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
*/
package com.papercut.silken;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Locale;
import com.google.common.base.Strings;
import com.google.template.soy.data.SoyMapData;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Strings;
import com.google.template.soy.data.SoyMapData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Locale;

/**
* The main Silken servlet class. This is designed to be put on a url path like /soy. Requests supported include:
Expand Down Expand Up @@ -81,7 +80,9 @@
* searchPath - Advanced: Modify the default search path used to locate *.soy and associated files. Value is a semicolon
* separated path that may contain/reference $CLASSPATH and $WEBROOT.
* Default: $CLASSPATH:$WEBROOT/templates:$WEBROOT/WEB-INF/templates
*
*
* moduleProvider - Provide a custom iterable of Guice modules used in generation. Default: none
*
* @author chris
*/
public class SilkenServlet extends HttpServlet {
Expand Down Expand Up @@ -185,6 +186,16 @@ public void init(ServletConfig servletConfig) throws ServletException {
config.setSearchPath(searchPath);
}

final String moduleProvider = servletConfig.getInitParameter("moduleProvider");
if (moduleProvider != null) {
try {
Object provider = Class.forName(moduleProvider).newInstance();
config.setModuleProvider((ModuleProvider) provider);
} catch (Exception e) {
throw new ServletException("Unable to create moduleProvider", e);
}
}

// Store a reference config in our context so external code can modify.
getServletContext().setAttribute("silken.config", config);

Expand Down