Permalink
Browse files

Merge pull request #118 from cliffano/master

running couchdb-lucene using .war file
  • Loading branch information...
2 parents 9d3402a + 60e5d08 commit 0081272a30dc679effc1cf1298e365b953f568a5 Robert Newson committed May 26, 2011
View
6 README.md
@@ -42,6 +42,12 @@ brew install couchdb-lucene
The zip file contains all the couchdb-lucene code, dependencies, startup scripts and configuration files you need, so unzip it wherever you wish to install couchdb-lucene.
+If you want to run couchdb-lucene on a servlet container like Tomcat, you can build the war file using Maven
+
+<pre>
+mvn war:war
+</pre>
+
<h1>Configure CouchDB</h1>
The following settings are needed in CouchDB's local.ini file in order for it to communicate with couchdb-lucene;
View
4 src/main/assembly/dist.xml
@@ -26,12 +26,12 @@
<outputDirectory>/bin</outputDirectory>
</file>
<file>
- <source>${project.basedir}/src/main/conf/couchdb-lucene.ini</source>
+ <source>${project.basedir}/src/main/resources/couchdb-lucene.ini</source>
<fileMode>644</fileMode>
<outputDirectory>/conf</outputDirectory>
</file>
<file>
- <source>${project.basedir}/src/main/conf/log4j.xml</source>
+ <source>${project.basedir}/src/main/resources/log4j.xml</source>
<fileMode>644</fileMode>
<outputDirectory>/conf</outputDirectory>
</file>
View
54 src/main/java/com/github/rnewson/couchdb/lucene/Config.java
@@ -0,0 +1,54 @@
+package com.github.rnewson.couchdb.lucene;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.HierarchicalINIConfiguration;
+import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
+import org.apache.http.client.HttpClient;
+import org.apache.log4j.Logger;
+
+public final class Config {
+
+ private static final Logger LOG = Logger.getLogger(Config.class);
+
+ private static final String CONFIG_FILE = "couchdb-lucene.ini";
+ private static final String LUCENE_DIR = "lucene.dir";
+ private static final String DEFAULT_DIR = "indexes";
+
+ private final HierarchicalINIConfiguration configuration;
+
+ public Config() throws ConfigurationException {
+ this.configuration = new HierarchicalINIConfiguration(Config.class
+ .getClassLoader().getResource(CONFIG_FILE));
+ this.configuration
+ .setReloadingStrategy(new FileChangedReloadingStrategy());
+ }
+
+ public final HierarchicalINIConfiguration getConfiguration() {
+ return this.configuration;
+ }
+
+ public final File getDir() throws IOException {
+ final File dir = new File(this.configuration.getString(LUCENE_DIR,
+ DEFAULT_DIR));
+ if (!dir.exists() && !dir.mkdir()) {
+ throw new IOException("Could not create " + dir.getCanonicalPath());
+ }
+ if (!dir.canRead()) {
+ throw new IOException(dir + " is not readable.");
+ }
+ if (!dir.canWrite()) {
+ throw new IOException(dir + " is not writable.");
+ }
+ LOG.info("Index output goes to: " + dir.getCanonicalPath());
+ return dir;
+ }
+
+ public final HttpClient getClient() throws MalformedURLException {
+ HttpClientFactory.setIni(this.configuration);
+ return HttpClientFactory.getInstance();
+ }
+}
View
7 src/main/java/com/github/rnewson/couchdb/lucene/LuceneServlet.java
@@ -30,6 +30,7 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
@@ -61,6 +62,12 @@
private final Map<Database, Thread> threads = new HashMap<Database, Thread>();
+ public LuceneServlet() throws ConfigurationException, IOException {
+ final Config config = new Config();
+ this.client = config.getClient();
+ this.root = config.getDir();
+ this.ini = config.getConfiguration();
+ }
public LuceneServlet(final HttpClient client, final File root,
final HierarchicalINIConfiguration ini) {
this.client = client;
View
36 src/main/java/com/github/rnewson/couchdb/lucene/Main.java
@@ -17,13 +17,7 @@
*/
import java.io.File;
-import java.io.IOException;
-import java.util.Iterator;
-import org.apache.commons.configuration.HierarchicalINIConfiguration;
-import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
-import org.apache.commons.io.FileUtils;
-import org.apache.http.client.HttpClient;
import org.apache.log4j.Logger;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
@@ -42,41 +36,21 @@
* Run couchdb-lucene.
*/
public static void main(String[] args) throws Exception {
- final HierarchicalINIConfiguration configuration = new HierarchicalINIConfiguration(
- Main.class.getClassLoader().getResource("couchdb-lucene.ini"));
- configuration.setReloadingStrategy(new FileChangedReloadingStrategy());
-
- final File dir = new File(configuration.getString("lucene.dir", "indexes"));
-
- if (!dir.exists() && !dir.mkdir()) {
- LOG.error("Could not create " + dir.getCanonicalPath());
- System.exit(1);
- }
- if (!dir.canRead()) {
- LOG.error(dir + " is not readable.");
- System.exit(1);
- }
- if (!dir.canWrite()) {
- LOG.error(dir + " is not writable.");
- System.exit(1);
- }
- LOG.info("Index output goes to: " + dir.getCanonicalPath());
+ final Config config = new Config();
+ final File dir = config.getDir();
final Server server = new Server();
final SelectChannelConnector connector = new SelectChannelConnector();
- connector.setHost(configuration.getString("lucene.host", "localhost"));
- connector.setPort(configuration.getInt("lucene.port", 5985));
+ connector.setHost(config.getConfiguration().getString("lucene.host", "localhost"));
+ connector.setPort(config.getConfiguration().getInt("lucene.port", 5985));
LOG.info("Accepting connections with " + connector);
server.setConnectors(new Connector[]{connector});
server.setStopAtShutdown(true);
server.setSendServerVersion(false);
- HttpClientFactory.setIni(configuration);
- final HttpClient httpClient = HttpClientFactory.getInstance();
-
- final LuceneServlet servlet = new LuceneServlet(httpClient, dir, configuration);
+ final LuceneServlet servlet = new LuceneServlet(config.getClient(), dir, config.getConfiguration());
final Context context = new Context(server, "/", Context.NO_SESSIONS | Context.NO_SECURITY);
context.addServlet(new ServletHolder(servlet), "/*");
View
0 src/main/conf/couchdb-lucene.ini → src/main/resources/couchdb-lucene.ini
File renamed without changes.
View
0 src/main/conf/log4j.xml → src/main/resources/log4j.xml
File renamed without changes.
View
19 src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
+
+<web-app>
+
+ <display-name>couchdb-lucene</display-name>
+ <description>Enables full-text searching of CouchDB documents using Lucene</description>
+
+ <servlet>
+ <servlet-name>lucene</servlet-name>
+ <servlet-class>com.github.rnewson.couchdb.lucene.LuceneServlet</servlet-class>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>lucene</servlet-name>
+ <url-pattern>/</url-pattern>
+ </servlet-mapping>
+
+</web-app>
View
64 src/test/java/com/github/rnewson/couchdb/lucene/ConfigTest.java
@@ -0,0 +1,64 @@
+package com.github.rnewson.couchdb.lucene;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.HierarchicalINIConfiguration;
+import org.apache.http.client.HttpClient;
+import org.junit.Test;
+
+public class ConfigTest {
+
+ @Test
+ public void testGetConfiguration() {
+ try {
+ final Config config = new Config();
+ HierarchicalINIConfiguration configuration = config
+ .getConfiguration();
+ assertEquals("localhost", configuration.getString("lucene.host"));
+ assertEquals(5985, configuration.getInt("lucene.port"));
+ } catch (ConfigurationException ce) {
+ fail("ConfigurationException shouldn't have been thrown."
+ + ce.getMessage());
+ }
+ }
+
+ @Test
+ public void testGetDir() {
+ try {
+ final Config config = new Config();
+ File dir = config.getDir();
+ assertTrue(dir.exists());
+ assertTrue(dir.canRead());
+ assertTrue(dir.canWrite());
+ assertEquals("target/indexes", dir.getPath());
+ } catch (ConfigurationException ce) {
+ fail("ConfigurationException shouldn't have been thrown."
+ + ce.getMessage());
+ } catch (IOException ioe) {
+ fail("IOException shouldn't have been thrown." + ioe.getMessage());
+ }
+ }
+
+ @Test
+ public void testGetClient() {
+ try {
+ final Config config = new Config();
+ HttpClient client = config.getClient();
+ assertNotNull(client);
+ } catch (ConfigurationException ce) {
+ fail("ConfigurationException shouldn't have been thrown."
+ + ce.getMessage());
+ } catch (MalformedURLException mue) {
+ fail("MalformedURLException shouldn't have been thrown."
+ + mue.getMessage());
+ }
+ }
+}

0 comments on commit 0081272

Please sign in to comment.