Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add template management

  • Loading branch information...
commit 6ee646979e1cca63e85abda9797ffc9d8b35a0d9 1 parent 140c497
@eunomie eunomie authored
View
2  .gitignore
@@ -4,4 +4,4 @@ build
.project
.settings
bin
-libs/*.jar
+lib/*.jar
View
110 src/main/java/taist/ResourceList.java
@@ -0,0 +1,110 @@
+/**
+ *
+ */
+package taist;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+
+import java.util.regex.Pattern;
+
+/**
+ * @author CrEv
+ * from forums.devx.com/showthread.php?t=153784
+ *
+ */
+/**
+ * list resources available from the classpath @ *
+ */
+public class ResourceList{
+
+ /**
+ * for all elements of java.class.path get a Collection of resources Pattern
+ * pattern = Pattern.compile(".*"); gets all resources
+ *
+ * @param pattern
+ * the pattern to match
+ * @return the resources in the order they are found
+ */
+ public static Collection<String> getResources(
+ final Pattern pattern){
+ final ArrayList<String> retval = new ArrayList<String>();
+ final String classPath = System.getProperty("java.class.path", ".");
+ final String[] classPathElements = classPath.split(File.pathSeparator);
+ for(final String element : classPathElements){
+ retval.addAll(getResources(element, pattern));
+ }
+ return retval;
+ }
+
+ private static Collection<String> getResources(
+ final String element,
+ final Pattern pattern){
+ final ArrayList<String> retval = new ArrayList<String>();
+ final File file = new File(element);
+ if(file.isDirectory()){
+ retval.addAll(getResourcesFromDirectory(file, pattern));
+ } else{
+ retval.addAll(getResourcesFromJarFile(file, pattern));
+ }
+ return retval;
+ }
+
+ private static Collection<String> getResourcesFromJarFile(
+ final File file,
+ final Pattern pattern){
+ final ArrayList<String> retval = new ArrayList<String>();
+ ZipFile zf;
+ try{
+ zf = new ZipFile(file);
+ } catch(final ZipException e){
+ throw new Error(e);
+ } catch(final IOException e){
+ throw new Error(e);
+ }
+ final Enumeration<? extends ZipEntry> e = zf.entries();
+ while(e.hasMoreElements()){
+ final ZipEntry ze = (ZipEntry) e.nextElement();
+ final String fileName = ze.getName();
+ final boolean accept = pattern.matcher(fileName).matches();
+ if(accept){
+ retval.add(fileName);
+ }
+ }
+ try{
+ zf.close();
+ } catch(final IOException e1){
+ throw new Error(e1);
+ }
+ return retval;
+ }
+
+ private static Collection<String> getResourcesFromDirectory(
+ final File directory,
+ final Pattern pattern){
+ final ArrayList<String> retval = new ArrayList<String>();
+ final File[] fileList = directory.listFiles();
+ for(final File file : fileList){
+ if(file.isDirectory()){
+ retval.addAll(getResourcesFromDirectory(file, pattern));
+ } else{
+ try{
+ final String fileName = file.getCanonicalPath();
+ final boolean accept = pattern.matcher(fileName).matches();
+ if(accept){
+ retval.add(fileName);
+ }
+ } catch(final IOException e){
+ throw new Error(e);
+ }
+ }
+ }
+ return retval;
+ }
+}
View
9 src/main/java/taist/endpoint/Hello.java
@@ -9,7 +9,11 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
+import com.google.inject.Inject;
+
import taist.HelloPojo;
+import taist.templates.TemplateFactory;
+import taist.templates.TemplateKey;
/**
@@ -19,6 +23,9 @@
*/
@Path("/hello")
public class Hello {
+ @Inject
+ private TemplateFactory templates;
+
/**
*
* @return Return the HelloPojo serialized in json
@@ -37,6 +44,6 @@ public Object get() {
@Produces(MediaType.TEXT_HTML)
@Path("/world")
public String world() {
- return "<!DOCTYPE html>\n<html><head><meta charset=\"utf-8\"><title>Plop</title></head><body><h1>Hello World!</h1></body></html>";
+ return templates.get(TemplateKey.get("page", "blank")).render();
}
}
View
11 src/main/java/taist/inject/TaistModule.java
@@ -1,6 +1,12 @@
package taist.inject;
+import taist.templates.TemplateCacheFactory;
+import taist.templates.TemplateCacheLoader;
+import taist.templates.TemplateFactory;
+
import com.google.inject.AbstractModule;
+import com.google.template.soy.SoyModule;
+import com.google.template.soy.xliffmsgplugin.XliffMsgPluginModule;
/**
* Guice module
@@ -10,5 +16,10 @@
public class TaistModule extends AbstractModule {
@Override
protected void configure() {
+ install(new SoyModule());
+ install(new XliffMsgPluginModule());
+
+ bind(TemplateCacheLoader.class);
+ bind(TemplateFactory.class).to(TemplateCacheFactory.class);
}
}
View
38 src/main/java/taist/templates/TemplateCacheFactory.java
@@ -0,0 +1,38 @@
+/**
+ *
+ */
+package taist.templates;
+
+import java.util.concurrent.ExecutionException;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.LoadingCache;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.google.template.soy.tofu.SoyTofu;
+
+/**
+ * @author CrEv
+ *
+ */
+@Singleton
+public class TemplateCacheFactory implements TemplateFactory {
+ private LoadingCache<TemplateKey, SoyTofu.Renderer> loadingCache;
+
+ @Inject
+ public TemplateCacheFactory(TemplateCacheLoader loader) {
+ loadingCache = CacheBuilder.newBuilder().maximumSize(50).build(loader);
+ }
+
+ @Override
+ public SoyTofu.Renderer get(TemplateKey key) {
+ SoyTofu.Renderer renderer = null;
+ try {
+ renderer = loadingCache.get(key);
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ }
+ return renderer;
+ }
+
+}
View
44 src/main/java/taist/templates/TemplateCacheLoader.java
@@ -0,0 +1,44 @@
+/**
+ *
+ */
+package taist.templates;
+
+
+import java.io.File;
+import java.util.Collection;
+import java.util.regex.Pattern;
+
+import taist.ResourceList;
+
+import com.google.common.cache.CacheLoader;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.google.template.soy.SoyFileSet;
+import com.google.template.soy.tofu.SoyTofu;
+
+/**
+ * A cache loader to load tofus.
+ * @author CrEv
+ *
+ */
+@Singleton
+public class TemplateCacheLoader extends CacheLoader<TemplateKey, SoyTofu.Renderer> {
+ private static final String FILE_PATTERN = ".*\\.soy";
+
+ private SoyFileSet sfs;
+
+ @Inject
+ public TemplateCacheLoader(SoyFileSet.Builder builder) {
+ Collection<String> templates = ResourceList.getResources(Pattern.compile(FILE_PATTERN));
+ for(String name : templates) {
+ builder.add(new File(name));
+ }
+ sfs = builder.build();
+ }
+
+ @Override
+ public SoyTofu.Renderer load(TemplateKey key) throws Exception {
+ SoyTofu tofu = sfs.compileToTofu().forNamespace(key.getNamespace());
+ return tofu.newRenderer("." + key.getTemplate());
+ }
+}
View
20 src/main/java/taist/templates/TemplateFactory.java
@@ -0,0 +1,20 @@
+/**
+ *
+ */
+package taist.templates;
+
+import com.google.template.soy.tofu.SoyTofu;
+
+
+/**
+ * @author CrEv
+ *
+ */
+public interface TemplateFactory {
+ /**
+ * Get a template
+ * @param key Key
+ * @return Template renderer
+ */
+ SoyTofu.Renderer get(TemplateKey key);
+}
View
60 src/main/java/taist/templates/TemplateKey.java
@@ -0,0 +1,60 @@
+/**
+ *
+ */
+package taist.templates;
+
+/**
+ * @author CrEv
+ *
+ */
+public final class TemplateKey {
+ /**
+ * Static helper
+ * @param namespace Namespace
+ * @param template Template
+ * @return Template key
+ */
+ public static TemplateKey get(String namespace, String template) {
+ return new TemplateKey(namespace, template);
+ }
+
+ private final String namespace;
+ private final String template;
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
+ result = prime * result + ((template == null) ? 0 : template.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (getClass() != obj.getClass()) return false;
+ TemplateKey other = (TemplateKey) obj;
+ if (namespace == null) {
+ if (other.namespace != null) return false;
+ } else if (!namespace.equals(other.namespace)) return false;
+ if (template == null) {
+ if (other.template != null) return false;
+ } else if (!template.equals(other.template)) return false;
+ return true;
+ }
+
+ public TemplateKey(String namespace, String template) {
+ this.namespace = namespace;
+ this.template = template;
+ }
+
+ public String getNamespace() {
+ return namespace;
+ }
+
+ public String getTemplate() {
+ return template;
+ }
+}
View
0  src/main/resources/dir.empty
No changes.
View
14 src/main/resources/templates/page.soy
@@ -0,0 +1,14 @@
+{namespace page}
+
+/** */
+{template .blank}
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Test</title>
+ </head>
+ <body>
+ <h1>Hello World!</h1>
+ </body>
+</html>
+{/template}
Please sign in to comment.
Something went wrong with that request. Please try again.