SOLR-726 -- Jdbc Drivers and DataSources fail to load if placed in multicore sharedLib or core's lib directory.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@690131 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2008-08-29 07:00:48 +00:00
parent adf3dff354
commit aa9ff47086
10 changed files with 55 additions and 43 deletions

View File

@ -35,6 +35,9 @@ Bug Fixes
2. SOLR-729: Context.getDataSource(String) gives current entity's DataSource instance regardless of argument.
(Noble Paul, shalin)
3. SOLR-726: Jdbc Drivers and DataSources fail to load if placed in multicore sharedLib or core's lib directory.
(Noble Paul, shalin)
Other Changes

View File

@ -137,6 +137,6 @@ public class ContextImpl extends Context {
public SolrCore getSolrCore() {
return dataImporter.getCore();
return dataImporter == null ? null : dataImporter.getCore();
}
}

View File

@ -262,7 +262,7 @@ public class DataConfig {
DataImportHandlerException.SEVERE,
"<function> must have a 'name' and 'class' attributes");
try {
evaluators.put(func, (Evaluator) DocBuilder.loadClass(clz)
evaluators.put(func, (Evaluator) DocBuilder.loadClass(clz, null)
.newInstance());
} catch (Exception exp) {
throw new DataImportHandlerException(

View File

@ -300,9 +300,6 @@ public class DataImportHandler extends RequestHandlerBase implements
}
public Class loadClass(String name) throws ClassNotFoundException {
return loader.findClass(name);
}
public SolrDoc getSolrDocInstance() {
return new SolrDocumentWrapper();

View File

@ -82,7 +82,7 @@ public class DataImporter {
DataImporter() {
}
public DataImporter(String dataConfig, SolrCore core,
DataImporter(String dataConfig, SolrCore core,
Map<String, Properties> ds) {
if (dataConfig == null)
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
@ -236,31 +236,31 @@ public class DataImporter {
}
public DataConfig getConfig() {
DataConfig getConfig() {
return config;
}
public Date getIndexStartTime() {
Date getIndexStartTime() {
return indexStartTime;
}
public void setIndexStartTime(Date indextStartTime) {
void setIndexStartTime(Date indextStartTime) {
this.indexStartTime = indextStartTime;
}
public Date getLastIndexTime() {
Date getLastIndexTime() {
return lastIndexTime;
}
public void setLastIndexTime(Date lastIndexTime) {
void setLastIndexTime(Date lastIndexTime) {
this.lastIndexTime = lastIndexTime;
}
public void store(Object key, Object value) {
void store(Object key, Object value) {
store.put(key, value);
}
public Object retrieve(Object key) {
Object retrieve(Object key) {
return store.get(key);
}
@ -291,7 +291,7 @@ public class DataImporter {
dataSrc = new JdbcDataSource();
} else {
try {
dataSrc = (DataSource) DocBuilder.loadClass(impl).newInstance();
dataSrc = (DataSource) DocBuilder.loadClass(impl, getCore()).newInstance();
} catch (Exception e) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
"Invalid type for data source: " + impl, e);
@ -406,11 +406,11 @@ public class DataImporter {
}
public DocBuilder getDocBuilder() {
DocBuilder getDocBuilder() {
return docBuilder;
}
public static final ThreadLocal<AtomicLong> QUERY_COUNT = new ThreadLocal<AtomicLong>() {
static final ThreadLocal<AtomicLong> QUERY_COUNT = new ThreadLocal<AtomicLong>() {
protected AtomicLong initialValue() {
return new AtomicLong();
}
@ -516,7 +516,7 @@ public class DataImporter {
}
public SolrCore getCore() {
SolrCore getCore() {
return core;
}

View File

@ -17,6 +17,8 @@
package org.apache.solr.handler.dataimport;
import org.apache.solr.core.SolrCore;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@ -232,7 +234,7 @@ public class DocBuilder {
Map<String, Object> pk, DataConfig.Entity entity, boolean isRoot,
ContextImpl parentCtx) {
EntityProcessor entityProcessor = getEntityProcessor(entity);
EntityProcessor entityProcessor = getEntityProcessor(entity, dataImporter.getCore());
DataSource ds = entity.dataSrc;
if (verboseDebug) {
ds = DebugLogger.wrapDs(ds);
@ -415,7 +417,7 @@ public class DocBuilder {
}
}
public static EntityProcessor getEntityProcessor(DataConfig.Entity entity) {
public static EntityProcessor getEntityProcessor(DataConfig.Entity entity, SolrCore core) {
if (entity.processor != null)
return entity.processor;
EntityProcessor entityProcessor;
@ -423,7 +425,7 @@ public class DocBuilder {
entityProcessor = new SqlEntityProcessor();
} else {
try {
entityProcessor = (EntityProcessor) loadClass(entity.proc)
entityProcessor = (EntityProcessor) loadClass(entity.proc, core)
.newInstance();
} catch (Exception e) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
@ -469,7 +471,7 @@ public class DocBuilder {
Set<Map<String, Object>> deltaSet = new HashSet<Map<String, Object>>();
resolver.addNamespace(null, (Map) entity.allAttributes);
EntityProcessor entityProcessor = getEntityProcessor(entity);
EntityProcessor entityProcessor = getEntityProcessor(entity, context.getCore());
entityProcessor.init(new ContextImpl(entity, resolver, entity.dataSrc,
Context.FIND_DELTA, requestParameters.requestParams, session, null,
dataImporter));
@ -506,7 +508,7 @@ public class DocBuilder {
myModifiedPks.addAll(deltaSet);
Set<Map<String, Object>> parentKeyList = new HashSet<Map<String, Object>>();
if (parentEntity != null && parentEntity.isDocRoot) {
EntityProcessor parentEntityProcessor = getEntityProcessor(parentEntity);
EntityProcessor parentEntityProcessor = getEntityProcessor(parentEntity, context.getCore());
parentEntityProcessor.init(new ContextImpl(parentEntity, resolver,
parentEntity.dataSrc, Context.FIND_DELTA,
requestParameters.requestParams, session, null, dataImporter));
@ -570,17 +572,16 @@ public class DocBuilder {
}
@SuppressWarnings("unchecked")
static Class loadClass(String name) throws ClassNotFoundException {
DocBuilder inst = INSTANCE.get();
static Class loadClass(String name, SolrCore core) throws ClassNotFoundException {
try {
return inst != null ?
inst.writer.loadClass(name) :
return core != null ?
core.getResourceLoader().findClass(name) :
Class.forName(name);
} catch (Exception e) {
try {
String n = DocBuilder.class.getPackage().getName() + "." + name;
return inst != null ?
inst.writer.loadClass(n) :
return core != null ?
core.getResourceLoader().findClass(n) :
Class.forName(n);
} catch (Exception e1) {
throw new ClassNotFoundException("Unable to load " + name + " or " + DocBuilder.class.getPackage().getName() + "." + name, e);

View File

@ -93,7 +93,7 @@ public class EntityProcessorBase extends EntityProcessor {
continue;
}
try {
Class clazz = DocBuilder.loadClass(trans);
Class clazz = DocBuilder.loadClass(trans, context.getSolrCore());
if (clazz.newInstance() instanceof Transformer) {
transformers.add((Transformer) clazz.newInstance());
} else {

View File

@ -16,6 +16,8 @@
*/
package org.apache.solr.handler.dataimport;
import org.apache.solr.common.SolrException;
import java.sql.*;
import java.util.*;
import java.util.concurrent.Callable;
@ -97,26 +99,39 @@ public class JdbcDataSource extends
final Properties initProps) {
final String url = initProps.getProperty(URL);
String driver = initProps.getProperty(DRIVER);
final String driver = initProps.getProperty(DRIVER);
if (url == null)
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
"JDBC URL cannot be null");
if (driver != null) {
try {
if (driver != null)
Class.forName(driver);
DocBuilder.loadClass(driver, context.getSolrCore());
} catch (ClassNotFoundException e) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
"driver could not be loaded");
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Could not load driver: " + driver, e);
}
} else {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Driver must be specified");
}
factory = new Callable<Connection>() {
public Connection call() throws Exception {
LOG.info("Creating a connection for entity "
+ context.getEntityAttribute(DataImporter.NAME) + " with URL: "
+ url);
long start = System.currentTimeMillis();
Connection c = DriverManager.getConnection(url, initProps);
Connection c = null;
try {
c = DriverManager.getConnection(url, initProps);
} catch (SQLException e) {
// DriverManager does not allow you to use a driver which is not loaded through
// the class loader of the class which is trying to make the connection.
// This is a workaround for cases where the user puts the driver jar in the
// solr.home/lib or solr.home/core/lib directories.
Driver d = (Driver) DocBuilder.loadClass(driver, context.getSolrCore()).newInstance();
c = d.connect(url, initProps);
}
LOG.info("Time taken for getConnection(): "
+ (System.currentTimeMillis() - start));
return c;

View File

@ -230,10 +230,6 @@ public abstract class SolrWriter {
return this.getStartTime();
}
public Class loadClass(String name) throws ClassNotFoundException {
return Class.forName(name);
}
/**
* <p>
* Stores the last indexed time into the <code>IMPORTER_PROPERTIES</code>

View File

@ -38,7 +38,7 @@ public class TestDocBuilder {
@Test
public void loadClass() throws Exception {
Class clz = DocBuilder.loadClass("RegexTransformer");
Class clz = DocBuilder.loadClass("RegexTransformer", null);
Assert.assertNotNull(clz);
}