Package manager

This commit is contained in:
jamesagnew 2020-05-24 18:26:03 -04:00
parent 444b612805
commit 37f6a6ed8c
3 changed files with 75 additions and 49 deletions

View File

@ -11,6 +11,13 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
private List<String> myPackageServers = new ArrayList<>(); private List<String> myPackageServers = new ArrayList<>();
/**
* Constructor
*/
public BasePackageCacheManager() {
super();
}
public List<String> getPackageServers() { public List<String> getPackageServers() {
return myPackageServers; return myPackageServers;
} }
@ -26,12 +33,55 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager {
} }
/**
* Load the latest version of the identified package from the cache - it it exists
*
* @param id
* @return
* @throws IOException
*/
public NpmPackage loadPackageFromCacheOnly(String id) throws IOException {
return loadPackageFromCacheOnly(id, null);
}
protected abstract NpmPackage loadPackageFromCacheOnly(String id, String version) throws IOException;
@Override @Override
public String getPackageId(String canonical) throws IOException { public String getPackageUrl(String packageId) throws IOException {
String result = null;
NpmPackage npm = loadPackageFromCacheOnly(packageId);
if (npm != null) {
return npm.canonical();
}
for (String nextPackageServer : getPackageServers()) {
result = getPackageUrl(packageId, nextPackageServer);
if (result != null) {
return result;
}
}
return result;
}
private String getPackageUrl(String packageId, String server) throws IOException {
PackageClient pc = new PackageClient(server);
List<PackageClient.PackageInfo> res = pc.search(packageId, null, null, false);
if (res.size() == 0) {
return null;
} else {
return res.get(0).getUrl();
}
}
@Override
public String getPackageId(String canonicalUrl) throws IOException {
String result = null; String result = null;
for (String nextPackageServer : getPackageServers()) { for (String nextPackageServer : getPackageServers()) {
result = getPackageId(canonical, nextPackageServer); result = getPackageId(canonicalUrl, nextPackageServer);
if (result != null) { if (result != null) {
break; break;
} }

View File

@ -80,10 +80,12 @@ import java.util.Map.Entry;
* @author Grahame Grieve * @author Grahame Grieve
*/ */
public class FilesystemPackageCacheManager extends BasePackageCacheManager implements IPackageCacheManager { public class FilesystemPackageCacheManager extends BasePackageCacheManager implements IPackageCacheManager {
private static final Logger ourLog = LoggerFactory.getLogger(FilesystemPackageCacheManager.class);
public static final String PACKAGE_REGEX = "^[a-z][a-z0-9\\_\\-]*(\\.[a-z0-9\\_\\-]+)+$"; public static final String PACKAGE_REGEX = "^[a-z][a-z0-9\\_\\-]*(\\.[a-z0-9\\_\\-]+)+$";
public static final String PACKAGE_VERSION_REGEX = "^[a-z][a-z0-9\\_\\-]*(\\.[a-z0-9\\_\\-]+)+\\#[a-z0-9\\-\\_]+(\\.[a-z0-9\\-\\_]+)*$"; public static final String PACKAGE_VERSION_REGEX = "^[a-z][a-z0-9\\_\\-]*(\\.[a-z0-9\\_\\-]+)+\\#[a-z0-9\\-\\_]+(\\.[a-z0-9\\-\\_]+)*$";
private static final Logger ourLog = LoggerFactory.getLogger(FilesystemPackageCacheManager.class);
private static final String CACHE_VERSION = "3"; // second version - see wiki page private static final String CACHE_VERSION = "3"; // second version - see wiki page
public static final String PRIMARY_SERVER = "http://packages.fhir.org";
public static final String SECONDARY_SERVER = "http://packages2.fhir.org/packages";
private String cacheFolder; private String cacheFolder;
private boolean progress = true; private boolean progress = true;
private List<NpmPackage> temporaryPackages = new ArrayList<NpmPackage>(); private List<NpmPackage> temporaryPackages = new ArrayList<NpmPackage>();
@ -95,8 +97,8 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
* Constructor * Constructor
*/ */
public FilesystemPackageCacheManager(boolean userMode, int toolsVersion) throws IOException { public FilesystemPackageCacheManager(boolean userMode, int toolsVersion) throws IOException {
addPackageServer("http://packages.fhir.org"); addPackageServer(PRIMARY_SERVER);
addPackageServer("http://packages2.fhir.org/packages"); addPackageServer(SECONDARY_SERVER);
if (userMode) if (userMode)
cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages"); cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages");
@ -196,15 +198,6 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
} }
private String getPackageUrl(String packageId, String server) throws IOException {
PackageClient pc = new PackageClient(server);
List<PackageInfo> res = pc.search(packageId, null, null, false);
if (res.size() == 0) {
return null;
} else {
return res.get(0).getUrl();
}
}
private void listSpecs(Map<String, String> specList, String server) throws IOException { private void listSpecs(Map<String, String> specList, String server) throws IOException {
PackageClient pc = new PackageClient(server); PackageClient pc = new PackageClient(server);
@ -293,17 +286,6 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
} }
/**
* Load the latest version of the identified package from the cache - it it exists
*
* @param id
* @return
* @throws IOException
*/
public NpmPackage loadPackageFromCacheOnly(String id) throws IOException {
return loadPackageFromCacheOnly(id, null);
}
/** /**
* Load the identified package from the cache - it it exists * Load the identified package from the cache - it it exists
* <p> * <p>
@ -315,7 +297,8 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
* @return * @return
* @throws IOException * @throws IOException
*/ */
public NpmPackage loadPackageFromCacheOnly(String id, String version) throws IOException { @Override
protected NpmPackage loadPackageFromCacheOnly(String id, String version) throws IOException {
if (!Utilities.noString(version) && version.startsWith("file:")) { if (!Utilities.noString(version) && version.startsWith("file:")) {
return loadPackageFromFile(id, version.substring(5)); return loadPackageFromFile(id, version.substring(5));
} }
@ -345,14 +328,15 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
/** /**
* Add an already fetched package to the cache * Add an already fetched package to the cache
*/ */
public NpmPackage addPackageToCache(String id, String version, InputStream tgz, String sourceDesc) throws IOException { @Override
public NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException {
checkValidVersionString(version, id); checkValidVersionString(version, id);
if (progress) { if (progress) {
System.out.println("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache"); System.out.println("Installing " + id + "#" + (version == null ? "?" : version) + " to the package cache");
System.out.print(" Fetching:"); System.out.print(" Fetching:");
} }
NpmPackage npm = NpmPackage.fromPackage(tgz, sourceDesc, true); NpmPackage npm = NpmPackage.fromPackage(packageTgzInputStream, sourceDesc, true);
if (progress) { if (progress) {
System.out.println(); System.out.println();
@ -435,19 +419,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
@Override @Override
public String getPackageUrl(String packageId) throws IOException { public String getPackageUrl(String packageId) throws IOException {
String result = null; String result = super.getPackageUrl(packageId);
NpmPackage npm = loadPackageFromCacheOnly(packageId);
if (npm != null) {
return npm.canonical();
}
for (String nextPackageServer : getPackageServers()) {
result = getPackageUrl(nextPackageServer);
if (result != null) {
return result;
}
}
if (result == null) { if (result == null) {
result = getPackageUrlFromBuildList(packageId); result = getPackageUrlFromBuildList(packageId);
} }
@ -459,8 +431,9 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
for (NpmPackage p : temporaryPackages) { for (NpmPackage p : temporaryPackages) {
specList.put(p.name(), p.canonical()); specList.put(p.name(), p.canonical());
} }
listSpecs(specList, PRIMARY_SERVER); for (String next : getPackageServers()) {
listSpecs(specList, SECONDARY_SERVER); listSpecs(specList, next);
}
addCIBuildSpecs(specList); addCIBuildSpecs(specList);
} }
@ -550,11 +523,11 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} }
@Override @Override
public String getPackageId(String canonical) throws IOException { public String getPackageId(String canonicalUrl) throws IOException {
String retVal = super.getPackageId(canonical); String retVal = super.getPackageId(canonicalUrl);
if (retVal == null) { if (retVal == null) {
retVal = getPackageIdFromBuildList(canonical); retVal = getPackageIdFromBuildList(canonicalUrl);
} }
return retVal; return retVal;

View File

@ -3,11 +3,14 @@ package org.hl7.fhir.utilities.cache;
import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.exceptions.FHIRException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
public interface IPackageCacheManager { public interface IPackageCacheManager {
String getPackageId(String canonical) throws IOException; String getPackageId(String canonicalUrl) throws IOException;
NpmPackage addPackageToCache(String id, String version, InputStream packageTgzInputStream, String sourceDesc) throws IOException;
String getPackageUrl(String packageId) throws IOException; String getPackageUrl(String packageId) throws IOException;