Search in sources :

Example 21 with NpmPackage

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;
}
Also used : SimpleWorkerContext(org.hl7.fhir.r5.context.SimpleWorkerContext)

Example 22 with NpmPackage

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);
    }
}
Also used : UcumEssenceService(org.fhir.ucum.UcumEssenceService) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) IWorkerContext(org.hl7.fhir.r5.context.IWorkerContext) Parameters(org.hl7.fhir.r5.model.Parameters) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 23 with NpmPackage

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;
}
Also used : JsonObject(com.google.gson.JsonObject) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 24 with NpmPackage

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());
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 25 with NpmPackage

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;
    });
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile) GsonBuilder(com.google.gson.GsonBuilder) IOException(java.io.IOException) NpmPackageFolder(org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException) RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Aggregations

NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)34 File (java.io.File)15 FHIRException (org.hl7.fhir.exceptions.FHIRException)15 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)13 IOException (java.io.IOException)11 Test (org.junit.jupiter.api.Test)11 FileNotFoundException (java.io.FileNotFoundException)9 TextFile (org.hl7.fhir.utilities.TextFile)8 FileOutputStream (java.io.FileOutputStream)7 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 PackageResourceInformation (org.hl7.fhir.utilities.npm.NpmPackage.PackageResourceInformation)6 SimpleWorkerContext (org.hl7.fhir.r5.context.SimpleWorkerContext)4 IniFile (org.hl7.fhir.utilities.IniFile)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 URISyntaxException (java.net.URISyntaxException)3 ParseException (java.text.ParseException)3 NotImplementedException (org.apache.commons.lang3.NotImplementedException)3 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)3