diff --git a/contrib/dataimporthandler/CHANGES.txt b/contrib/dataimporthandler/CHANGES.txt index e272d81d449..c9fca8211f7 100644 --- a/contrib/dataimporthandler/CHANGES.txt +++ b/contrib/dataimporthandler/CHANGES.txt @@ -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 diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/ContextImpl.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/ContextImpl.java index bafe7510e1b..c57ed0e6f9e 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/ContextImpl.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/ContextImpl.java @@ -137,6 +137,6 @@ public class ContextImpl extends Context { public SolrCore getSolrCore() { - return dataImporter.getCore(); + return dataImporter == null ? null : dataImporter.getCore(); } } diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataConfig.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataConfig.java index eb904baa8d1..5788cf69d8d 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataConfig.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataConfig.java @@ -262,7 +262,7 @@ public class DataConfig { DataImportHandlerException.SEVERE, " 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( diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java index fedc6ac2f5e..47ed3e912a2 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java @@ -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(); diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java index 5499de5d3a6..7cdd12142e1 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java @@ -82,7 +82,7 @@ public class DataImporter { DataImporter() { } - public DataImporter(String dataConfig, SolrCore core, + DataImporter(String dataConfig, SolrCore core, Map 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 QUERY_COUNT = new ThreadLocal() { + static final ThreadLocal QUERY_COUNT = new ThreadLocal() { protected AtomicLong initialValue() { return new AtomicLong(); } @@ -516,7 +516,7 @@ public class DataImporter { } - public SolrCore getCore() { + SolrCore getCore() { return core; } diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DocBuilder.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DocBuilder.java index 60237e80b5d..d30ba4166a6 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DocBuilder.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DocBuilder.java @@ -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 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> deltaSet = new HashSet>(); 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> parentKeyList = new HashSet>(); 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,18 +572,17 @@ 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) : - Class.forName(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); } diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/EntityProcessorBase.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/EntityProcessorBase.java index 3d6ecd7e41f..d6b5e1260a8 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/EntityProcessorBase.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/EntityProcessorBase.java @@ -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 { diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java index 7a068678da1..36b51ef6566 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/JdbcDataSource.java @@ -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"); - try { - if (driver != null) - Class.forName(driver); - } catch (ClassNotFoundException e) { - throw new DataImportHandlerException(DataImportHandlerException.SEVERE, - "driver could not be loaded"); + if (driver != null) { + try { + DocBuilder.loadClass(driver, context.getSolrCore()); + } catch (ClassNotFoundException e) { + 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() { 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; diff --git a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/SolrWriter.java b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/SolrWriter.java index a6ff1213c88..ffda5bd12f9 100644 --- a/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/SolrWriter.java +++ b/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/SolrWriter.java @@ -230,10 +230,6 @@ public abstract class SolrWriter { return this.getStartTime(); } - public Class loadClass(String name) throws ClassNotFoundException { - return Class.forName(name); - } - /** *

* Stores the last indexed time into the IMPORTER_PROPERTIES diff --git a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestDocBuilder.java b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestDocBuilder.java index f453c7f4852..ec298a544b5 100644 --- a/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestDocBuilder.java +++ b/contrib/dataimporthandler/src/test/java/org/apache/solr/handler/dataimport/TestDocBuilder.java @@ -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); }