use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class TestingUtilities method getWorkerContext.
public static SimpleWorkerContext getWorkerContext(NpmPackage npmPackage, IWorkerContext.IContextResourceLoader loader) throws Exception {
SimpleWorkerContext swc = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).withUserAgent(TestConstants.USER_AGENT).withTerminologyCachePath(TestConstants.TX_CACHE).fromPackage(npmPackage, loader);
TerminologyCache.setCacheErrors(true);
return swc;
}
use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class TestingUtilities method getWorkerContext.
public static IWorkerContext getWorkerContext(String version) {
FilesystemPackageCacheManager pcm;
try {
pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
IWorkerContext fcontext = getWorkerContext(pcm.loadPackage(VersionUtilities.packageForVersion(version), version));
fcontext.setUcumService(new UcumEssenceService(TestingUtilities.loadTestResourceStream("ucum", "ucum-essence.xml")));
fcontext.setExpansionProfile(new Parameters());
if (!fcontext.hasPackage("hl7.terminology", null)) {
NpmPackage utg = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage("hl7.terminology");
System.out.println("Loading THO: " + utg.name() + "#" + utg.version());
fcontext.loadFromPackage(utg, new TestPackageLoader(new String[] { "CodeSystem", "ValueSet" }));
}
return fcontext;
} catch (Exception e) {
e.printStackTrace();
throw new Error(e);
}
}
use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method checkCurrency.
private NpmPackage checkCurrency(String id, NpmPackage p) throws IOException {
checkBuildLoaded();
// special case: current versions roll over, and we have to check their currency
try {
String url = ciList.get(id);
JsonObject json = JsonTrackingParser.fetchJson(Utilities.pathURL(url, "package.manifest.json"));
String currDate = JSONUtil.str(json, "date");
String packDate = p.date();
if (!currDate.equals(packDate)) {
// nup, we need a new copy
return null;
}
} catch (Exception e) {
}
return p;
}
use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method loadPackageFromFile.
private NpmPackage loadPackageFromFile(String id, String folder) throws IOException {
File f = new File(Utilities.path(folder, id));
if (!f.exists()) {
throw new FHIRException("Package '" + id + " not found in folder " + folder);
}
if (!f.isDirectory()) {
throw new FHIRException("File for '" + id + " found in folder " + folder + ", not a folder");
}
File fp = new File(Utilities.path(folder, id, "package", "package.json"));
if (!fp.exists()) {
throw new FHIRException("Package '" + id + " found in folder " + folder + ", but does not contain a package.json file in /package");
}
return NpmPackage.fromFolder(f.getAbsolutePath());
}
use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method addPackageToCache.
/**
* Add an already fetched package to the cache
*/
@Override
public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException {
checkValidVersionString(version, id);
if (progress) {
log("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache");
log(" Fetching:");
}
NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true);
if (progress) {
log("");
logn(" Installing: ");
}
if (!suppressErrors && npm.name() == null || id == null || !id.equalsIgnoreCase(npm.name())) {
if (!id.equals("hl7.fhir.r5.core") && !id.equals("hl7.fhir.us.immds")) {
// temporary work around
throw new IOException("Attempt to import a mis-identified package. Expected " + id + ", got " + npm.name());
}
}
if (version == null)
version = npm.version();
String v = version;
return new CacheLock(id + "#" + version).doWithLock(() -> {
NpmPackage pck = null;
String packRoot = Utilities.path(cacheFolder, id + "#" + v);
try {
// ok, now we have a lock on it... check if something created it while we were waiting
if (!new File(packRoot).exists() || Utilities.existsInList(v, "current", "dev")) {
Utilities.createDirectory(packRoot);
try {
Utilities.clearDirectory(packRoot);
} catch (Throwable t) {
log("Unable to clear directory: " + packRoot + ": " + t.getMessage() + " - this may cause problems later");
}
int i = 0;
int c = 0;
int size = 0;
for (Entry<String, NpmPackageFolder> e : npm.getFolders().entrySet()) {
String dir = e.getKey().equals("package") ? Utilities.path(packRoot, "package") : Utilities.path(packRoot, "package", e.getKey());
if (!(new File(dir).exists()))
Utilities.createDirectory(dir);
for (Entry<String, byte[]> fe : e.getValue().getContent().entrySet()) {
String fn = Utilities.path(dir, Utilities.cleanFileName(fe.getKey()));
byte[] cnt = fe.getValue();
TextFile.bytesToFile(cnt, fn);
size = size + cnt.length;
i++;
if (progress && i % 50 == 0) {
c++;
logn(".");
if (c == 120) {
log("");
logn(" ");
c = 2;
}
}
}
}
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat("yyyyMMddhhmmss");
ini.setTimestampProperty("packages", id + "#" + v, Timestamp.from(Instant.now()), null);
ini.setIntegerProperty("package-sizes", id + "#" + v, size, null);
ini.save();
if (progress)
log(" done.");
}
pck = loadPackageInfo(packRoot);
if (!id.equals(JSONUtil.str(npm.getNpm(), "name")) || !v.equals(JSONUtil.str(npm.getNpm(), "version"))) {
if (!id.equals(JSONUtil.str(npm.getNpm(), "name"))) {
npm.getNpm().addProperty("original-name", JSONUtil.str(npm.getNpm(), "name"));
npm.getNpm().remove("name");
npm.getNpm().addProperty("name", id);
}
if (!v.equals(JSONUtil.str(npm.getNpm(), "version"))) {
npm.getNpm().addProperty("original-version", JSONUtil.str(npm.getNpm(), "version"));
npm.getNpm().remove("version");
npm.getNpm().addProperty("version", v);
}
TextFile.stringToFile(new GsonBuilder().setPrettyPrinting().create().toJson(npm.getNpm()), Utilities.path(cacheFolder, id + "#" + v, "package", "package.json"), false);
}
} catch (Exception e) {
try {
// don't leave a half extracted package behind
log("Clean up package " + packRoot + " because installation failed: " + e.getMessage());
e.printStackTrace();
Utilities.clearDirectory(packRoot);
new File(packRoot).delete();
} catch (Exception ei) {
// nothing
}
throw e;
}
return pck;
});
}
Aggregations