diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java index 00cb450c8..d3834ebe9 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java @@ -18,6 +18,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { private static final Logger ourLog = LoggerFactory.getLogger(BasePackageCacheManager.class); private List myPackageServers = new ArrayList<>(); private Function myClientFactory = address -> new CachingPackageClient(address); + protected boolean silent; /** * Constructor @@ -77,7 +78,9 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { String url = packageClient.url(id, version); return new InputStreamWithSrc(stream, url, version); } catch (IOException e) { - ourLog.info("Failed to resolve package {}#{} from server: {} ({})", id, version, nextPackageServer, e.getMessage()); + if (!silent) { + ourLog.info("Failed to resolve package {}#{} from server: {} ({})", id, version, nextPackageServer, e.getMessage()); + } } } } @@ -177,4 +180,9 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { return loadPackage(idAndVer, null); } + + public void setSilent(boolean silent) { + this.silent = silent; + } + } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java index da4d91bc5..08e2a148a 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java @@ -344,15 +344,15 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException { checkValidVersionString(version, id); if (progress) { - System.out.println("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache"); - System.out.print(" Fetching:"); + log("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache"); + log(" Fetching:"); } NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true); if (progress) { - System.out.println(); - System.out.print(" Installing: "); + log(""); + logn(" Installing: "); } if (!suppressErrors && npm.name() == null || id == null || !id.equalsIgnoreCase(npm.name())) { @@ -374,7 +374,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple try { Utilities.clearDirectory(packRoot); } catch (Throwable t) { - System.out.println("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later"); + log("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later"); } int i = 0; @@ -392,10 +392,10 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple i++; if (progress && i % 50 == 0) { c++; - System.out.print("."); + logn("."); if (c == 120) { - System.out.println(""); - System.out.print(" "); + log(""); + logn(" "); c = 2; } } @@ -409,7 +409,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple ini.setIntegerProperty("package-sizes", id + "#" + v, size, null); ini.save(); if (progress) - System.out.println(" done."); + log(" done."); } pck = loadPackageInfo(packRoot); if (!id.equals(JSONUtil.str(npm.getNpm(), "name")) || !v.equals(JSONUtil.str(npm.getNpm(), "version"))) { @@ -428,7 +428,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } catch (Exception e) { try { // don't leave a half extracted package behind - System.out.println("Clean up package " + packRoot + " because installation failed: " + e.getMessage()); + log("Clean up package " + packRoot + " because installation failed: " + e.getMessage()); e.printStackTrace(); Utilities.clearDirectory(packRoot); new File(packRoot).delete(); @@ -441,6 +441,18 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple }); } + private void log(String s) { + if (!silent) { + System.out.println(s); + } + } + + private void logn(String s) { + if (!silent) { + System.out.print(s); + } + } + @Override public String getPackageUrl(String packageId) throws IOException { String result = super.getPackageUrl(packageId); @@ -646,7 +658,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple try { loadFromBuildServer(); } catch (Exception e) { - System.out.println("Error connecting to build server - running without build (" + e.getMessage() + ")"); + log("Error connecting to build server - running without build (" + e.getMessage() + ")"); e.printStackTrace(); } return false; diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java index 96c3bc346..fbb155854 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java @@ -63,8 +63,10 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; +import org.hl7.fhir.utilities.SimpleHTTPClient; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.json.JSONUtil; import org.hl7.fhir.utilities.json.JsonTrackingParser; import org.hl7.fhir.utilities.npm.NpmPackage.ITransformingLoader; @@ -1159,6 +1161,13 @@ public class NpmPackage { return new Date(); } } + + public static NpmPackage fromUrl(String source) throws IOException { + SimpleHTTPClient fetcher = new SimpleHTTPClient(); + HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis()); + res.checkThrowException(); + return fromPackage(new ByteArrayInputStream(res.getContent())); + } } \ No newline at end of file