Commits

Sam Adams committed e774bba

Migrated restlet/servlet config to chempound core

Comments (0)

Files changed (4)

             <artifactId>guice-servlet</artifactId>
             <version>3.0</version>
         </dependency>
-        <dependency>
-            <groupId>org.restlet.jee</groupId>
-            <artifactId>org.restlet</artifactId>
-            <version>2.0.7</version>
-        </dependency>
-        <dependency>
-            <groupId>org.restlet.jee</groupId>
-            <artifactId>org.restlet.ext.servlet</artifactId>
-            <version>2.0.7</version>
-        </dependency>
 
     </dependencies>
 

src/main/java/uk/ac/cam/ch/wwmm/quixote/QuixoteChempoundServletConfig.java

 package uk.ac.cam.ch.wwmm.quixote;
 
 import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Module;
-import com.google.inject.multibindings.MapBinder;
-import com.google.inject.multibindings.Multibinder;
-import com.google.inject.servlet.GuiceServletContextListener;
-import com.google.inject.servlet.ServletModule;
-import com.hp.hpl.jena.query.Dataset;
-import com.hp.hpl.jena.tdb.TDBFactory;
-import org.apache.commons.io.FileUtils;
-import org.restlet.Application;
-import org.restlet.Component;
-import org.restlet.Context;
-import org.restlet.data.Protocol;
-import uk.ac.cam.ch.wwmm.chempound.config.ChempoundConfiguration;
-import uk.ac.cam.ch.wwmm.chempound.DefaultChempoundModule;
 import uk.ac.cam.ch.wwmm.chempound.comp.CompChemModule;
 import uk.ac.cam.ch.wwmm.chempound.crystal.CrystallographyModule;
-import uk.ac.cam.ch.wwmm.chempound.datastore.DataStore;
-import uk.ac.cam.ch.wwmm.chempound.datastore.TripleStore;
 import uk.ac.cam.ch.wwmm.chempound.plugins.jmol.JmolModule;
 import uk.ac.cam.ch.wwmm.chempound.plugins.jquery.JQueryModule;
-import uk.ac.cam.ch.wwmm.chempound.storage.FileSystemResourceStore;
-import uk.ac.cam.ch.wwmm.chempound.storage.ResourceStore;
-import uk.ac.cam.ch.wwmm.chempound.webapp.ChempoundApplication;
-import uk.ac.cam.ch.wwmm.chempound.webapp.DefaultFreemarkerService;
-import uk.ac.cam.ch.wwmm.chempound.webapp.FreemarkerService;
-import uk.ac.cam.ch.wwmm.chempound.webapp.output.ContentHandler;
-import uk.ac.cam.ch.wwmm.chempound.webapp.plugins.ChempoundWebPlugin;
+import uk.ac.cam.ch.wwmm.chempound.webapp.ChempoundServletConfig;
 import uk.ac.cam.ch.wwmm.chempound.webapp.search.chemistry.StructureSearchProviderModule;
-import uk.ac.cam.ch.wwmm.chempound.webapp.sword.impl.SwordModule;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URI;
 
 /**
  * @author sea36
  */
-public class QuixoteChempoundServletConfig extends GuiceServletContextListener {
-
-    private String workspace;
-    private String baseUri;
+public class QuixoteChempoundServletConfig extends ChempoundServletConfig {
 
     public QuixoteChempoundServletConfig() {
-        workspace = System.getProperty("chempound.workspace");
-        if (workspace == null) {
-            workspace = System.getenv("CHEMPOUND_WORKSPACE");
-        }
-        if (workspace == null) {
-            throw new RuntimeException("Chempound workspace not set");
-        }
-
-        baseUri = System.getProperty("chempound.uri");
-        if (baseUri == null) {
-            baseUri = System.getenv("CHEMPOUND_URI");
-        }
-        if (baseUri == null) {
-            throw new RuntimeException("Chempound base URI not set");
-        }
+        super();
     }
 
-    public QuixoteChempoundServletConfig(String workspace, String baseUri) {
-        this.workspace = workspace;
-        this.baseUri = baseUri;
-    }
-
-
-    private static DataStore createDataStore(ChempoundConfiguration config) throws IOException {
-        File dir = new File(config.getWorkspace(), "tdb/");
-        FileUtils.forceMkdir(dir);
-        Dataset dataset = TDBFactory.createDataset(dir.getPath());
-        return new DataStore(dataset);
-    }
-
-    private static ResourceStore createResourceStore(ChempoundConfiguration config) throws IOException {
-        File dir = new File(config.getWorkspace(), "content/");
-        FileUtils.forceMkdir(dir);
-        return new FileSystemResourceStore(dir);
-    }
-
-    private Module createConfigurationModule() {
+    protected AbstractModule getPluginsModule() {
         return new AbstractModule() {
             @Override
             protected void configure() {
-                try {
-                    File workspaceDir = new File(workspace);
-                    URI uri = URI.create(baseUri);
-                    ChempoundConfiguration config = ChempoundConfiguration.load(uri, workspaceDir);
 
-                    bind(ChempoundConfiguration.class).toInstance(config);
+                install(new StructureSearchProviderModule());
+                install(new CrystallographyModule());
+                install(new CompChemModule());
 
-                    TripleStore dataStore = createDataStore(config);
-                    ResourceStore resourceStore = createResourceStore(config);
-                    bind(TripleStore.class).toInstance(dataStore);
-                    bind(ResourceStore.class).toInstance(resourceStore);
-                } catch (IOException e) {
-                    throw new RuntimeException("Error initialising chempound", e);
-                }
-            }
-        };
-    }
+                install(new JQueryModule());
+                install(new JmolModule());
 
-    protected Module createChempoundModule() {
-        return new DefaultChempoundModule();
-    }
-
-    protected ServletModule createServletModule() {
-        return new ServletModule() {
-            @Override
-            protected void configureServlets() {
-                Multibinder.newSetBinder(binder(), ContentHandler.class);
-                MapBinder.newMapBinder(binder(), String.class, ChempoundWebPlugin.class);
-
-                bind(FreemarkerService.class).to(DefaultFreemarkerService.class);
-                serve("/*").with(RestletServlet.class);
-            }
-        };
-    }
-
-    @Override
-    protected Injector getInjector() {
-        return Guice.createInjector(
-
-                createConfigurationModule(),
-                createChempoundModule(),
-                createServletModule(),
-
-                new StructureSearchProviderModule(),
-                new CrystallographyModule(),
-                new CompChemModule(),
-
-                new JQueryModule(),
-                new JmolModule(),
-
-                new SwordModule(),
-
-                getRestletModule()
-
-        );
-    }
-
-    private AbstractModule getRestletModule() {
-        return new AbstractModule() {
-            @Override
-            protected void configure() {
-                Component component = new Component();
-                component.getClients().add(Protocol.CLAP);
-
-                bind(Context.class).toInstance(component.getContext().createChildContext());
-                bind(Application.class).to(ChempoundApplication.class);
             }
         };
     }

src/main/java/uk/ac/cam/ch/wwmm/quixote/RestletServlet.java

-package uk.ac.cam.ch.wwmm.quixote;
-
-import com.google.inject.Injector;
-import org.restlet.Application;
-import org.restlet.ext.servlet.ServletAdapter;
-import uk.ac.cam.ch.wwmm.chempound.webapp.ChempoundApplication;
-
-import javax.inject.Inject;
-import javax.inject.Singleton;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-
-/**
- * @author sea36
- */
-@Singleton
-public class RestletServlet extends HttpServlet {
-
-    private Application application;
-    private ServletAdapter adapter;
-
-    @Inject
-    public RestletServlet(Application application) {
-        this.application = application;
-    }
-
-    @Override
-    public void init() throws ServletException {
-        this.adapter = new ServletAdapter(getServletContext(), application);
-    }
-
-    @Override
-    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-        adapter.service(req, resp);
-    }
-
-}

src/main/resources/log4j.properties

 log4j.appender.A.layout.ConversionPattern = %-4r [%t] %-5p %c %x - %m%n
 log4j.appender.A.target = System.err
 
-log4j.logger.uk.ac.cam.ch.wwmm.chempound = DEBUG
+# log4j.logger.uk.ac.cam.ch.wwmm.chempound = DEBUG