checkCreateBundleVersionAuthz(subject, bundle.getId());
createdBundle = false;
}
// now create the bundle version with the bundle we either found or created
BundleVersion bundleVersion = bundleManager.createBundleVersionInternal(bundle, name, version, description,
recipe, info.getRecipeParseResults().getConfigurationDefinition());
// now that we have the bundle version we can actually create the bundle files that were provided in
// the bundle distribution
try {
Map<String, File> bundleFiles = info.getBundleFiles();
if (bundleFiles != null) {
for (String fileName : bundleFiles.keySet()) {
File file = bundleFiles.get(fileName);
InputStream is = null;
try {
is = new FileInputStream(file);
// peg the file version to the bundle version. In the future we may allow a distribution
// to refer to existing versions of a file.
BundleFile bundleFile = bundleManager.addBundleFile(subject, bundleVersion.getId(), fileName,
bundleVersion.getVersion(), null, is);
if (LOG.isDebugEnabled()) {
LOG.debug("Added bundle file [" + bundleFile + "] to BundleVersion [" + bundleVersion + "]");
}
} finally {
safeClose(is);
if (null != file) {
file.delete();
}
}
}
}
} catch (Exception e) {
// we failed to add one or more bundle files to the bundle version. Since this means the distribution file
// did not fully get its bundle data persisted, we need to abort the entire effort. Let's delete
// the bundle version including the bundle definition if we were the ones that initially created it
// (thus this should completely wipe the database of any knowledge of what we just did previously)
LOG.error("Failed to add bundle file to new bundle version [" + bundleVersion
+ "], will not create the new bundle", e);
try {
bundleManager.deleteBundleVersion(subjectManager.getOverlord(), bundleVersion.getId(), createdBundle);
} catch (Exception e1) {
LOG.error("Failed to delete the partially created bundle version: " + bundleVersion, e1);
}
throw e;
}
// because the distribution file can define things like bundle files and default tags, let's
// ask for the full bundle version data so we can return that back to the caller; thus we let
// the caller know exactly what the distribution file had inside of it and what we persisted to the DB
BundleVersionCriteria bvCriteria = new BundleVersionCriteria();
bvCriteria.addFilterId(bundleVersion.getId());
bvCriteria.fetchBundle(true);
bvCriteria.fetchBundleFiles(true);
bvCriteria.fetchConfigurationDefinition(true);
bvCriteria.fetchTags(true);
PageList<BundleVersion> bundleVersions = bundleManager.findBundleVersionsByCriteria(subject, bvCriteria);
if (bundleVersions != null && bundleVersions.size() == 1) {
bundleVersion = bundleVersions.get(0);
List<BundleFile> bundleFiles = bundleVersion.getBundleFiles();
if (bundleFiles != null && bundleFiles.size() > 0) {
final BundleFileCriteria bfCriteria = new BundleFileCriteria();
bfCriteria.addFilterBundleVersionId(bundleVersion.getId());
bfCriteria.fetchPackageVersion(true);
//Use CriteriaQuery to automatically chunk/page through criteria query results
CriteriaQueryExecutor<BundleFile, BundleFileCriteria> queryExecutor = new CriteriaQueryExecutor<BundleFile, BundleFileCriteria>() {
@Override
public PageList<BundleFile> execute(BundleFileCriteria criteria) {
return bundleManager.findBundleFilesByCriteria(subjectManager.getOverlord(), bfCriteria);
}
};
CriteriaQuery<BundleFile, BundleFileCriteria> bfs = new CriteriaQuery<BundleFile, BundleFileCriteria>(
bfCriteria, queryExecutor);
bundleFiles.clear();
for (BundleFile bf : bfs) {
bundleFiles.add(bf);
}
}
bundleVersion.setBundleDeployments(new ArrayList<BundleDeployment>());
} else {
LOG.error("Failed to obtain the full bundle version, returning only what we currently know about it: "
+ bundleVersion);
}