Specify configuration file in servlet init-parameters #153

Open
wants to merge 2 commits into
from
@@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
+import java.net.URL;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
@@ -21,8 +22,11 @@
private final HierarchicalINIConfiguration configuration;
public Config() throws ConfigurationException {
- this.configuration = new HierarchicalINIConfiguration(Config.class
- .getClassLoader().getResource(CONFIG_FILE));
+ this(Config.class.getClassLoader().getResource(CONFIG_FILE));
+ }
+
+ public Config(URL file) throws ConfigurationException {
+ this.configuration = new HierarchicalINIConfiguration(file);
this.configuration
.setReloadingStrategy(new FileChangedReloadingStrategy());
}
@@ -177,6 +177,7 @@ public static synchronized HttpClient getInstance() throws MalformedURLException
}
public static void setIni(final HierarchicalINIConfiguration ini) {
+ instance = null;
INI = ini;
}
@@ -19,12 +19,15 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
+import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -53,21 +56,20 @@
private static final long serialVersionUID = 1L;
- private final HttpClient client;
+ private HttpClient client;
private final Map<Database, DatabaseIndexer> indexers = new HashMap<Database, DatabaseIndexer>();
- private final HierarchicalINIConfiguration ini;
+ private HierarchicalINIConfiguration ini;
- private final File root;
+ private File root;
private final Map<Database, Thread> threads = new HashMap<Database, Thread>();
+ private final static String CONFIG_FILE_PARAM = "config";
+
public LuceneServlet() throws ConfigurationException, IOException {
- final Config config = new Config();
- this.client = config.getClient();
- this.root = config.getDir();
- this.ini = config.getConfiguration();
+ init(new Config());
}
public LuceneServlet(final HttpClient client, final File root,
final HierarchicalINIConfiguration ini) {
@@ -76,6 +78,41 @@ public LuceneServlet(final HttpClient client, final File root,
this.ini = ini;
}
+ @Override
+ public void init(ServletConfig config) throws ServletException {
+ super.init(config);
+ final String configFile = config.getInitParameter(CONFIG_FILE_PARAM);
+ try {
+ if (configFile != null) {
+ final URL url = new URL(configFile);
+ InputStream ignoredStream = null;
+ try {
+ ignoredStream = url.openStream(); //Check that file actually exists
+ init(new Config(url));
+ } catch (FileNotFoundException e ) {
+ LOG.error("Cannot open: " + configFile + ". Using defaults");
+ init(new Config());
+ } finally {
+ if (ignoredStream != null) {
+ ignoredStream.close();
+ }
+ }
+ } else {
+ init(new Config());
+ }
+ } catch (ConfigurationException e) {
+ throw new IllegalArgumentException("Bad config file: ", e);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Bad config file: ", e);
+ }
+ }
+
+ private void init(Config config) throws ConfigurationException, IOException{
+ this.client = config.getClient();
+ this.root = config.getDir();
+ this.ini = config.getConfiguration();
+ }
+
private void cleanup(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException, JSONException {
final Couch couch = getCouch(req);
@@ -9,11 +9,16 @@
<servlet>
<servlet-name>lucene</servlet-name>
<servlet-class>com.github.rnewson.couchdb.lucene.LuceneServlet</servlet-class>
+ <init-param>
+ <description>CouchDB-Lucene configuration file URL</description>
+ <param-name>config</param-name>
+ <param-value>file:///etc/couchdb-lucene/couchdb-lucene.ini</param-value>
+ </init-param>
</servlet>
<servlet-mapping>
<servlet-name>lucene</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-</web-app>
+</web-app>