Remove direct Lucene dependency from InMemoryModule

Custom secondary index implementations should be able to use this
module for writing index tests without linking against Lucene.
However, for other potential uses of this module for tests within
Gerrit, it makes sense to still use Lucene by default without
installing any additional modules. So, as a hack, keep the Lucene
dependency but construct it via reflection.

The same argument could be made about the H2 cache dependency, but the
specific use case we are interested in at the moment is the index
implementation, so extract that one first.

Change-Id: I7d213a9f233b819ec0bc6632323e65ac9b270200
This commit is contained in:
Dave Borowitz
2013-10-11 16:08:22 -07:00
parent c7230a14bd
commit a1fe821470

View File

@@ -19,7 +19,6 @@ import static com.google.inject.Scopes.SINGLETON;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.DisabledChangeHooks;
import com.google.gerrit.lucene.LuceneIndexModule;
import com.google.gerrit.reviewdb.client.AuthType;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.GerritPersonIdent;
@@ -55,6 +54,7 @@ import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.ProvisionException;
@@ -66,6 +66,8 @@ import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@@ -121,9 +123,7 @@ public class InMemoryModule extends FactoryModule {
bind(SocketAddress.class).annotatedWith(RemotePeer.class)
.toInstance(new InetSocketAddress(InetAddress.getLocalHost(), 1234));
} catch (UnknownHostException e) {
ProvisionException pe = new ProvisionException(e.getMessage());
pe.initCause(e);
throw pe;
throw newProvisionException(e);
}
bind(PersonIdent.class)
.annotatedWith(GerritPersonIdent.class)
@@ -165,10 +165,7 @@ public class InMemoryModule extends FactoryModule {
if (indexType != null) {
switch (indexType) {
case LUCENE:
int version = cfg.getInt("index", "lucene", "testVersion", -1);
checkState(ChangeSchemas.ALL.containsKey(version),
"invalid index.lucene.testVersion %s", version);
install(new LuceneIndexModule(version, 0, null));
install(luceneIndexModule());
break;
case SQL:
install(new NoIndexModule());
@@ -186,4 +183,37 @@ public class InMemoryModule extends FactoryModule {
SchemaCreator schemaCreator) throws OrmException {
return new InMemoryDatabase(schemaVersion, schemaCreator);
}
private Module luceneIndexModule() {
try {
int version = cfg.getInt("index", "lucene", "testVersion", -1);
checkState(ChangeSchemas.ALL.containsKey(version),
"invalid index.lucene.testVersion %s", version);
Class<?> clazz =
Class.forName("com.google.gerrit.lucene.LuceneIndexModule");
Constructor<?> c =
clazz.getConstructor(Integer.class, int.class, String.class);
return (Module) c.newInstance(Integer.valueOf(version), 0, (String) null);
} catch (ClassNotFoundException e) {
throw newProvisionException(e);
} catch (SecurityException e) {
throw newProvisionException(e);
} catch (NoSuchMethodException e) {
throw newProvisionException(e);
} catch (IllegalArgumentException e) {
throw newProvisionException(e);
} catch (InstantiationException e) {
throw newProvisionException(e);
} catch (IllegalAccessException e) {
throw newProvisionException(e);
} catch (InvocationTargetException e) {
throw newProvisionException(e);
}
}
private static ProvisionException newProvisionException(Throwable cause) {
ProvisionException pe = new ProvisionException(cause.getMessage());
pe.initCause(cause);
return pe;
}
}