Github user geomacy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/485#discussion_r107480164
--- 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(
+ ((LocalManagementContext)
mgmt()).getOsgiManager().get().getFramework().getBundleContext(),
+ mgmt()).addingBundle(bundle, null);
+ }
+
+ return Response.status(Status.CREATED).build();
+
+ } finally {
+ if (f!=null) f.delete();
--- End diff --
Is this safe, does the loaded bundle not require access to these files any
more? What will happen upon restarts, or a Karaf `clean`, especially if the
bundle is a proper jar with classes/resources in it?
---
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.
---