Skip to content

Commit

Permalink
start an embedded solr server
Browse files Browse the repository at this point in the history
  • Loading branch information
mguymon committed Aug 20, 2012
1 parent 5e97fba commit ae0a057
Show file tree
Hide file tree
Showing 122 changed files with 20,301 additions and 0 deletions.
86 changes: 86 additions & 0 deletions pom.xml
@@ -0,0 +1,86 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.slackworks</groupId>
<artifactId>solrsail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Solr Sail</name>
<url>https://github.com/mguymon/solr_sail</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>8.1.4.v20120524</jetty.version>
</properties>

<dependencies>
<dependency>
<groupId>com.slackworks.command</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jndi</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-plus</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>${jetty.version}</version>
</dependency>

<!-- JSP Compiler -->
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.7.2</version>
</dependency>

<!-- Solr -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>3.6.0</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.6</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
92 changes: 92 additions & 0 deletions src/main/java/com/slackworks/solrsail/JettyServer.java
@@ -0,0 +1,92 @@
package com.slackworks.solrsail;

import java.io.IOException;

import org.eclipse.jetty.plus.jndi.EnvEntry;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.slackworks.command.ConfigFactory;
import com.slackworks.command.Runner;
import com.slackworks.command.annotation.ByYourCommand;
import com.slackworks.command.annotation.Command;
import com.typesafe.config.Config;

/**
* Jetty Server
*
*/
@ByYourCommand
public class JettyServer {
private static Logger logger = LoggerFactory.getLogger( JettyServer.class );

private String solrHome = "solr";
private String contextPath = "/solr";
private int port = 8080;

public JettyServer() throws IOException {
Config config = ConfigFactory.load();
solrHome = config.getString("solrsail.solr.home");
}

@Command
public void run() throws Exception {

System.setProperty("java.naming.factory.url.pkgs", "org.eclipse.jetty.jndi");
System.setProperty("java.naming.factory.initial", "org.eclipse.jetty.jndi.InitialContextFactory");

System.setProperty("org.apache.jasper.compiler.disablejsr199", "true" );

Server server = new Server(getPort());

String webDir = JettyServer.class.getClassLoader().getResource("webapp").toExternalForm();

EnvEntry env = new EnvEntry("java:comp/env/solr/home", getSolrHome() );

WebAppContext context = new WebAppContext();
context.setDisplayName("Solr");
context.setDescriptor( webDir +"/WEB-INF/web.xml" );
context.setResourceBase( webDir );
context.setContextPath( getContextPath() );
context.setParentLoaderPriority(true);

server.setHandler(context);

logger.info( "starting Jetty" );

server.start();
server.join();
}

public String getContextPath() {
return contextPath;
}

public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}

public String getSolrHome() {
return solrHome;
}

public void setSolrHome(String solrHome) {
this.solrHome = solrHome;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public static void main( String[] args ) throws Exception {
Runner.run(args);
}

}
109 changes: 109 additions & 0 deletions src/main/java/com/slackworks/solrsail/Solr.java
@@ -0,0 +1,109 @@
package com.slackworks.solrsail;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.metapossum.utils.scanner.PackageScanner;
import com.slackworks.command.ConfigFactory;
import com.slackworks.command.Runner;
import com.slackworks.command.annotation.ByYourCommand;
import com.slackworks.command.annotation.Command;
import com.typesafe.config.Config;

@ByYourCommand
public class Solr {
private static Logger logger = LoggerFactory.getLogger( Solr.class );

private String solrHome;

public Solr() throws IOException {
Config config = ConfigFactory.load();
solrHome = config.getString("solrsail.solr.home");
}

public Solr( String solrHome ) {
this.solrHome = solrHome;
}

public String getSolrHome() {
return solrHome;
}

public void createSolrHomeDir() {
File file = new File( solrHome );
if ( !file.exists() ) {
file.mkdirs();
}
}

/**
* Explode the solr config from the classpath
*
* @throws IOException
*/
@Command
public void installConfig() throws IOException {
for ( String path : findSolrInClasspath() ) {
if ( path.startsWith( "solr/") && !path.endsWith("/") ) {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream( path );

// Remove the solr prefix from the path
final String destPath = path.substring(5);
final File destFile = new File(
new StringBuilder(solrHome).append( File.separator ).append( destPath ).toString() );

logger.debug( "Copying {} to {}", path, destFile );

if ( !destFile.getParentFile().exists() ) {
destFile.getParentFile().mkdirs();
}

final FileOutputStream outputStream = new FileOutputStream( destFile );

/*
// expands tokens on xml configs
if ( destPath.endsWith( ".xml" ) ) {
String xml = IOUtils.toString( inputStream );
if ( xml.contains("${solr.home}") ) {
logger.debug( "Replacing ${solr.home} in {}", destPath );
xml = xml.replaceAll("\\$\\{solr.home\\}", ( new File( solrHome ) ).getAbsolutePath() );
IOUtils.write( xml, outputStream );
}
} else {
IOUtils.copy( inputStream, outputStream );
}
*/
IOUtils.copy( inputStream, outputStream );

outputStream.close();
inputStream.close();
} else {
logger.debug( "skipping {}", path );
}
}
}

/**
* Find Solr dir in the classpath
*
* @return Set<String> of paths
* @throws IOException
*/
public Set<String> findSolrInClasspath() throws IOException {
PackageScanner<String> scanner = new PackageScanner<String>( new SolrResourceLoader( this.getClass().getClassLoader() ) );
scanner.setRecursive( true );

return scanner.scan( "solr" );
}

public static void main( String[] args ) throws Exception {
Runner.run(args);
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/slackworks/solrsail/SolrResourceLoader.java
@@ -0,0 +1,53 @@
package com.slackworks.solrsail;

import java.io.File;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.metapossum.utils.scanner.ResourceLoader;

/**
* Searches the classpath for the solr directory
*
* @author Michael Guymon
*/
public class SolrResourceLoader implements ResourceLoader<String> {

private static final Logger logger = LoggerFactory.getLogger( SolrResourceLoader.class );

protected ClassLoader classLoader;

public SolrResourceLoader(final ClassLoader classLoader) {
logger.debug( "init" );
this.classLoader = classLoader;
}

public String loadFromJarfile(final String packageName, final JarEntry entry) {
return loadFromFile(packageName, entry.getName());
}

public String loadFromJarfile(String arg0, JarFile arg1, JarEntry arg2) {
return null;
}

public String loadFromFilesystem(final String packageName, final File directory, final String fileName) {
return loadFromFile(packageName, fileName);
}

protected String loadFromFile(final String packageName, final String fileName) {

//logger.debug( "{} {}", packageName, fileName );

// XXX: Hack to fix production packageName duplicated in fileName
final String path = packageName.replace( ".", "/");
if ( !packageName.equals( fileName ) && !fileName.contains( path + "/" ) && !fileName.contains(packageName + "/") ) {
return new StringBuilder( path ).append( "/" ).append( fileName ).toString();
} else {
return fileName;
}
}

}
13 changes: 13 additions & 0 deletions src/main/resources/logback.xml
@@ -0,0 +1,13 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyyMMdd}] %d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

<root>
<level value="INFO" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
2 changes: 2 additions & 0 deletions src/main/resources/slackworks.conf
@@ -0,0 +1,2 @@
command.packages=com.slackworks.solrsail
solrsail.solr.home=solr
31 changes: 31 additions & 0 deletions src/main/resources/solr/conf/admin-extra.html
@@ -0,0 +1,31 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- The content of this page will be statically included into the top
of the admin page. Uncomment this as an example to see there the content
will show up.
<hr>
<i>This line will appear before the first table</i>
<tr>
<td colspan="2">
This row will be appended to the end of the first table
</td>
</tr>
<hr>
-->

0 comments on commit ae0a057

Please sign in to comment.