Github user geomacy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/485#discussion_r107482279
--- Diff:
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
---
@@ -129,6 +176,88 @@ public Response create(String yaml) {
return Response.status(Status.CREATED).entity(result).build();
}
+ @Override
+ public Response createFromArchive(byte[] zipInput) {
+ if (!Entitlements.isEntitled(mgmt().getEntitlementManager(),
Entitlements.ROOT, null)) {
+ throw WebResourceUtils.forbidden("User '%s' is not authorized
to add catalog item",
+ Entitlements.getEntitlementContext().user());
+ }
+
+ BundleMaker bm = new BundleMaker(mgmt());
+ File f=null, f2=null;
+ try {
+ f = Os.newTempFile("brooklyn-posted-archive", "zip");
+ try {
+ Files.write(zipInput, f);
+ } catch (IOException e) {
+ Exceptions.propagate(e);
+ }
+
+ ZipFile zf;
+ try {
+ zf = new ZipFile(f);
+ } catch (IOException e) {
+ throw WebResourceUtils.badRequest("Invalid ZIP/JAR
archive: "+e);
+ }
+ ZipArchiveEntry bom = zf.getEntry("catalog.bom");
+ if (bom==null) {
+ bom = zf.getEntry("/catalog.bom");
+ }
+ if (bom==null) {
+ throw WebResourceUtils.badRequest("Archive must contain a
catalog.bom file in the root");
+ }
+ String bomS;
+ try {
+ bomS = Streams.readFullyString(zf.getInputStream(bom));
+ } catch (IOException e) {
+ throw WebResourceUtils.badRequest("Error reading
catalog.bom from ZIP/JAR archive: "+e);
+ }
+ VersionedName vn = BasicBrooklynCatalog.getVersionedName(
BasicBrooklynCatalog.getCatalogMetadata(bomS) );
+
+ Manifest mf = bm.getManifest(f);
+ if (mf==null) {
+ mf = new Manifest();
+ }
+ String bundleNameInMF =
mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
+ if (Strings.isNonBlank(bundleNameInMF)) {
+ if (!bundleNameInMF.equals(vn.getSymbolicName())) {
+ throw new IllegalStateException("JAR MANIFEST
symbolic-name '"+bundleNameInMF+"' does not match '"+vn.getSymbolicName()+"'
defined in BOM");
+ }
+ } else {
+
mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME,
vn.getSymbolicName());
+ }
+
+ String bundleVersionInMF =
mf.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
+ if (Strings.isNonBlank(bundleVersionInMF)) {
+ if (!bundleVersionInMF.equals(vn.getVersion().toString()))
{
+ throw new IllegalStateException("JAR MANIFEST version
'"+bundleVersionInMF+"' does not match '"+vn.getVersion()+"' defined in BOM");
+ }
+ } else {
+ mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION,
vn.getVersion().toString());
+ }
+ if
(mf.getMainAttributes().getValue(Attributes.Name.MANIFEST_VERSION)==null) {
+
mf.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(),
"1.0");
+ }
+
+ f2 = bm.copyAddingManifest(f, mf);
+
+ Bundle bundle = bm.installBundle(f2, true);
+
+ if
(!BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_LOAD_BUNDLE_CATALOG_BOM))
{
+ // if the above feature is not enabled, let's do it
manually (as a contract of this method)
+ new CatalogBomScanner().new CatalogPopulator(
--- End diff --
The `CatalogBomScanner` is designed to be used as a singleton; the
`CatalogPopulator` is an OSGI `BundleTracker` and is meant to be used with an
open/close lifecycle. The constructor does the `open()` but here nothing does
the `close()`. Upon `open()` It will fire the `addingBundle()` method on
_**all**_ active bundles in the bundle context, and subsequently on any bundles
that get added to that context; not just on this `bundle`.
We will probably need to look at splitting `CatalogPopulator` apart a bit
more and factor out a tool that can be used on a single bundle in an ad-hoc way
like this. We'll need to think about deactivating the bundle and removing
catalog items, which is taken care of by the `BundleTracker` side of things in
the implementation of `CatalogPopulator.removedBundle()`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---