@RequiredPermission(Permission.MANAGE_REPOSITORIES)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionTimeout(90 * 60)
public PackageBits downloadPackageBits(Subject subject, PackageVersionContentSource pvcs) {
PackageVersionContentSourcePK pk = pvcs.getPackageVersionContentSourcePK();
int contentSourceId = pk.getContentSource().getId();
int packageVersionId = pk.getPackageVersion().getId();
String packageVersionLocation = pvcs.getLocation();
switch (pk.getContentSource().getDownloadMode()) {
case NEVER: {
return null; // no-op, our content source was told to never download package bits
}
case DATABASE: {
log.debug("Downloading package bits to DB for package located at [" + packageVersionLocation
+ "] on content source [" + contentSourceId + "]");
break;
}
case FILESYSTEM: {
log.debug("Downloading package bits to filesystem for package located at [" + packageVersionLocation
+ "] on content source [" + contentSourceId + "]");
break;
}
default: {
throw new IllegalStateException(" Unknown download mode - this is a bug, please report it: " + pvcs);
}
}
InputStream bitsStream = null;
PackageBits packageBits = null;
try {
ContentServerPluginContainer pc = ContentManagerHelper.getPluginContainer();
bitsStream = pc.getAdapterManager().loadPackageBits(contentSourceId, packageVersionLocation);
Connection conn = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
try {
packageBits = createPackageBits(pk.getContentSource().getDownloadMode() == DownloadMode.DATABASE);
PackageVersion pv = entityManager.find(PackageVersion.class, packageVersionId);
pv.setPackageBits(packageBits); // associate the entities
entityManager.flush(); // may not be necessary
if (pk.getContentSource().getDownloadMode() == DownloadMode.DATABASE) {
conn = dataSource.getConnection();
// The blob has been initialized to EMPTY_BLOB already by createPackageBits...
// we need to lock the row which will be updated so we are using FOR UPDATE
ps = conn.prepareStatement("SELECT BITS FROM " + PackageBits.TABLE_NAME
+ " WHERE ID = ? FOR UPDATE");