use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.
the class BaseInit method createSiteInit.
private SiteInit createSiteInit() {
final ConsoleUI ui = getConsoleUI();
final Path sitePath = getSitePath();
final List<Module> m = new ArrayList<>();
final SecureStoreInitData secureStoreInitData = discoverSecureStoreClass();
final String currentSecureStoreClassName = getConfiguredSecureStoreClass();
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);
throw die(err);
}
m.add(new GerritServerConfigModule());
m.add(new InitModule(standalone));
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<>());
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());
bind(MetricMaker.class).to(DisabledMetricMaker.class);
}
});
try {
return Guice.createInjector(PRODUCTION, m).getInstance(SiteInit.class);
} catch (CreationException ce) {
final Message first = ce.getErrorMessages().iterator().next();
Throwable why = first.getCause();
if (why instanceof Die) {
throw (Die) why;
}
final StringBuilder buf = new StringBuilder(ce.getMessage());
while (why != null) {
buf.append("\n");
buf.append(why.getMessage());
why = why.getCause();
if (why != null) {
buf.append("\n caused by ");
}
}
throw die(buf.toString(), new RuntimeException("InitInjector failed", ce));
}
}
use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.
the class LibraryDownloader method doGet.
private void doGet() {
if (!Files.exists(lib_dir)) {
try {
Files.createDirectories(lib_dir);
} catch (IOException e) {
throw new Die("Cannot create " + lib_dir, e);
}
}
try {
remover.remove(remove);
if (download) {
doGetByHttp();
} else {
doGetByLocalCopy();
}
verifyFileChecksum();
} catch (IOException err) {
try {
Files.delete(dst);
} catch (IOException e) {
// Delete failed; leave alone.
}
if (ui.isBatch()) {
throw new Die("error: Cannot get " + jarUrl, err);
}
System.err.println();
System.err.println();
System.err.println("error: " + err.getMessage());
System.err.println("Please download:");
System.err.println();
System.err.println(" " + jarUrl);
System.err.println();
System.err.println("and save as:");
System.err.println();
System.err.println(" " + dst.toAbsolutePath());
System.err.println();
System.err.flush();
ui.waitForUser();
if (Files.exists(dst)) {
verifyFileChecksum();
} else if (!ui.yesno(!required, "Continue without this library")) {
throw new Die("aborted by user");
}
}
if (Files.exists(dst)) {
exists = true;
IoUtil.loadJARs(dst);
}
}
use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.
the class LibraryDownloader method verifyFileChecksum.
private void verifyFileChecksum() {
if (sha1 == null) {
System.err.println();
System.err.flush();
return;
}
Hasher h = Hashing.sha1().newHasher();
try (InputStream in = Files.newInputStream(dst);
OutputStream out = Funnels.asOutputStream(h)) {
ByteStreams.copy(in, out);
} catch (IOException e) {
deleteDst();
throw new Die("cannot checksum " + dst, e);
}
if (sha1.equals(h.hash().toString())) {
System.err.println("Checksum " + dst.getFileName() + " OK");
System.err.flush();
} else if (ui.isBatch()) {
deleteDst();
throw new Die(dst + " SHA-1 checksum does not match");
} else if (!ui.yesno(null, //
"error: SHA-1 checksum does not match\nUse %s anyway", dst.getFileName())) {
deleteDst();
throw new Die("aborted by user");
}
}
use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.
the class AbstractProgram method main.
public final int main(String[] argv) throws Exception {
final CmdLineParser clp = new CmdLineParser(OptionHandlers.empty(), this);
try {
clp.parseArgument(argv);
} catch (CmdLineException err) {
if (!clp.wasHelpRequestedByOption()) {
System.err.println("fatal: " + err.getMessage());
return 1;
}
}
if (clp.wasHelpRequestedByOption()) {
StringWriter msg = new StringWriter();
clp.printDetailedUsage(getName(), msg);
System.err.println(msg.toString());
return 1;
}
try {
ProxyUtil.configureHttpProxy();
return run();
} catch (Die err) {
if (showStackTrace) {
err.printStackTrace();
} else {
final Throwable cause = err.getCause();
final String diemsg = err.getMessage();
if (cause != null && !cause.getMessage().equals(diemsg)) {
System.err.println("fatal: " + cause.getMessage().replace("\n", "\nfatal: "));
}
System.err.println("fatal: " + diemsg.replace("\n", "\nfatal: "));
}
return 128;
}
}
use of com.google.gerrit.common.Die in project gerrit by GerritCodeReview.
the class StaleLibraryRemover method remove.
public void remove(String pattern) {
if (!Strings.isNullOrEmpty(pattern)) {
DirectoryStream.Filter<Path> filter = entry -> entry.getFileName().toString().matches("^" + pattern + "$");
try (DirectoryStream<Path> paths = Files.newDirectoryStream(lib_dir, filter)) {
for (Path p : paths) {
String old = p.getFileName().toString();
String bak = "." + old + ".backup";
Path dest = p.resolveSibling(bak);
if (Files.exists(dest)) {
ui.message("WARNING: not renaming %s to %s: already exists\n", old, bak);
continue;
}
ui.message("Renaming %s to %s\n", old, bak);
try {
Files.move(p, dest);
} catch (IOException e) {
throw new Die("cannot rename " + old, e);
}
}
} catch (IOException e) {
throw new Die("cannot remove stale library versions", e);
}
}
}
Aggregations