SOLR-68 - new ClassLoader that knows about jars in solr/lib/ used for accessing resources specified in Config files, ie: plugins. Added example/solr/README.txt while i was at it so the various subdirs including 'lib' would be documented.

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@474625 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2006-11-14 02:05:32 +00:00
parent 28fc069610
commit 11d9ba2463
3 changed files with 82 additions and 3 deletions

View File

@ -63,7 +63,9 @@ New Features
useful in AJAX with dynamic script tags for specifying a JavaScript
callback function. (Bertrand Delacretaz via yonik, SOLR-56)
29. autoCommit can be specified every so many documents added (klaas, SOLR-65)
30. ${solr.home}/lib directory can now be used for specifying "plugin" jars
(hossman, SOLR-68)
Changes in runtime behavior
1. classes reorganized into different packages, package names changed to Apache
2. force read of document stored fields in QuerySenderListener

36
example/solr/README.txt Normal file
View File

@ -0,0 +1,36 @@
Example "Solr Home" Directory
=============================
This directory is provided as an example of what a "Solr Home" directory
should look like.
It's not strictly necessary that you copy all of the files in this
directory when setting up a new instance of Solr, but it is recommended.
Basic Directory Structure
-------------------------
The Solr Home directory typically contains the following subdirectories...
conf/
This directory is mandatory and must contain your solrconfig.xml
and schema.xml. Any other optional configuration files would also
be kept here.
data/
This directory is the default location where Solr will keep your
index, and is used by the replication scripts for dealing with
snapshots. You can override this location in the solrconfig.xml
and scripts.conf files. Solr will create this directory if it
does not already exist.
lib/
This directory is optional. If it exists, Solr will load any Jars
found in this directory and use them to resolve any "plugins"
specified in your solrconfig.xml or schema.xml (ie: Analyzers,
Request Handlers, etc...)
bin/
This directory is optional. It is the default location used for
keeping the replication scripts.

View File

@ -33,6 +33,10 @@ import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.net.URLClassLoader;
import java.net.URI;
import java.net.URL;
import java.net.MalformedURLException;
/**
* @author yonik
@ -181,7 +185,7 @@ public class Config {
private static final String[] packages = {"","analysis.","schema.","search.","update.","core.","request.","util."};
public static Class findClass(String cname, String... subpackages) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassLoader loader = getClassLoader();
if (subpackages.length==0) subpackages = packages;
// first try cname == full name
@ -226,6 +230,7 @@ public class Config {
public static void setInstanceDir(String dir) {
instanceDir = normalizeDir(dir);
classLoader = null;
log.info("Solr home set to '" + instanceDir + "'");
}
@ -249,6 +254,42 @@ public class Config {
return getInstanceDir() + "conf/";
}
/** Singleton classloader loading resources specified in any configs */
private static ClassLoader classLoader = null;
/**
* Returns the singleton classloader to be use when loading resources
* specified in any configs.
*
* <p>
* This loader will delegate to the context classloader when possible,
* otherwise it will attempt to resolve resources useing any jar files
* found in the "lib/" directory in the "Solr Home" directory.
* <p>
*/
static ClassLoader getClassLoader() {
if (null == classLoader) {
classLoader = Thread.currentThread().getContextClassLoader();
File f = new File(getInstanceDir() + "lib/");
if (f.canRead() && f.isDirectory()) {
File[] jarFiles = f.listFiles();
URL[] jars = new URL[jarFiles.length];
try {
for (int j = 0; j < jarFiles.length; j++) {
jars[j] = jarFiles[j].toURI().toURL();
log.info("Adding '" + jars[j].toString() + "' to Solr classloader");
}
classLoader = URLClassLoader.newInstance(jars, classLoader);
} catch (MalformedURLException e) {
SolrException.log(log,"Can't construct solr lib class loader", e);
}
}
}
return classLoader;
}
public static InputStream openResource(String resource) {
InputStream is=null;
@ -268,7 +309,7 @@ public class Config {
}
}
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ClassLoader loader = getClassLoader();
is = loader.getResourceAsStream(resource);
} catch (Exception e) {
throw new RuntimeException("Error opening " + resource, e);