private boolean writeModList(File file, ForgeModList modList) { try { log.info("Writing mod list to '{}'...", file.getAbsolutePath()); File parent = file.getParentFile(); if (!parent.exists() && !parent.mkdirs()) { log.error("Failed to create required directories for file '{}', aborting!", file.getAbsolutePath()); return false; } BufferedWriter writer = new BufferedWriter(new FileWriter(file)); JsonWriter jsonWriter = new JsonWriter(writer); jsonWriter.setIndent(" "); CurseSync.GSON.toJson(modList, ForgeModList.class, jsonWriter); writer.close(); } catch (IOException e) { log.error(new FormattedMessageFactory().newMessage("Failed to write Mod List JSON '{}', aborting!", file.getAbsolutePath()), e); return false; } return true; }
@Override public boolean validateOldChecksums(File directory) { if (installation.overrides == null) { return true; } boolean success = true; for (FileOverride override : installation.overrides) { File overrideFile = new File(directory, override.path); try { if (!overrideFile.exists()) { log.log(config.failDiscrepancies ? Level.ERROR : Level.WARN, "Expected file '{}' did not exit, can't calculate checksum!", overrideFile.getAbsolutePath()); success = !config.failDiscrepancies; continue; } InputStream digestStream = new FileInputStream(overrideFile); String checksum = DigestUtils.md5Hex(digestStream); digestStream.close(); if (!Objects.equals(checksum, override.checksum)) { log.log(config.failDiscrepancies ? Level.ERROR : Level.WARN, "Found discrepancies between existing file '{}' and it's last known checksum!", overrideFile.getAbsolutePath()); log.log(config.failDiscrepancies ? Level.ERROR : Level.WARN, " - Stored Checksum: {}", override.checksum); log.log(config.failDiscrepancies ? Level.ERROR : Level.WARN, " - Calculated Checksum: {}", checksum); success = !config.failDiscrepancies; } } catch (IOException e) { log.log(config.failDiscrepancies ? Level.ERROR : Level.WARN, new FormattedMessageFactory().newMessage("Failed to calculate checksum of file {}!", overrideFile.getAbsolutePath()), e); success = !config.failDiscrepancies; } } return success; }
@Nullable private List<FileOverride> getChecksums(File relative, File directory) { ImmutableList.Builder<FileOverride> checksums = new ImmutableList.Builder<>(); File[] files = directory.listFiles(); if (files != null) { for (File f : files) { if (f.isDirectory()) { List<FileOverride> subResult = getChecksums(relative, f); if (subResult == null) return null; checksums.addAll(subResult); } else { try { InputStream digestStream = new FileInputStream(f); String checksum = DigestUtils.md5Hex(digestStream); digestStream.close(); checksums.add(new FileOverride(relative.toPath().relativize(f.toPath()).toString(), checksum)); } catch (IOException e) { log.error(new FormattedMessageFactory().newMessage("Failed to calculate checksum of file {}!", f.getAbsolutePath()), e); return null; } } } } return checksums.build(); }
public boolean downloadFile(URI url, File destination, int trials) { log.info("Downloading '{}' to '{}'...", url, destination.getAbsolutePath()); if (destination.exists()) { log.info("File already exists, assuming equivalence and skipping download..."); return true; } File parent = destination.getParentFile(); if (!parent.exists() && !destination.getParentFile().mkdirs()) { log.error("Failed to create required directories, cancelling download."); return false; } for (int trial = 1; trial <= trials; trial++) { try { HttpUriRequest request = new HttpGet(url.toURL().toString()); HttpResponse response = http.execute(request); if (response.getStatusLine().getStatusCode() == 404) { log.error("'{}' could not be found on the server, cancelling download.", url); return false; } BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destination)); int inByte; while ((inByte = bis.read()) != -1) bos.write(inByte); bis.close(); bos.close(); log.info("Successfully downloaded file to '{}'", destination.getAbsolutePath()); return true; } catch (Exception e) { log.error(new FormattedMessageFactory().newMessage("Failed to download file, starting attempt #{}.", trial + 1), e); } } log.error("Failed to download file after {} attempts.", trials); return false; }
ApimanLog4j2LogDelegate(final String name) { logger = (ExtendedLogger) org.apache.logging.log4j.LogManager.getLogger(name, new FormattedMessageFactory()); }
@Override public IApimanLogger createLogger(String name) { return new Log4j2LoggerImpl(LogManager.getLogger(name, new FormattedMessageFactory())); }
@Override public IApimanLogger createLogger(Class<?> klazz) { return new Log4j2LoggerImpl(LogManager.getLogger(klazz, new FormattedMessageFactory())); }
public ApacheLogging(final String name) { logger = LogManager.getLogger(name, new FormattedMessageFactory()); }
public ApacheLogging(final Class<?> datClass) { logger = LogManager.getLogger(datClass, new FormattedMessageFactory()); }