Format all Java files with google-java-format

Having a standard tool for formatting saves reviewers' valuable time.
google-java-format is Google's standard formatter and is somewhat
inspired by gofmt[1]. This commit formats everything using
google-java-format version 1.2.

The downside of this one-off formatting is breaking blame. This can be
somewhat hacked around with a tool like git-hyper-blame[2], but it's
definitely not optimal until/unless this kind of feature makes its way
to git core.

Not in this change:
* Tool support, e.g. Eclipse. The command must be run manually [3].
* Documentation of best practice, e.g. new 100-column default.

[1] https://talks.golang.org/2015/gofmt-en.slide#3
[2] https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/git-hyper-blame.html
[3] git ls-files | grep java$ | xargs google-java-format -i

Change-Id: Id5f3c6de95ce0b68b41f0a478b5c99a93675aaa3
Signed-off-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:
Dave Borowitz
2016-11-13 09:56:32 -08:00
committed by David Pursehouse
parent 6723b6d0fa
commit 292fa154c1
2443 changed files with 54816 additions and 57825 deletions

View File

@@ -16,11 +16,9 @@ package com.google.gerrit.pgm;
import com.google.gerrit.launcher.GerritLauncher;
import com.google.gerrit.pgm.util.AbstractProgram;
import org.kohsuke.args4j.Argument;
import java.io.IOException;
import java.io.InputStream;
import org.kohsuke.args4j.Argument;
/** Dump the contents of a file in our archive. */
public class Cat extends AbstractProgram {

View File

@@ -98,20 +98,17 @@ import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Stage;
import org.eclipse.jgit.lib.Config;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jgit.lib.Config;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Run SSH daemon portions of Gerrit. */
public class Daemon extends SiteProgram {
@@ -151,8 +148,11 @@ public class Daemon extends SiteProgram {
@Option(name = "--polygerrit-dev", usage = "Force PolyGerrit UI for development")
private boolean polyGerritDev;
@Option(name = "--init", aliases = {"-i"},
usage = "Init site before starting the daemon")
@Option(
name = "--init",
aliases = {"-i"},
usage = "Init site before starting the daemon"
)
private boolean doInit;
@Option(name = "--stop-only", usage = "Stop the daemon", hidden = true)
@@ -174,12 +174,11 @@ public class Daemon extends SiteProgram {
private Runnable serverStarted;
private IndexType indexType;
public Daemon() {
}
public Daemon() {}
@VisibleForTesting
public Daemon(Runnable serverStarted, Path sitePath) {
super (sitePath);
super(sitePath);
this.serverStarted = serverStarted;
}
@@ -206,12 +205,13 @@ public class Daemon extends SiteProgram {
}
}
mustHaveValidSite();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("Thread " + t.getName() + " threw exception", e);
}
});
Thread.setDefaultUncaughtExceptionHandler(
new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("Thread " + t.getName() + " threw exception", e);
}
});
if (runId != null) {
runFile = getSitePath().resolve("logs").resolve("gerrit.run");
@@ -227,13 +227,14 @@ public class Daemon extends SiteProgram {
try {
start();
RuntimeShutdown.add(new Runnable() {
@Override
public void run() {
log.info("caught shutdown, cleaning up");
stop();
}
});
RuntimeShutdown.add(
new Runnable() {
@Override
public void run() {
log.info("caught shutdown, cleaning up");
stop();
}
});
log.info("Gerrit Code Review " + myVersion() + " ready");
if (runId != null) {
@@ -295,14 +296,12 @@ public class Daemon extends SiteProgram {
dbInjector = createDbInjector(true /* enableMetrics */, MULTI_USER);
}
cfgInjector = createCfgInjector();
config = cfgInjector.getInstance(
Key.get(Config.class, GerritServerConfig.class));
config = cfgInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
if (!slave) {
initIndexType();
}
sysInjector = createSysInjector();
sysInjector.getInstance(PluginGuiceEnvironment.class)
.setDbCfgInjector(dbInjector, cfgInjector);
sysInjector.getInstance(PluginGuiceEnvironment.class).setDbCfgInjector(dbInjector, cfgInjector);
manager.add(dbInjector, cfgInjector, sysInjector);
if (!consoleLog) {
@@ -360,9 +359,10 @@ public class Daemon extends SiteProgram {
modules.add(new WorkQueue.Module());
modules.add(new StreamEventsApiListener.Module());
modules.add(new EventBroker.Module());
modules.add(test
? new H2AccountPatchReviewStore.InMemoryModule()
: new H2AccountPatchReviewStore.Module());
modules.add(
test
? new H2AccountPatchReviewStore.InMemoryModule()
: new H2AccountPatchReviewStore.Module());
modules.add(new ReceiveCommitsExecutorModule());
modules.add(new DiffExecutorModule());
modules.add(new MimeUtil2Module());
@@ -382,37 +382,41 @@ public class Daemon extends SiteProgram {
modules.add(new GpgModule(config));
modules.add(new StartupChecks.Module());
if (MoreObjects.firstNonNull(httpd, true)) {
modules.add(new CanonicalWebUrlModule() {
@Override
protected Class<? extends Provider<String>> provider() {
return HttpCanonicalWebUrlProvider.class;
}
});
modules.add(
new CanonicalWebUrlModule() {
@Override
protected Class<? extends Provider<String>> provider() {
return HttpCanonicalWebUrlProvider.class;
}
});
} else {
modules.add(new CanonicalWebUrlModule() {
@Override
protected Class<? extends Provider<String>> provider() {
return CanonicalWebUrlProvider.class;
}
});
modules.add(
new CanonicalWebUrlModule() {
@Override
protected Class<? extends Provider<String>> provider() {
return CanonicalWebUrlProvider.class;
}
});
}
if (sshd) {
modules.add(SshKeyCacheImpl.module());
} else {
modules.add(NoSshKeyCache.module());
}
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(GerritOptions.class).toInstance(
new GerritOptions(config, headless, slave, polyGerritDev));
if (test) {
bind(String.class).annotatedWith(SecureStoreClassName.class)
.toInstance(DefaultSecureStore.class.getName());
bind(SecureStore.class).toProvider(SecureStoreProvider.class);
}
}
});
modules.add(
new AbstractModule() {
@Override
protected void configure() {
bind(GerritOptions.class)
.toInstance(new GerritOptions(config, headless, slave, polyGerritDev));
if (test) {
bind(String.class)
.annotatedWith(SecureStoreClassName.class)
.toInstance(DefaultSecureStore.class.getName());
bind(SecureStore.class).toProvider(SecureStoreProvider.class);
}
}
});
modules.add(new GarbageCollectionModule());
if (!slave) {
modules.add(new ChangeCleanupRunner.Module());
@@ -450,8 +454,7 @@ public class Daemon extends SiteProgram {
private void initSshd() {
sshInjector = createSshInjector();
sysInjector.getInstance(PluginGuiceEnvironment.class)
.setSshInjector(sshInjector);
sysInjector.getInstance(PluginGuiceEnvironment.class).setSshInjector(sshInjector);
manager.add(sshInjector);
}
@@ -461,9 +464,11 @@ public class Daemon extends SiteProgram {
if (!test) {
modules.add(new SshHostKeyModule());
}
modules.add(new DefaultCommandModule(slave,
sysInjector.getInstance(DownloadConfig.class),
sysInjector.getInstance(LfsPluginAuthCommand.Module.class)));
modules.add(
new DefaultCommandModule(
slave,
sysInjector.getInstance(DownloadConfig.class),
sysInjector.getInstance(LfsPluginAuthCommand.Module.class)));
if (!slave && indexType == IndexType.LUCENE) {
modules.add(new IndexCommandsModule());
}
@@ -473,12 +478,11 @@ public class Daemon extends SiteProgram {
private void initHttpd() {
webInjector = createWebInjector();
sysInjector.getInstance(PluginGuiceEnvironment.class)
.setHttpInjector(webInjector);
sysInjector.getInstance(PluginGuiceEnvironment.class).setHttpInjector(webInjector);
sysInjector.getInstance(HttpCanonicalWebUrlProvider.class)
.setHttpServletRequest(
webInjector.getProvider(HttpServletRequest.class));
sysInjector
.getInstance(HttpCanonicalWebUrlProvider.class)
.setHttpServletRequest(webInjector.getProvider(HttpServletRequest.class));
httpdInjector = createHttpdInjector();
manager.add(webInjector, httpdInjector);
@@ -504,8 +508,8 @@ public class Daemon extends SiteProgram {
}
AuthConfig authConfig = cfgInjector.getInstance(AuthConfig.class);
if (authConfig.getAuthType() == AuthType.OPENID ||
authConfig.getAuthType() == AuthType.OPENID_SSO) {
if (authConfig.getAuthType() == AuthType.OPENID
|| authConfig.getAuthType() == AuthType.OPENID_SSO) {
modules.add(new OpenIdModule());
} else if (authConfig.getAuthType() == AuthType.OAUTH) {
modules.add(new OAuthModule());

View File

@@ -23,10 +23,8 @@ import com.google.gerrit.pgm.util.SiteProgram;
import com.google.gerrit.sshd.commands.QueryShell;
import com.google.gerrit.sshd.commands.QueryShell.Factory;
import com.google.inject.Injector;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import org.kohsuke.args4j.Option;
/** Run Gerrit's SQL query tool */
public class Gsql extends SiteProgram {
@@ -46,17 +44,18 @@ public class Gsql extends SiteProgram {
dbInjector = createDbInjector(SINGLE_USER);
manager.add(dbInjector);
manager.start();
RuntimeShutdown.add(new Runnable() {
@Override
public void run() {
try {
System.in.close();
} catch (IOException e) {
// Ignored
}
manager.stop();
}
});
RuntimeShutdown.add(
new Runnable() {
@Override
public void run() {
try {
System.in.close();
} catch (IOException e) {
// Ignored
}
manager.stop();
}
});
final QueryShell shell = shellFactory().create(System.in, System.out);
shell.setOutputFormat(format);
if (query != null) {
@@ -68,11 +67,14 @@ public class Gsql extends SiteProgram {
}
private Factory shellFactory() {
return dbInjector.createChildInjector(new FactoryModule() {
@Override
protected void configure() {
factory(QueryShell.Factory.class);
}
}).getInstance(QueryShell.Factory.class);
return dbInjector
.createChildInjector(
new FactoryModule() {
@Override
protected void configure() {
factory(QueryShell.Factory.class);
}
})
.getInstance(QueryShell.Factory.class);
}
}

View File

@@ -33,24 +33,24 @@ import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.util.Providers;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.kohsuke.args4j.Option;
/** Initialize a new Gerrit installation. */
public class Init extends BaseInit {
@Option(name = "--batch", aliases = {"-b"},
usage = "Batch mode; skip interactive prompting")
@Option(
name = "--batch",
aliases = {"-b"},
usage = "Batch mode; skip interactive prompting"
)
private boolean batchMode;
@Option(name = "--delete-caches",
usage = "Delete all persistent caches without asking")
@Option(name = "--delete-caches", usage = "Delete all persistent caches without asking")
private boolean deleteCaches;
@Option(name = "--no-auto-start", usage = "Don't automatically start daemon after init")
@@ -65,16 +65,16 @@ public class Init extends BaseInit {
@Option(name = "--install-plugin", usage = "Install given plugin without asking")
private List<String> installPlugins;
@Option(name = "--install-all-plugins",
usage = "Install all plugins from war without asking")
@Option(name = "--install-all-plugins", usage = "Install all plugins from war without asking")
private boolean installAllPlugins;
@Option(name = "--secure-store-lib",
usage = "Path to jar providing SecureStore implementation class")
@Option(
name = "--secure-store-lib",
usage = "Path to jar providing SecureStore implementation class"
)
private String secureStoreLib;
@Option(name = "--dev",
usage = "Setup site with default options suitable for developers")
@Option(name = "--dev", usage = "Setup site with default options suitable for developers")
private boolean dev;
@Option(name = "--skip-all-downloads", usage = "Don't download libraries")
@@ -83,8 +83,7 @@ public class Init extends BaseInit {
@Option(name = "--skip-download", usage = "Don't download given library")
private List<String> skippedDownloads;
@Inject
Browser browser;
@Inject Browser browser;
public Init() {
super(new WarDistribution(), null);
@@ -102,12 +101,10 @@ public class Init extends BaseInit {
if (!skipPlugins) {
final List<PluginData> plugins =
InitPlugins.listPluginsAndRemoveTempFiles(init.site,
pluginsDistribution);
InitPlugins.listPluginsAndRemoveTempFiles(init.site, pluginsDistribution);
ConsoleUI ui = ConsoleUI.getInstance(false);
if (installAllPlugins && !nullOrEmpty(installPlugins)) {
ui.message(
"Cannot use --install-plugin together with --install-all-plugins.\n");
ui.message("Cannot use --install-plugin together with --install-all-plugins.\n");
return true;
}
verifyInstallPluginList(ui, plugins);
@@ -129,15 +126,17 @@ public class Init extends BaseInit {
@Override
protected void afterInit(SiteRun run) throws Exception {
List<Module> modules = new ArrayList<>();
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
bind(Browser.class);
bind(String.class).annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
});
modules.add(
new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
bind(Browser.class);
bind(String.class)
.annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
});
modules.add(new GerritServerConfigModule());
Guice.createInjector(modules).injectMembers(this);
start(run);
@@ -168,7 +167,6 @@ public class Init extends BaseInit {
return deleteCaches;
}
@Override
protected boolean skipPlugins() {
return skipPlugins;
@@ -186,9 +184,7 @@ public class Init extends BaseInit {
@Override
protected List<String> getSkippedDownloads() {
return skippedDownloads != null
? skippedDownloads
: Collections.<String> emptyList();
return skippedDownloads != null ? skippedDownloads : Collections.<String>emptyList();
}
@Override
@@ -230,7 +226,7 @@ public class Init extends BaseInit {
IoUtil.copyWithThread(proc.getInputStream(), System.err);
IoUtil.copyWithThread(proc.getErrorStream(), System.err);
for (;;) {
for (; ; ) {
try {
int rc = proc.waitFor();
if (rc != 0) {

View File

@@ -15,10 +15,6 @@
package com.google.gerrit.pgm;
import com.google.gerrit.launcher.GerritLauncher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -28,6 +24,8 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JythonShell {
private static final Logger log = LoggerFactory.getLogger(JythonShell.class);
@@ -38,7 +36,7 @@ public class JythonShell {
private Class<?> pyObject;
private Class<?> pySystemState;
private Object shell;
private ArrayList <String> injectedVariables;
private ArrayList<String> injectedVariables;
public JythonShell() {
Properties env = new Properties();
@@ -72,10 +70,12 @@ public class JythonShell {
pyObject = findClass("org.python.core.PyObject");
pySystemState = findClass("org.python.core.PySystemState");
runMethod(pySystemState, pySystemState, "initialize",
new Class[] { Properties.class, Properties.class },
new Object[] { null, env }
);
runMethod(
pySystemState,
pySystemState,
"initialize",
new Class[] {Properties.class, Properties.class},
new Object[] {null, env});
try {
shell = console.newInstance();
@@ -87,21 +87,23 @@ public class JythonShell {
set("Shell", this);
}
protected Object runMethod0(Class<?> klazz, Object instance,
String name, Class<?>[] sig, Object[] args)
protected Object runMethod0(
Class<?> klazz, Object instance, String name, Class<?>[] sig, Object[] args)
throws InvocationTargetException {
try {
Method m;
m = klazz.getMethod(name, sig);
return m.invoke(instance, args);
} catch (NoSuchMethodException | IllegalAccessException
| IllegalArgumentException | SecurityException e) {
} catch (NoSuchMethodException
| IllegalAccessException
| IllegalArgumentException
| SecurityException e) {
throw cannotStart(e);
}
}
protected Object runMethod(Class<?> klazz, Object instance,
String name, Class<?>[] sig, Object[] args) {
protected Object runMethod(
Class<?> klazz, Object instance, String name, Class<?>[] sig, Object[] args) {
try {
return runMethod0(klazz, instance, name, sig, args);
} catch (InvocationTargetException e) {
@@ -114,15 +116,14 @@ public class JythonShell {
}
protected String getDefaultBanner() {
return (String)runInterpreter("getDefaultBanner",
new Class[] { }, new Object[] { });
return (String) runInterpreter("getDefaultBanner", new Class[] {}, new Object[] {});
}
protected void printInjectedVariable(String id) {
runInterpreter("exec",
new Class[] { String.class },
new Object[] { "print '\"%s\" is \"%s\"' % (\"" + id + "\", " + id + ")" }
);
runInterpreter(
"exec",
new Class[] {String.class},
new Object[] {"print '\"%s\" is \"%s\"' % (\"" + id + "\", " + id + ")"});
}
public void run() {
@@ -130,18 +131,19 @@ public class JythonShell {
printInjectedVariable(key);
}
reload();
runInterpreter("interact",
new Class[] { String.class, pyObject },
new Object[] { getDefaultBanner() +
" running for Gerrit " + com.google.gerrit.common.Version.getVersion(),
null, });
runInterpreter(
"interact",
new Class[] {String.class, pyObject},
new Object[] {
getDefaultBanner()
+ " running for Gerrit "
+ com.google.gerrit.common.Version.getVersion(),
null,
});
}
public void set(String key, Object content) {
runInterpreter("set",
new Class[] { String.class, Object.class },
new Object[] { key, content }
);
runInterpreter("set", new Class[] {String.class, Object.class}, new Object[] {key, content});
injectedVariables.add(key);
}
@@ -174,14 +176,17 @@ public class JythonShell {
try {
File script = new File(parent, p);
if (script.canExecute()) {
runMethod0(console, shell, "execfile",
new Class[] { String.class },
new Object[] { script.getAbsolutePath() }
);
runMethod0(
console,
shell,
"execfile",
new Class[] {String.class},
new Object[] {script.getAbsolutePath()});
} else {
log.info("User initialization file "
+ script.getAbsolutePath()
+ " is not found or not executable");
log.info(
"User initialization file "
+ script.getAbsolutePath()
+ " is not found or not executable");
}
} catch (InvocationTargetException e) {
log.error("Exception occurred while loading file " + p + " : ", e);
@@ -192,10 +197,12 @@ public class JythonShell {
protected void execStream(final InputStream in, final String p) {
try {
runMethod0(console, shell, "execfile",
new Class[] { InputStream.class, String.class },
new Object[] { in, p }
);
runMethod0(
console,
shell,
"execfile",
new Class[] {InputStream.class, String.class},
new Object[] {in, p});
} catch (InvocationTargetException e) {
log.error("Exception occurred while loading " + p + " : ", e);
}

View File

@@ -25,14 +25,12 @@ import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Injector;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.kohsuke.args4j.Option;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.kohsuke.args4j.Option;
/** Converts the local username for all accounts to lower case */
public class LocalUsernamesToLowerCase extends SiteProgram {
@@ -45,8 +43,7 @@ public class LocalUsernamesToLowerCase extends SiteProgram {
private Injector dbInjector;
@Inject
private SchemaFactory<ReviewDb> database;
@Inject private SchemaFactory<ReviewDb> database;
@Override
public int run() throws Exception {
@@ -55,8 +52,7 @@ public class LocalUsernamesToLowerCase extends SiteProgram {
}
dbInjector = createDbInjector(MULTI_USER);
manager.add(dbInjector,
dbInjector.createChildInjector(SchemaVersionCheck.module()));
manager.add(dbInjector, dbInjector.createChildInjector(SchemaVersionCheck.module()));
manager.start();
dbInjector.injectMembers(this);
@@ -83,15 +79,13 @@ public class LocalUsernamesToLowerCase extends SiteProgram {
return 0;
}
private void convertLocalUserToLowerCase(final ReviewDb db,
final AccountExternalId extId) {
private void convertLocalUserToLowerCase(final ReviewDb db, final AccountExternalId extId) {
if (extId.isScheme(AccountExternalId.SCHEME_GERRIT)) {
final String localUser = extId.getSchemeRest();
final String localUserLowerCase = localUser.toLowerCase(Locale.US);
if (!localUser.equals(localUserLowerCase)) {
final AccountExternalId.Key extIdKeyLowerCase =
new AccountExternalId.Key(AccountExternalId.SCHEME_GERRIT,
localUserLowerCase);
new AccountExternalId.Key(AccountExternalId.SCHEME_GERRIT, localUserLowerCase);
final AccountExternalId extIdLowerCase =
new AccountExternalId(extId.getAccountId(), extIdKeyLowerCase);
try {
@@ -117,7 +111,7 @@ public class LocalUsernamesToLowerCase extends SiteProgram {
@Override
public void run() {
try (ReviewDb db = database.open()) {
for (;;) {
for (; ; ) {
final AccountExternalId extId = next();
if (extId == null) {
break;

View File

@@ -16,7 +16,6 @@ package com.google.gerrit.pgm;
import com.google.gerrit.launcher.GerritLauncher;
import com.google.gerrit.pgm.util.AbstractProgram;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;

View File

@@ -28,30 +28,31 @@ import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Providers;
import org.kohsuke.args4j.Argument;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.kohsuke.args4j.Argument;
public class Passwd extends SiteProgram {
private String section;
private String key;
@Argument(metaVar = "SECTION.KEY", index = 0, required = true,
usage = "Section and key separated by a dot of the password to set")
@Argument(
metaVar = "SECTION.KEY",
index = 0,
required = true,
usage = "Section and key separated by a dot of the password to set"
)
private String sectionAndKey;
@Argument(metaVar = "PASSWORD", index = 1, required = false,
usage = "Password to set")
@Argument(metaVar = "PASSWORD", index = 1, required = false, usage = "Password to set")
private String password;
private void init() {
String[] varParts = sectionAndKey.split("\\.");
if (varParts.length != 2) {
throw new IllegalArgumentException("Invalid name '" + sectionAndKey
+ "': expected section.key format");
throw new IllegalArgumentException(
"Invalid name '" + sectionAndKey + "': expected section.key format");
}
section = varParts[0];
key = varParts[1];
@@ -67,22 +68,22 @@ public class Passwd extends SiteProgram {
private Injector getSysInjector() {
List<Module> modules = new ArrayList<>();
modules.add(new FactoryModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class)
.toInstance(getSitePath());
bind(ConsoleUI.class).toInstance(
ConsoleUI.getInstance(password != null));
factory(Section.Factory.class);
bind(Boolean.class).annotatedWith(InstallAllPlugins.class).toInstance(
Boolean.FALSE);
bind(new TypeLiteral<List<String>>() {}).annotatedWith(
InstallPlugins.class).toInstance(new ArrayList<String>());
bind(String.class).annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
});
modules.add(
new FactoryModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
bind(ConsoleUI.class).toInstance(ConsoleUI.getInstance(password != null));
factory(Section.Factory.class);
bind(Boolean.class).annotatedWith(InstallAllPlugins.class).toInstance(Boolean.FALSE);
bind(new TypeLiteral<List<String>>() {})
.annotatedWith(InstallPlugins.class)
.toInstance(new ArrayList<String>());
bind(String.class)
.annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
});
modules.add(new GerritServerConfigModule());
return Guice.createInjector(modules);
}

View File

@@ -15,19 +15,16 @@
package com.google.gerrit.pgm;
import com.google.gerrit.pgm.util.AbstractProgram;
import com.googlecode.prolog_cafe.exceptions.HaltException;
import com.googlecode.prolog_cafe.lang.BufferingPrologControl;
import com.googlecode.prolog_cafe.lang.Prolog;
import com.googlecode.prolog_cafe.lang.PrologClassLoader;
import com.googlecode.prolog_cafe.lang.SymbolTerm;
import org.kohsuke.args4j.Option;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.kohsuke.args4j.Option;
public class PrologShell extends AbstractProgram {
@Option(name = "-s", metaVar = "FILE.pl", usage = "file to load")
@@ -67,11 +64,12 @@ public class PrologShell extends AbstractProgram {
}
private void banner() {
System.err.format("Gerrit Code Review %s - Interactive Prolog Shell",
System.err.format(
"Gerrit Code Review %s - Interactive Prolog Shell",
com.google.gerrit.common.Version.getVersion());
System.err.println();
System.err.println("(type Ctrl-D or \"halt.\" to exit,"
+ " \"['path/to/file.pl'].\" to load a file)");
System.err.println(
"(type Ctrl-D or \"halt.\" to exit," + " \"['path/to/file.pl'].\" to load a file)");
System.err.println();
System.err.flush();
}

View File

@@ -19,11 +19,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gerrit.pgm.util.AbstractProgram;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.schema.java.JavaSchemaModel;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.util.IO;
import org.kohsuke.args4j.Option;
import java.io.BufferedWriter;
import java.io.File;
import java.io.InputStream;
@@ -31,9 +26,18 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.util.IO;
import org.kohsuke.args4j.Option;
public class ProtoGen extends AbstractProgram {
@Option(name = "--output", aliases = {"-o"}, required = true, metaVar = "FILE", usage = "File to write .proto into")
@Option(
name = "--output",
aliases = {"-o"},
required = true,
metaVar = "FILE",
usage = "File to write .proto into"
)
private File file;
@Override
@@ -45,8 +49,7 @@ public class ProtoGen extends AbstractProgram {
try {
JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
try (OutputStream o = lock.getOutputStream();
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(o, UTF_8)))) {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, UTF_8)))) {
String header;
try (InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt")) {
ByteBuffer buf = IO.readWholeStream(in, 1024);

View File

@@ -37,11 +37,6 @@ import com.google.inject.Injector;
import com.google.protobuf.ByteString;
import com.google.protobuf.Parser;
import com.google.protobuf.UnknownFieldSet;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.kohsuke.args4j.Option;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -53,29 +48,35 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.kohsuke.args4j.Option;
/**
* Import data from a protocol buffer dump into the database.
* <p>
* Takes as input a file containing protocol buffers concatenated together with
* varint length encoding, as in {@link Parser#parseDelimitedFrom(InputStream)}.
* Each message contains a single field with a tag corresponding to the relation
* ID in the {@link com.google.gwtorm.server.Relation} annotation.
* <p>
* <strong>Warning</strong>: This method blindly upserts data into the database.
* It should only be used to restore a protobuf-formatted backup into a new,
* empty site.
*
* <p>Takes as input a file containing protocol buffers concatenated together with varint length
* encoding, as in {@link Parser#parseDelimitedFrom(InputStream)}. Each message contains a single
* field with a tag corresponding to the relation ID in the {@link
* com.google.gwtorm.server.Relation} annotation.
*
* <p><strong>Warning</strong>: This method blindly upserts data into the database. It should only
* be used to restore a protobuf-formatted backup into a new, empty site.
*/
public class ProtobufImport extends SiteProgram {
@Option(name = "--file", aliases = {"-f"}, required = true, metaVar = "FILE",
usage = "File to import from")
@Option(
name = "--file",
aliases = {"-f"},
required = true,
metaVar = "FILE",
usage = "File to import from"
)
private File file;
private final LifecycleManager manager = new LifecycleManager();
private final Map<Integer, Relation> relations = new HashMap<>();
@Inject
private SchemaFactory<ReviewDb> schemaFactory;
@Inject private SchemaFactory<ReviewDb> schemaFactory;
@Override
public int run() throws Exception {
@@ -84,34 +85,36 @@ public class ProtobufImport extends SiteProgram {
Injector dbInjector = createDbInjector(SINGLE_USER);
manager.add(dbInjector);
manager.start();
RuntimeShutdown.add(new Runnable() {
@Override
public void run() {
manager.stop();
}
});
RuntimeShutdown.add(
new Runnable() {
@Override
public void run() {
manager.stop();
}
});
dbInjector.injectMembers(this);
ProgressMonitor progress = new TextProgressMonitor();
progress.beginTask("Importing entities", ProgressMonitor.UNKNOWN);
try (ReviewDb db = schemaFactory.open()) {
for (RelationModel model
: new JavaSchemaModel(ReviewDb.class).getRelations()) {
for (RelationModel model : new JavaSchemaModel(ReviewDb.class).getRelations()) {
relations.put(model.getRelationID(), Relation.create(model, db));
}
Parser<UnknownFieldSet> parser =
UnknownFieldSet.getDefaultInstance().getParserForType();
Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType();
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
UnknownFieldSet msg;
while ((msg = parser.parseDelimitedFrom(in)) != null) {
Map.Entry<Integer, UnknownFieldSet.Field> e =
Iterables.getOnlyElement(msg.asMap().entrySet());
Relation rel = checkNotNull(relations.get(e.getKey()),
"unknown relation ID %s in message: %s", e.getKey(), msg);
Relation rel =
checkNotNull(
relations.get(e.getKey()),
"unknown relation ID %s in message: %s",
e.getKey(),
msg);
List<ByteString> values = e.getValue().getLengthDelimitedList();
checkState(values.size() == 1,
"expected one string field in message: %s", msg);
checkState(values.size() == 1, "expected one string field in message: %s", msg);
upsert(rel, values.get(0));
progress.update(1);
}
@@ -123,8 +126,7 @@ public class ProtobufImport extends SiteProgram {
}
@SuppressWarnings({"rawtypes", "unchecked"})
private static void upsert(Relation rel, ByteString s)
throws OrmException {
private static void upsert(Relation rel, ByteString s) throws OrmException {
Collection ents = Collections.singleton(rel.codec().decode(s));
try {
// Not all relations support update; fall back manually.
@@ -138,16 +140,16 @@ public class ProtobufImport extends SiteProgram {
@AutoValue
abstract static class Relation {
private static Relation create(RelationModel model, ReviewDb db)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException, ClassNotFoundException {
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
ClassNotFoundException {
Method m = db.getClass().getMethod(model.getMethodName());
Class<?> clazz = Class.forName(model.getEntityTypeClassName());
return new AutoValue_ProtobufImport_Relation(
(Access<?, ?>) m.invoke(db),
CodecFactory.encoder(clazz));
(Access<?, ?>) m.invoke(db), CodecFactory.encoder(clazz));
}
abstract Access<?, ?> access();
abstract ProtobufCodec<?> codec();
}
}

View File

@@ -58,7 +58,14 @@ import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Injector;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.NullProgressMonitor;
@@ -75,61 +82,41 @@ import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class RebuildNoteDb extends SiteProgram {
private static final Logger log =
LoggerFactory.getLogger(RebuildNoteDb.class);
private static final Logger log = LoggerFactory.getLogger(RebuildNoteDb.class);
@Option(name = "--threads",
usage = "Number of threads to use for rebuilding NoteDb")
@Option(name = "--threads", usage = "Number of threads to use for rebuilding NoteDb")
private int threads = Runtime.getRuntime().availableProcessors();
@Option(name = "--project",
usage = "Projects to rebuild; recommended for debugging only")
@Option(name = "--project", usage = "Projects to rebuild; recommended for debugging only")
private List<String> projects = new ArrayList<>();
@Option(name = "--change",
usage = "Individual change numbers to rebuild; recommended for debugging only")
@Option(
name = "--change",
usage = "Individual change numbers to rebuild; recommended for debugging only"
)
private List<Integer> changes = new ArrayList<>();
private Injector dbInjector;
private Injector sysInjector;
@Inject
private AllUsersName allUsersName;
@Inject private AllUsersName allUsersName;
@Inject
private ChangeRebuilder rebuilder;
@Inject private ChangeRebuilder rebuilder;
@Inject
@GerritServerConfig
private Config cfg;
@Inject @GerritServerConfig private Config cfg;
@Inject
private GitRepositoryManager repoManager;
@Inject private GitRepositoryManager repoManager;
@Inject
private NoteDbUpdateManager.Factory updateManagerFactory;
@Inject private NoteDbUpdateManager.Factory updateManagerFactory;
@Inject
private NotesMigration notesMigration;
@Inject private NotesMigration notesMigration;
@Inject
private SchemaFactory<ReviewDb> schemaFactory;
@Inject private SchemaFactory<ReviewDb> schemaFactory;
@Inject
private WorkQueue workQueue;
@Inject private WorkQueue workQueue;
@Inject
private ChangeBundleReader bundleReader;
@Inject private ChangeBundleReader bundleReader;
@Override
public int run() throws Exception {
@@ -153,36 +140,34 @@ public class RebuildNoteDb extends SiteProgram {
ListeningExecutorService executor = newExecutor();
System.out.println("Rebuilding the NoteDb");
ImmutableListMultimap<Project.NameKey, Change.Id> changesByProject =
getChangesByProject();
ImmutableListMultimap<Project.NameKey, Change.Id> changesByProject = getChangesByProject();
boolean ok;
Stopwatch sw = Stopwatch.createStarted();
try (Repository allUsersRepo = repoManager.openRepository(allUsersName)) {
deleteRefs(RefNames.REFS_DRAFT_COMMENTS, allUsersRepo);
List<ListenableFuture<Boolean>> futures = new ArrayList<>();
List<Project.NameKey> projectNames = Ordering.usingToString()
.sortedCopy(changesByProject.keySet());
List<Project.NameKey> projectNames =
Ordering.usingToString().sortedCopy(changesByProject.keySet());
for (final Project.NameKey project : projectNames) {
ListenableFuture<Boolean> future = executor.submit(
new Callable<Boolean>() {
@Override
public Boolean call() {
try (ReviewDb db = unwrapDb(schemaFactory.open())) {
return rebuildProject(
db, changesByProject, project, allUsersRepo);
} catch (Exception e) {
log.error("Error rebuilding project " + project, e);
return false;
}
}
});
ListenableFuture<Boolean> future =
executor.submit(
new Callable<Boolean>() {
@Override
public Boolean call() {
try (ReviewDb db = unwrapDb(schemaFactory.open())) {
return rebuildProject(db, changesByProject, project, allUsersRepo);
} catch (Exception e) {
log.error("Error rebuilding project " + project, e);
return false;
}
}
});
futures.add(future);
}
try {
ok = Iterables.all(
Futures.allAsList(futures).get(), Predicates.equalTo(true));
ok = Iterables.all(Futures.allAsList(futures).get(), Predicates.equalTo(true));
} catch (InterruptedException | ExecutionException e) {
log.error("Error rebuilding projects", e);
ok = false;
@@ -190,53 +175,53 @@ public class RebuildNoteDb extends SiteProgram {
}
double t = sw.elapsed(TimeUnit.MILLISECONDS) / 1000d;
System.out.format("Rebuild %d changes in %.01fs (%.01f/s)\n",
System.out.format(
"Rebuild %d changes in %.01fs (%.01f/s)\n",
changesByProject.size(), t, changesByProject.size() / t);
return ok ? 0 : 1;
}
private static void execute(BatchRefUpdate bru, Repository repo)
throws IOException {
private static void execute(BatchRefUpdate bru, Repository repo) throws IOException {
try (RevWalk rw = new RevWalk(repo)) {
bru.execute(rw, NullProgressMonitor.INSTANCE);
}
for (ReceiveCommand command : bru.getCommands()) {
if (command.getResult() != ReceiveCommand.Result.OK) {
throw new IOException(String.format("Command %s failed: %s",
command.toString(), command.getResult()));
throw new IOException(
String.format("Command %s failed: %s", command.toString(), command.getResult()));
}
}
}
private void deleteRefs(String prefix, Repository allUsersRepo)
throws IOException {
private void deleteRefs(String prefix, Repository allUsersRepo) throws IOException {
RefDatabase refDb = allUsersRepo.getRefDatabase();
Map<String, Ref> allRefs = refDb.getRefs(prefix);
BatchRefUpdate bru = refDb.newBatchUpdate();
for (Map.Entry<String, Ref> ref : allRefs.entrySet()) {
bru.addCommand(new ReceiveCommand(ref.getValue().getObjectId(),
ObjectId.zeroId(), prefix + ref.getKey()));
bru.addCommand(
new ReceiveCommand(
ref.getValue().getObjectId(), ObjectId.zeroId(), prefix + ref.getKey()));
}
execute(bru, allUsersRepo);
}
private Injector createSysInjector() {
return dbInjector.createChildInjector(new FactoryModule() {
@Override
public void configure() {
install(dbInjector.getInstance(BatchProgramModule.class));
DynamicSet.bind(binder(), GitReferenceUpdatedListener.class).to(
ReindexAfterUpdate.class);
install(new DummyIndexModule());
factory(ChangeResource.Factory.class);
}
});
return dbInjector.createChildInjector(
new FactoryModule() {
@Override
public void configure() {
install(dbInjector.getInstance(BatchProgramModule.class));
DynamicSet.bind(binder(), GitReferenceUpdatedListener.class)
.to(ReindexAfterUpdate.class);
install(new DummyIndexModule());
factory(ChangeResource.Factory.class);
}
});
}
private ListeningExecutorService newExecutor() {
if (threads > 0) {
return MoreExecutors.listeningDecorator(
workQueue.createQueue(threads, "RebuildChange"));
return MoreExecutors.listeningDecorator(workQueue.createQueue(threads, "RebuildChange"));
}
return MoreExecutors.newDirectExecutorService();
}
@@ -249,8 +234,8 @@ public class RebuildNoteDb extends SiteProgram {
MultimapBuilder.hashKeys().arrayListValues().build();
try (ReviewDb db = schemaFactory.open()) {
if (projects.isEmpty() && !changes.isEmpty()) {
Iterable<Change> todo = unwrapDb(db).changes()
.get(Iterables.transform(changes, Change.Id::new));
Iterable<Change> todo =
unwrapDb(db).changes().get(Iterables.transform(changes, Change.Id::new));
for (Change c : todo) {
changesByProject.put(c.getProject(), c.getId());
}
@@ -259,8 +244,7 @@ public class RebuildNoteDb extends SiteProgram {
boolean include = false;
if (projects.isEmpty() && changes.isEmpty()) {
include = true;
} else if (!projects.isEmpty()
&& projects.contains(c.getProject().get())) {
} else if (!projects.isEmpty() && projects.contains(c.getProject().get())) {
include = true;
} else if (!changes.isEmpty() && changes.contains(c.getId().get())) {
include = true;
@@ -274,24 +258,24 @@ public class RebuildNoteDb extends SiteProgram {
}
}
private boolean rebuildProject(ReviewDb db,
private boolean rebuildProject(
ReviewDb db,
ImmutableListMultimap<Project.NameKey, Change.Id> allChanges,
Project.NameKey project, Repository allUsersRepo)
Project.NameKey project,
Repository allUsersRepo)
throws IOException, OrmException {
checkArgument(allChanges.containsKey(project));
boolean ok = true;
ProgressMonitor pm = new TextProgressMonitor(new PrintWriter(System.out));
pm.beginTask(
FormatUtil.elide(project.get(), 50), allChanges.get(project).size());
pm.beginTask(FormatUtil.elide(project.get(), 50), allChanges.get(project).size());
try (NoteDbUpdateManager manager = updateManagerFactory.create(project);
ObjectInserter allUsersInserter = allUsersRepo.newObjectInserter();
RevWalk allUsersRw = new RevWalk(allUsersInserter.newReader())) {
manager.setAllUsersRepo(allUsersRepo, allUsersRw, allUsersInserter,
new ChainedReceiveCommands(allUsersRepo));
manager.setAllUsersRepo(
allUsersRepo, allUsersRw, allUsersInserter, new ChainedReceiveCommands(allUsersRepo));
for (Change.Id changeId : allChanges.get(project)) {
try {
rebuilder.buildUpdates(
manager, bundleReader.fromReviewDb(db, changeId));
rebuilder.buildUpdates(manager, bundleReader.fromReviewDb(db, changeId));
} catch (NoPatchSetsException e) {
log.warn(e.getMessage());
} catch (Throwable t) {

View File

@@ -39,11 +39,6 @@ import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.util.io.NullOutputStream;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@@ -53,13 +48,18 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.util.io.NullOutputStream;
import org.kohsuke.args4j.Option;
public class Reindex extends SiteProgram {
@Option(name = "--threads", usage = "Number of threads to use for indexing")
private int threads = Runtime.getRuntime().availableProcessors();
@Option(name = "--changes-schema-version",
usage = "Schema version to reindex, for changes; default is most recent version")
@Option(
name = "--changes-schema-version",
usage = "Schema version to reindex, for changes; default is most recent version"
)
private Integer changesVersion;
@Option(name = "--verbose", usage = "Output debug information for each change")
@@ -75,15 +75,13 @@ public class Reindex extends SiteProgram {
private Injector sysInjector;
private Config globalConfig;
@Inject
private Collection<IndexDefinition<?, ?, ?>> indexDefs;
@Inject private Collection<IndexDefinition<?, ?, ?>> indexDefs;
@Override
public int run() throws Exception {
mustHaveValidSite();
dbInjector = createDbInjector(MULTI_USER);
globalConfig =
dbInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
globalConfig = dbInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
threads = ThreadLimiter.limitThreads(dbInjector, threads);
checkNotSlaveMode();
disableLuceneAutomaticCommit();
@@ -133,15 +131,14 @@ public class Reindex extends SiteProgram {
}
checkNotNull(indexDefs, "Called this method before injectMembers?");
Set<String> valid = indexDefs.stream()
.map(IndexDefinition::getName).sorted().collect(toSet());
Set<String> valid = indexDefs.stream().map(IndexDefinition::getName).sorted().collect(toSet());
Set<String> invalid = Sets.difference(Sets.newHashSet(indices), valid);
if (invalid.isEmpty()) {
return;
}
throw die("invalid index name(s): " + new TreeSet<>(invalid)
+ " available indices are: " + valid);
throw die(
"invalid index name(s): " + new TreeSet<>(invalid) + " available indices are: " + valid);
}
private void checkNotSlaveMode() throws Die {
@@ -159,24 +156,23 @@ public class Reindex extends SiteProgram {
Module indexModule;
switch (IndexModule.getIndexType(dbInjector)) {
case LUCENE:
indexModule = LuceneIndexModule.singleVersionWithExplicitVersions(
versions, threads);
indexModule = LuceneIndexModule.singleVersionWithExplicitVersions(versions, threads);
break;
case ELASTICSEARCH:
indexModule = ElasticIndexModule
.singleVersionWithExplicitVersions(versions, threads);
indexModule = ElasticIndexModule.singleVersionWithExplicitVersions(versions, threads);
break;
default:
throw new IllegalStateException("unsupported index.type");
}
modules.add(indexModule);
modules.add(dbInjector.getInstance(BatchProgramModule.class));
modules.add(new FactoryModule() {
@Override
protected void configure() {
factory(ChangeResource.Factory.class);
}
});
modules.add(
new FactoryModule() {
@Override
protected void configure() {
factory(ChangeResource.Factory.class);
}
});
return dbInjector.createChildInjector(modules);
}
@@ -192,11 +188,10 @@ public class Reindex extends SiteProgram {
globalConfig.setLong("cache", "changes", "maximumWeight", 0);
}
private <K, V, I extends Index<K, V>> boolean reindex(
IndexDefinition<K, V, I> def) throws IOException {
private <K, V, I extends Index<K, V>> boolean reindex(IndexDefinition<K, V, I> def)
throws IOException {
I index = def.getIndexCollection().getSearchIndex();
checkNotNull(index,
"no active search index configured for %s", def.getName());
checkNotNull(index, "no active search index configured for %s", def.getName());
index.markReady(false);
index.deleteAll();
@@ -206,8 +201,8 @@ public class Reindex extends SiteProgram {
SiteIndexer.Result result = siteIndexer.indexAll(index);
int n = result.doneCount() + result.failedCount();
double t = result.elapsed(TimeUnit.MILLISECONDS) / 1000d;
System.out.format("Reindexed %d documents in %s index in %.01fs (%.01f/s)\n",
n, def.getName(), t, n / t);
System.out.format(
"Reindexed %d documents in %s index in %.01fs (%.01f/s)\n", n, def.getName(), t, n / t);
if (result.success()) {
index.markReady(true);
}

View File

@@ -24,20 +24,17 @@ import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.googlecode.prolog_cafe.exceptions.CompileException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import org.eclipse.jgit.lib.Repository;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
/**
* Gets rules.pl at refs/meta/config and compiles into jar file called
* rules-(sha1 of rules.pl).jar in (site-path)/cache/rules
* Gets rules.pl at refs/meta/config and compiles into jar file called rules-(sha1 of rules.pl).jar
* in (site-path)/cache/rules
*/
public class Rulec extends SiteProgram {
@Option(name = "--all", usage = "recompile all rules")
@@ -46,30 +43,36 @@ public class Rulec extends SiteProgram {
@Option(name = "--quiet", usage = "suppress some messages")
private boolean quiet;
@Argument(index = 0, multiValued = true, metaVar = "PROJECT", usage = "project to compile rules for")
@Argument(
index = 0,
multiValued = true,
metaVar = "PROJECT",
usage = "project to compile rules for"
)
private List<String> projectNames = new ArrayList<>();
private Injector dbInjector;
private final LifecycleManager manager = new LifecycleManager();
@Inject
private GitRepositoryManager gitManager;
@Inject private GitRepositoryManager gitManager;
@Inject
private PrologCompiler.Factory jarFactory;
@Inject private PrologCompiler.Factory jarFactory;
@Override
public int run() throws Exception {
dbInjector = createDbInjector(SINGLE_USER);
manager.add(dbInjector);
manager.start();
dbInjector.createChildInjector(new FactoryModule() {
@Override
protected void configure() {
factory(PrologCompiler.Factory.class);
}
}).injectMembers(this);
dbInjector
.createChildInjector(
new FactoryModule() {
@Override
protected void configure() {
factory(PrologCompiler.Factory.class);
}
})
.injectMembers(this);
LinkedHashSet<Project.NameKey> names = new LinkedHashSet<>();
for (String name : projectNames) {

View File

@@ -28,14 +28,6 @@ import com.google.gerrit.server.securestore.DefaultSecureStore;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.gerrit.server.securestore.SecureStore.EntryKey;
import com.google.inject.Injector;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -44,11 +36,16 @@ import java.util.Arrays;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SwitchSecureStore extends SiteProgram {
private static String getSecureStoreClassFromGerritConfig(SitePaths sitePaths) {
FileBasedConfig cfg =
new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.DETECTED);
FileBasedConfig cfg = new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.DETECTED);
try {
cfg.load();
} catch (IOException | ConfigInvalidException e) {
@@ -57,12 +54,13 @@ public class SwitchSecureStore extends SiteProgram {
return cfg.getString("gerrit", null, "secureStoreClass");
}
private static final Logger log = LoggerFactory
.getLogger(SwitchSecureStore.class);
private static final Logger log = LoggerFactory.getLogger(SwitchSecureStore.class);
@Option(name = "--new-secure-store-lib",
usage = "Path to new SecureStore implementation",
required = true)
@Option(
name = "--new-secure-store-lib",
usage = "Path to new SecureStore implementation",
required = true
)
private String newSecureStoreLib;
@Override
@@ -70,8 +68,7 @@ public class SwitchSecureStore extends SiteProgram {
SitePaths sitePaths = new SitePaths(getSitePath());
Path newSecureStorePath = Paths.get(newSecureStoreLib);
if (!Files.exists(newSecureStorePath)) {
log.error(String.format("File %s doesn't exist",
newSecureStorePath.toAbsolutePath()));
log.error(String.format("File %s doesn't exist", newSecureStorePath.toAbsolutePath()));
return -1;
}
@@ -79,19 +76,21 @@ public class SwitchSecureStore extends SiteProgram {
String currentSecureStoreName = getCurrentSecureStoreClassName(sitePaths);
if (currentSecureStoreName.equals(newSecureStore)) {
log.error("Old and new SecureStore implementation names "
+ "are the same. Migration will not work");
log.error(
"Old and new SecureStore implementation names "
+ "are the same. Migration will not work");
return -1;
}
IoUtil.loadJARs(newSecureStorePath);
SiteLibraryLoaderUtil.loadSiteLib(sitePaths.lib_dir);
log.info("Current secureStoreClass property ({}) will be replaced with {}",
currentSecureStoreName, newSecureStore);
log.info(
"Current secureStoreClass property ({}) will be replaced with {}",
currentSecureStoreName,
newSecureStore);
Injector dbInjector = createDbInjector(SINGLE_USER);
SecureStore currentStore =
getSecureStore(currentSecureStoreName, dbInjector);
SecureStore currentStore = getSecureStore(currentSecureStoreName, dbInjector);
SecureStore newStore = getSecureStore(newSecureStore, dbInjector);
migrateProperties(currentStore, newStore);
@@ -107,14 +106,11 @@ public class SwitchSecureStore extends SiteProgram {
private void migrateProperties(SecureStore currentStore, SecureStore newStore) {
log.info("Migrate entries");
for (EntryKey key : currentStore.list()) {
String[] value =
currentStore.getList(key.section, key.subsection, key.name);
String[] value = currentStore.getList(key.section, key.subsection, key.name);
if (value != null) {
newStore.setList(key.section, key.subsection, key.name,
Arrays.asList(value));
newStore.setList(key.section, key.subsection, key.name, Arrays.asList(value));
} else {
String msg =
String.format("Cannot migrate entry for %s", key.section);
String msg = String.format("Cannot migrate entry for %s", key.section);
if (key.subsection != null) {
msg = msg + String.format(".%s", key.subsection);
}
@@ -124,57 +120,49 @@ public class SwitchSecureStore extends SiteProgram {
}
}
private void removeOldLib(SitePaths sitePaths, String currentSecureStoreName)
throws IOException {
Path oldSecureStore =
findJarWithSecureStore(sitePaths, currentSecureStoreName);
private void removeOldLib(SitePaths sitePaths, String currentSecureStoreName) throws IOException {
Path oldSecureStore = findJarWithSecureStore(sitePaths, currentSecureStoreName);
if (oldSecureStore != null) {
log.info("Removing old SecureStore ({}) from lib/ directory",
oldSecureStore.getFileName());
log.info("Removing old SecureStore ({}) from lib/ directory", oldSecureStore.getFileName());
try {
Files.delete(oldSecureStore);
} catch (IOException e) {
log.error("Cannot remove {}", oldSecureStore.toAbsolutePath(), e);
}
} else {
log.info("Cannot find jar with old SecureStore ({}) in lib/ directory",
currentSecureStoreName);
log.info(
"Cannot find jar with old SecureStore ({}) in lib/ directory", currentSecureStoreName);
}
}
private void copyNewLib(SitePaths sitePaths, Path newSecureStorePath)
throws IOException {
log.info("Copy new SecureStore ({}) into lib/ directory",
newSecureStorePath.getFileName());
Files.copy(newSecureStorePath,
sitePaths.lib_dir.resolve(newSecureStorePath.getFileName()));
private void copyNewLib(SitePaths sitePaths, Path newSecureStorePath) throws IOException {
log.info("Copy new SecureStore ({}) into lib/ directory", newSecureStorePath.getFileName());
Files.copy(newSecureStorePath, sitePaths.lib_dir.resolve(newSecureStorePath.getFileName()));
}
private void updateGerritConfig(SitePaths sitePaths, String newSecureStore)
throws IOException, ConfigInvalidException {
log.info("Set gerrit.secureStoreClass property of gerrit.config to {}",
newSecureStore);
FileBasedConfig config =
new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.DETECTED);
log.info("Set gerrit.secureStoreClass property of gerrit.config to {}", newSecureStore);
FileBasedConfig config = new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.DETECTED);
config.load();
config.setString("gerrit", null, "secureStoreClass", newSecureStore);
config.save();
}
private String getNewSecureStoreClassName(Path secureStore)
throws IOException {
private String getNewSecureStoreClassName(Path secureStore) throws IOException {
try (JarScanner scanner = new JarScanner(secureStore)) {
List<String> newSecureStores =
scanner.findSubClassesOf(SecureStore.class);
List<String> newSecureStores = scanner.findSubClassesOf(SecureStore.class);
if (newSecureStores.isEmpty()) {
throw new RuntimeException(String.format(
"Cannot find implementation of SecureStore interface in %s",
secureStore.toAbsolutePath()));
throw new RuntimeException(
String.format(
"Cannot find implementation of SecureStore interface in %s",
secureStore.toAbsolutePath()));
}
if (newSecureStores.size() > 1) {
throw new RuntimeException(String.format(
"Found too many implementations of SecureStore:\n%s\nin %s", Joiner
.on("\n").join(newSecureStores), secureStore.toAbsolutePath()));
throw new RuntimeException(
String.format(
"Found too many implementations of SecureStore:\n%s\nin %s",
Joiner.on("\n").join(newSecureStores), secureStore.toAbsolutePath()));
}
return Iterables.getOnlyElement(newSecureStores);
}
@@ -191,8 +179,7 @@ public class SwitchSecureStore extends SiteProgram {
private SecureStore getSecureStore(String className, Injector injector) {
try {
@SuppressWarnings("unchecked")
Class<? extends SecureStore> clazz =
(Class<? extends SecureStore>) Class.forName(className);
Class<? extends SecureStore> clazz = (Class<? extends SecureStore>) Class.forName(className);
return injector.getInstance(clazz);
} catch (ClassNotFoundException e) {
throw new RuntimeException(
@@ -200,8 +187,8 @@ public class SwitchSecureStore extends SiteProgram {
}
}
private Path findJarWithSecureStore(SitePaths sitePaths,
String secureStoreClass) throws IOException {
private Path findJarWithSecureStore(SitePaths sitePaths, String secureStoreClass)
throws IOException {
List<Path> jars = SiteLibraryLoaderUtil.listJars(sitePaths.lib_dir);
String secureStoreClassPath = secureStoreClass.replace('.', '/') + ".class";
for (Path jar : jars) {

View File

@@ -20,7 +20,6 @@ import static com.google.gerrit.pgm.init.InitPlugins.PLUGIN_DIR;
import com.google.gerrit.launcher.GerritLauncher;
import com.google.gerrit.pgm.init.PluginsDistribution;
import com.google.inject.Singleton;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -47,8 +46,7 @@ public class WarDistribution implements PluginsDistribution {
if (ze.getName().startsWith(PLUGIN_DIR) && ze.getName().endsWith(JAR)) {
String pluginJarName = new File(ze.getName()).getName();
String pluginName = pluginJarName.substring(0,
pluginJarName.length() - JAR.length());
String pluginName = pluginJarName.substring(0, pluginJarName.length() - JAR.length());
try (InputStream in = zf.getInputStream(ze)) {
processor.process(pluginName, in);
}

View File

@@ -18,7 +18,10 @@ import static java.nio.charset.StandardCharsets.ISO_8859_1;
import com.google.common.base.Strings;
import com.google.gwtexpui.server.CacheHeaders;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.HttpConnection;
@@ -27,18 +30,13 @@ import org.eclipse.jetty.server.handler.ErrorHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
class HiddenErrorHandler extends ErrorHandler {
private static final Logger log = LoggerFactory.getLogger(HiddenErrorHandler.class);
@Override
public void handle(String target, Request baseRequest,
HttpServletRequest req, HttpServletResponse res) throws IOException {
public void handle(
String target, Request baseRequest, HttpServletRequest req, HttpServletResponse res)
throws IOException {
HttpConnection conn = HttpConnection.getCurrentConnection();
baseRequest.setHandled(true);
try {
@@ -48,11 +46,9 @@ class HiddenErrorHandler extends ErrorHandler {
}
}
private void reply(HttpConnection conn, HttpServletResponse res)
throws IOException {
private void reply(HttpConnection conn, HttpServletResponse res) throws IOException {
byte[] msg = message(conn);
res.setHeader(HttpHeader.CONTENT_TYPE.asString(),
"text/plain; charset=ISO-8859-1");
res.setHeader(HttpHeader.CONTENT_TYPE.asString(), "text/plain; charset=ISO-8859-1");
res.setContentLength(msg.length);
try {
CacheHeaders.setNotCacheable(res);
@@ -70,15 +66,14 @@ class HiddenErrorHandler extends ErrorHandler {
} else {
msg = conn.getHttpChannel().getResponse().getReason();
if (msg == null) {
msg = HttpStatus.getMessage(conn.getHttpChannel()
.getResponse().getStatus());
msg = HttpStatus.getMessage(conn.getHttpChannel().getResponse().getStatus());
}
}
return msg.getBytes(ISO_8859_1);
}
private static void log(HttpServletRequest req) {
Throwable err = (Throwable)req.getAttribute("javax.servlet.error.exception");
Throwable err = (Throwable) req.getAttribute("javax.servlet.error.exception");
if (err != null) {
String uri = req.getRequestURI();
if (!Strings.isNullOrEmpty(req.getQueryString())) {

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.httpd.GetUserFilter;
import com.google.gerrit.server.util.SystemLog;
import com.google.inject.Inject;
import org.apache.log4j.AsyncAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
@@ -55,8 +54,7 @@ class HttpLog extends AbstractLifeCycle implements RequestLog {
}
@Override
protected void doStart() throws Exception {
}
protected void doStart() throws Exception {}
@Override
protected void doStop() throws Exception {
@@ -65,18 +63,19 @@ class HttpLog extends AbstractLifeCycle implements RequestLog {
@Override
public void log(final Request req, final Response rsp) {
final LoggingEvent event = new LoggingEvent( //
Logger.class.getName(), // fqnOfCategoryClass
log, // logger
TimeUtil.nowMs(), // when
Level.INFO, // level
"", // message text
"HTTPD", // thread name
null, // exception information
null, // current NDC string
null, // caller location
null // MDC properties
);
final LoggingEvent event =
new LoggingEvent( //
Logger.class.getName(), // fqnOfCategoryClass
log, // logger
TimeUtil.nowMs(), // when
Level.INFO, // level
"", // message text
"HTTPD", // thread name
null, // exception information
null, // current NDC string
null, // caller location
null // MDC properties
);
String uri = req.getRequestURI();
String qs = req.getQueryString();

View File

@@ -14,12 +14,11 @@
package com.google.gerrit.pgm.http.jetty;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
public final class HttpLogLayout extends Layout {
private final SimpleDateFormat dateFormat;
@@ -116,7 +115,5 @@ public final class HttpLogLayout extends Layout {
}
@Override
public void activateOptions() {
}
public void activateOptions() {}
}

View File

@@ -29,7 +29,18 @@ import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.GuiceServletContextListener;
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Connector;
@@ -59,20 +70,6 @@ import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
@Singleton
public class JettyServer {
private static final Logger log = LoggerFactory.getLogger(JettyServer.class);
@@ -91,14 +88,13 @@ public class JettyServer {
public void start() {
try {
String origUrl = cfg.getString("httpd", null, "listenUrl");
boolean rewrite = !Strings.isNullOrEmpty(origUrl)
&& origUrl.endsWith(":0/");
boolean rewrite = !Strings.isNullOrEmpty(origUrl) && origUrl.endsWith(":0/");
server.httpd.start();
if (rewrite) {
Connector con = server.httpd.getConnectors()[0];
if (con instanceof ServerConnector) {
@SuppressWarnings("resource")
ServerConnector serverCon = (ServerConnector)con;
ServerConnector serverCon = (ServerConnector) con;
String host = serverCon.getHost();
int port = serverCon.getLocalPort();
String url = String.format("http://%s:%d", host, port);
@@ -128,7 +124,8 @@ public class JettyServer {
private boolean reverseProxy;
@Inject
JettyServer(@GerritServerConfig Config cfg,
JettyServer(
@GerritServerConfig Config cfg,
ThreadSettingsConfig threadSettingsConfig,
SitePaths site,
JettyEnv env,
@@ -146,8 +143,7 @@ public class JettyServer {
app = handler;
}
if (cfg.getBoolean("httpd", "registerMBeans", false)) {
MBeanContainer mbean =
new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
MBeanContainer mbean = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
httpd.addEventListener(mbean);
httpd.addBean(Log.getRootLogger());
httpd.addBean(mbean);
@@ -163,8 +159,7 @@ public class JettyServer {
// need to use a larger default header size to ensure we have
// the space required.
//
final int requestHeaderSize =
cfg.getInt("httpd", "requestheadersize", 16386);
final int requestHeaderSize = cfg.getInt("httpd", "requestheadersize", 16386);
final URI[] listenUrls = listenURLs(cfg);
final boolean reuseAddress = cfg.getBoolean("httpd", "reuseaddress", true);
final int acceptors = cfg.getInt("httpd", "acceptorThreads", 2);
@@ -178,11 +173,16 @@ public class JettyServer {
final ServerConnector c;
HttpConfiguration config = defaultConfig(requestHeaderSize);
if (AuthType.CLIENT_SSL_CERT_LDAP.equals(authType) && ! "https".equals(u.getScheme())) {
throw new IllegalArgumentException("Protocol '" + u.getScheme()
+ "' " + " not supported in httpd.listenurl '" + u
+ "' when auth.type = '" + AuthType.CLIENT_SSL_CERT_LDAP.name()
+ "'; only 'https' is supported");
if (AuthType.CLIENT_SSL_CERT_LDAP.equals(authType) && !"https".equals(u.getScheme())) {
throw new IllegalArgumentException(
"Protocol '"
+ u.getScheme()
+ "' "
+ " not supported in httpd.listenurl '"
+ u
+ "' when auth.type = '"
+ AuthType.CLIENT_SSL_CERT_LDAP.name()
+ "'; only 'https' is supported");
}
if ("http".equals(u.getScheme())) {
@@ -214,10 +214,16 @@ public class JettyServer {
defaultPort = 443;
config.addCustomizer(new SecureRequestCustomizer());
c = new ServerConnector(server,
null, null, null, 0, acceptors,
new SslConnectionFactory(ssl, "http/1.1"),
new HttpConnectionFactory(config));
c =
new ServerConnector(
server,
null,
null,
null,
0,
acceptors,
new SslConnectionFactory(ssl, "http/1.1"),
new HttpConnectionFactory(config));
} else if ("proxy-http".equals(u.getScheme())) {
defaultPort = 8080;
@@ -227,32 +233,38 @@ public class JettyServer {
} else if ("proxy-https".equals(u.getScheme())) {
defaultPort = 8080;
config.addCustomizer(new ForwardedRequestCustomizer());
config.addCustomizer(new HttpConfiguration.Customizer() {
@Override
public void customize(Connector connector,
HttpConfiguration channelConfig, Request request) {
request.setScheme(HttpScheme.HTTPS.asString());
request.setSecure(true);
}
});
config.addCustomizer(
new HttpConfiguration.Customizer() {
@Override
public void customize(
Connector connector, HttpConfiguration channelConfig, Request request) {
request.setScheme(HttpScheme.HTTPS.asString());
request.setSecure(true);
}
});
c = newServerConnector(server, acceptors, config);
} else {
throw new IllegalArgumentException("Protocol '" + u.getScheme() + "' "
+ " not supported in httpd.listenurl '" + u + "';"
+ " only 'http', 'https', 'proxy-http, 'proxy-https'"
+ " are supported");
throw new IllegalArgumentException(
"Protocol '"
+ u.getScheme()
+ "' "
+ " not supported in httpd.listenurl '"
+ u
+ "';"
+ " only 'http', 'https', 'proxy-http, 'proxy-https'"
+ " are supported");
}
try {
if (u.getHost() == null && (u.getAuthority().equals("*") //
|| u.getAuthority().startsWith("*:"))) {
if (u.getHost() == null
&& (u.getAuthority().equals("*") //
|| u.getAuthority().startsWith("*:"))) {
// Bind to all local addresses. Port wasn't parsed right by URI
// due to the illegal host of "*" so replace with a legal name
// and parse the URI.
//
final URI r =
new URI(u.toString().replace('*', 'A')).parseServerAuthority();
final URI r = new URI(u.toString().replace('*', 'A')).parseServerAuthority();
c.setHost(null);
c.setPort(0 < r.getPort() ? r.getPort() : defaultPort);
} else {
@@ -270,10 +282,10 @@ public class JettyServer {
return connectors;
}
private static ServerConnector newServerConnector(Server server,
int acceptors, HttpConfiguration config) {
return new ServerConnector(server, null, null, null, 0, acceptors,
new HttpConnectionFactory(config));
private static ServerConnector newServerConnector(
Server server, int acceptors, HttpConfiguration config) {
return new ServerConnector(
server, null, null, null, 0, acceptors, new HttpConnectionFactory(config));
}
private HttpConfiguration defaultConfig(int requestHeaderSize) {
@@ -323,19 +335,18 @@ public class JettyServer {
int maxThreads = threadSettingsConfig.getHttpdMaxThreads();
int minThreads = cfg.getInt("httpd", null, "minthreads", 5);
int maxQueued = cfg.getInt("httpd", null, "maxqueued", 200);
int idleTimeout = (int)MILLISECONDS.convert(60, SECONDS);
int maxCapacity = maxQueued == 0
? Integer.MAX_VALUE
: Math.max(minThreads, maxQueued);
QueuedThreadPool pool = new QueuedThreadPool(
maxThreads,
minThreads,
idleTimeout,
new BlockingArrayQueue<Runnable>(
minThreads, // capacity,
minThreads, // growBy,
maxCapacity // maxCapacity
));
int idleTimeout = (int) MILLISECONDS.convert(60, SECONDS);
int maxCapacity = maxQueued == 0 ? Integer.MAX_VALUE : Math.max(minThreads, maxQueued);
QueuedThreadPool pool =
new QueuedThreadPool(
maxThreads,
minThreads,
idleTimeout,
new BlockingArrayQueue<Runnable>(
minThreads, // capacity,
minThreads, // growBy,
maxCapacity // maxCapacity
));
pool.setName("HTTP");
return pool;
}
@@ -373,8 +384,8 @@ public class JettyServer {
return r;
}
private ContextHandler makeContext(final String contextPath,
final JettyEnv env, final Config cfg) {
private ContextHandler makeContext(
final String contextPath, final JettyEnv env, final Config cfg) {
final ServletContextHandler app = new ServletContextHandler();
// This enables the use of sessions in Jetty, feature available
@@ -398,11 +409,12 @@ public class JettyServer {
Class<? extends Filter> filterClass =
(Class<? extends Filter>) Class.forName(filterClassName);
Filter filter = env.webInjector.getInstance(filterClass);
app.addFilter(new FilterHolder(filter), "/*",
app.addFilter(
new FilterHolder(filter),
"/*",
EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
} catch (Throwable e) {
String errorMessage =
"Unable to instantiate front-end HTTP Filter " + filterClassName;
String errorMessage = "Unable to instantiate front-end HTTP Filter " + filterClassName;
log.error(errorMessage, e);
throw new IllegalArgumentException(errorMessage, e);
}
@@ -413,15 +425,15 @@ public class JettyServer {
// already have built.
//
GuiceFilter filter = env.webInjector.getInstance(GuiceFilter.class);
app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(
DispatcherType.REQUEST,
DispatcherType.ASYNC));
app.addEventListener(new GuiceServletContextListener() {
@Override
protected Injector getInjector() {
return env.webInjector;
}
});
app.addFilter(
new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
app.addEventListener(
new GuiceServletContextListener() {
@Override
protected Injector getInjector() {
return env.webInjector;
}
});
// Jetty requires at least one servlet be bound before it will
// bother running the filter above. Since the filter has all

View File

@@ -29,16 +29,9 @@ import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.servlet.ServletModule;
import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationListener;
import org.eclipse.jetty.continuation.ContinuationSupport;
import org.eclipse.jgit.lib.Config;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
@@ -48,20 +41,23 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationListener;
import org.eclipse.jetty.continuation.ContinuationSupport;
import org.eclipse.jgit.lib.Config;
/**
* Use Jetty continuations to defer execution until threads are available.
* <p>
* We actually schedule a task into the same execution queue as the SSH daemon
* uses for command execution, and then park the web request in a continuation
* until an execution thread is available. This ensures that the overall JVM
* process doesn't exceed the configured limit on concurrent Git requests.
* <p>
* During Git request execution however we have to use the Jetty service thread,
* not the thread from the SSH execution queue. Trying to complete the request
* on the SSH execution queue caused Jetty's HTTP parser to crash, so we instead
* block the SSH execution queue thread and ask Jetty to resume processing on
* the web service thread.
*
* <p>We actually schedule a task into the same execution queue as the SSH daemon uses for command
* execution, and then park the web request in a continuation until an execution thread is
* available. This ensures that the overall JVM process doesn't exceed the configured limit on
* concurrent Git requests.
*
* <p>During Git request execution however we have to use the Jetty service thread, not the thread
* from the SSH execution queue. Trying to complete the request on the SSH execution queue caused
* Jetty's HTTP parser to crash, so we instead block the SSH execution queue thread and ask Jetty to
* resume processing on the web service thread.
*/
@Singleton
public class ProjectQoSFilter implements Filter {
@@ -69,16 +65,14 @@ public class ProjectQoSFilter implements Filter {
private static final String TASK = ATT_SPACE + "/TASK";
private static final String CANCEL = ATT_SPACE + "/CANCEL";
private static final String FILTER_RE =
"^/(.*)/(git-upload-pack|git-receive-pack)$";
private static final String FILTER_RE = "^/(.*)/(git-upload-pack|git-receive-pack)$";
private static final Pattern URI_PATTERN = Pattern.compile(FILTER_RE);
public static class Module extends ServletModule {
@Override
protected void configureServlets() {
bind(QueueProvider.class).to(CommandExecutorQueueProvider.class)
.in(SINGLETON);
bind(QueueProvider.class).to(CommandExecutorQueueProvider.class).in(SINGLETON);
filterRegex(FILTER_RE).through(ProjectQoSFilter.class);
}
}
@@ -90,8 +84,10 @@ public class ProjectQoSFilter implements Filter {
private final long maxWait;
@Inject
ProjectQoSFilter(final Provider<CurrentUser> user,
QueueProvider queue, final ServletContext context,
ProjectQoSFilter(
final Provider<CurrentUser> user,
QueueProvider queue,
final ServletContext context,
@GerritServerConfig final Config cfg) {
this.user = user;
this.queue = queue;
@@ -100,8 +96,8 @@ public class ProjectQoSFilter implements Filter {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse rsp = (HttpServletResponse) response;
final Continuation cont = ContinuationSupport.getContinuation(req);
@@ -145,15 +141,12 @@ public class ProjectQoSFilter implements Filter {
}
@Override
public void init(FilterConfig config) {
}
public void init(FilterConfig config) {}
@Override
public void destroy() {
}
public void destroy() {}
private final class TaskThunk implements CancelableRunnable,
ContinuationListener {
private final class TaskThunk implements CancelableRunnable, ContinuationListener {
private final WorkQueue.Executor executor;
private final Continuation cont;
@@ -163,8 +156,8 @@ public class ProjectQoSFilter implements Filter {
private Thread worker;
private String fullName;
TaskThunk(final WorkQueue.Executor executor, final Continuation cont,
final HttpServletRequest req) {
TaskThunk(
final WorkQueue.Executor executor, final Continuation cont, final HttpServletRequest req) {
this.executor = executor;
this.cont = cont;
this.name = generateName(req);
@@ -210,8 +203,7 @@ public class ProjectQoSFilter implements Filter {
}
@Override
public void onComplete(Continuation self) {
}
public void onComplete(Continuation self) {}
@Override
public void onTimeout(Continuation self) {

View File

@@ -27,8 +27,7 @@ public class AllUsersNameOnInitProvider implements Provider<String> {
@Inject
AllUsersNameOnInitProvider(Section.Factory sections) {
String n = sections.get("gerrit", null).get("allUsers");
name = MoreObjects.firstNonNull(
Strings.emptyToNull(n), AllUsersNameProvider.DEFAULT);
name = MoreObjects.firstNonNull(Strings.emptyToNull(n), AllUsersNameProvider.DEFAULT);
}
@Override

View File

@@ -61,10 +61,6 @@ import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.Message;
import com.google.inject.util.Providers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileVisitResult;
@@ -77,13 +73,13 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Initialize a new Gerrit installation. */
public class BaseInit extends SiteProgram {
private static final Logger log =
LoggerFactory.getLogger(BaseInit.class);
private static final Logger log = LoggerFactory.getLogger(BaseInit.class);
private final boolean standalone;
private final boolean initDb;
@@ -92,22 +88,29 @@ public class BaseInit extends SiteProgram {
private Injector sysInjector;
protected BaseInit(PluginsDistribution pluginsDistribution,
List<String> pluginsToInstall) {
protected BaseInit(PluginsDistribution pluginsDistribution, List<String> pluginsToInstall) {
this.standalone = true;
this.initDb = true;
this.pluginsDistribution = pluginsDistribution;
this.pluginsToInstall = pluginsToInstall;
}
public BaseInit(Path sitePath, boolean standalone, boolean initDb,
PluginsDistribution pluginsDistribution, List<String> pluginsToInstall) {
public BaseInit(
Path sitePath,
boolean standalone,
boolean initDb,
PluginsDistribution pluginsDistribution,
List<String> pluginsToInstall) {
this(sitePath, null, standalone, initDb, pluginsDistribution, pluginsToInstall);
}
public BaseInit(Path sitePath, final Provider<DataSource> dsProvider,
boolean standalone, boolean initDb,
PluginsDistribution pluginsDistribution, List<String> pluginsToInstall) {
public BaseInit(
Path sitePath,
final Provider<DataSource> dsProvider,
boolean standalone,
boolean initDb,
PluginsDistribution pluginsDistribution,
List<String> pluginsToInstall) {
super(sitePath, dsProvider);
this.standalone = standalone;
this.initDb = initDb;
@@ -127,15 +130,13 @@ public class BaseInit extends SiteProgram {
init.flags.skipPlugins = skipPlugins();
init.flags.deleteCaches = getDeleteCaches();
final SiteRun run;
try {
init.initializer.run();
init.flags.deleteOnFailure = false;
Injector sysInjector = createSysInjector(init);
IndexManagerOnInit indexManager =
sysInjector.getInstance(IndexManagerOnInit.class);
IndexManagerOnInit indexManager = sysInjector.getInstance(IndexManagerOnInit.class);
try {
indexManager.start();
run = createSiteRun(init);
@@ -189,8 +190,7 @@ public class BaseInit extends SiteProgram {
* @param run completed run instance.
* @throws Exception
*/
protected void afterInit(SiteRun run) throws Exception {
}
protected void afterInit(SiteRun run) throws Exception {}
protected List<String> getInstallPlugins() {
try {
@@ -199,7 +199,7 @@ public class BaseInit extends SiteProgram {
}
List<String> names = pluginsDistribution.listPluginNames();
if (pluginsToInstall != null) {
for (Iterator<String> i = names.iterator(); i.hasNext();) {
for (Iterator<String> i = names.iterator(); i.hasNext(); ) {
String n = i.next();
if (!pluginsToInstall.contains(n)) {
i.remove();
@@ -208,8 +208,7 @@ public class BaseInit extends SiteProgram {
}
return names;
} catch (FileNotFoundException e) {
log.warn("Couldn't find distribution archive location."
+ " No plugin will be installed");
log.warn("Couldn't find distribution archive location." + " No plugin will be installed");
return null;
}
}
@@ -229,7 +228,10 @@ public class BaseInit extends SiteProgram {
final SitePathInitializer initializer;
@Inject
SiteInit(final SitePaths site, final InitFlags flags, final ConsoleUI ui,
SiteInit(
final SitePaths site,
final InitFlags flags,
final ConsoleUI ui,
final SitePathInitializer initializer) {
this.site = site;
this.flags = flags;
@@ -245,53 +247,57 @@ public class BaseInit extends SiteProgram {
final SecureStoreInitData secureStoreInitData = discoverSecureStoreClass();
final String currentSecureStoreClassName = getConfiguredSecureStoreClass();
if (secureStoreInitData != null && currentSecureStoreClassName != null
if (secureStoreInitData != null
&& currentSecureStoreClassName != null
&& !currentSecureStoreClassName.equals(secureStoreInitData.className)) {
String err = String.format(
"Different secure store was previously configured: %s. "
+ "Use SwitchSecureStore program to switch between implementations.",
currentSecureStoreClassName);
String err =
String.format(
"Different secure store was previously configured: %s. "
+ "Use SwitchSecureStore program to switch between implementations.",
currentSecureStoreClassName);
throw die(err);
}
m.add(new GerritServerConfigModule());
m.add(new InitModule(standalone, initDb));
m.add(new AbstractModule() {
@Override
protected void configure() {
bind(ConsoleUI.class).toInstance(ui);
bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
List<String> plugins =
MoreObjects.firstNonNull(
getInstallPlugins(), new ArrayList<String>());
bind(new TypeLiteral<List<String>>() {}).annotatedWith(
InstallPlugins.class).toInstance(plugins);
bind(new TypeLiteral<Boolean>() {}).annotatedWith(
InstallAllPlugins.class).toInstance(installAllPlugins());
bind(PluginsDistribution.class).toInstance(pluginsDistribution);
m.add(
new AbstractModule() {
@Override
protected void configure() {
bind(ConsoleUI.class).toInstance(ui);
bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
List<String> plugins =
MoreObjects.firstNonNull(getInstallPlugins(), new ArrayList<String>());
bind(new TypeLiteral<List<String>>() {})
.annotatedWith(InstallPlugins.class)
.toInstance(plugins);
bind(new TypeLiteral<Boolean>() {})
.annotatedWith(InstallAllPlugins.class)
.toInstance(installAllPlugins());
bind(PluginsDistribution.class).toInstance(pluginsDistribution);
String secureStoreClassName;
if (secureStoreInitData != null) {
secureStoreClassName = secureStoreInitData.className;
} else {
secureStoreClassName = currentSecureStoreClassName;
}
if (secureStoreClassName != null) {
ui.message("Using secure store: %s\n", secureStoreClassName);
}
bind(SecureStoreInitData.class).toProvider(
Providers.of(secureStoreInitData));
bind(String.class).annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(secureStoreClassName));
bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
bind(new TypeLiteral<List<String>>() {}).annotatedWith(
LibraryDownload.class).toInstance(getSkippedDownloads());
bind(Boolean.class).annotatedWith(
LibraryDownload.class).toInstance(skipAllDownloads());
String secureStoreClassName;
if (secureStoreInitData != null) {
secureStoreClassName = secureStoreInitData.className;
} else {
secureStoreClassName = currentSecureStoreClassName;
}
if (secureStoreClassName != null) {
ui.message("Using secure store: %s\n", secureStoreClassName);
}
bind(SecureStoreInitData.class).toProvider(Providers.of(secureStoreInitData));
bind(String.class)
.annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(secureStoreClassName));
bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
bind(new TypeLiteral<List<String>>() {})
.annotatedWith(LibraryDownload.class)
.toInstance(getSkippedDownloads());
bind(Boolean.class).annotatedWith(LibraryDownload.class).toInstance(skipAllDownloads());
bind(MetricMaker.class).to(DisabledMetricMaker.class);
}
});
bind(MetricMaker.class).to(DisabledMetricMaker.class);
}
});
try {
return Guice.createInjector(PRODUCTION, m).getInstance(SiteInit.class);
@@ -328,27 +334,26 @@ public class BaseInit extends SiteProgram {
Path secureStoreLib = Paths.get(secureStore);
if (!Files.exists(secureStoreLib)) {
throw new InvalidSecureStoreException(String.format(
"File %s doesn't exist", secureStore));
throw new InvalidSecureStoreException(String.format("File %s doesn't exist", secureStore));
}
try (JarScanner scanner = new JarScanner(secureStoreLib)) {
List<String> secureStores =
scanner.findSubClassesOf(SecureStore.class);
List<String> secureStores = scanner.findSubClassesOf(SecureStore.class);
if (secureStores.isEmpty()) {
throw new InvalidSecureStoreException(String.format(
"Cannot find class implementing %s interface in %s",
SecureStore.class.getName(), secureStore));
throw new InvalidSecureStoreException(
String.format(
"Cannot find class implementing %s interface in %s",
SecureStore.class.getName(), secureStore));
}
if (secureStores.size() > 1) {
throw new InvalidSecureStoreException(String.format(
"%s has more that one implementation of %s interface",
secureStore, SecureStore.class.getName()));
throw new InvalidSecureStoreException(
String.format(
"%s has more that one implementation of %s interface",
secureStore, SecureStore.class.getName()));
}
IoUtil.loadJARs(secureStoreLib);
return new SecureStoreInitData(secureStoreLib, secureStores.get(0));
} catch (IOException e) {
throw new InvalidSecureStoreException(String.format("%s is not a valid jar",
secureStore));
throw new InvalidSecureStoreException(String.format("%s is not a valid jar", secureStore));
}
}
@@ -361,7 +366,8 @@ public class BaseInit extends SiteProgram {
final GitRepositoryManager repositoryManager;
@Inject
SiteRun(ConsoleUI ui,
SiteRun(
ConsoleUI ui,
SitePaths site,
InitFlags flags,
SchemaUpdater schemaUpdater,
@@ -377,32 +383,33 @@ public class BaseInit extends SiteProgram {
void upgradeSchema() throws OrmException {
final List<String> pruneList = new ArrayList<>();
schemaUpdater.update(new UpdateUI() {
@Override
public void message(String msg) {
System.err.println(msg);
System.err.flush();
}
@Override
public boolean yesno(boolean def, String msg) {
return ui.yesno(def, msg);
}
@Override
public boolean isBatch() {
return ui.isBatch();
}
@Override
public void pruneSchema(StatementExecutor e, List<String> prune) {
for (String p : prune) {
if (!pruneList.contains(p)) {
pruneList.add(p);
schemaUpdater.update(
new UpdateUI() {
@Override
public void message(String msg) {
System.err.println(msg);
System.err.flush();
}
}
}
});
@Override
public boolean yesno(boolean def, String msg) {
return ui.yesno(def, msg);
}
@Override
public boolean isBatch() {
return ui.isBatch();
}
@Override
public void pruneSchema(StatementExecutor e, List<String> prune) {
for (String p : prune) {
if (!pruneList.contains(p)) {
pruneList.add(p);
}
}
}
});
if (!pruneList.isEmpty()) {
StringBuilder msg = new StringBuilder();
@@ -437,13 +444,14 @@ public class BaseInit extends SiteProgram {
private Injector createSysInjector(final SiteInit init) {
if (sysInjector == null) {
final List<Module> modules = new ArrayList<>();
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(ConsoleUI.class).toInstance(init.ui);
bind(InitFlags.class).toInstance(init.flags);
}
});
modules.add(
new AbstractModule() {
@Override
protected void configure() {
bind(ConsoleUI.class).toInstance(init.ui);
bind(InitFlags.class).toInstance(init.flags);
}
});
Injector dbInjector = createDbInjector(SINGLE_USER);
switch (IndexModule.getIndexType(dbInjector)) {
case LUCENE:
@@ -463,36 +471,37 @@ public class BaseInit extends SiteProgram {
private static void recursiveDelete(Path path) {
final String msg = "warn: Cannot remove ";
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path f, BasicFileAttributes attrs)
throws IOException {
try {
Files.delete(f);
} catch (IOException e) {
System.err.println(msg + f);
}
return FileVisitResult.CONTINUE;
}
Files.walkFileTree(
path,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path f, BasicFileAttributes attrs) throws IOException {
try {
Files.delete(f);
} catch (IOException e) {
System.err.println(msg + f);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException err) {
try {
// Previously warned if err was not null; if dir is not empty as a
// result, will cause an error that will be logged below.
Files.delete(dir);
} catch (IOException e) {
System.err.println(msg + dir);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException err) {
try {
// Previously warned if err was not null; if dir is not empty as a
// result, will cause an error that will be logged below.
Files.delete(dir);
} catch (IOException e) {
System.err.println(msg + dir);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path f, IOException e) {
System.err.println(msg + f);
return FileVisitResult.CONTINUE;
}
});
@Override
public FileVisitResult visitFileFailed(Path f, IOException e) {
System.err.println(msg + f);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
System.err.println(msg + path);
}

View File

@@ -18,13 +18,11 @@ import com.google.common.base.Strings;
import com.google.gerrit.pgm.init.api.InitUtil;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import org.eclipse.jgit.lib.Config;
import java.io.IOException;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.jgit.lib.Config;
/** Opens the user's web browser to the web UI. */
public class Browser {
@@ -67,7 +65,7 @@ public class Browser {
int port = InitUtil.portOf(uri);
System.err.format("Waiting for server on %s:%d ... ", host, port);
System.err.flush();
for (;;) {
for (; ; ) {
Socket s;
try {
s = new Socket(host, port);

View File

@@ -18,7 +18,6 @@ import static com.google.gerrit.pgm.init.api.InitUtil.username;
import com.google.gerrit.pgm.init.api.Section;
public class DB2Initializer implements DatabaseConfigInitializer {
@Override

View File

@@ -20,8 +20,8 @@ import com.google.gerrit.pgm.init.api.Section;
interface DatabaseConfigInitializer {
/**
* Performs database platform specific configuration steps and writes
* configuration parameters into the given database section
* Performs database platform specific configuration steps and writes configuration parameters
* into the given database section
*/
void initConfig(Section databaseSection);
}

View File

@@ -29,23 +29,30 @@ public class DatabaseConfigModule extends AbstractModule {
@Override
protected void configure() {
bind(SitePaths.class).toInstance(site);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("db2")).to(DB2Initializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("derby")).to(DerbyInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("h2")).to(H2Initializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("jdbc")).to(JDBCInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("mysql")).to(MySqlInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("oracle")).to(OracleInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("postgresql")).to(PostgreSQLInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("maxdb")).to(MaxDbInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(
Names.named("hana")).to(HANAInitializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("db2"))
.to(DB2Initializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("derby"))
.to(DerbyInitializer.class);
bind(DatabaseConfigInitializer.class).annotatedWith(Names.named("h2")).to(H2Initializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("jdbc"))
.to(JDBCInitializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("mysql"))
.to(MySqlInitializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("oracle"))
.to(OracleInitializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("postgresql"))
.to(PostgreSQLInitializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("maxdb"))
.to(MaxDbInitializer.class);
bind(DatabaseConfigInitializer.class)
.annotatedWith(Names.named("hana"))
.to(HANAInitializer.class);
}
}

View File

@@ -20,7 +20,6 @@ import com.google.gerrit.common.FileUtil;
import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import java.nio.file.Path;
class DerbyInitializer implements DatabaseConfigInitializer {

View File

@@ -20,7 +20,6 @@ import com.google.gerrit.common.FileUtil;
import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import java.nio.file.Path;
class H2Initializer implements DatabaseConfigInitializer {

View File

@@ -26,8 +26,7 @@ public class HANAInitializer implements DatabaseConfigInitializer {
public void initConfig(Section databaseSection) {
final String defInstanceNumber = "00";
databaseSection.string("Server hostname", "hostname", "localhost");
databaseSection.string("Instance number", "instance", defInstanceNumber,
false);
databaseSection.string("Instance number", "instance", defInstanceNumber, false);
String instance = databaseSection.get("instance");
Integer instanceNumber = Ints.tryParse(instance);
if (instanceNumber == null || instanceNumber < 0 || instanceNumber > 99) {

View File

@@ -34,9 +34,6 @@ import com.google.gerrit.server.index.account.AccountIndex;
import com.google.gerrit.server.index.account.AccountIndexCollection;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import org.apache.commons.validator.routines.EmailValidator;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -45,6 +42,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.validator.routines.EmailValidator;
public class InitAdminUser implements InitStep {
private final ConsoleUI ui;
@@ -55,17 +53,14 @@ public class InitAdminUser implements InitStep {
@Inject
InitAdminUser(
InitFlags flags,
ConsoleUI ui,
VersionedAuthorizedKeysOnInit.Factory authorizedKeysFactory) {
InitFlags flags, ConsoleUI ui, VersionedAuthorizedKeysOnInit.Factory authorizedKeysFactory) {
this.flags = flags;
this.ui = ui;
this.authorizedKeysFactory = authorizedKeysFactory;
}
@Override
public void run() {
}
public void run() {}
@Inject(optional = true)
void set(SchemaFactory<ReviewDb> dbFactory) {
@@ -79,8 +74,7 @@ public class InitAdminUser implements InitStep {
@Override
public void postRun() throws Exception {
AuthType authType =
flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
AuthType authType = flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
if (authType != AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT) {
return;
}
@@ -98,8 +92,8 @@ public class InitAdminUser implements InitStep {
List<AccountExternalId> extIds = new ArrayList<>(2);
AccountExternalId extUser =
new AccountExternalId(id, new AccountExternalId.Key(
AccountExternalId.SCHEME_USERNAME, username));
new AccountExternalId(
id, new AccountExternalId.Key(AccountExternalId.SCHEME_USERNAME, username));
if (!Strings.isNullOrEmpty(httpPassword)) {
extUser.setPassword(httpPassword);
}
@@ -108,8 +102,8 @@ public class InitAdminUser implements InitStep {
if (email != null) {
AccountExternalId extMailto =
new AccountExternalId(id, new AccountExternalId.Key(
AccountExternalId.SCHEME_MAILTO, email));
new AccountExternalId(
id, new AccountExternalId.Key(AccountExternalId.SCHEME_MAILTO, email));
extMailto.setEmailAddress(email);
extIds.add(extMailto);
db.accountExternalIds().insert(Collections.singleton(extMailto));
@@ -120,25 +114,22 @@ public class InitAdminUser implements InitStep {
a.setPreferredEmail(email);
db.accounts().insert(Collections.singleton(a));
AccountGroupName adminGroupName = db.accountGroupNames().get(
new AccountGroup.NameKey("Administrators"));
AccountGroupName adminGroupName =
db.accountGroupNames().get(new AccountGroup.NameKey("Administrators"));
AccountGroupMember m =
new AccountGroupMember(new AccountGroupMember.Key(id,
adminGroupName.getId()));
new AccountGroupMember(new AccountGroupMember.Key(id, adminGroupName.getId()));
db.accountGroupMembers().insert(Collections.singleton(m));
if (sshKey != null) {
VersionedAuthorizedKeysOnInit authorizedKeys =
authorizedKeysFactory.create(id).load();
VersionedAuthorizedKeysOnInit authorizedKeys = authorizedKeysFactory.create(id).load();
authorizedKeys.addKey(sshKey.getSshPublicKey());
authorizedKeys.save("Added SSH key for initial admin user\n");
}
AccountGroup adminGroup =
db.accountGroups().get(adminGroupName.getId());
AccountState as = new AccountState(a,
Collections.singleton(adminGroup.getGroupUUID()), extIds,
new HashMap<>());
AccountGroup adminGroup = db.accountGroups().get(adminGroupName.getId());
AccountState as =
new AccountState(
a, Collections.singleton(adminGroup.getGroupUUID()), extIds, new HashMap<>());
for (AccountIndex accountIndex : indexCollection.getWriteIndexes()) {
accountIndex.replace(as);
}
@@ -150,10 +141,10 @@ public class InitAdminUser implements InitStep {
private String readEmail(AccountSshKey sshKey) {
String defaultEmail = "admin@example.com";
if (sshKey != null && sshKey.getComment() != null) {
String c = sshKey.getComment().trim();
if (EmailValidator.getInstance().isValid(c)) {
defaultEmail = c;
}
String c = sshKey.getComment().trim();
if (EmailValidator.getInstance().isValid(c)) {
defaultEmail = c;
}
}
return readEmail(defaultEmail);
}
@@ -169,23 +160,18 @@ public class InitAdminUser implements InitStep {
private AccountSshKey readSshKey(Account.Id id) throws IOException {
String defaultPublicSshKeyFile = "";
Path defaultPublicSshKeyPath =
Paths.get(System.getProperty("user.home"), ".ssh", "id_rsa.pub");
Path defaultPublicSshKeyPath = Paths.get(System.getProperty("user.home"), ".ssh", "id_rsa.pub");
if (Files.exists(defaultPublicSshKeyPath)) {
defaultPublicSshKeyFile = defaultPublicSshKeyPath.toString();
}
String publicSshKeyFile =
ui.readString(defaultPublicSshKeyFile, "public SSH key file");
return !Strings.isNullOrEmpty(publicSshKeyFile)
? createSshKey(id, publicSshKeyFile) : null;
String publicSshKeyFile = ui.readString(defaultPublicSshKeyFile, "public SSH key file");
return !Strings.isNullOrEmpty(publicSshKeyFile) ? createSshKey(id, publicSshKeyFile) : null;
}
private AccountSshKey createSshKey(Account.Id id, String keyFile)
throws IOException {
private AccountSshKey createSshKey(Account.Id id, String keyFile) throws IOException {
Path p = Paths.get(keyFile);
if (!Files.exists(p)) {
throw new IOException(String.format(
"Cannot add public SSH key: %s is not a file", keyFile));
throw new IOException(String.format("Cannot add public SSH key: %s is not a file", keyFile));
}
String content = new String(Files.readAllBytes(p), UTF_8);
return new AccountSshKey(new AccountSshKey.Id(id, 1), content);

View File

@@ -39,10 +39,7 @@ class InitAuth implements InitStep {
private final InitFlags flags;
@Inject
InitAuth(InitFlags flags,
ConsoleUI ui,
Libraries libraries,
Section.Factory sections) {
InitAuth(InitFlags flags, ConsoleUI ui, Libraries libraries, Section.Factory sections) {
this.flags = flags;
this.ui = ui;
this.auth = sections.get("auth", null);
@@ -64,20 +61,24 @@ class InitAuth implements InitStep {
}
private void initAuthType() {
AuthType authType = auth.select("Authentication method", "type",
flags.dev ? AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT : AuthType.OPENID);
AuthType authType =
auth.select(
"Authentication method",
"type",
flags.dev ? AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT : AuthType.OPENID);
switch (authType) {
case HTTP:
case HTTP_LDAP: {
String hdr = auth.get("httpHeader");
if (ui.yesno(hdr != null, "Get username from custom HTTP header")) {
auth.string("Username HTTP header", "httpHeader", "SM_USER");
} else if (hdr != null) {
auth.unset("httpHeader");
case HTTP_LDAP:
{
String hdr = auth.get("httpHeader");
if (ui.yesno(hdr != null, "Get username from custom HTTP header")) {
auth.string("Username HTTP header", "httpHeader", "SM_USER");
} else if (hdr != null) {
auth.unset("httpHeader");
}
auth.string("SSO logout URL", "logoutUrl", null);
break;
}
auth.string("SSO logout URL", "logoutUrl", null);
break;
}
case CLIENT_SSL_CERT_LDAP:
case CUSTOM_EXTENSION:
@@ -93,28 +94,28 @@ class InitAuth implements InitStep {
switch (authType) {
case LDAP:
case LDAP_BIND:
case HTTP_LDAP: {
String server =
ldap.string("LDAP server", "server", "ldap://localhost");
if (server != null //
&& !server.startsWith("ldap://") //
&& !server.startsWith("ldaps://")) {
if (ui.yesno(false, "Use SSL")) {
server = "ldaps://" + server;
} else {
server = "ldap://" + server;
case HTTP_LDAP:
{
String server = ldap.string("LDAP server", "server", "ldap://localhost");
if (server != null //
&& !server.startsWith("ldap://") //
&& !server.startsWith("ldaps://")) {
if (ui.yesno(false, "Use SSL")) {
server = "ldaps://" + server;
} else {
server = "ldap://" + server;
}
ldap.set("server", server);
}
ldap.set("server", server);
ldap.string("LDAP username", "username", null);
ldap.password("username", "password");
String aBase = ldap.string("Account BaseDN", "accountBase", dnOf(server));
ldap.string("Group BaseDN", "groupBase", aBase);
break;
}
ldap.string("LDAP username", "username", null);
ldap.password("username", "password");
String aBase = ldap.string("Account BaseDN", "accountBase", dnOf(server));
ldap.string("Group BaseDN", "groupBase", aBase);
break;
}
case CLIENT_SSL_CERT_LDAP:
case CUSTOM_EXTENSION:
case DEVELOPMENT_BECOME_ANY_ACCOUNT:

View File

@@ -22,7 +22,6 @@ import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
@@ -39,8 +38,11 @@ class InitCache implements InitStep {
private final Section cache;
@Inject
InitCache(final ConsoleUI ui, final InitFlags flags,
final SitePaths site, final Section.Factory sections) {
InitCache(
final ConsoleUI ui,
final InitFlags flags,
final SitePaths site,
final Section.Factory sections) {
this.ui = ui;
this.flags = flags;
this.site = site;
@@ -67,8 +69,7 @@ class InitCache implements InitStep {
Path loc = site.resolve(path);
FileUtil.mkdirsOrDie(loc, "cannot create cache.directory");
List<Path> cacheFiles = new ArrayList<>();
try (DirectoryStream<Path> stream =
Files.newDirectoryStream(loc, "*.{lock,h2,trace}.db")) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(loc, "*.{lock,h2,trace}.db")) {
for (Path entry : stream) {
cacheFiles.add(entry);
}
@@ -78,8 +79,7 @@ class InitCache implements InitStep {
}
if (!cacheFiles.isEmpty()) {
for (Path entry : cacheFiles) {
if (flags.deleteCaches ||
ui.yesno(false, "Delete cache file %s", entry)) {
if (flags.deleteCaches || ui.yesno(false, "Delete cache file %s", entry)) {
try {
Files.deleteIfExists(entry);
} catch (IOException e) {

View File

@@ -25,15 +25,13 @@ import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.internal.storage.file.LockFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jgit.internal.storage.file.LockFile;
/** Initialize the {@code container} configuration section. */
@Singleton
@@ -43,8 +41,7 @@ class InitContainer implements InitStep {
private final Section container;
@Inject
InitContainer(final ConsoleUI ui, final SitePaths site,
final Section.Factory sections) {
InitContainer(final ConsoleUI ui, final SitePaths site, final Section.Factory sections) {
this.ui = ui;
this.site = site;
this.container = sections.get("container", null);
@@ -67,8 +64,9 @@ class InitContainer implements InitStep {
String path = container.get("war");
if (path != null) {
path = container.string("Gerrit runtime", "war",
myWar != null ? myWar.toAbsolutePath().toString() : null);
path =
container.string(
"Gerrit runtime", "war", myWar != null ? myWar.toAbsolutePath().toString() : null);
if (path == null || path.isEmpty()) {
throw die("container.war is required");
}

View File

@@ -32,7 +32,6 @@ import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Set;
@@ -47,7 +46,10 @@ class InitDatabase implements InitStep {
private final Section idSection;
@Inject
InitDatabase(final ConsoleUI ui, final SitePaths site, final Libraries libraries,
InitDatabase(
final ConsoleUI ui,
final SitePaths site,
final Libraries libraries,
final Section.Factory sections) {
this.ui = ui;
this.site = site;
@@ -76,12 +78,10 @@ class InitDatabase implements InitStep {
database.set("type", "jdbc");
}
String dbType =
database.select("Database server type", "type", "h2", allowedValues);
String dbType = database.select("Database server type", "type", "h2", allowedValues);
DatabaseConfigInitializer dci =
i.getInstance(Key.get(DatabaseConfigInitializer.class,
Names.named(dbType.toLowerCase())));
i.getInstance(Key.get(DatabaseConfigInitializer.class, Names.named(dbType.toLowerCase())));
if (dci instanceof MySqlInitializer) {
libraries.mysqlDriver.downloadRequired();
@@ -98,8 +98,7 @@ class InitDatabase implements InitStep {
// Initialize UUID for NoteDb on first init.
String id = idSection.get(GerritServerIdProvider.KEY);
if (Strings.isNullOrEmpty(id)) {
idSection.set(
GerritServerIdProvider.KEY, GerritServerIdProvider.generate());
idSection.set(GerritServerIdProvider.KEY, GerritServerIdProvider.generate());
}
}
}

View File

@@ -26,8 +26,7 @@ public class InitDev implements InitStep {
private final Section plugins;
@Inject
InitDev(InitFlags flags,
Section.Factory sections) {
InitDev(InitFlags flags, Section.Factory sections) {
this.flags = flags;
this.plugins = sections.get("plugins", null);
}

View File

@@ -22,7 +22,6 @@ import com.google.gerrit.pgm.init.api.InitStep;
import com.google.gerrit.pgm.init.api.Section;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.nio.file.Path;
/** Initialize the GitRepositoryManager configuration section. */

View File

@@ -28,7 +28,6 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gwtjsonrpc.server.SignedToken;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -45,7 +44,10 @@ class InitHttpd implements InitStep {
private final Section gerrit;
@Inject
InitHttpd(final ConsoleUI ui, final SitePaths site, final InitFlags flags,
InitHttpd(
final ConsoleUI ui,
final SitePaths site,
final InitFlags flags,
final Section.Factory sections) {
this.ui = ui;
this.site = site;
@@ -129,8 +131,7 @@ class InitHttpd implements InitStep {
generateSslCertificate();
}
private void generateSslCertificate() throws IOException,
InterruptedException {
private void generateSslCertificate() throws IOException, InterruptedException {
final String listenUrl = httpd.get("listenUrl");
if (!listenUrl.startsWith("https://")) {
@@ -152,8 +153,7 @@ class InitHttpd implements InitStep {
}
Path store = site.ssl_keystore;
if (!ui.yesno(!Files.exists(store),
"Create new self-signed SSL certificate")) {
if (!ui.yesno(!Files.exists(store), "Create new self-signed SSL certificate")) {
return;
}
@@ -164,11 +164,9 @@ class InitHttpd implements InitStep {
}
hostname = ui.readString(hostname, "Certificate server name");
final String validity =
ui.readString("365", "Certificate expires in (days)");
final String validity = ui.readString("365", "Certificate expires in (days)");
final String dname =
"CN=" + hostname + ",OU=Gerrit Code Review,O=" + domainOf(hostname);
final String dname = "CN=" + hostname + ",OU=Gerrit Code Review,O=" + domainOf(hostname);
Path tmpdir = site.etc_dir.resolve("tmp.sslcertgen");
try {
@@ -179,16 +177,27 @@ class InitHttpd implements InitStep {
chmod(0600, tmpdir);
Path tmpstore = tmpdir.resolve("keystore");
Runtime.getRuntime().exec(new String[] {"keytool", //
"-keystore", tmpstore.toAbsolutePath().toString(), //
"-storepass", ssl_pass, //
"-genkeypair", //
"-alias", hostname, //
"-keyalg", "RSA", //
"-validity", validity, //
"-dname", dname, //
"-keypass", ssl_pass, //
}).waitFor();
Runtime.getRuntime()
.exec(
new String[] {
"keytool", //
"-keystore",
tmpstore.toAbsolutePath().toString(), //
"-storepass",
ssl_pass, //
"-genkeypair", //
"-alias",
hostname, //
"-keyalg",
"RSA", //
"-validity",
validity, //
"-dname",
dname, //
"-keypass",
ssl_pass, //
})
.waitFor();
chmod(0600, tmpstore);
try {

View File

@@ -27,7 +27,6 @@ import com.google.gerrit.server.index.IndexUtils;
import com.google.gerrit.server.index.SchemaDefinitions;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
@@ -43,10 +42,7 @@ class InitIndex implements InitStep {
private final Section gerrit;
@Inject
InitIndex(ConsoleUI ui,
Section.Factory sections,
SitePaths site,
InitFlags initFlags) {
InitIndex(ConsoleUI ui, Section.Factory sections, SitePaths site, InitFlags initFlags) {
this.ui = ui;
this.index = sections.get("index", null);
this.gerrit = sections.get("gerrit", null);
@@ -63,8 +59,7 @@ class InitIndex implements InitStep {
}
if (type == IndexType.ELASTICSEARCH) {
index.select("Transport protocol", "protocol", "http",
Sets.newHashSet("http", "https"));
index.select("Transport protocol", "protocol", "http", Sets.newHashSet("http", "https"));
index.string("Hostname", "hostname", "localhost");
index.string("Port", "port", "9200");
index.string("Index Name", "name", "gerrit");
@@ -72,17 +67,17 @@ class InitIndex implements InitStep {
if ((site.isNew || isEmptySite()) && type == IndexType.LUCENE) {
for (SchemaDefinitions<?> def : IndexModule.ALL_SCHEMA_DEFS) {
IndexUtils.setReady(
site, def.getName(), def.getLatest().getVersion(), true);
IndexUtils.setReady(site, def.getName(), def.getLatest().getVersion(), true);
}
} else {
if (IndexType.values().length <= 1) {
ui.header("Index");
}
String message = String.format(
"\nThe index must be %sbuilt before starting Gerrit:\n"
+ " java -jar gerrit.war reindex -d site_path\n",
site.isNew ? "" : "re");
String message =
String.format(
"\nThe index must be %sbuilt before starting Gerrit:\n"
+ " java -jar gerrit.war reindex -d site_path\n",
site.isNew ? "" : "re");
ui.message(message);
initFlags.autoStart = false;
}

View File

@@ -19,15 +19,12 @@ import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.pgm.init.api.InitStep;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
import java.util.Arrays;
import org.eclipse.jgit.lib.Config;
@Singleton
public class InitLabels implements InitStep {
private static final String KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE =
"copyAllScoresIfNoCodeChange";
private static final String KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE = "copyAllScoresIfNoCodeChange";
private static final String KEY_LABEL = "label";
private static final String KEY_FUNCTION = "function";
private static final String KEY_VALUE = "value";
@@ -58,7 +55,10 @@ public class InitLabels implements InitStep {
Config cfg = allProjectsConfig.load().getConfig();
if (installVerified) {
cfg.setString(KEY_LABEL, LABEL_VERIFIED, KEY_FUNCTION, "MaxWithBlock");
cfg.setStringList(KEY_LABEL, LABEL_VERIFIED, KEY_VALUE,
cfg.setStringList(
KEY_LABEL,
LABEL_VERIFIED,
KEY_VALUE,
Arrays.asList(new String[] {"-1 Fails", " 0 No score", "+1 Verified"}));
cfg.setBoolean(KEY_LABEL, LABEL_VERIFIED, KEY_COPY_ALL_SCORES_IF_NO_CODE_CHANGE, true);
allProjectsConfig.save("Configure 'Verified' label");

View File

@@ -21,7 +21,6 @@ import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.internal.UniqueAnnotations;
import java.lang.annotation.Annotation;
/** Injection configuration for the site initialization process. */

View File

@@ -26,7 +26,6 @@ import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
@@ -44,8 +43,8 @@ public class InitPluginStepsLoader {
final ConsoleUI ui;
@Inject
public InitPluginStepsLoader(final ConsoleUI ui, final SitePaths sitePaths,
final Injector initInjector) {
public InitPluginStepsLoader(
final ConsoleUI ui, final SitePaths sitePaths, final Injector initInjector) {
this.pluginsDir = sitePaths.plugins_dir;
this.initInjector = initInjector;
this.ui = ui;
@@ -68,8 +67,8 @@ public class InitPluginStepsLoader {
private InitStep loadInitStep(Path jar) {
try {
URLClassLoader pluginLoader =
new URLClassLoader(new URL[] {jar.toUri().toURL()},
InitPluginStepsLoader.class.getClassLoader());
new URLClassLoader(
new URL[] {jar.toUri().toURL()}, InitPluginStepsLoader.class.getClassLoader());
try (JarFile jarFile = new JarFile(jar.toFile())) {
Attributes jarFileAttributes = jarFile.getManifest().getMainAttributes();
String initClassName = jarFileAttributes.getValue("Gerrit-InitStep");
@@ -100,24 +99,23 @@ public class InitPluginStepsLoader {
}
private Injector getPluginInjector(Path jarPath) throws IOException {
final String pluginName = MoreObjects.firstNonNull(
JarPluginProvider.getJarPluginName(jarPath),
PluginLoader.nameOf(jarPath));
return initInjector.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).annotatedWith(PluginName.class).toInstance(
pluginName);
}
});
final String pluginName =
MoreObjects.firstNonNull(
JarPluginProvider.getJarPluginName(jarPath), PluginLoader.nameOf(jarPath));
return initInjector.createChildInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(String.class).annotatedWith(PluginName.class).toInstance(pluginName);
}
});
}
private List<Path> scanJarsInPluginsDirectory() {
try {
return PluginLoader.listPlugins(pluginsDir, ".jar");
} catch (IOException e) {
ui.message("WARN: Cannot list %s: %s", pluginsDir.toAbsolutePath(),
e.getMessage());
ui.message("WARN: Cannot list %s: %s", pluginsDir.toAbsolutePath(), e.getMessage());
return ImmutableList.of();
}
}

View File

@@ -24,7 +24,6 @@ import com.google.gerrit.server.plugins.JarPluginProvider;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
@@ -42,37 +41,42 @@ public class InitPlugins implements InitStep {
public static final String PLUGIN_DIR = "WEB-INF/plugins/";
public static final String JAR = ".jar";
public static List<PluginData> listPlugins(SitePaths site,
PluginsDistribution pluginsDistribution) throws IOException {
public static List<PluginData> listPlugins(
SitePaths site, PluginsDistribution pluginsDistribution) throws IOException {
return listPlugins(site, false, pluginsDistribution);
}
public static List<PluginData> listPluginsAndRemoveTempFiles(SitePaths site,
PluginsDistribution pluginsDistribution) throws IOException {
public static List<PluginData> listPluginsAndRemoveTempFiles(
SitePaths site, PluginsDistribution pluginsDistribution) throws IOException {
return listPlugins(site, true, pluginsDistribution);
}
private static List<PluginData> listPlugins(final SitePaths site,
final boolean deleteTempPluginFile, PluginsDistribution pluginsDistribution)
throws IOException {
private static List<PluginData> listPlugins(
final SitePaths site,
final boolean deleteTempPluginFile,
PluginsDistribution pluginsDistribution)
throws IOException {
final List<PluginData> result = new ArrayList<>();
pluginsDistribution.foreach(new PluginsDistribution.Processor() {
@Override
public void process(String pluginName, InputStream in) throws IOException {
Path tmpPlugin = JarPluginProvider.storeInTemp(pluginName, in, site);
String pluginVersion = getVersion(tmpPlugin);
if (deleteTempPluginFile) {
Files.delete(tmpPlugin);
}
result.add(new PluginData(pluginName, pluginVersion, tmpPlugin));
}
});
return FluentIterable.from(result).toSortedList(new Comparator<PluginData>() {
@Override
public int compare(PluginData a, PluginData b) {
return a.name.compareTo(b.name);
}
});
pluginsDistribution.foreach(
new PluginsDistribution.Processor() {
@Override
public void process(String pluginName, InputStream in) throws IOException {
Path tmpPlugin = JarPluginProvider.storeInTemp(pluginName, in, site);
String pluginVersion = getVersion(tmpPlugin);
if (deleteTempPluginFile) {
Files.delete(tmpPlugin);
}
result.add(new PluginData(pluginName, pluginVersion, tmpPlugin));
}
});
return FluentIterable.from(result)
.toSortedList(
new Comparator<PluginData>() {
@Override
public int compare(PluginData a, PluginData b) {
return a.name.compareTo(b.name);
}
});
}
private final ConsoleUI ui;
@@ -84,8 +88,11 @@ public class InitPlugins implements InitStep {
private Injector postRunInjector;
@Inject
InitPlugins(final ConsoleUI ui, final SitePaths site,
InitFlags initFlags, InitPluginStepsLoader pluginLoader,
InitPlugins(
final ConsoleUI ui,
final SitePaths site,
InitFlags initFlags,
InitPluginStepsLoader pluginLoader,
PluginsDistribution pluginsDistribution) {
this.ui = ui;
this.site = site;
@@ -124,24 +131,26 @@ public class InitPlugins implements InitStep {
if (!(initFlags.installPlugins.contains(pluginName)
|| initFlags.installAllPlugins
|| ui.yesno(upgrade, "Install plugin %s version %s", pluginName,
plugin.version))) {
|| ui.yesno(upgrade, "Install plugin %s version %s", pluginName, plugin.version))) {
Files.deleteIfExists(tmpPlugin);
continue;
}
if (upgrade) {
final String installedPluginVersion = getVersion(p);
if (!ui.yesno(upgrade, "%s %s is already installed, overwrite it",
plugin.name, installedPluginVersion)) {
if (!ui.yesno(
upgrade,
"%s %s is already installed, overwrite it",
plugin.name,
installedPluginVersion)) {
Files.deleteIfExists(tmpPlugin);
continue;
}
try {
Files.delete(p);
} catch (IOException e) {
throw new IOException("Failed to delete plugin " + pluginName
+ ": " + p.toAbsolutePath(), e);
throw new IOException(
"Failed to delete plugin " + pluginName + ": " + p.toAbsolutePath(), e);
}
}
try {
@@ -153,9 +162,14 @@ public class InitPlugins implements InitStep {
ui.message("Installed %s %s\n", plugin.name, plugin.version);
}
} catch (IOException e) {
throw new IOException("Failed to install plugin " + pluginName
+ ": " + tmpPlugin.toAbsolutePath() + " -> "
+ p.toAbsolutePath(), e);
throw new IOException(
"Failed to install plugin "
+ pluginName
+ ": "
+ tmpPlugin.toAbsolutePath()
+ " -> "
+ p.toAbsolutePath(),
e);
}
} finally {
Files.deleteIfExists(plugin.pluginPath);

View File

@@ -24,7 +24,6 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.mail.Encryption;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.nio.file.Files;
/** Initialize the {@code sendemail} configuration section. */
@@ -35,8 +34,7 @@ class InitSendEmail implements InitStep {
private final SitePaths site;
@Inject
InitSendEmail(final ConsoleUI ui, final SitePaths site,
final Section.Factory sections) {
InitSendEmail(final ConsoleUI ui, final SitePaths site, final Section.Factory sections) {
this.ui = ui;
this.sendemail = sections.get("sendemail", null);
this.site = site;
@@ -46,14 +44,12 @@ class InitSendEmail implements InitStep {
public void run() {
ui.header("Email Delivery");
final String hostname =
sendemail.string("SMTP server hostname", "smtpServer", "localhost");
final String hostname = sendemail.string("SMTP server hostname", "smtpServer", "localhost");
sendemail.string("SMTP server port", "smtpServerPort", "(default)", true);
final Encryption enc =
sendemail.select("SMTP encryption", "smtpEncryption", Encryption.NONE,
true);
sendemail.select("SMTP encryption", "smtpEncryption", Encryption.NONE, true);
String username = null;
if (Files.exists(site.gerrit_config)) {

View File

@@ -27,15 +27,13 @@ import com.google.gerrit.server.util.HostPlatform;
import com.google.gerrit.server.util.SocketUtil;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.apache.sshd.common.util.SecurityUtils;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.sshd.common.util.SecurityUtils;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
/** Initialize the {@code sshd} configuration section. */
@Singleton
@@ -46,7 +44,10 @@ class InitSshd implements InitStep {
private final Section sshd;
@Inject
InitSshd(final ConsoleUI ui, final SitePaths site, final Libraries libraries,
InitSshd(
final ConsoleUI ui,
final SitePaths site,
final Libraries libraries,
final Section.Factory sections) {
this.ui = ui;
this.site = site;
@@ -110,25 +111,39 @@ class InitSshd implements InitStep {
System.err.print(" rsa...");
System.err.flush();
new ProcessBuilder("ssh-keygen",
"-q" /* quiet */,
"-t", "rsa",
"-P", emptyPassphraseArg,
"-C", comment,
"-f", site.ssh_rsa.toAbsolutePath().toString()
).redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT)
.start().waitFor();
new ProcessBuilder(
"ssh-keygen",
"-q" /* quiet */,
"-t",
"rsa",
"-P",
emptyPassphraseArg,
"-C",
comment,
"-f",
site.ssh_rsa.toAbsolutePath().toString())
.redirectError(Redirect.INHERIT)
.redirectOutput(Redirect.INHERIT)
.start()
.waitFor();
System.err.print(" dsa...");
System.err.flush();
new ProcessBuilder("ssh-keygen",
"-q" /* quiet */,
"-t", "dsa",
"-P", emptyPassphraseArg,
"-C", comment,
"-f", site.ssh_dsa.toAbsolutePath().toString()
).redirectError(Redirect.INHERIT).redirectOutput(Redirect.INHERIT)
.start().waitFor();
new ProcessBuilder(
"ssh-keygen",
"-q" /* quiet */,
"-t",
"dsa",
"-P",
emptyPassphraseArg,
"-C",
comment,
"-f",
site.ssh_dsa.toAbsolutePath().toString())
.redirectError(Redirect.INHERIT)
.redirectOutput(Redirect.INHERIT)
.start()
.waitFor();
} else {
// Generate the SSH daemon host key ourselves. This is complex

View File

@@ -20,10 +20,6 @@ import com.google.gerrit.pgm.init.api.LibraryDownload;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -32,28 +28,30 @@ import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
/** Standard {@link LibraryDownloader} instances derived from configuration. */
@Singleton
class Libraries {
private static final String RESOURCE_FILE =
"com/google/gerrit/pgm/init/libraries.config";
private static final String RESOURCE_FILE = "com/google/gerrit/pgm/init/libraries.config";
private final Provider<LibraryDownloader> downloadProvider;
private final List<String> skippedDownloads;
private final boolean skipAllDownloads;
/* final */LibraryDownloader bouncyCastlePGP;
/* final */LibraryDownloader bouncyCastleProvider;
/* final */LibraryDownloader bouncyCastleSSL;
/* final */LibraryDownloader db2Driver;
/* final */LibraryDownloader db2DriverLicense;
/* final */LibraryDownloader hanaDriver;
/* final */LibraryDownloader mysqlDriver;
/* final */LibraryDownloader oracleDriver;
/* final */ LibraryDownloader bouncyCastlePGP;
/* final */ LibraryDownloader bouncyCastleProvider;
/* final */ LibraryDownloader bouncyCastleSSL;
/* final */ LibraryDownloader db2Driver;
/* final */ LibraryDownloader db2DriverLicense;
/* final */ LibraryDownloader hanaDriver;
/* final */ LibraryDownloader mysqlDriver;
/* final */ LibraryDownloader oracleDriver;
@Inject
Libraries(final Provider<LibraryDownloader> downloadProvider,
Libraries(
final Provider<LibraryDownloader> downloadProvider,
@LibraryDownload List<String> skippedDownloads,
@LibraryDownload Boolean skipAllDownloads) {
this.downloadProvider = downloadProvider;
@@ -71,8 +69,7 @@ class Libraries {
}
for (Field f : Libraries.class.getDeclaredFields()) {
if ((f.getModifiers() & Modifier.STATIC) == 0
&& f.getType() == LibraryDownloader.class) {
if ((f.getModifiers() & Modifier.STATIC) == 0 && f.getType() == LibraryDownloader.class) {
try {
f.set(this, downloadProvider.get());
} catch (IllegalArgumentException | IllegalAccessException e) {
@@ -82,20 +79,22 @@ class Libraries {
}
for (Field f : Libraries.class.getDeclaredFields()) {
if ((f.getModifiers() & Modifier.STATIC) == 0
&& f.getType() == LibraryDownloader.class) {
if ((f.getModifiers() & Modifier.STATIC) == 0 && f.getType() == LibraryDownloader.class) {
try {
init(f, cfg);
} catch (IllegalArgumentException | IllegalAccessException
| NoSuchFieldException | SecurityException e) {
} catch (IllegalArgumentException
| IllegalAccessException
| NoSuchFieldException
| SecurityException e) {
throw new IllegalStateException("Cannot configure " + f.getName());
}
}
}
}
private void init(Field field, Config cfg) throws IllegalArgumentException,
IllegalAccessException, NoSuchFieldException, SecurityException {
private void init(Field field, Config cfg)
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,
SecurityException {
String n = field.getName();
LibraryDownloader dl = (LibraryDownloader) field.get(this);
dl.setName(get(cfg, n, "name"));
@@ -116,12 +115,11 @@ class Libraries {
return doGet(cfg, name, key, true);
}
private static String doGet(Config cfg, String name, String key,
boolean required) {
private static String doGet(Config cfg, String name, String key, boolean required) {
String val = cfg.getString("library", name, key);
if ((val == null || val.isEmpty()) && required) {
throw new IllegalStateException("Variable library." + name + "." + key
+ " is required within " + RESOURCE_FILE);
throw new IllegalStateException(
"Variable library." + name + "." + key + " is required within " + RESOURCE_FILE);
}
return val;
}

View File

@@ -24,9 +24,6 @@ import com.google.gerrit.common.IoUtil;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import org.eclipse.jgit.util.HttpSupport;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -42,6 +39,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.util.HttpSupport;
/** Get optional or required 3rd party library files into $site_path/lib. */
class LibraryDownloader {
@@ -150,17 +148,14 @@ class LibraryDownloader {
msg.append("\n");
msg.append("Gerrit Code Review is not shipped with %s\n");
if (neededBy != null) {
msg.append(String.format(
"** This library is required by %s. **\n",
neededBy.name));
msg.append(String.format("** This library is required by %s. **\n", neededBy.name));
} else if (required) {
msg.append("** This library is required for your configuration. **\n");
} else {
msg.append(" If available, Gerrit can take advantage of features\n");
msg.append(" in the library, but will also function without it.\n");
}
msg.append(String.format(
"%s and install it now", download ? "Download" : "Copy"));
msg.append(String.format("%s and install it now", download ? "Download" : "Copy"));
return ui.yesno(true, msg.toString(), name);
}
@@ -223,15 +218,14 @@ class LibraryDownloader {
private void removeStaleVersions() {
if (!Strings.isNullOrEmpty(remove)) {
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) {
return entry.getFileName().toString()
.matches("^" + remove + "$");
}
};
try (DirectoryStream<Path> paths =
Files.newDirectoryStream(lib_dir, filter)) {
DirectoryStream.Filter<Path> filter =
new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) {
return entry.getFileName().toString().matches("^" + remove + "$");
}
};
try (DirectoryStream<Path> paths = Files.newDirectoryStream(lib_dir, filter)) {
for (Path p : paths) {
String old = p.getFileName().toString();
String bak = "." + old + ".backup";
@@ -252,10 +246,11 @@ class LibraryDownloader {
System.err.print("Copying " + jarUrl + " ...");
Path p = url2file(jarUrl);
if (!Files.exists(p)) {
StringBuilder msg = new StringBuilder()
.append("\n")
.append("Can not find the %s at this location: %s\n")
.append("Please provide alternative URL");
StringBuilder msg =
new StringBuilder()
.append("\n")
.append("Can not find the %s at this location: %s\n")
.append("Please provide alternative URL");
p = url2file(ui.readString(null, msg.toString(), name, jarUrl));
}
Files.copy(p, dst);
@@ -300,8 +295,8 @@ class LibraryDownloader {
throw new FileNotFoundException(url.toString());
default:
throw new IOException(url.toString() + ": " + HttpSupport.response(c)
+ " " + c.getResponseMessage());
throw new IOException(
url.toString() + ": " + HttpSupport.response(c) + " " + c.getResponseMessage());
}
}
@@ -326,8 +321,9 @@ class LibraryDownloader {
deleteDst();
throw new Die(dst + " SHA-1 checksum does not match");
} else if (!ui.yesno(null /* force an answer */,
"error: SHA-1 checksum does not match\n" + "Use %s anyway",//
} else if (!ui.yesno(
null /* force an answer */,
"error: SHA-1 checksum does not match\n" + "Use %s anyway", //
dst.getFileName())) {
deleteDst();
throw new Die("aborted by user");

View File

@@ -18,7 +18,6 @@ import static com.google.gerrit.pgm.init.api.InitUtil.username;
import com.google.gerrit.pgm.init.api.Section;
public class OracleInitializer implements DatabaseConfigInitializer {
@Override

View File

@@ -19,19 +19,15 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Represents the plugins packaged in the Gerrit distribution
*/
/** Represents the plugins packaged in the Gerrit distribution */
public interface PluginsDistribution {
public interface Processor {
/**
* @param pluginName the name of the plugin (without the .jar extension)
* @param in the content of the plugin .jar file. Implementors don't have to
* close this stream.
* @throws IOException implementations will typically propagate any
* IOException caused by dealing with the InputStream back to the
* caller
* @param in the content of the plugin .jar file. Implementors don't have to close this stream.
* @throws IOException implementations will typically propagate any IOException caused by
* dealing with the InputStream back to the caller
*/
void process(String pluginName, InputStream in) throws IOException;
}
@@ -40,18 +36,16 @@ public interface PluginsDistribution {
* Iterate over plugins package in the Gerrit distribution
*
* @param processor invoke for each plugin via its process method
* @throws FileNotFoundException if the location of the plugins couldn't be
* determined
* @throws IOException in case of any other IO error caused by reading the
* plugin input stream
* @throws FileNotFoundException if the location of the plugins couldn't be determined
* @throws IOException in case of any other IO error caused by reading the plugin input stream
*/
void foreach(Processor processor) throws FileNotFoundException, IOException;
/**
* List plugins included in the Gerrit distribution
*
* @return list of plugins names included in the Gerrit distribution
* @throws FileNotFoundException if the location of the plugins couldn't be
* determined
* @throws FileNotFoundException if the location of the plugins couldn't be determined
*/
List<String> listPluginNames() throws FileNotFoundException;
}

View File

@@ -34,7 +34,6 @@ import com.google.inject.Binding;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -51,8 +50,11 @@ public class SitePathInitializer {
private final SecureStoreInitData secureStoreInitData;
@Inject
public SitePathInitializer(final Injector injector, final ConsoleUI ui,
final InitFlags flags, final SitePaths site,
public SitePathInitializer(
final Injector injector,
final ConsoleUI ui,
final InitFlags flags,
final SitePaths site,
final Section.Factory sectionFactory,
@Nullable final SecureStoreInitData secureStoreInitData) {
this.ui = ui;
@@ -85,8 +87,7 @@ public class SitePathInitializer {
mkdir(site.data_dir);
for (InitStep step : steps) {
if (step instanceof InitPlugins
&& flags.skipPlugins) {
if (step instanceof InitPlugins && flags.skipPlugins) {
continue;
}
step.run();
@@ -137,8 +138,7 @@ public class SitePathInitializer {
public void postRun(Injector injector) throws Exception {
for (InitStep step : steps) {
if (step instanceof InitPlugins
&& flags.skipPlugins) {
if (step instanceof InitPlugins && flags.skipPlugins) {
continue;
}
injector.injectMembers(step);
@@ -148,8 +148,7 @@ public class SitePathInitializer {
private void saveSecureStore() throws IOException {
if (secureStoreInitData != null) {
Path dst =
site.lib_dir.resolve(secureStoreInitData.jarFile.getFileName());
Path dst = site.lib_dir.resolve(secureStoreInitData.jarFile.getFileName());
Files.copy(secureStoreInitData.jarFile, dst);
Section gerritSection = sectionFactory.get("gerrit", null);
gerritSection.set("secureStoreClass", secureStoreInitData.className);

View File

@@ -27,10 +27,6 @@ import com.google.gerrit.server.securestore.SecureStore;
import com.google.gerrit.server.util.SocketUtil;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -42,24 +38,27 @@ import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
/** Upgrade from a 2.0.x site to a 2.1 site. */
@Singleton
class UpgradeFrom2_0_x implements InitStep {
static final String[] etcFiles = {"gerrit.config", //
"secure.config", //
"replication.config", //
"ssh_host_rsa_key", //
"ssh_host_rsa_key.pub", //
"ssh_host_dsa_key", //
"ssh_host_dsa_key.pub", //
"ssh_host_key", //
"contact_information.pub", //
"gitweb_config.perl", //
"keystore", //
"GerritSite.css", //
"GerritSiteFooter.html", //
"GerritSiteHeader.html", //
static final String[] etcFiles = {
"gerrit.config", //
"secure.config", //
"replication.config", //
"ssh_host_rsa_key", //
"ssh_host_rsa_key.pub", //
"ssh_host_dsa_key", //
"ssh_host_dsa_key.pub", //
"ssh_host_key", //
"contact_information.pub", //
"gitweb_config.perl", //
"keystore", //
"GerritSite.css", //
"GerritSiteFooter.html", //
"GerritSiteHeader.html", //
};
private final ConsoleUI ui;
@@ -71,8 +70,11 @@ class UpgradeFrom2_0_x implements InitStep {
private final Section.Factory sections;
@Inject
UpgradeFrom2_0_x(final SitePaths site, final InitFlags flags,
final ConsoleUI ui, final Section.Factory sections) {
UpgradeFrom2_0_x(
final SitePaths site,
final InitFlags flags,
final ConsoleUI ui,
final Section.Factory sections) {
this.ui = ui;
this.sections = sections;

View File

@@ -27,13 +27,11 @@ import com.google.gerrit.server.account.VersionedAuthorizedKeys;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
public class VersionedAuthorizedKeysOnInit extends VersionedMetaDataOnInit {
public interface Factory {
@@ -54,8 +52,7 @@ public class VersionedAuthorizedKeysOnInit extends VersionedMetaDataOnInit {
}
@Override
public VersionedAuthorizedKeysOnInit load()
throws IOException, ConfigInvalidException {
public VersionedAuthorizedKeysOnInit load() throws IOException, ConfigInvalidException {
super.load();
return this;
}
@@ -69,8 +66,7 @@ public class VersionedAuthorizedKeysOnInit extends VersionedMetaDataOnInit {
checkState(keys != null, "SSH keys not loaded yet");
int seq = keys.isEmpty() ? 1 : keys.size() + 1;
AccountSshKey.Id keyId = new AccountSshKey.Id(accountId, seq);
AccountSshKey key =
new VersionedAuthorizedKeys.SimpleSshKeyCreator().create(keyId, pub);
AccountSshKey key = new VersionedAuthorizedKeys.SimpleSshKeyCreator().create(keyId, pub);
keys.add(Optional.of(key));
return key;
}

View File

@@ -20,7 +20,7 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.git.GroupList;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.inject.Inject;
import java.io.IOException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Config;
@@ -29,8 +29,6 @@ import org.eclipse.jgit.lib.RepositoryCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class AllProjectsConfig extends VersionedMetaDataOnInit {
private static final Logger log = LoggerFactory.getLogger(AllProjectsConfig.class);
@@ -39,10 +37,8 @@ public class AllProjectsConfig extends VersionedMetaDataOnInit {
private GroupList groupList;
@Inject
AllProjectsConfig(AllProjectsNameOnInitProvider allProjects, SitePaths site,
InitFlags flags) {
AllProjectsConfig(AllProjectsNameOnInitProvider allProjects, SitePaths site, InitFlags flags) {
super(flags, site, allProjects.get(), RefNames.REFS_CONFIG);
}
public Config getConfig() {
@@ -54,8 +50,7 @@ public class AllProjectsConfig extends VersionedMetaDataOnInit {
}
@Override
public AllProjectsConfig load()
throws IOException, ConfigInvalidException {
public AllProjectsConfig load() throws IOException, ConfigInvalidException {
super.load();
return this;
}
@@ -73,15 +68,14 @@ public class AllProjectsConfig extends VersionedMetaDataOnInit {
GroupList.createLoggerSink(GroupList.FILE_NAME, log));
}
public void save(String pluginName, String message)
throws IOException, ConfigInvalidException {
save(new PersonIdent(pluginName, pluginName + "@gerrit"),
public void save(String pluginName, String message) throws IOException, ConfigInvalidException {
save(
new PersonIdent(pluginName, pluginName + "@gerrit"),
"Update from plugin " + pluginName + ": " + message);
}
@Override
protected void save(PersonIdent ident, String msg)
throws IOException, ConfigInvalidException {
protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
super.save(ident, msg);
// we need to invalidate the JGit cache if the group list is invalidated in
@@ -90,8 +84,7 @@ public class AllProjectsConfig extends VersionedMetaDataOnInit {
}
@Override
protected boolean onSave(CommitBuilder commit) throws IOException,
ConfigInvalidException {
protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException {
saveConfig(ProjectConfig.PROJECT_CONFIG, cfg);
saveGroupList();
return true;

View File

@@ -26,8 +26,7 @@ public class AllProjectsNameOnInitProvider implements Provider<String> {
@Inject
AllProjectsNameOnInitProvider(Section.Factory sections) {
String n = sections.get("gerrit", null).get("allProjects");
name = MoreObjects.firstNonNull(
Strings.emptyToNull(n), AllProjectsNameProvider.DEFAULT);
name = MoreObjects.firstNonNull(Strings.emptyToNull(n), AllProjectsNameProvider.DEFAULT);
}
@Override

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.pgm.init.api;
import com.google.gerrit.common.Die;
import java.io.Console;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
@@ -43,8 +42,10 @@ public abstract class ConsoleUI {
protected static <T extends Enum<?>> T[] all(final T value) {
try {
return (T[]) value.getClass().getMethod("values").invoke(null);
} catch (IllegalArgumentException | NoSuchMethodException
| InvocationTargetException | IllegalAccessException
} catch (IllegalArgumentException
| NoSuchMethodException
| InvocationTargetException
| IllegalAccessException
| SecurityException e) {
throw new IllegalArgumentException("Cannot obtain enumeration values", e);
}
@@ -69,12 +70,12 @@ public abstract class ConsoleUI {
public abstract String readString(String def, String fmt, Object... args);
/** Prompt the user to make a choice from an allowed list of values. */
public abstract String readString(String def, Set<String> allowedValues,
String fmt, Object... args);
public abstract String readString(
String def, Set<String> allowedValues, String fmt, Object... args);
/** Prompt the user for an integer value, suggesting a default. */
public int readInt(int def, String fmt, Object... args) {
for (;;) {
for (; ; ) {
String p = readString(String.valueOf(def), fmt, args);
try {
return Integer.parseInt(p.trim(), 10);
@@ -94,8 +95,7 @@ public abstract class ConsoleUI {
}
/** Prompt the user to make a choice from an enumeration's values. */
public abstract <T extends Enum<?>> T readEnum(T def, String fmt,
Object... args);
public abstract <T extends Enum<?>> T readEnum(T def, String fmt, Object... args);
private static class Interactive extends ConsoleUI {
private final Console console;
@@ -112,7 +112,7 @@ public abstract class ConsoleUI {
@Override
public boolean yesno(Boolean def, String fmt, Object... args) {
final String prompt = String.format(fmt, args);
for (;;) {
for (; ; ) {
String y = "y";
String n = "n";
if (def != null) {
@@ -167,9 +167,8 @@ public abstract class ConsoleUI {
}
@Override
public String readString(String def, Set<String> allowedValues, String fmt,
Object... args) {
for (;;) {
public String readString(String def, Set<String> allowedValues, String fmt, Object... args) {
for (; ; ) {
String r = readString(def, fmt, args);
if (allowedValues.contains(r.toLowerCase())) {
return r.toLowerCase();
@@ -187,7 +186,7 @@ public abstract class ConsoleUI {
@Override
public String password(String fmt, Object... args) {
final String prompt = String.format(fmt, args);
for (;;) {
for (; ; ) {
final char[] a1 = console.readPassword("%-30s : ", prompt);
if (a1 == null) {
throw abort();
@@ -212,7 +211,7 @@ public abstract class ConsoleUI {
public <T extends Enum<?>> T readEnum(T def, String fmt, Object... args) {
final String prompt = String.format(fmt, args);
final T[] options = all(def);
for (;;) {
for (; ; ) {
String r = console.readLine("%-30s [%s/?]: ", prompt, def.toString());
if (r == null) {
throw abort();
@@ -265,14 +264,12 @@ public abstract class ConsoleUI {
}
@Override
public String readString(String def, Set<String> allowedValues, String fmt,
Object... args) {
public String readString(String def, Set<String> allowedValues, String fmt, Object... args) {
return def;
}
@Override
public void waitForUser() {
}
public void waitForUser() {}
@Override
public String password(String fmt, Object... args) {
@@ -285,11 +282,9 @@ public abstract class ConsoleUI {
}
@Override
public void header(String fmt, Object... args) {
}
public void header(String fmt, Object... args) {}
@Override
public void message(String fmt, Object... args) {
}
public void message(String fmt, Object... args) {}
}
}

View File

@@ -19,14 +19,12 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.List;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import java.io.IOException;
import java.util.List;
/** Global variables used by the 'init' command. */
@Singleton
public class InitFlags {
@@ -52,11 +50,12 @@ public class InitFlags {
@VisibleForTesting
@Inject
public InitFlags(final SitePaths site,
public InitFlags(
final SitePaths site,
final SecureStore secureStore,
@InstallPlugins final List<String> installPlugins,
@InstallAllPlugins final Boolean installAllPlugins) throws IOException,
ConfigInvalidException {
@InstallAllPlugins final Boolean installAllPlugins)
throws IOException, ConfigInvalidException {
sec = secureStore;
this.installPlugins = installPlugins;
this.installAllPlugins = installAllPlugins;

View File

@@ -18,11 +18,6 @@ import static com.google.gerrit.common.FileUtil.modified;
import com.google.common.io.ByteStreams;
import com.google.gerrit.common.Die;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.SystemReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
@@ -37,6 +32,9 @@ import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Arrays;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.SystemReader;
/** Utility functions to help initialize a site. */
public class InitUtil {
@@ -121,8 +119,7 @@ public class InitUtil {
return name;
}
public static void extract(Path dst, Class<?> sibling, String name)
throws IOException {
public static void extract(Path dst, Class<?> sibling, String name) throws IOException {
try (InputStream in = open(sibling, name)) {
if (in != null) {
copy(dst, ByteStreams.toByteArray(in));
@@ -147,8 +144,7 @@ public class InitUtil {
return in;
}
public static void copy(Path dst, byte[] buf)
throws FileNotFoundException, IOException {
public static void copy(Path dst, byte[] buf) throws FileNotFoundException, IOException {
// If the file already has the content we want to put there,
// don't attempt to overwrite the file.
//
@@ -203,6 +199,5 @@ public class InitUtil {
return port;
}
private InitUtil() {
}
private InitUtil() {}
}

View File

@@ -15,11 +15,9 @@
package com.google.gerrit.pgm.init.api;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
public @interface InstallAllPlugins {
}
public @interface InstallAllPlugins {}

View File

@@ -15,11 +15,9 @@
package com.google.gerrit.pgm.init.api;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
public @interface InstallPlugins {
}
public @interface InstallPlugins {}

View File

@@ -15,11 +15,9 @@
package com.google.gerrit.pgm.init.api;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
public @interface LibraryDownload {
}
public @interface LibraryDownload {}

View File

@@ -19,7 +19,6 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
@@ -28,8 +27,7 @@ import java.util.Set;
/** Helper to edit a section of the configuration files. */
public class Section {
public interface Factory {
Section get(@Assisted("section") String section,
@Assisted("subsection") String subsection);
Section get(@Assisted("section") String section, @Assisted("subsection") String subsection);
}
private final InitFlags flags;
@@ -40,8 +38,11 @@ public class Section {
private final SecureStore secureStore;
@Inject
public Section(final InitFlags flags, final SitePaths site,
final SecureStore secureStore, final ConsoleUI ui,
public Section(
final InitFlags flags,
final SitePaths site,
final SecureStore secureStore,
final ConsoleUI ui,
@Assisted("section") final String section,
@Assisted("subsection") @Nullable final String subsection) {
this.flags = flags;
@@ -92,8 +93,8 @@ public class Section {
return string(title, name, dv, false);
}
public String string(final String title, final String name, final String dv,
final boolean nullIfDefault) {
public String string(
final String title, final String name, final String dv, final boolean nullIfDefault) {
final String ov = get(name);
String nv = ui.readString(ov != null ? ov : dv, "%s", title);
if (nullIfDefault && nv.equals(dv)) {
@@ -109,13 +110,12 @@ public class Section {
return site.resolve(string(title, name, defValue));
}
public <T extends Enum<?>> T select(final String title, final String name,
final T defValue) {
public <T extends Enum<?>> T select(final String title, final String name, final T defValue) {
return select(title, name, defValue, false);
}
public <T extends Enum<?>> T select(final String title, final String name,
final T defValue, final boolean nullIfDefault) {
public <T extends Enum<?>> T select(
final String title, final String name, final T defValue, final boolean nullIfDefault) {
final boolean set = get(name) != null;
T oldValue = flags.cfg.getEnum(section, subsection, name, defValue);
T newValue = ui.readEnum(oldValue, "%s", title);
@@ -132,8 +132,8 @@ public class Section {
return newValue;
}
public String select(final String title, final String name, final String dv,
Set<String> allowedValues) {
public String select(
final String title, final String name, final String dv, Set<String> allowedValues) {
final String ov = get(name);
String nv = ui.readString(ov != null ? ov : dv, allowedValues, "%s", title);
if (!eq(ov, nv)) {

View File

@@ -16,7 +16,9 @@ package com.google.gerrit.pgm.init.api;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.git.VersionedMetaData;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.CommitBuilder;
@@ -31,10 +33,6 @@ import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.util.FS;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
public abstract class VersionedMetaDataOnInit extends VersionedMetaData {
protected final String project;
@@ -42,8 +40,7 @@ public abstract class VersionedMetaDataOnInit extends VersionedMetaData {
private final SitePaths site;
private final String ref;
protected VersionedMetaDataOnInit(InitFlags flags, SitePaths site,
String project, String ref) {
protected VersionedMetaDataOnInit(InitFlags flags, SitePaths site, String project, String ref) {
this.flags = flags;
this.site = site;
this.project = project;
@@ -55,8 +52,7 @@ public abstract class VersionedMetaDataOnInit extends VersionedMetaData {
return ref;
}
public VersionedMetaDataOnInit load()
throws IOException, ConfigInvalidException {
public VersionedMetaDataOnInit load() throws IOException, ConfigInvalidException {
File path = getPath();
if (path != null) {
try (Repository repo = new FileRepository(path)) {
@@ -70,8 +66,7 @@ public abstract class VersionedMetaDataOnInit extends VersionedMetaData {
save(new PersonIdent("Gerrit Initialization", "init@gerrit"), message);
}
protected void save(PersonIdent ident, String msg)
throws IOException, ConfigInvalidException {
protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
File path = getPath();
if (path == null) {
throw new IOException(project + " does not exist.");
@@ -112,15 +107,15 @@ public abstract class VersionedMetaDataOnInit extends VersionedMetaData {
}
}
private void updateRef(Repository repo, PersonIdent ident,
ObjectId newRevision, String refLogMsg) throws IOException {
private void updateRef(Repository repo, PersonIdent ident, ObjectId newRevision, String refLogMsg)
throws IOException {
RefUpdate ru = repo.updateRef(getRefName());
ru.setRefLogIdent(ident);
ru.setNewObjectId(newRevision);
ru.setExpectedOldObjectId(revision);
ru.setRefLogMessage(refLogMsg, false);
RefUpdate.Result r = ru.update();
switch(r) {
switch (r) {
case FAST_FORWARD:
case NEW:
case NO_CHANGE:
@@ -133,8 +128,8 @@ public abstract class VersionedMetaDataOnInit extends VersionedMetaData {
case REJECTED_CURRENT_BRANCH:
case RENAMED:
default:
throw new IOException("Failed to update " + getRefName() + " of "
+ project + ": " + r.name());
throw new IOException(
"Failed to update " + getRefName() + " of " + project + ": " + r.name());
}
}

View File

@@ -18,12 +18,11 @@ import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.server.index.IndexDefinition;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.Collection;
/**
* This class starts/stops the indexes from the init program so that init can
* write updates the indexes.
* This class starts/stops the indexes from the init program so that init can write updates the
* indexes.
*/
public class IndexManagerOnInit {
private final LifecycleListener indexManager;

View File

@@ -39,7 +39,6 @@ import com.google.inject.ProvisionException;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
@@ -48,9 +47,8 @@ public class IndexModuleOnInit extends AbstractModule {
static final String INDEX_MANAGER = "IndexModuleOnInit/IndexManager";
private static final ImmutableCollection<SchemaDefinitions<?>> ALL_SCHEMA_DEFS =
ImmutableList.<SchemaDefinitions<?>> of(
AccountSchemaDefinitions.INSTANCE,
GroupSchemaDefinitions.INSTANCE);
ImmutableList.<SchemaDefinitions<?>>of(
AccountSchemaDefinitions.INSTANCE, GroupSchemaDefinitions.INSTANCE);
@Override
protected void configure() {
@@ -81,30 +79,23 @@ public class IndexModuleOnInit extends AbstractModule {
bind(new TypeLiteral<Map<String, Integer>>() {})
.annotatedWith(Names.named(SingleVersionModule.SINGLE_VERSIONS))
.toInstance(ImmutableMap.<String, Integer> of());
bind(LifecycleListener.class).annotatedWith(Names.named(INDEX_MANAGER))
.toInstance(ImmutableMap.<String, Integer>of());
bind(LifecycleListener.class)
.annotatedWith(Names.named(INDEX_MANAGER))
.to(SingleVersionListener.class);
}
@Provides
Collection<IndexDefinition<?, ?, ?>> getIndexDefinitions(
AccountIndexDefinition accounts,
GroupIndexDefinition groups) {
AccountIndexDefinition accounts, GroupIndexDefinition groups) {
Collection<IndexDefinition<?, ?, ?>> result =
ImmutableList.<IndexDefinition<?, ?, ?>> of(
accounts,
groups);
ImmutableList.<IndexDefinition<?, ?, ?>>of(accounts, groups);
Set<String> expected =
FluentIterable.from(ALL_SCHEMA_DEFS)
.transform(SchemaDefinitions::getName)
.toSet();
Set<String> actual = FluentIterable.from(result)
.transform(IndexDefinition::getName)
.toSet();
FluentIterable.from(ALL_SCHEMA_DEFS).transform(SchemaDefinitions::getName).toSet();
Set<String> actual = FluentIterable.from(result).transform(IndexDefinition::getName).toSet();
if (!expected.equals(actual)) {
throw new ProvisionException(
"need index definitions for all schemas: "
+ expected + " != " + actual);
"need index definitions for all schemas: " + expected + " != " + actual);
}
return result;
}

View File

@@ -28,13 +28,13 @@ public class ElasticIndexModuleOnInit extends AbstractModule {
protected void configure() {
install(
new FactoryModuleBuilder()
.implement(AccountIndex.class, ElasticAccountIndex.class)
.build(AccountIndex.Factory.class));
.implement(AccountIndex.class, ElasticAccountIndex.class)
.build(AccountIndex.Factory.class));
install(
new FactoryModuleBuilder()
.implement(GroupIndex.class, ElasticGroupIndex.class)
.build(GroupIndex.Factory.class));
.implement(GroupIndex.class, ElasticGroupIndex.class)
.build(GroupIndex.Factory.class));
install(new IndexModuleOnInit());
}

View File

@@ -21,15 +21,8 @@ import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.googlecode.prolog_cafe.compiler.Compiler;
import com.googlecode.prolog_cafe.exceptions.CompileException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -48,18 +41,20 @@ import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
/**
* Helper class for Rulec: does the actual prolog -> java src -> class -> jar work
* Finds rules.pl in refs/meta/config branch
* Creates rules-(sha1 of rules.pl).jar in (site-path)/cache/rules
* Helper class for Rulec: does the actual prolog -> java src -> class -> jar work Finds rules.pl in
* refs/meta/config branch Creates rules-(sha1 of rules.pl).jar in (site-path)/cache/rules
*/
public class PrologCompiler implements Callable<PrologCompiler.Status> {
public interface Factory {
@@ -67,15 +62,16 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
}
public enum Status {
NO_RULES, COMPILED
NO_RULES,
COMPILED
}
private final Path ruleDir;
private final Repository git;
@Inject
PrologCompiler(@GerritServerConfig Config config, SitePaths site,
@Assisted Repository gitRepository) {
PrologCompiler(
@GerritServerConfig Config config, SitePaths site, @Assisted Repository gitRepository) {
Path cacheDir = site.resolve(config.getString("cache", null, "directory"));
ruleDir = cacheDir != null ? cacheDir.resolve("rules") : null;
git = gitRepository;
@@ -123,8 +119,7 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
}
/** Creates a copy of rules.pl and compiles it into Java sources. */
private void compileProlog(ObjectId prolog, File tempDir)
throws IOException, CompileException {
private void compileProlog(ObjectId prolog, File tempDir) throws IOException, CompileException {
File tempRules = copyToTempFile(prolog, tempDir);
try {
Compiler comp = new Compiler();
@@ -152,12 +147,11 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
throw new CompileException("JDK required (running inside of JRE)");
}
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<>();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(diagnostics, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromFiles(getAllFiles(tempDir, ".java"));
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjectsFromFiles(getAllFiles(tempDir, ".java"));
ArrayList<String> options = new ArrayList<>();
String classpath = getMyClasspath();
if (classpath != null) {
@@ -166,13 +160,8 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
}
options.add("-d");
options.add(tempDir.getPath());
JavaCompiler.CompilationTask task = compiler.getTask(
null,
fileManager,
diagnostics,
options,
null,
compilationUnits);
JavaCompiler.CompilationTask task =
compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
if (!task.call()) {
Locale myLocale = Locale.getDefault();
StringBuilder msg = new StringBuilder();
@@ -217,8 +206,9 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
}
/** Takes compiled prolog .class files, puts them into the jar file. */
private void createJar(Path archiveFile, List<String> toBeJared,
File tempDir, ObjectId metaConfig, ObjectId rulesId) throws IOException {
private void createJar(
Path archiveFile, List<String> toBeJared, File tempDir, ObjectId metaConfig, ObjectId rulesId)
throws IOException {
long now = TimeUtil.nowMs();
Manifest mf = new Manifest();
mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
@@ -229,8 +219,7 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
mf.getMainAttributes().putValue("Source-Commit", metaConfig.name());
mf.getMainAttributes().putValue("Source-Blob", rulesId.name());
Path tmpjar =
Files.createTempFile(archiveFile.getParent(), ".rulec_", ".jar");
Path tmpjar = Files.createTempFile(archiveFile.getParent(), ".rulec_", ".jar");
try (OutputStream stream = Files.newOutputStream(tmpjar);
JarOutputStream out = new JarOutputStream(stream, mf)) {
byte[] buffer = new byte[10240];
@@ -262,15 +251,13 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
}
}
private List<File> getAllFiles(File dir, String extension)
throws IOException {
private List<File> getAllFiles(File dir, String extension) throws IOException {
ArrayList<File> fileList = new ArrayList<>();
getAllFiles(dir, extension, fileList);
return fileList;
}
private void getAllFiles(File dir, String extension, List<File> fileList)
throws IOException {
private void getAllFiles(File dir, String extension, List<File> fileList) throws IOException {
for (File f : listFiles(dir)) {
if (f.getName().endsWith(extension)) {
fileList.add(f);
@@ -281,15 +268,14 @@ public class PrologCompiler implements Callable<PrologCompiler.Status> {
}
}
private List<String> getRelativePaths(File dir, String extension)
throws IOException {
private List<String> getRelativePaths(File dir, String extension) throws IOException {
ArrayList<String> pathList = new ArrayList<>();
getRelativePaths(dir, extension, "", pathList);
return pathList;
}
private static void getRelativePaths(File dir, String extension, String path,
List<String> pathList) throws IOException {
private static void getRelativePaths(
File dir, String extension, String path, List<String> pathList) throws IOException {
for (File f : listFiles(dir)) {
if (f.getName().endsWith(extension)) {
pathList.add(path + f.getName());

View File

@@ -14,16 +14,13 @@
package com.google.gerrit.pgm.util;
import com.google.gerrit.common.Die;
import com.google.gerrit.util.cli.CmdLineParser;
import com.google.gerrit.util.cli.OptionHandlers;
import java.io.StringWriter;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;
import java.io.StringWriter;
/** Base class for command line invocations of Gerrit Code Review. */
public abstract class AbstractProgram {
private final Object sleepLock = new Object();
@@ -69,8 +66,7 @@ public abstract class AbstractProgram {
final Throwable cause = err.getCause();
final String diemsg = err.getMessage();
if (cause != null && !cause.getMessage().equals(diemsg)) {
System.err.println("fatal: "
+ cause.getMessage().replaceAll("\n", "\nfatal: "));
System.err.println("fatal: " + cause.getMessage().replaceAll("\n", "\nfatal: "));
}
System.err.println("fatal: " + diemsg.replaceAll("\n", "\nfatal: "));
}
@@ -104,9 +100,9 @@ public abstract class AbstractProgram {
/**
* Run this program's logic, returning the command exit status.
* <p>
* When this method completes, the JVM is terminated. To keep the JVM running,
* use {@code return never()}.
*
* <p>When this method completes, the JVM is terminated. To keep the JVM running, use {@code
* return never()}.
*/
public abstract int run() throws Exception;
}

View File

@@ -73,27 +73,23 @@ import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Providers;
import org.eclipse.jgit.lib.Config;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.lib.Config;
/**
* Module for programs that perform batch operations on a site.
* <p>
* Any program that requires this module likely also requires using
* {@link ThreadLimiter} to limit the number of threads accessing the database
* concurrently.
*
* <p>Any program that requires this module likely also requires using {@link ThreadLimiter} to
* limit the number of threads accessing the database concurrently.
*/
public class BatchProgramModule extends FactoryModule {
private final Config cfg;
private final Module reviewDbModule;
@Inject
BatchProgramModule(@GerritServerConfig Config cfg,
PerThreadReviewDbModule reviewDbModule) {
BatchProgramModule(@GerritServerConfig Config cfg, PerThreadReviewDbModule reviewDbModule) {
this.cfg = cfg;
this.reviewDbModule = reviewDbModule;
}
@@ -109,20 +105,23 @@ public class BatchProgramModule extends FactoryModule {
// Plugins are not loaded and we're just running through each change
// once, so don't worry about cache removal.
bind(new TypeLiteral<DynamicSet<CacheRemovalListener>>() {})
.toInstance(DynamicSet.<CacheRemovalListener> emptySet());
.toInstance(DynamicSet.<CacheRemovalListener>emptySet());
bind(new TypeLiteral<DynamicMap<Cache<?, ?>>>() {})
.toInstance(DynamicMap.<Cache<?, ?>> emptyMap());
.toInstance(DynamicMap.<Cache<?, ?>>emptyMap());
bind(new TypeLiteral<List<CommentLinkInfo>>() {})
.toProvider(CommentLinkProvider.class).in(SINGLETON);
bind(String.class).annotatedWith(CanonicalWebUrl.class)
.toProvider(CommentLinkProvider.class)
.in(SINGLETON);
bind(String.class)
.annotatedWith(CanonicalWebUrl.class)
.toProvider(CanonicalWebUrlProvider.class);
bind(Boolean.class).annotatedWith(DisableReverseDnsLookup.class)
.toProvider(DisableReverseDnsLookupProvider.class).in(SINGLETON);
bind(Boolean.class)
.annotatedWith(DisableReverseDnsLookup.class)
.toProvider(DisableReverseDnsLookupProvider.class)
.in(SINGLETON);
bind(Realm.class).to(FakeRealm.class);
bind(IdentifiedUser.class)
.toProvider(Providers.<IdentifiedUser> of(null));
bind(ReplacePatchSetSender.Factory.class).toProvider(
Providers.<ReplacePatchSetSender.Factory>of(null));
bind(IdentifiedUser.class).toProvider(Providers.<IdentifiedUser>of(null));
bind(ReplacePatchSetSender.Factory.class)
.toProvider(Providers.<ReplacePatchSetSender.Factory>of(null));
bind(CurrentUser.class).to(IdentifiedUser.class);
factory(BatchUpdate.Factory.class);
factory(MergeUtil.Factory.class);
@@ -131,18 +130,17 @@ public class BatchProgramModule extends FactoryModule {
// As Reindex is a batch program, don't assume the index is available for
// the change cache.
bind(SearchingChangeCacheImpl.class).toProvider(
Providers.<SearchingChangeCacheImpl>of(null));
bind(SearchingChangeCacheImpl.class).toProvider(Providers.<SearchingChangeCacheImpl>of(null));
bind(new TypeLiteral<ImmutableSet<GroupReference>>() {})
.annotatedWith(AdministrateServerGroups.class)
.toInstance(ImmutableSet.<GroupReference> of());
.annotatedWith(AdministrateServerGroups.class)
.toInstance(ImmutableSet.<GroupReference>of());
bind(new TypeLiteral<Set<AccountGroup.UUID>>() {})
.annotatedWith(GitUploadPackGroups.class)
.toInstance(Collections.<AccountGroup.UUID> emptySet());
.annotatedWith(GitUploadPackGroups.class)
.toInstance(Collections.<AccountGroup.UUID>emptySet());
bind(new TypeLiteral<Set<AccountGroup.UUID>>() {})
.annotatedWith(GitReceivePackGroups.class)
.toInstance(Collections.<AccountGroup.UUID> emptySet());
.annotatedWith(GitReceivePackGroups.class)
.toInstance(Collections.<AccountGroup.UUID>emptySet());
bind(ChangeControl.Factory.class);
factory(ProjectControl.AssistedFactory.class);
@@ -165,10 +163,7 @@ public class BatchProgramModule extends FactoryModule {
factory(ChangeData.Factory.class);
factory(ProjectState.Factory.class);
bind(ChangeJson.Factory.class).toProvider(
Providers.<ChangeJson.Factory>of(null));
bind(AccountVisibility.class)
.toProvider(AccountVisibilityProvider.class)
.in(SINGLETON);
bind(ChangeJson.Factory.class).toProvider(Providers.<ChangeJson.Factory>of(null));
bind(AccountVisibility.class).toProvider(AccountVisibilityProvider.class).in(SINGLETON);
}
}

View File

@@ -18,9 +18,9 @@ import com.google.gerrit.common.FileUtil;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.util.SystemLog;
import java.io.IOException;
import java.nio.file.Path;
import net.logstash.log4j.JSONEventLayoutV1;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
@@ -28,9 +28,6 @@ import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.eclipse.jgit.lib.Config;
import java.io.IOException;
import java.nio.file.Path;
public class ErrorLogFile {
static final String LOG_NAME = "error_log";
static final String JSON_SUFFIX = ".json";
@@ -54,16 +51,15 @@ public class ErrorLogFile {
public static LifecycleListener start(final Path sitePath, final Config config)
throws IOException {
Path logdir = FileUtil.mkdirsOrDie(new SitePaths(sitePath).logs_dir,
"Cannot create log directory");
Path logdir =
FileUtil.mkdirsOrDie(new SitePaths(sitePath).logs_dir, "Cannot create log directory");
if (SystemLog.shouldConfigure()) {
initLogSystem(logdir, config);
}
return new LifecycleListener() {
@Override
public void start() {
}
public void start() {}
@Override
public void stop() {
@@ -80,13 +76,14 @@ public class ErrorLogFile {
boolean text = config.getBoolean("log", "textLogging", true) || !json;
if (text) {
root.addAppender(SystemLog.createAppender(logdir, LOG_NAME,
new PatternLayout("[%d] [%t] %-5p %c %x: %m%n")));
root.addAppender(
SystemLog.createAppender(
logdir, LOG_NAME, new PatternLayout("[%d] [%t] %-5p %c %x: %m%n")));
}
if (json) {
root.addAppender(SystemLog.createAppender(logdir, LOG_NAME + JSON_SUFFIX,
new JSONEventLayoutV1()));
root.addAppender(
SystemLog.createAppender(logdir, LOG_NAME + JSON_SUFFIX, new JSONEventLayoutV1()));
}
}
}

View File

@@ -23,19 +23,22 @@ import java.util.logging.StreamHandler;
public class GuiceLogger {
private static final Handler HANDLER;
static {
HANDLER = new StreamHandler(System.out, new Formatter() {
@Override
public String format(LogRecord record) {
return String.format("[Guice %s] %s%n", record.getLevel().getName(),
record.getMessage());
}
});
HANDLER =
new StreamHandler(
System.out,
new Formatter() {
@Override
public String format(LogRecord record) {
return String.format(
"[Guice %s] %s%n", record.getLevel().getName(), record.getMessage());
}
});
HANDLER.setLevel(Level.ALL);
}
private GuiceLogger() {
}
private GuiceLogger() {}
public static Logger getLogger() {
return Logger.getLogger("com.google.inject");

View File

@@ -23,10 +23,6 @@ import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.git.WorkQueue;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -36,11 +32,12 @@ import java.nio.file.Path;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.zip.GZIPOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Compresses the old error logs. */
public class LogFileCompressor implements Runnable {
private static final Logger log =
LoggerFactory.getLogger(LogFileCompressor.class);
private static final Logger log = LoggerFactory.getLogger(LogFileCompressor.class);
public static class Module extends LifecycleModule {
@Override
@@ -54,8 +51,7 @@ public class LogFileCompressor implements Runnable {
private final LogFileCompressor compressor;
@Inject
Lifecycle(WorkQueue queue,
LogFileCompressor compressor) {
Lifecycle(WorkQueue queue, LogFileCompressor compressor) {
this.queue = queue;
this.compressor = compressor;
}
@@ -66,15 +62,14 @@ public class LogFileCompressor implements Runnable {
queue.getDefaultQueue().execute(compressor);
ZoneId zone = ZoneId.systemDefault();
LocalDate now = LocalDate.now(zone);
long milliSecondsUntil11pm = now.atStartOfDay(zone)
.plusHours(23).toInstant().toEpochMilli();
queue.getDefaultQueue().scheduleAtFixedRate(compressor,
milliSecondsUntil11pm, HOURS.toMillis(24), MILLISECONDS);
long milliSecondsUntil11pm = now.atStartOfDay(zone).plusHours(23).toInstant().toEpochMilli();
queue
.getDefaultQueue()
.scheduleAtFixedRate(compressor, milliSecondsUntil11pm, HOURS.toMillis(24), MILLISECONDS);
}
@Override
public void stop() {
}
public void stop() {}
}
private final Path logs_dir;

View File

@@ -22,15 +22,14 @@ import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Module to bind a single {@link ReviewDb} instance per thread.
* <p>
* New instances are opened on demand, but are closed only at shutdown.
*
* <p>New instances are opened on demand, but are closed only at shutdown.
*/
class PerThreadReviewDbModule extends LifecycleModule {
private final SchemaFactory<ReviewDb> schema;
@@ -42,38 +41,41 @@ class PerThreadReviewDbModule extends LifecycleModule {
@Override
protected void configure() {
final List<ReviewDb> dbs = Collections.synchronizedList(
new ArrayList<ReviewDb>());
final List<ReviewDb> dbs = Collections.synchronizedList(new ArrayList<ReviewDb>());
final ThreadLocal<ReviewDb> localDb = new ThreadLocal<>();
bind(ReviewDb.class).toProvider(new Provider<ReviewDb>() {
@Override
public ReviewDb get() {
ReviewDb db = localDb.get();
if (db == null) {
try {
db = schema.open();
dbs.add(db);
localDb.set(db);
} catch (OrmException e) {
throw new ProvisionException("unable to open ReviewDb", e);
}
}
return db;
}
});
listener().toInstance(new LifecycleListener() {
@Override
public void start() {
// Do nothing.
}
bind(ReviewDb.class)
.toProvider(
new Provider<ReviewDb>() {
@Override
public ReviewDb get() {
ReviewDb db = localDb.get();
if (db == null) {
try {
db = schema.open();
dbs.add(db);
localDb.set(db);
} catch (OrmException e) {
throw new ProvisionException("unable to open ReviewDb", e);
}
}
return db;
}
});
listener()
.toInstance(
new LifecycleListener() {
@Override
public void start() {
// Do nothing.
}
@Override
public void stop() {
for (ReviewDb db : dbs) {
db.close();
}
}
});
@Override
public void stop() {
for (ReviewDb db : dbs) {
db.close();
}
}
});
}
}

View File

@@ -38,24 +38,20 @@
package com.google.gerrit.pgm.util;
import com.google.common.base.Strings;
import org.eclipse.jgit.util.CachedAuthenticator;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.jgit.util.CachedAuthenticator;
final class ProxyUtil {
/**
* Configure the JRE's standard HTTP based on {@code http_proxy}.
* <p>
* The popular libcurl library honors the {@code http_proxy} environment
* variable as a means of specifying an HTTP proxy for requests made behind a
* firewall. This is not natively recognized by the JRE, so this method can be
* used by command line utilities to configure the JRE before the first
* request is sent.
*
* @throws MalformedURLException the value in {@code http_proxy} is
* unsupportable.
* <p>The popular libcurl library honors the {@code http_proxy} environment variable as a means of
* specifying an HTTP proxy for requests made behind a firewall. This is not natively recognized
* by the JRE, so this method can be used by command line utilities to configure the JRE before
* the first request is sent.
*
* @throws MalformedURLException the value in {@code http_proxy} is unsupportable.
*/
static void configureHttpProxy() throws MalformedURLException {
final String s = System.getenv("http_proxy");
@@ -65,8 +61,7 @@ final class ProxyUtil {
final URL u = new URL((!s.contains("://")) ? "http://" + s : s);
if (!"http".equals(u.getProtocol())) {
throw new MalformedURLException("Invalid http_proxy: " + s
+ ": Only http supported.");
throw new MalformedURLException("Invalid http_proxy: " + s + ": Only http supported.");
}
final String proxyHost = u.getHost();
@@ -82,11 +77,10 @@ final class ProxyUtil {
final int c = userpass.indexOf(':');
final String user = userpass.substring(0, c);
final String pass = userpass.substring(c + 1);
CachedAuthenticator.add(new CachedAuthenticator.CachedAuthentication(
proxyHost, proxyPort, user, pass));
CachedAuthenticator.add(
new CachedAuthenticator.CachedAuthentication(proxyHost, proxyPort, user, pass));
}
}
ProxyUtil() {
}
ProxyUtil() {}
}

View File

@@ -14,11 +14,10 @@
package com.google.gerrit.pgm.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RuntimeShutdown {
private static final ShutdownCallback cb = new ShutdownCallback();
@@ -43,12 +42,10 @@ public class RuntimeShutdown {
cb.manualShutdown();
}
private RuntimeShutdown() {
}
private RuntimeShutdown() {}
private static class ShutdownCallback extends Thread {
private static final Logger log =
LoggerFactory.getLogger(ShutdownCallback.class);
private static final Logger log = LoggerFactory.getLogger(ShutdownCallback.class);
private final List<Runnable> tasks = new ArrayList<>();
private boolean shutdownStarted;
@@ -66,7 +63,6 @@ public class RuntimeShutdown {
}
tasks.add(newTask);
return true;
}
// We don't permit adding a task once shutdown has started.
//

View File

@@ -23,12 +23,9 @@ import com.google.gerrit.server.schema.DataSourceProvider;
import com.google.gerrit.server.schema.DataSourceType;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
import java.nio.file.Path;
import javax.sql.DataSource;
import org.eclipse.jgit.lib.Config;
/** Loads the site library if not yet loaded. */
@Singleton
@@ -37,7 +34,8 @@ public class SiteLibraryBasedDataSourceProvider extends DataSourceProvider {
private boolean init;
@Inject
SiteLibraryBasedDataSourceProvider(SitePaths site,
SiteLibraryBasedDataSourceProvider(
SitePaths site,
@GerritServerConfig Config cfg,
MetricMaker metrics,
ThreadSettingsConfig tsc,

View File

@@ -51,10 +51,6 @@ import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.spi.Message;
import com.google.inject.util.Providers;
import org.eclipse.jgit.lib.Config;
import org.kohsuke.args4j.Option;
import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -63,11 +59,16 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.eclipse.jgit.lib.Config;
import org.kohsuke.args4j.Option;
public abstract class SiteProgram extends AbstractProgram {
@Option(name = "--site-path", aliases = {"-d"}, usage = "Local directory containing site data")
@Option(
name = "--site-path",
aliases = {"-d"},
usage = "Local directory containing site data"
)
private void setSitePath(String path) {
sitePath = Paths.get(path);
}
@@ -76,8 +77,7 @@ public abstract class SiteProgram extends AbstractProgram {
private Path sitePath = Paths.get(".");
protected SiteProgram() {
}
protected SiteProgram() {}
protected SiteProgram(Path sitePath) {
this.sitePath = sitePath;
@@ -96,8 +96,8 @@ public abstract class SiteProgram extends AbstractProgram {
/** Ensures we are running inside of a valid site, otherwise throws a Die. */
protected void mustHaveValidSite() throws Die {
if (!Files.exists(sitePath.resolve("etc").resolve("gerrit.config"))) {
throw die("not a Gerrit site: '" + getSitePath() + "'\n"
+ "Perhaps you need to run init first?");
throw die(
"not a Gerrit site: '" + getSitePath() + "'\n" + "Perhaps you need to run init first?");
}
}
@@ -107,51 +107,55 @@ public abstract class SiteProgram extends AbstractProgram {
}
/** @return provides database connectivity and site path. */
protected Injector createDbInjector(final boolean enableMetrics,
final DataSourceProvider.Context context) {
protected Injector createDbInjector(
final boolean enableMetrics, final DataSourceProvider.Context context) {
final Path sitePath = getSitePath();
final List<Module> modules = new ArrayList<>();
Module sitePathModule = new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
bind(String.class).annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
};
Module sitePathModule =
new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
bind(String.class)
.annotatedWith(SecureStoreClassName.class)
.toProvider(Providers.of(getConfiguredSecureStoreClass()));
}
};
modules.add(sitePathModule);
if (enableMetrics) {
modules.add(new DropWizardMetricMaker.ApiModule());
} else {
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(MetricMaker.class).to(DisabledMetricMaker.class);
}
});
modules.add(
new AbstractModule() {
@Override
protected void configure() {
bind(MetricMaker.class).to(DisabledMetricMaker.class);
}
});
}
modules.add(new LifecycleModule() {
@Override
protected void configure() {
bind(DataSourceProvider.Context.class).toInstance(context);
if (dsProvider != null) {
bind(Key.get(DataSource.class, Names.named("ReviewDb")))
.toProvider(dsProvider)
.in(SINGLETON);
if (LifecycleListener.class.isAssignableFrom(dsProvider.getClass())) {
listener().toInstance((LifecycleListener) dsProvider);
modules.add(
new LifecycleModule() {
@Override
protected void configure() {
bind(DataSourceProvider.Context.class).toInstance(context);
if (dsProvider != null) {
bind(Key.get(DataSource.class, Names.named("ReviewDb")))
.toProvider(dsProvider)
.in(SINGLETON);
if (LifecycleListener.class.isAssignableFrom(dsProvider.getClass())) {
listener().toInstance((LifecycleListener) dsProvider);
}
} else {
bind(Key.get(DataSource.class, Names.named("ReviewDb")))
.toProvider(SiteLibraryBasedDataSourceProvider.class)
.in(SINGLETON);
listener().to(SiteLibraryBasedDataSourceProvider.class);
}
}
} else {
bind(Key.get(DataSource.class, Names.named("ReviewDb")))
.toProvider(SiteLibraryBasedDataSourceProvider.class)
.in(SINGLETON);
listener().to(SiteLibraryBasedDataSourceProvider.class);
}
}
});
});
Module configModule = new GerritServerConfigModule();
modules.add(configModule);
Injector cfgInjector = Guice.createInjector(sitePathModule, configModule);
@@ -167,16 +171,17 @@ public abstract class SiteProgram extends AbstractProgram {
throw new ProvisionException("database.type must be defined");
}
final DataSourceType dst = Guice.createInjector(new DataSourceModule(), configModule,
sitePathModule).getInstance(
Key.get(DataSourceType.class, Names.named(dbType.toLowerCase())));
final DataSourceType dst =
Guice.createInjector(new DataSourceModule(), configModule, sitePathModule)
.getInstance(Key.get(DataSourceType.class, Names.named(dbType.toLowerCase())));
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(DataSourceType.class).toInstance(dst);
}
});
modules.add(
new AbstractModule() {
@Override
protected void configure() {
bind(DataSourceType.class).toInstance(dst);
}
});
modules.add(new DatabaseModule());
modules.add(new SchemaModule());
modules.add(cfgInjector.getInstance(GitRepositoryManagerModule.class));
@@ -192,7 +197,8 @@ public abstract class SiteProgram extends AbstractProgram {
if (why instanceof SQLException) {
throw die("Cannot connect to SQL database", why);
}
if (why instanceof OrmException && why.getCause() != null
if (why instanceof OrmException
&& why.getCause() != null
&& "Unable to determine driver URL".equals(why.getMessage())) {
why = why.getCause();
if (isCannotCreatePoolException(why)) {
@@ -230,12 +236,13 @@ public abstract class SiteProgram extends AbstractProgram {
}
List<Module> modules = new ArrayList<>();
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
}
});
modules.add(
new AbstractModule() {
@Override
protected void configure() {
bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
}
});
modules.add(new GerritServerConfigModule());
modules.add(new DataSourceModule());
Injector i = Guice.createInjector(modules);
@@ -249,16 +256,15 @@ public abstract class SiteProgram extends AbstractProgram {
}
}
}
throw new IllegalStateException(String.format(
"Cannot guess database type from the database product name '%s'",
dbProductName));
throw new IllegalStateException(
String.format(
"Cannot guess database type from the database product name '%s'", dbProductName));
}
@SuppressWarnings("deprecation")
private static boolean isCannotCreatePoolException(Throwable why) {
return why instanceof org.apache.commons.dbcp.SQLNestedException
&& why.getCause() != null
&& why.getMessage().startsWith(
"Cannot create PoolableConnectionFactory");
&& why.getMessage().startsWith("Cannot create PoolableConnectionFactory");
}
}

View File

@@ -19,7 +19,6 @@ import com.google.gerrit.server.config.ThreadSettingsConfig;
import com.google.gerrit.server.schema.DataSourceType;
import com.google.inject.Injector;
import com.google.inject.Key;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -27,8 +26,7 @@ import org.slf4j.LoggerFactory;
// TODO(dborowitz): Not necessary once we switch to NoteDb.
/** Utility to limit threads used by a batch program. */
public class ThreadLimiter {
private static final Logger log =
LoggerFactory.getLogger(ThreadLimiter.class);
private static final Logger log = LoggerFactory.getLogger(ThreadLimiter.class);
public static int limitThreads(Injector dbInjector, int threads) {
return limitThreads(
@@ -38,19 +36,16 @@ public class ThreadLimiter {
threads);
}
private static int limitThreads(Config cfg, DataSourceType dst,
ThreadSettingsConfig threadSettingsConfig, int threads) {
boolean usePool = cfg.getBoolean("database", "connectionpool",
dst.usePool());
private static int limitThreads(
Config cfg, DataSourceType dst, ThreadSettingsConfig threadSettingsConfig, int threads) {
boolean usePool = cfg.getBoolean("database", "connectionpool", dst.usePool());
int poolLimit = threadSettingsConfig.getDatabasePoolLimit();
if (usePool && threads > poolLimit) {
log.warn("Limiting program to " + poolLimit
+ " threads due to database.poolLimit");
log.warn("Limiting program to " + poolLimit + " threads due to database.poolLimit");
return poolLimit;
}
return threads;
}
private ThreadLimiter() {
}
private ThreadLimiter() {}
}

View File

@@ -14,11 +14,10 @@
package com.google.gerrit.pgm.init;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.junit.Ignore;
import java.io.IOException;
import java.nio.file.Path;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.junit.Ignore;
@Ignore
public abstract class InitTestCase extends LocalDiskRepositoryTestCase {

View File

@@ -22,11 +22,9 @@ import static org.junit.Assert.assertNotNull;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Provider;
import org.junit.Test;
import java.nio.file.Paths;
import java.util.Collections;
import org.junit.Test;
public class LibrariesTest {
@Test
@@ -36,12 +34,16 @@ public class LibrariesTest {
replay(ui);
Libraries lib = new Libraries(new Provider<LibraryDownloader>() {
@Override
public LibraryDownloader get() {
return new LibraryDownloader(ui, site);
}
}, Collections.<String> emptyList(), false);
Libraries lib =
new Libraries(
new Provider<LibraryDownloader>() {
@Override
public LibraryDownloader get() {
return new LibraryDownloader(ui, site);
}
},
Collections.<String>emptyList(),
false);
assertNotNull(lib.bouncyCastleProvider);
assertNotNull(lib.mysqlDriver);

View File

@@ -32,20 +32,17 @@ import com.google.gerrit.pgm.init.api.InitFlags;
import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.securestore.SecureStore;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.junit.Test;
public class UpgradeFrom2_0_xTest extends InitTestCase {
@@ -60,8 +57,7 @@ public class UpgradeFrom2_0_xTest extends InitTestCase {
Files.write(p.resolve(n), ("# " + n + "\n").getBytes(UTF_8));
}
FileBasedConfig old =
new FileBasedConfig(p.resolve("gerrit.config").toFile(), FS.DETECTED);
FileBasedConfig old = new FileBasedConfig(p.resolve("gerrit.config").toFile(), FS.DETECTED);
old.setString("ldap", null, "username", "ldap.user");
old.setString("ldap", null, "password", "ldap.s3kr3t");
@@ -71,21 +67,19 @@ public class UpgradeFrom2_0_xTest extends InitTestCase {
old.save();
final InMemorySecureStore secureStore = new InMemorySecureStore();
final InitFlags flags = new InitFlags(site, secureStore,
Collections.<String> emptyList(), false);
final InitFlags flags =
new InitFlags(site, secureStore, Collections.<String>emptyList(), false);
final ConsoleUI ui = createStrictMock(ConsoleUI.class);
Section.Factory sections = new Section.Factory() {
@Override
public Section get(String name, String subsection) {
return new Section(flags, site, secureStore, ui, name, subsection);
}
};
Section.Factory sections =
new Section.Factory() {
@Override
public Section get(String name, String subsection) {
return new Section(flags, site, secureStore, ui, name, subsection);
}
};
expect(ui.yesno(
eq(true),
eq("Upgrade '%s'"),
eq(p.toAbsolutePath().normalize())))
.andReturn(true);
expect(ui.yesno(eq(true), eq("Upgrade '%s'"), eq(p.toAbsolutePath().normalize())))
.andReturn(true);
replay(ui);
UpgradeFrom2_0_x u = new UpgradeFrom2_0_x(site, flags, ui, sections);
@@ -99,13 +93,11 @@ public class UpgradeFrom2_0_xTest extends InitTestCase {
continue;
}
try (InputStream in = Files.newInputStream(site.etc_dir.resolve(n))) {
assertEquals("# " + n + "\n",
new String(ByteStreams.toByteArray(in), UTF_8));
assertEquals("# " + n + "\n", new String(ByteStreams.toByteArray(in), UTF_8));
}
}
FileBasedConfig cfg =
new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
cfg.load();
assertEquals("email.user", cfg.getString("sendemail", null, "smtpUser"));
@@ -128,14 +120,13 @@ public class UpgradeFrom2_0_xTest extends InitTestCase {
}
@Override
public String[] getListForPlugin(String pluginName, String section,
String subsection, String name) {
public String[] getListForPlugin(
String pluginName, String section, String subsection, String name) {
throw new UnsupportedOperationException("not used by tests");
}
@Override
public void setList(String section, String subsection, String name,
List<String> values) {
public void setList(String section, String subsection, String name, List<String> values) {
cfg.setStringList(section, subsection, name, values);
}