make solr more easily embeddable: SOLR-149

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@508859 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2007-02-18 03:33:13 +00:00
parent a6f0f036ac
commit 28d75b04bc
6 changed files with 114 additions and 28 deletions

View File

@ -103,6 +103,10 @@ New Features
configuration files loaded, including schema.xml and solrconfig.xml. configuration files loaded, including schema.xml and solrconfig.xml.
(Erik Hatcher with inspiration from Andrew Saar) (Erik Hatcher with inspiration from Andrew Saar)
12. SOLR-149: Changes to make Solr more easily embeddable, in addition
to logging which request handler handled each request.
(Ryan McKinley via yonik)
Changes in runtime behavior Changes in runtime behavior
1. Highlighting using DisMax will only pick up terms from the main 1. Highlighting using DisMax will only pick up terms from the main
user query, not boost or filter queries (klaas). user query, not boost or filter queries (klaas).

View File

@ -249,6 +249,11 @@ public class Config {
return instanceDir; return instanceDir;
} }
public static boolean isInstanceDirInitalized()
{
return instanceDir != null;
}
// The directory where solr will look for config files by default. // The directory where solr will look for config files by default.
// defaults to "./solr/conf/" // defaults to "./solr/conf/"
static String getConfigDir() { static String getConfigDir() {

View File

@ -608,7 +608,8 @@ public final class SolrCore {
handler.handleRequest(req,rsp); handler.handleRequest(req,rsp);
setResponseHeaderValues(responseHeader,req,rsp); setResponseHeaderValues(responseHeader,req,rsp);
log.info(req.getParamString()+ " 0 "+ log.info(req.getContext().get("path") + " "
+ req.getParamString()+ " 0 "+
(int)(rsp.getEndTime() - req.getStartTime())); (int)(rsp.getEndTime() - req.getStartTime()));
} }

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- <!--
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
@ -34,6 +34,16 @@
<filter> <filter>
<filter-name>SolrRequestFilter</filter-name> <filter-name>SolrRequestFilter</filter-name>
<filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class> <filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class>
<!--
<init-param>
<param-name>handle-select</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>path-prefix</param-name>
<param-value>/xxx</param-value>
</init-param>
-->
</filter> </filter>
<filter-mapping> <filter-mapping>
<filter-name>SolrRequestFilter</filter-name> <filter-name>SolrRequestFilter</filter-name>

View File

@ -40,6 +40,7 @@ import org.apache.solr.core.SolrConfig;
import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrException; import org.apache.solr.core.SolrException;
import org.apache.solr.request.QueryResponseWriter; import org.apache.solr.request.QueryResponseWriter;
import org.apache.solr.request.SolrParams;
import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse; import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.request.SolrRequestHandler; import org.apache.solr.request.SolrRequestHandler;
@ -53,23 +54,17 @@ public class SolrDispatchFilter implements Filter
protected SolrCore core; protected SolrCore core;
protected SolrRequestParsers parsers; protected SolrRequestParsers parsers;
protected boolean handleSelect = false;
protected String pathPrefix = null; // strip this from the begging of a path
public void init(FilterConfig config) throws ServletException public void init(FilterConfig config) throws ServletException
{ {
log.info("SolrDispatchFilter.init()"); log.info("SolrDispatchFilter.init()");
// Only initalize the directory if it has not been done yet
if( !Config.isInstanceDirInitalized() ) {
try { try {
Context c = new InitialContext(); Context c = new InitialContext();
/***
System.out.println("Enumerating JNDI Context=" + c);
NamingEnumeration<NameClassPair> en = c.list("java:comp/env");
while (en.hasMore()) {
NameClassPair ncp = en.next();
System.out.println(" ENTRY:" + ncp);
}
System.out.println("JNDI lookup=" + c.lookup("java:comp/env/solr/home"));
***/
String home = (String)c.lookup("java:comp/env/solr/home"); String home = (String)c.lookup("java:comp/env/solr/home");
if (home!=null) Config.setInstanceDir(home); if (home!=null) Config.setInstanceDir(home);
} catch (NoInitialContextException e) { } catch (NoInitialContextException e) {
@ -77,6 +72,11 @@ public class SolrDispatchFilter implements Filter
} catch (NamingException e) { } catch (NamingException e) {
log.info("No /solr/home in JNDI"); log.info("No /solr/home in JNDI");
} }
}
// web.xml configuration
this.pathPrefix = config.getInitParameter( "path-prefix" );
this.handleSelect = "true".equals( config.getInitParameter( "handle-select" ) );
log.info("user.dir=" + System.getProperty("user.dir")); log.info("user.dir=" + System.getProperty("user.dir"));
core = SolrCore.getSolrCore(); core = SolrCore.getSolrCore();
@ -99,17 +99,34 @@ public class SolrDispatchFilter implements Filter
// this lets you handle /update/commit when /update is a servlet // this lets you handle /update/commit when /update is a servlet
path += req.getPathInfo(); path += req.getPathInfo();
} }
if( pathPrefix != null && path.startsWith( pathPrefix ) ) {
path = path.substring( pathPrefix.length() );
}
int idx = path.indexOf( ':' ); int idx = path.indexOf( ':' );
if( idx > 0 ) { if( idx > 0 ) {
// save the portion after the ':' for a 'handler' path parameter // save the portion after the ':' for a 'handler' path parameter
path = path.substring( 0, idx ); path = path.substring( 0, idx );
} }
SolrQueryRequest solrReq = null;
SolrRequestHandler handler = core.getRequestHandler( path ); SolrRequestHandler handler = core.getRequestHandler( path );
if( handler == null && handleSelect ) {
if( "/select".equals( path ) || "/select/".equals( path ) ) {
solrReq = parsers.parse( path, req );
String qt = solrReq.getParams().get( SolrParams.QT );
handler = core.getRequestHandler( qt );
if( handler == null ) {
throw new SolrException( 400, "unknown handler: "+qt);
}
}
}
if( handler != null ) { if( handler != null ) {
SolrQueryRequest solrReq = parsers.parse( path, req ); if( solrReq == null ) {
solrReq = parsers.parse( path, req );
}
SolrQueryResponse solrRsp = new SolrQueryResponse(); SolrQueryResponse solrRsp = new SolrQueryResponse();
core.execute( handler, solrReq, solrRsp ); this.execute( req, handler, solrReq, solrRsp );
if( solrRsp.getException() != null ) { if( solrRsp.getException() != null ) {
sendError( (HttpServletResponse)response, solrRsp.getException() ); sendError( (HttpServletResponse)response, solrRsp.getException() );
return; return;
@ -133,6 +150,12 @@ public class SolrDispatchFilter implements Filter
chain.doFilter(request, response); chain.doFilter(request, response);
} }
protected void execute( HttpServletRequest req, SolrRequestHandler handler, SolrQueryRequest sreq, SolrQueryResponse rsp) {
// a custom filter could add more stuff to the request before passing it on.
// for example: sreq.getContext().put( "HttpServletRequest", req );
core.execute( handler, sreq, rsp );
}
protected void sendError(HttpServletResponse res, Throwable ex) throws IOException protected void sendError(HttpServletResponse res, Throwable ex) throws IOException
{ {
int code=500; int code=500;
@ -157,4 +180,49 @@ public class SolrDispatchFilter implements Filter
} }
res.sendError( code, ex.getMessage() + trace ); res.sendError( code, ex.getMessage() + trace );
} }
//---------------------------------------------------------------------
//---------------------------------------------------------------------
/**
* Should the filter handle /select even if it is not mapped in solrconfig.xml
*
* This will use consistent error handling for /select?qt=xxx and /update/xml
*
*/
public boolean isHandleSelect() {
return handleSelect;
}
public void setHandleSelect(boolean handleSelect) {
this.handleSelect = handleSelect;
}
/**
* set the prefix for all paths. This is useful if you want to apply the
* filter to something other then *.
*
* For example, if web.xml specifies:
*
* <filter-mapping>
* <filter-name>SolrRequestFilter</filter-name>
* <url-pattern>/xxx/*</url-pattern>
* </filter-mapping>
*
* Make sure to set the PathPrefix to "/xxx" either with this function
* or in web.xml
*
* <init-param>
* <param-name>path-prefix</param-name>
* <param-value>/xxx</param-value>
* </init-param>
*
*/
public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
public String getPathPrefix() {
return pathPrefix;
}
} }

View File

@ -109,11 +109,9 @@ public class SolrRequestParsers
SolrParams params = parser.parseParamsAndFillStreams( req, streams ); SolrParams params = parser.parseParamsAndFillStreams( req, streams );
SolrQueryRequest sreq = buildRequestFrom( params, streams ); SolrQueryRequest sreq = buildRequestFrom( params, streams );
// If there is some path left over, add it to the context // Handlers and loggin will want to know the path. If it contains a ':'
int idx = req.getServletPath().indexOf( ':' ); // the handler could use it for RESTfull URLs
if( idx > 0 ) { sreq.getContext().put( "path", path );
sreq.getContext().put( "path", req.getServletPath().substring( idx+1 ) );
}
return sreq; return sreq;
} }