Commits

Anonymous committed a515f2a

Refactoring configuration / property loading

  • Participants
  • Parent commits 4a61cc7

Comments (0)

Files changed (18)

chempound-api/src/main/java/net/chempound/config/DefaultPropertySource.java

+package net.chempound.config;
+
+import javax.inject.Singleton;
+
+@Singleton
+public class DefaultPropertySource implements PropertySource {
+
+    @Override
+    public String getProperty(final String key) {
+        final String value = System.getProperty(key);
+        if (value == null) {
+            return System.getenv(key.toUpperCase().replace('.', '_'));
+        }
+        return value;
+    }
+
+}

chempound-api/src/main/java/net/chempound/config/PropertySource.java

+package net.chempound.config;
+
+public interface PropertySource {
+
+    String getProperty(String key);
+
+}

chempound-app/src/main/java/net/chempound/ChempoundConfigurationModule.java

 public class ChempoundConfigurationModule extends AbstractModule {
 
     private final ChempoundConfiguration configuration;
+    private final PropertySource propertySource;
 
-    public ChempoundConfigurationModule(final ChempoundConfiguration configuration) {
+    public ChempoundConfigurationModule(final PropertySource propertySource) {
+        this(new DefaultChempoundConfiguration(propertySource), propertySource);
+    }
+
+    public ChempoundConfigurationModule(final ChempoundConfiguration configuration, final PropertySource propertySource) {
         this.configuration = configuration;
+        this.propertySource = propertySource;
     }
 
     @Override
     protected void configure() {
+        bind(PropertySource.class).toInstance(propertySource);
         bind(ChempoundConfiguration.class).toInstance(configuration);
     }
 

chempound-app/src/main/java/net/chempound/config/AbstractChempoundConfiguration.java

+package net.chempound.config;
+
+import java.io.File;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+public abstract class AbstractChempoundConfiguration implements ChempoundConfiguration {
+
+    private File cacheDirectory;
+    private final List<FeedConfiguration> feeds = new CopyOnWriteArrayList<FeedConfiguration>();
+
+    @Override
+    public File getCacheDirectory() {
+        if (cacheDirectory == null) {
+            cacheDirectory = new File(getWorkspace(), "cache");
+        }
+        return cacheDirectory;
+    }
+
+    @Override
+    public List<FeedConfiguration> getFeeds() {
+        return feeds;
+    }
+
+}

chempound-app/src/main/java/net/chempound/config/AbstractConfiguration.java

+package net.chempound.config;
+
+public abstract class AbstractConfiguration {
+
+    private final String prefix;
+    private final PropertySource propertySource;
+
+    protected AbstractConfiguration(final String prefix, final PropertySource propertySource) {
+        this.prefix = prefix;
+        this.propertySource = propertySource;
+    }
+
+    public String getRequiredProperty(final String key) {
+        final String name = prefix + key;
+        final String value = propertySource.getProperty(name);
+        if (value == null) {
+            throw new IllegalStateException("Property '" + name + "' not set");
+        }
+        return value;
+    }
+
+    public String getProperty(final String key) {
+        final String name = prefix + key;
+        return propertySource.getProperty(name);
+    }
+
+}

chempound-app/src/main/java/net/chempound/config/ConfigurationIO.java

     private static final XStream XSTREAM = new XStream();
 
     static {
-        XSTREAM.alias("configuration", DefaultChempoundConfiguration.class);
+        XSTREAM.alias("configuration", StubChempoundConfiguration.class);
         XSTREAM.alias("feed", FeedConfiguration.class);
         XSTREAM.registerConverter(new URIConverter());
     }

chempound-app/src/main/java/net/chempound/config/DefaultChempoundConfiguration.java

 package net.chempound.config;
 
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import java.io.*;
+import java.io.File;
 import java.net.URI;
-import java.util.ArrayList;
-import java.util.List;
 
-/**
- * @author Sam Adams
- */
 @Singleton
-public class DefaultChempoundConfiguration implements ChempoundConfiguration {
+public class DefaultChempoundConfiguration extends AbstractChempoundConfiguration {
 
-    private static final Logger LOG = LoggerFactory.getLogger(DefaultChempoundConfiguration.class);
-
-    private transient File workspace;
-
-    private URI baseUri;
-    private File configDirectory;
-    private File tempDirectory;
-    private File cacheDirectory;
-
-    private List<FeedConfiguration> feeds;
-
+    private final File workspace;
+    private final URI baseUri;
 
     @Inject
-    public DefaultChempoundConfiguration(final File workspace) throws IOException {
-        setWorkspace(workspace);
-        init();
-    }
+    public DefaultChempoundConfiguration(final PropertySource propertySource) {
+        final DefaultConfiguration config = new DefaultConfiguration(propertySource);
 
-    private void init() throws IOException {
-        final File configFile = getConfigurationFile();
-        if (configFile.exists()) {
-            loadConfiguration();
-        } else {
-            loadDefaults();
-            saveConfiguration();
-        }
-    }
-
-    private void loadDefaults() throws FileNotFoundException {
-        final InputStream in = getClass().getResourceAsStream("default-config.xml");
-        if (in == null) {
-            throw new FileNotFoundException("Default configuration missing");
-        }
-        try {
-            LOG.info("Loading default configuration");
-            ConfigurationIO.load(this, in);
-        } finally {
-            IOUtils.closeQuietly(in);
-        }
-    }
-
-
-    public DefaultChempoundConfiguration(final URI baseUri, final File workspace) {
-        setBaseUri(baseUri);
-        setWorkspace(workspace);
-    }
-
-    public void setBaseUri(final URI baseUri) {
-        if (baseUri.toString().endsWith("/")) {
-            this.baseUri = baseUri;
-        } else {
-            this.baseUri = URI.create(baseUri.toString() + '/');
-        }
+        this.baseUri = URI.create(config.getRequiredProperty("uri"));
+        this.workspace = new File(config.getRequiredProperty("workspace"));
     }
 
     @Override
 
     @Override
     public File getWorkspace() {
-        if (!workspace.exists()) {
-            if (!workspace.mkdirs()) {
-                throw new RuntimeException("Could not create workspace directory: " + workspace);
-            }
-        }
         return workspace;
     }
 
-    void setWorkspace(final File workspace) {
-        this.workspace = workspace;
-    }
-
-    public File getConfigurationDirectory() {
-        if (configDirectory == null) {
-            configDirectory = new File(getWorkspace(), "config");
-        }
-        if (!configDirectory.exists()) {
-            configDirectory.mkdirs();
-        }
-        return configDirectory;
-    }
-
-    @Override
-    public File getCacheDirectory() {
-        if (cacheDirectory == null) {
-            cacheDirectory = new File(getWorkspace(), "cache");
-        }
-        return cacheDirectory;
-    }
-
-    public File getConfigurationFile() {
-        final File configDirectory = getConfigurationDirectory();
-        final File configFile = new File(configDirectory, "config.xml");
-        return configFile;
-    }
-
-    @Override
-    public List<FeedConfiguration> getFeeds() {
-        if (feeds == null) {
-            feeds = new ArrayList<FeedConfiguration>();
-        }
-        return feeds;
-    }
-
-
-    public synchronized void loadConfiguration() throws IOException {
-        final File configFile = getConfigurationFile();
-        if (configFile.exists()) {
-            LOG.info("Loading configuration file: " + configFile);
-            final BufferedInputStream in = new BufferedInputStream(new FileInputStream(configFile));
-            try {
-                ConfigurationIO.load(this, in);
-            } finally {
-                IOUtils.closeQuietly(in);
-            }
-        }
-    }
-
-    public synchronized void saveConfiguration() throws IOException {
-        final File configFile = getConfigurationFile();
-        final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile));
-        try {
-            ConfigurationIO.save(this, out);
-        } finally {
-            IOUtils.closeQuietly(out);
-        }
-    }
-
-    public static ChempoundConfiguration load(final URI uri, final File workspaceDir) throws IOException {
-        final DefaultChempoundConfiguration config = new DefaultChempoundConfiguration(uri, workspaceDir);
-        config.init();
-        return config;
-    }
-
 }

chempound-app/src/main/java/net/chempound/config/DefaultConfiguration.java

+package net.chempound.config;
+
+public class DefaultConfiguration {
+
+    private final String prefix;
+    private final PropertySource propertySource;
+
+    public DefaultConfiguration(final PropertySource propertySource) {
+        this("chempound.", propertySource);
+    }
+
+    protected DefaultConfiguration(final String prefix, final PropertySource propertySource) {
+        this.prefix = prefix;
+        this.propertySource = propertySource;
+    }
+
+    public String getRequiredProperty(final String key) {
+        final String name = prefix + key;
+        final String value = propertySource.getProperty(name);
+        if (value == null) {
+            throw new IllegalStateException("Property '" + name + "' not set");
+        }
+        return value;
+    }
+
+    public String getProperty(final String key) {
+        final String name = prefix + key;
+        return propertySource.getProperty(name);
+    }
+
+}

chempound-app/src/main/java/net/chempound/config/ExtensionConfiguration.java

+package net.chempound.config;
+
+public class ExtensionConfiguration extends DefaultConfiguration {
+
+    private static final String PREFIX_FORMAT = "chempound.ext.%s.";
+
+    public ExtensionConfiguration(final String id, final PropertySource propertySource) {
+        super(generatePrefix(id), propertySource);
+    }
+
+    private static String generatePrefix(final String id) {
+        return String.format(PREFIX_FORMAT, id);
+    }
+
+}

chempound-app/src/main/java/net/chempound/config/StubChempoundConfiguration.java

+package net.chempound.config;
+
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class StubChempoundConfiguration extends AbstractChempoundConfiguration {
+
+    private static final Logger LOG = LoggerFactory.getLogger(StubChempoundConfiguration.class);
+
+    private transient File workspace;
+
+    private URI baseUri;
+    private File configDirectory;
+
+    public StubChempoundConfiguration(final File workspace) throws IOException {
+        setWorkspace(workspace);
+        init();
+    }
+
+    private void init() throws IOException {
+        final File configFile = getConfigurationFile();
+        if (configFile.exists()) {
+            loadConfiguration();
+        } else {
+            loadDefaults();
+            saveConfiguration();
+        }
+    }
+
+    private void loadDefaults() throws FileNotFoundException {
+        final InputStream in = getClass().getResourceAsStream("default-config.xml");
+        if (in == null) {
+            throw new FileNotFoundException("Default configuration missing");
+        }
+        try {
+            LOG.info("Loading default configuration");
+            ConfigurationIO.load(this, in);
+        } finally {
+            IOUtils.closeQuietly(in);
+        }
+    }
+
+
+    public StubChempoundConfiguration(final URI baseUri, final File workspace) {
+        setBaseUri(baseUri);
+        setWorkspace(workspace);
+    }
+
+    public void setBaseUri(final URI baseUri) {
+        if (baseUri.toString().endsWith("/")) {
+            this.baseUri = baseUri;
+        } else {
+            this.baseUri = URI.create(baseUri.toString() + '/');
+        }
+    }
+
+    @Override
+    public URI getBaseUri() {
+        return baseUri;
+    }
+
+    @Override
+    public File getWorkspace() {
+        if (!workspace.exists()) {
+            if (!workspace.mkdirs()) {
+                throw new RuntimeException("Could not create workspace directory: " + workspace);
+            }
+        }
+        return workspace;
+    }
+
+    void setWorkspace(final File workspace) {
+        this.workspace = workspace;
+    }
+
+    public File getConfigurationDirectory() {
+        if (configDirectory == null) {
+            configDirectory = new File(getWorkspace(), "config");
+        }
+        if (!configDirectory.exists()) {
+            configDirectory.mkdirs();
+        }
+        return configDirectory;
+    }
+
+
+    public File getConfigurationFile() {
+        final File configDirectory = getConfigurationDirectory();
+        final File configFile = new File(configDirectory, "config.xml");
+        return configFile;
+    }
+
+
+
+    public synchronized void loadConfiguration() throws IOException {
+        final File configFile = getConfigurationFile();
+        if (configFile.exists()) {
+            LOG.info("Loading configuration file: " + configFile);
+            final BufferedInputStream in = new BufferedInputStream(new FileInputStream(configFile));
+            try {
+                ConfigurationIO.load(this, in);
+            } finally {
+                IOUtils.closeQuietly(in);
+            }
+        }
+    }
+
+    public synchronized void saveConfiguration() throws IOException {
+        final File configFile = getConfigurationFile();
+        final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(configFile));
+        try {
+            ConfigurationIO.save(this, out);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+    public static ChempoundConfiguration load(final URI uri, final File workspaceDir) throws IOException {
+        final StubChempoundConfiguration config = new StubChempoundConfiguration(uri, workspaceDir);
+        config.init();
+        return config;
+    }
+
+}

chempound-app/src/test/java/net/chempound/config/ChempoundConfigurationTest.java

 
     @Test
     public void testLoadDefaults() throws IOException {
-        ChempoundConfiguration config = new DefaultChempoundConfiguration(workspace);
+        ChempoundConfiguration config = new StubChempoundConfiguration(workspace);
         assertNotNull(config);
     }
 
     @Test
     public void testDefaultFeed() throws IOException {
-        ChempoundConfiguration config = new DefaultChempoundConfiguration(workspace);
+        ChempoundConfiguration config = new StubChempoundConfiguration(workspace);
         assertEquals(1, config.getFeeds().size());
         assertEquals("all", config.getFeeds().get(0).getId());
     }
     @Test
     public void testLoadFile() throws IOException {
         writeConfigurationFile("config1.xml");
-        ChempoundConfiguration config = new DefaultChempoundConfiguration(workspace);
+        ChempoundConfiguration config = new StubChempoundConfiguration(workspace);
 //        assertEquals(URI.create("http://repo.example.com/"), config.getBaseUri());
     }
 

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

 package net.chempound.webapp;
 
 import com.google.inject.servlet.ServletModule;
-import net.chempound.ChempoundConfigurationModule;
-import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultChempoundConfiguration;
 import net.chempound.webapp.guice.GuiceRestletServlet;
 
-import javax.servlet.ServletContext;
-import java.io.File;
-import java.net.URI;
-
 /**
 * @author sea36
 */
 
     @Override
     protected void configureServlets() {
-        install(new ChempoundConfigurationModule(initConfiguration()));
+        install(new ServletConfigurationModule(getServletContext()));
         serve("/*").with(GuiceRestletServlet.class);
     }
 
-    private ChempoundConfiguration initConfiguration() {
-        return new DefaultChempoundConfiguration(getBaseUri(), getWorkspace());
-    }
-
-    private URI getBaseUri() {
-        final ServletContext context = getServletContext();
-        final String baseUri = firstNonNull(
-                context == null ? null : (String) context.getAttribute("chempound.uri"),
-                context == null ? null : context.getInitParameter("chempound.uri"),
-                System.getProperty("chempound.uri"),
-                System.getenv("CHEMPOUND_URI")
-        );
-        if (baseUri == null) {
-            throw new RuntimeException("Chempound base URI not set");
-        }
-        return URI.create(baseUri);
-    }
-
-    private File getWorkspace() {
-        final ServletContext context = getServletContext();
-        final String workspace = firstNonNull(
-                context == null ? null : (String) context.getAttribute("chempound.workspace"),
-                context == null ? null : context.getInitParameter("chempound.workspace"),
-                System.getProperty("chempound.workspace"),
-                System.getenv("CHEMPOUND_WORKSPACE")
-        );
-        if (workspace == null) {
-            throw new RuntimeException("Chempound workspace not set");
-        }
-        return new File(workspace);
-    }
-
-    private static String firstNonNull(final String... a) {
-        for (final String s : a) {
-            if (s != null) {
-                return s;
-            }
-        }
-        return null;
-    }
-
 }

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

 import net.chempound.DefaultChempoundModule;
 import net.chempound.config.BaseUri;
 import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultChempoundConfiguration;
+import net.chempound.config.DefaultPropertySource;
+import net.chempound.config.StubChempoundConfiguration;
 import org.restlet.Application;
 import org.restlet.Component;
 import org.restlet.data.Protocol;
     }
 
     protected Module getConfigurationModule() throws IOException {
-        return new ChempoundConfigurationModule(initConfiguration());
+        return new ChempoundConfigurationModule(initConfiguration(), new DefaultPropertySource());
     }
 
     protected Module getWebappModule() {
 
 
     protected ChempoundConfiguration initConfiguration() {
-        return new DefaultChempoundConfiguration(baseUri, workspace);
+        return new StubChempoundConfiguration(baseUri, workspace);
     }
 
 

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

+package net.chempound.webapp;
+
+import net.chempound.ChempoundConfigurationModule;
+import net.chempound.webapp.config.ServletPropertySource;
+
+import javax.servlet.ServletContext;
+
+public class ServletConfigurationModule extends ChempoundConfigurationModule {
+
+    public ServletConfigurationModule(final ServletContext context) {
+        super(new ServletPropertySource(context));
+    }
+
+}

chempound-webapp/src/main/java/net/chempound/webapp/config/ServletPropertySource.java

+package net.chempound.webapp.config;
+
+import net.chempound.config.DefaultPropertySource;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.ServletContext;
+
+@Singleton
+public class ServletPropertySource extends DefaultPropertySource {
+
+    private final ServletContext context;
+
+    @Inject
+    public ServletPropertySource(final ServletContext context) {
+        this.context = context;
+    }
+
+    @Override
+    public String getProperty(final String key) {
+        final String value = getServletProperty(key);
+        if (value != null) {
+            return value;
+        }
+        return super.getProperty(key);
+    }
+
+    private String getServletProperty(final String key) {
+        final Object o = context.getAttribute(key);
+        if (o != null && o instanceof String) {
+            return (String) o;
+        }
+        return context.getInitParameter(key);
+    }
+
+}

chempound-webapp/src/main/java/net/chempound/webapp/sword/CollectionResource.java

 import java.io.IOException;
 import java.io.InputStream;
 
+import static net.chempound.webapp.RestletConstants.ATTRIBUTE_HEADERS;
 import static org.restlet.data.MediaType.APPLICATION_ATOM;
-import static net.chempound.webapp.RestletConstants.ATTRIBUTE_HEADERS;
 
 /**
  * @author sea36

chempound-webapp/src/test/java/net/chempound/webapp/ChempoundWebAppIntegrationTest.java

 import net.chempound.ChempoundConfigurationModule;
 import net.chempound.DefaultChempoundModule;
 import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultChempoundConfiguration;
+import net.chempound.config.DefaultPropertySource;
+import net.chempound.config.StubChempoundConfiguration;
 import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.rdf.CPTerms;
         workspace = createWorkspace();
 
         injector = Guice.createInjector(
-                new ChempoundConfigurationModule(initConfiguration()),
+                new ChempoundConfigurationModule(initConfiguration(), new DefaultPropertySource()),
                 new DefaultChempoundModule(),
                 new DefaultChempoundWebModule()
             );
 
     protected ChempoundConfiguration initConfiguration() throws IOException {
         URI uri = URI.create("http://localhost:8711/");
-        return DefaultChempoundConfiguration.load(uri, workspace);
+        return StubChempoundConfiguration.load(uri, workspace);
     }
 
 

chempound-webapp/src/test/java/net/chempound/webapp/pingback/PingbackIntegrationTest.java

 import net.chempound.ChempoundConfigurationModule;
 import net.chempound.DefaultChempoundModule;
 import net.chempound.config.ChempoundConfiguration;
-import net.chempound.config.DefaultChempoundConfiguration;
+import net.chempound.config.DefaultPropertySource;
+import net.chempound.config.StubChempoundConfiguration;
 import net.chempound.content.DefaultDepositRequest;
 import net.chempound.content.DepositRequest;
 import net.chempound.datastore.TripleStore;
         workspace = createWorkspace();
 
         injector = Guice.createInjector(
-                new ChempoundConfigurationModule(initConfiguration()),
+                new ChempoundConfigurationModule(initConfiguration(), new DefaultPropertySource()),
                 new DefaultChempoundModule(),
                 new DefaultChempoundWebModule()
         );
 
     protected ChempoundConfiguration initConfiguration() throws IOException {
         URI uri = URI.create("http://localhost:8717/repo/");
-        return DefaultChempoundConfiguration.load(uri, workspace);
+        return StubChempoundConfiguration.load(uri, workspace);
     }
 
     @Test