use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.
the class TerminologyCacheManager method initialize.
public void initialize() throws IOException {
File f = new File(cacheFolder);
if (!f.exists()) {
Utilities.createDirectory(cacheFolder);
}
if (!version.equals(getCacheVersion())) {
clearCache();
fillCache("http://tx.fhir.org/tx-cache/" + ghOrg + "/" + ghRepo + "/" + ghBranch + ".zip");
}
if (!version.equals(getCacheVersion())) {
clearCache();
fillCache("http://tx.fhir.org/tx-cache/" + ghOrg + "/" + ghRepo + "/default.zip");
}
if (!version.equals(getCacheVersion())) {
clearCache();
}
IniFile ini = new IniFile(Utilities.path(cacheFolder, "cache.ini"));
ini.setStringProperty("cache", "version", version, null);
ini.setDateProperty("cache", "last-use", new Date(), null);
ini.save();
}
use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method createIniFile.
private void createIniFile() throws IOException {
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
boolean save = false;
String v = ini.getStringProperty("cache", "version");
if (!CACHE_VERSION.equals(v)) {
clearCache();
ini.setStringProperty("cache", "version", CACHE_VERSION, null);
ini.save();
}
}
use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method removePackage.
// ========================= Utilities ============================================================================
/**
* Remove a particular package from the cache
*
* @param id
* @param ver
* @throws IOException
*/
public void removePackage(String id, String ver) throws IOException {
new CacheLock(id + "#" + ver).doWithLock(() -> {
String f = Utilities.path(cacheFolder, id + "#" + ver);
File ff = new File(f);
if (ff.exists()) {
Utilities.clearDirectory(f);
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.removeProperty("packages", id + "#" + ver);
ini.save();
ff.delete();
}
return null;
});
}
use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method clearCache.
private void clearCache() throws IOException {
for (File f : new File(cacheFolder).listFiles()) {
if (f.isDirectory()) {
new CacheLock(f.getName()).doWithLock(() -> {
Utilities.clearDirectory(f.getAbsolutePath());
try {
FileUtils.deleteDirectory(f);
} catch (Exception e1) {
try {
FileUtils.deleteDirectory(f);
} catch (Exception e2) {
// just give up
}
}
// must return something
return null;
});
} else if (!f.getName().equals("packages.ini"))
FileUtils.forceDelete(f);
}
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.removeSection("packages");
ini.save();
}
use of org.hl7.fhir.utilities.IniFile in project org.hl7.fhir.core by hapifhir.
the class BatchLoader method sendFile.
private static void sendFile(FHIRToolingClient client, File f, int size, IniFile ini) throws FHIRFormatError, FileNotFoundException, IOException {
long ms = System.currentTimeMillis();
System.out.print("Loading " + f.getName() + ".. ");
IParser parser = f.getName().endsWith(".json") ? new JsonParser() : new XmlParser();
Resource res = parser.parse(new FileInputStream(f));
System.out.println(" done: (" + Long.toString(System.currentTimeMillis() - ms) + " ms)");
if (res instanceof Bundle) {
Bundle bnd = (Bundle) res;
int cursor = ini.hasProperty("progress", f.getName()) ? ini.getIntegerProperty("progress", f.getName()) : 0;
while (cursor < bnd.getEntry().size()) {
Bundle bt = new Bundle();
bt.setType(BundleType.BATCH);
bt.setId(UUID.randomUUID().toString().toLowerCase());
for (int i = cursor; i < Math.min(bnd.getEntry().size(), cursor + size); i++) {
if (i >= 0 && i < bnd.getEntry().size()) {
BundleEntryComponent be = bt.addEntry();
be.setResource(bnd.getEntry().get(i).getResource());
be.getRequest().setMethod(HTTPVerb.PUT);
be.getRequest().setUrl(be.getResource().getResourceType().toString() + "/" + be.getResource().getId());
}
}
System.out.print(f.getName() + " (" + cursor + "/" + bnd.getEntry().size() + "): ");
ms = System.currentTimeMillis();
Bundle resp = client.transaction(bt);
int ncursor = cursor + size;
for (int i = 0; i < resp.getEntry().size(); i++) {
BundleEntryComponent t = resp.getEntry().get(i);
if (!t.getResponse().getStatus().startsWith("2")) {
System.out.println("failed status at " + Integer.toString(i) + ": " + t.getResponse().getStatus());
ncursor = cursor + i - 1;
break;
}
}
cursor = ncursor;
System.out.println(" .. done: (" + Long.toString(System.currentTimeMillis() - ms) + " ms) " + SimpleDateFormat.getInstance().format(new Date()));
ini.setIntegerProperty("progress", f.getName(), cursor, null);
ini.save();
}
ini.setBooleanProperty("finished", f.getName(), true, null);
ini.save();
} else {
client.update(res);
ini.setBooleanProperty("finished", f.getName(), true, null);
ini.save();
}
}
Aggregations