+ "] has had its bits file deleted but cannot download it again.", e);
}
}
}
PackageVersionContentSource pvcs = null; // will be non-null only if package bits were not originally available
if (!packageBitsAreAvailable) {
if (resourceId == -1) {
throw new IllegalStateException("Package bits must be inserted prior to the agent asking for them "
+ "during a cotent-based resource creation");
}
// if we got here, the package bits have not been downloaded yet. This eliminates the
// possibility that the package version were directly uploaded by a user
// or auto-discovered by a resource and attached to a repo. So, that leaves
// the only possibility - the package version comes from a content source and therefore has
// a PackageVersionContentSource mapping. Let's find that mapping.
Query q2 = entityManager.createNamedQuery(PackageVersionContentSource.QUERY_FIND_BY_PKG_VER_ID_AND_RES_ID);
q2.setParameter("resourceId", resourceId);
q2.setParameter("packageVersionId", packageVersionId);
List<PackageVersionContentSource> pvcss = q2.getResultList();
// Note that its possible more than one content source can deliver a PV - if a resource is subscribed
// to repo(s) that contain multiple content sources that can deliver a PV, we just take
// the first one we find.
if (pvcss.size() == 0) {
throw new RuntimeException("Resource [" + resourceId + "] cannot access package version ["
+ packageDetailsKey + "] - no content source exists to deliver it");
}
pvcs = pvcss.get(0);
// Make it a true EJB call so we suspend our tx and get a new tx.
// This way, we start with a fresh tx timeout when downloading and this
// won't affect the time we are in in this method's tx (I hope that's how it works).
// This is because this method itself may take a long time to send the data to the output stream
// and we don't want out tx timing out due to the time it takes downloading.
PackageBits bits = null;
bits = contentSourceManager.downloadPackageBits(subjectManager.getOverlord(), pvcs);
if (bits != null) {
// rerun the query just to make sure we really downloaded it successfully
query.setParameter("id", pvcs.getPackageVersionContentSourcePK().getPackageVersion().getId());
composite = (LoadedPackageBitsComposite) query.getSingleResult();
if (!composite.isPackageBitsAvailable()) {
throw new RuntimeException("Failed to download package bits [" + packageDetailsKey
+ "] for resource [" + resourceId + "]");
}
} else {
// package bits are not loaded and never will be loaded due to content source's download mode == NEVER
composite = null;
}
}
Connection conn = null;
PreparedStatement ps = null;
ResultSet results = null;
InputStream bitsStream = null;
try {
if (composite == null) {
// this is DownloadMode.NEVER and we are really in pass-through mode, stream directly from adapter
ContentServerPluginContainer pc = ContentManagerHelper.getPluginContainer();
ContentProviderManager adapterMgr = pc.getAdapterManager();
int contentSourceId = pvcs.getPackageVersionContentSourcePK().getContentSource().getId();
bitsStream = adapterMgr.loadPackageBits(contentSourceId, pvcs.getLocation());
} else {
if (composite.isPackageBitsInDatabase()) {
// this is DownloadMode.DATABASE - put the bits in the database
conn = dataSource.getConnection();