Commits

Anonymous committed 357a8b7

Create skin registry

Comments (0)

Files changed (9)

chempound-api/src/main/java/net/chempound/services/FreemarkerService.java

 
     void registerPlugin(String name, Class<?> context, String path) throws IOException;
 
+    void registerSkin(String name, Class<?> context, String path) throws IOException;
+
 }

chempound-webapi/src/main/java/net/chempound/webapp/plugins/ChempoundPluginModule.java

         bind(pluginClass).asEagerSingleton();
     }
 
+    protected void bindSkin(final Class<? extends Skin> pluginClass) {
+        bind(pluginClass).asEagerSingleton();
+    }
+
 }

chempound-webapi/src/main/java/net/chempound/webapp/plugins/Skin.java

+package net.chempound.webapp.plugins;
+
+import javax.inject.Inject;
+
+/**
+ * @author Sam Adams
+ */
+public class Skin {
+
+    private final String id;
+    private final Class<?> context;
+    private final String resourcePath;
+    private final String templatePath;
+
+    public Skin(final String id, final Class<?> context, final String resourcePath, final String templatePath) {
+        this.id = id;
+        this.context = context;
+        this.resourcePath = resourcePath;
+        this.templatePath = templatePath;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public Class<?> getContext() {
+        return context;
+    }
+
+    public String getResourcePath() {
+        return resourcePath;
+    }
+
+    public String getTemplatePath() {
+        return templatePath;
+    }
+
+    @Inject
+    public void register(final SkinRegistry registry) {
+        registry.register(id, this);
+    }
+
+}

chempound-webapi/src/main/java/net/chempound/webapp/plugins/SkinRegistry.java

+package net.chempound.webapp.plugins;
+
+/**
+ * @author Sam Adams
+ */
+public interface SkinRegistry extends Registry<Skin> {
+
+}

chempound-webapp/src/main/java/net/chempound/webapp/ChempoundApplication.java

                 staticContentRouter.attachRoutes();
                 attach("/static/", staticContentRouter);
 
+                final SkinContentRouter skinContentRouter = injector.getInstance(SkinContentRouter.class);
+                skinContentRouter.attachRoutes();
+                attach("/skin/", skinContentRouter);
+
                 final CssContentRouter cssContentRouter = injector.getInstance(CssContentRouter.class);
                 cssContentRouter.attachRoutes();
                 attach("/css/", cssContentRouter);

chempound-webapp/src/main/java/net/chempound/webapp/DefaultChempoundWebModule.java

 import net.chempound.webapp.output.ContentType;
 import net.chempound.webapp.plugin.DefaultCssRegistry;
 import net.chempound.webapp.plugin.DefaultPluginRegistry;
+import net.chempound.webapp.plugin.DefaultSkinRegistry;
 import net.chempound.webapp.plugins.ChempoundWebPlugin;
 import net.chempound.webapp.plugins.CssRegistry;
 import net.chempound.webapp.plugins.PluginRegistry;
+import net.chempound.webapp.plugins.SkinRegistry;
 import net.chempound.webapp.sword.impl.SwordModule;
 import org.restlet.Application;
 import org.restlet.Component;
 
         bind(PluginRegistry.class).to(DefaultPluginRegistry.class);
         bind(CssRegistry.class).to(DefaultCssRegistry.class);
+        bind(SkinRegistry.class).to(DefaultSkinRegistry.class);
 
         Multibinder.newSetBinder(binder(), ContentType.class);
         MapBinder.newMapBinder(binder(), String.class, ChempoundWebPlugin.class);

chempound-webapp/src/main/java/net/chempound/webapp/DefaultFreemarkerService.java

         templateLoader.addPlugin(name, new FileTemplateLoader(templateDir));
     }
 
+    public void registerSkin(final String name, final Class<?> context, final String path) throws IOException {
+        final File templateDir = getSkinTemplateDir(name);
+        FileExtractor.extract(context, path, templateDir);
+        templateLoader.addPlugin(name, new FileTemplateLoader(templateDir));
+    }
+
     private File getPluginTemplateDir(final String name) throws IOException {
         final File templateDir = new File(templateRoot, "plugins/" + name);
         FileUtils.forceMkdir(templateDir);
         return templateDir;
     }
 
+    private File getSkinTemplateDir(final String name) throws IOException {
+        final File templateDir = new File(templateRoot, "skins/" + name);
+        FileUtils.forceMkdir(templateDir);
+        return templateDir;
+    }
+
     // Based on freemarker multiloader
     private static class PluginTemplateRouter implements StatefulTemplateLoader {
 
                 final String plugin = name.substring(8, i);
                 loader = loaderMap.get(plugin);
                 return findTemplateSource(loader, name.substring(i + 1), fullname);
+            } else if (name.startsWith("skins/")) {
+                final int i = name.indexOf('/', 6);
+                final String plugin = name.substring(6, i);
+                loader = loaderMap.get(plugin);
+                return findTemplateSource(loader, name.substring(i + 1), fullname);
             } else {
                 return findTemplateSource(defaultLoader, name, fullname);
             }

chempound-webapp/src/main/java/net/chempound/webapp/SkinContentRouter.java

+package net.chempound.webapp;
+
+import net.chempound.config.ChempoundConfiguration;
+import net.chempound.webapp.plugin.CachedStaticContentRouter;
+import net.chempound.webapp.plugins.Skin;
+import net.chempound.webapp.plugins.SkinRegistry;
+import org.restlet.Context;
+
+import javax.inject.Inject;
+import java.io.IOException;
+
+/**
+ * @author Sam Adams
+ */
+public class SkinContentRouter extends CachedStaticContentRouter {
+
+    private final SkinRegistry skinRegistry;
+
+    @Inject
+    public SkinContentRouter(final Context context, final ChempoundConfiguration configuration, final SkinRegistry skinRegistry) throws IOException {
+        super("skin", context, configuration);
+        this.skinRegistry = skinRegistry;
+    }
+
+    @Override
+    protected void doAttachRoutes() throws IOException {
+        for (final Skin skin : skinRegistry.getList()) {
+            attachRoute(skin.getId(), skin.getContext(), skin.getResourcePath());
+        }
+    }
+}

chempound-webapp/src/main/java/net/chempound/webapp/plugin/DefaultSkinRegistry.java

+package net.chempound.webapp.plugin;
+
+import net.chempound.services.FreemarkerService;
+import net.chempound.webapp.plugins.Skin;
+import net.chempound.webapp.plugins.SkinRegistry;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import java.io.IOException;
+
+/**
+ * @author Sam Adams
+ */
+@Singleton
+public class DefaultSkinRegistry extends DefaultRegistry<Skin> implements SkinRegistry {
+
+    private final FreemarkerService freemarkerService;
+
+    @Inject
+    public DefaultSkinRegistry(final FreemarkerService freemarkerService) {
+        this.freemarkerService = freemarkerService;
+    }
+
+    @Override
+    public synchronized void register(final String id, final Skin plugin) {
+        super.register(id, plugin);
+        try {
+            freemarkerService.registerSkin(id, plugin.getContext(), plugin.getTemplatePath());
+        } catch (IOException e) {
+            throw new RuntimeException("Error registering skin", e);
+        }
+    }
+}