Skip to content
Browse files

Use layered configuration for server: classpath conf < fs conf < meth…

…od params
  • Loading branch information...
1 parent 351ee98 commit 903cfcce1146ba9c8433eec09d1e6baf1e68f9d6 mguymon committed Sep 3, 2012
View
1 .gitignore
@@ -1,4 +1,5 @@
tmp
+solr
# rcov generated
coverage
View
2 VERSION
@@ -1 +1 @@
-0.0.1
+0.0.2
View
44 pom.xml
@@ -4,7 +4,7 @@
<groupId>com.tobedevoured</groupId>
<artifactId>solr_sail</artifactId>
- <version>0.0.1</version>
+ <version>0.0.2</version>
<packaging>jar</packaging>
<name>Solr Sail</name>
@@ -102,7 +102,7 @@
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
- <version>3.6.0</version>
+ <version>3.6.1</version>
</dependency>
<!-- Logging -->
@@ -112,28 +112,46 @@
<version>1.0.6</version>
</dependency>
-
-
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.solr</groupId>
+ <artifactId>solr-solrj</artifactId>
+ <version>3.6.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.2.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpmime</artifactId>
+ <version>4.2.1</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
-
<build>
<plugins>
- <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId>
- <version>1.7.1</version> <executions> <execution> <phase>package</phase>
- <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact>
- <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude>
- <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
- <mainClass>com.tobedevoured.command.Runner</mainClass> </transformer> </transformers>
- </configuration> </execution> </executions> </plugin> -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifest>
+ <mainClass>com.tobedevoured.command.Runner</mainClass>
+ </manifest>
+ </archive>
+ </configuration>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
View
41 src/main/java/com/tobedevoured/solrsail/JettyServer.java
@@ -1,5 +1,6 @@
package com.tobedevoured.solrsail;
+import java.io.File;
import java.io.IOException;
import org.eclipse.jetty.plus.jndi.EnvEntry;
@@ -27,17 +28,25 @@
private String solrHome;
private String contextPath;
- private int port;
+ private Integer port;
private Server server;
/**
* Create new instance
*/
- public JettyServer() {
+ public JettyServer( String solrHome ) {
+ this.solrHome = solrHome;
+
Config config = ConfigFactory.load();
- solrHome = config.getString("solrsail.solr.home");
- port = config.getInt("solrsail.solr.port");
- contextPath = config.getString("solrsail.solr.contextpath");
+
+ File localConfigFile = new File( new StringBuilder( this.solrHome ).append( File.separator ).append( "solr_sail.conf" ).toString() );
+ if ( localConfigFile.exists() ) {
+ Config localConfig = ConfigFactory.parseFile( localConfigFile );
+ config = localConfig.withFallback( config );
+ }
+
+ port = config.getInt("solr_sail.solr.port");
+ contextPath = config.getString("solr_sail.solr.contextpath");
}
/**
@@ -47,9 +56,15 @@ public JettyServer() {
*/
@Command
public void start() throws Exception {
- start(true);
+ start(false);
}
+ /**
+ * Start Jetty
+ *
+ * @param join boolean to join the thread
+ * @throws Exception
+ */
public void start(boolean join) throws Exception {
System.setProperty("java.naming.factory.url.pkgs", "org.eclipse.jetty.jndi");
@@ -77,10 +92,16 @@ public void start(boolean join) throws Exception {
server.start();
if ( join ) {
+ logger.info( "Joining thread" );
server.join();
}
}
+ /**
+ * Stop Jetty
+ *
+ * @throws Exception
+ */
public void stop() throws Exception {
server.stop();
}
@@ -96,16 +117,12 @@ public void setContextPath(String contextPath) {
public String getSolrHome() {
return solrHome;
}
-
- public void setSolrHome(String solrHome) {
- this.solrHome = solrHome;
- }
- public int getPort() {
+ public Integer getPort() {
return port;
}
- public void setPort(int port) {
+ public void setPort(Integer port) {
this.port = port;
}
View
4 src/main/resources/application.conf
@@ -1,4 +1,4 @@
command.packages=com.tobedevoured.solrsail
solrsail.solr.home=solr
-solrsail.solr.port=8080
-solrsail.solr.contextpath=/solr
+solr_sail.solr.port=8983
+solr_sail.solr.contextpath=/solr
View
2 src/main/resources/solr/solr_sail.conf
@@ -0,0 +1,2 @@
+solr_sail.solr.port=8983
+solr_sail.solr.contextpath=/solr
View
37 src/main/ruby/solr_sail.rb
@@ -10,32 +10,61 @@ module SolrSail
DEFAULT_JAR = File.expand_path("#{File.dirname(__FILE__)}/../#{DEFAULT_JAR_NAME}")
DEFAULT_LOCKFILE = File.expand_path("#{File.dirname(__FILE__)}/../Jarfile.lock")
+ # opts:
+ # * :jar: path to the solr_sail jar, default to jar packaged with the gem
+ # * :lockfile: path to lockfile that contains the jar dependencies, defaults to Jarfile.lock packaged with the gem
+ # * :solr_home: path to solr home, defaults to 'solr'
def self.install_config( opts )
- opts = { :jar => DEFAULT_JAR, :lockfile => DEFAULT_LOCKFILE }.merge( opts )
+ opts = { :solr_home => 'solr', :jar => DEFAULT_JAR, :lockfile => DEFAULT_LOCKFILE }.merge( opts )
lockfile = opts.delete(:lockfile)
jar = opts.delete(:jar)
# XXX: should use LockJar to prevent duplicates in the classpath
$CLASSPATH << File.expand_path( jar )
+ # XXX: need a safer way to pass opts to LockJar
LockJar.install( lockfile, opts )
LockJar.load( lockfile, opts )
- solr_config = com.tobedevoured.solrsail.SolrConfig.new( opts[:dest] )
+ solr_config = com.tobedevoured.solrsail.SolrConfig.new( opts[:solr_home] )
solr_config.install()
end
+ #
+ # The server is configued by solr_sail.conf loaded from the solr home. The
+ # opts passed in override solr_sail.conf
+ #
+ # opts:
+ # * :context_path: context path solr runs under, defaults to /solr
+ # * :jar: path to the solr_sail jar, default to jar packaged with the gem
+ # * :join: have the server join the thread. Defaults to false
+ # * :lockfile: path to lockfile that contains the jar dependencies, defaults to Jarfile.lock packaged with the gem
+ # * :port: the port solr runs on, defaults to 8983
+ # * :solr_home: path to solr home, defaults to 'solr'
+ #
def self.start( opts )
- opts = { :jar => DEFAULT_JAR, :lockfile => DEFAULT_LOCKFILE, :join => true }.merge( opts )
+ opts = {
+ :solr_home => 'solr', :jar => DEFAULT_JAR, :lockfile => DEFAULT_LOCKFILE, :join => false }.merge( opts )
+
lockfile = opts.delete(:lockfile)
jar = opts.delete(:jar)
# XXX: should use LockJar to prevent duplicates in the classpath
$CLASSPATH << File.expand_path( jar )
+ # XXX: need a safer way to pass opts to LockJar
LockJar.load( lockfile, opts )
- jetty = com.tobedevoured.solrsail.JettyServer.new()
+ jetty = com.tobedevoured.solrsail.JettyServer.new( opts[:solr_home] )
+
+ if opts[:port]
+ jetty.setPort( opts[:port] )
+ end
+
+ if opts[:context_path]
+ jetty.setContextPath(opts[:context_path])
+ end
+
jetty.start(opts[:join])
jetty
View
46 src/test/java/com/tobedevoured/solrsail/JettyServerTest.java
@@ -1,5 +1,51 @@
package com.tobedevoured.solrsail;
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.solr.client.solrj.impl.HttpSolrServer;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.junit.Test;
+
public class JettyServerTest {
+ @Test
+ public void useLocalConfig() {
+ JettyServer server = new JettyServer( "src/test/resources" );
+
+ assertEquals( "src/test/resources", server.getSolrHome() );
+ assertEquals( Integer.valueOf( 9999 ), server.getPort() );
+ assertEquals( "/test", server.getContextPath() );
+ }
+
+ @Test
+ public void start() throws Exception {
+ File configDir = new File("target/solr");
+
+ if ( configDir.exists() ) {
+ FileUtils.forceDelete( configDir );
+ }
+
+ SolrConfig config = new SolrConfig( "target/solr" );
+ config.install();
+
+ assertTrue( configDir.exists() );
+
+ JettyServer server = new JettyServer( "target/solr" );
+ server.start();
+
+ SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
+ SolrQuery query = new SolrQuery();
+ query.setQuery( "*:*" );
+
+ QueryResponse response = solr.query( query );
+
+ assertEquals( "{numFound=0,start=0,docs=[]}", response.getResponse().get("response").toString() );
+ }
}
View
4 src/test/java/com/tobedevoured/solrsail/SolrConfigTest.java
@@ -6,9 +6,9 @@
import java.io.IOException;
import java.net.URISyntaxException;
+import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
-import org.modeshape.common.util.FileUtil;
import com.tobedevoured.solrsail.SolrConfig;
@@ -24,7 +24,7 @@ public void setup() throws IOException {
solrHome = new File( helper.getSolrHome() );
if ( solrHome.exists() ) {
- FileUtil.delete( solrHome );
+ FileUtils.forceDelete( solrHome );
}
}
View
2 src/test/resources/solr_sail.conf
@@ -0,0 +1,2 @@
+solr_sail.solr.port=9999
+solr_sail.solr.contextpath=/test
View
12 src/test/spec/solr_sail_spec.rb
@@ -24,29 +24,25 @@
FileUtils.rm_rf( 'tmp/solr' )
end
- SolrSail.install_config( :dest => 'tmp/solr', :lockfile => 'Jarfile.lock', :jar => "target/#{SolrSail::DEFAULT_JAR_NAME}" )
+ SolrSail.install_config( :solr_home => 'tmp/solr', :lockfile => 'Jarfile.lock', :jar => "target/#{SolrSail::DEFAULT_JAR_NAME}" )
File.exists?( 'tmp/solr/solr.xml').should be_true
File.exists?( 'tmp/solr/conf').should be_true
end
context "start" do
before(:all) do
- @server = SolrSail.start( :join => false, :dest => 'tmp/solr', :lockfile => 'Jarfile.lock', :jar => "target/#{SolrSail::DEFAULT_JAR_NAME}" )
- @solr = RSolr.connect( :url => 'http://localhost:8080/solr' )
+ @server = SolrSail.start( :join => false, :solr_home => 'tmp/solr', :lockfile => 'Jarfile.lock', :jar => "target/#{SolrSail::DEFAULT_JAR_NAME}" )
+ @solr = RSolr.connect( :url => "http://localhost:#{@server.getPort()}/#{@server.getContextPath()}" )
end
after(:all) do
@server.stop()
end
- it "should be able to connect" do
- @solr = RSolr.connect :url => 'http://localhost:8080/solr'
- end
-
it "should be able to add and query a document" do
@solr.add :id=>1, :price=>1.00
-
+ @solr.commit
result = @solr.get('select', :params => {:q=>'id:1'})
result["response"]["docs"].should eql [{"id"=>"1", "price"=>1.0, "price_c"=>"1.0,USD"}]
end

0 comments on commit 903cfcc

Please sign in to comment.
Something went wrong with that request. Please try again.