Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-166 - JNDI solr.home code refactoring

Chris M. Hostettercacf143d65fd
diff --git a/CHANGES.txt b/CHANGES.txt
index 0c053742755..a322667fb20 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -88,7 +88,7 @@ New Features
8. SOLR-104: Support for "Update Plugins" -- RequestHandlers that want
access to streams of data for doing updates. ContentStreams can come
from the raw POST body, multi-part form data, or remote URLs.
- Included in this change is a new SlrDispatchFilter that allows
+ Included in this change is a new SolrDispatchFilter that allows
RequestHandlers registered with names that begin with a "/" to be
accessed using a URL structure based on that name.
(Ryan McKinley via hossman)
@@ -140,6 +140,14 @@ Bug Fixes
4. SOLR-145: Fix for bug introduced in SOLR-104 where some Exceptions
were being ignored by all "out of the box" RequestHandlers. (hossman)
+ 5. SOLR-166: JNDI solr.home code refactoring. SOLR-104 moved
+ some JNDI related code to the init method of a Servlet Filter -
+ according to the Servlet Spec, all Filter's should be initialized
+ prior to initializing any Servlets, but this is not the case in at
+ least one Servlet Container (Resin). This "bug fix" refactors
+ this JNDI code so that it should be executed the first time any
+ attempt is made to use the solr.home dir.
+
Other Changes
1. Updated to Lucene 2.1
diff --git a/src/java/org/apache/solr/core/Config.java b/src/java/org/apache/solr/core/Config.java
index 53471acf3fa..08a30d0a728 100644
--- a/src/java/org/apache/solr/core/Config.java
+++ b/src/java/org/apache/solr/core/Config.java
@@ -24,6 +24,10 @@ import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrException;
import org.apache.solr.util.DOMUtil;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.naming.NoInitialContextException;
import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
@@ -236,15 +240,38 @@ public class Config {
}
public static String getInstanceDir() {
- if (instanceDir==null) {
- String prop = project + ".solr.home";
- instanceDir = normalizeDir(System.getProperty(prop));
- if (instanceDir==null) {
- instanceDir=project + '/';
- log.info("Solr home defaulted to '" + instanceDir + "' (system property " + prop + " not set)");
- } else {
- log.info("Solr home set to '" + instanceDir + "' from system property " + prop);
+ if ( ! isInstanceDirInitalized() ) {
+ String home = null;
+ // Try JNDI
+ try {
+ Context c = new InitialContext();
+ home = (String)c.lookup("java:comp/env/solr/home");
+ log.info("Using JNDI solr.home: "+home );
+ } catch (NoInitialContextException e) {
+ log.info("JNDI not configured for Solr (NoInitialContextEx)");
+ } catch (NamingException e) {
+ log.info("No /solr/home in JNDI");
+ } catch( RuntimeException ex ) {
+ log.warning("Odd RuntimeException while testing for JNDI: "
+ + ex.getMessage());
+ }
+
+ // Now try system property
+ if( home == null ) {
+ String prop = project + ".solr.home";
+ home = normalizeDir(System.getProperty(prop));
+ if( home != null ) {
+ log.info("using system property solr.home: " + home );
+ }
+ }
+
+ // if all else fails, try
+ if( home == null ) {
+ home = project + '/';
+ log.info("Solr home defaulted to '" + instanceDir + "' (could not find system property or JNDI)");
}
+
+ setInstanceDir(home);
}
return instanceDir;
}
diff --git a/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java b/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java
index f46c9a0c6be..96b5b18f0f1 100644
--- a/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java
+++ b/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java
@@ -22,10 +22,6 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import javax.naming.NoInitialContextException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -35,7 +31,6 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.solr.core.Config;
import org.apache.solr.core.SolrConfig;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrException;
@@ -60,20 +55,7 @@ public class SolrDispatchFilter implements Filter
public void init(FilterConfig config) throws ServletException
{
log.info("SolrDispatchFilter.init()");
-
- // Only initalize the directory if it has not been done yet
- if( !Config.isInstanceDirInitalized() ) {
- try {
- Context c = new InitialContext();
- String home = (String)c.lookup("java:comp/env/solr/home");
- if (home!=null) Config.setInstanceDir(home);
- } catch (NoInitialContextException e) {
- log.info("JNDI not configured for Solr (NoInitialContextEx)");
- } catch (NamingException e) {
- log.info("No /solr/home in JNDI");
- }
- }
-
+
// web.xml configuration
this.pathPrefix = config.getInitParameter( "path-prefix" );
this.handleSelect = "true".equals( config.getInitParameter( "handle-select" ) );