Github user geomacy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/746#discussion_r126389365
--- Diff:
core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java
---
@@ -1060,28 +1337,87 @@ private String makeAsIndentedObject(String yaml) {
@Override
public List<? extends CatalogItem<?,?>> addItems(String yaml) {
- return addItems(yaml, null);
+ return addItems(yaml, false);
}
@Override
- public List<? extends CatalogItem<?, ?>> addItems(String yaml,
ManagedBundle bundle) {
- return addItems(yaml, bundle, false);
- }
-
- @Override
public CatalogItem<?,?> addItem(String yaml, boolean forceUpdate) {
return Iterables.getOnlyElement(addItems(yaml, forceUpdate));
}
@Override
public List<? extends CatalogItem<?,?>> addItems(String yaml, boolean
forceUpdate) {
+ Maybe<OsgiManager> osgiManager =
((ManagementContextInternal)mgmt).getOsgiManager();
+ if (osgiManager.isPresent() && AUTO_WRAP_CATALOG_YAML_AS_BUNDLE) {
+ // wrap in a bundle to be managed; need to get bundle and
version from yaml
+ Map<?, ?> cm = BasicBrooklynCatalog.getCatalogMetadata(yaml);
+ VersionedName vn = BasicBrooklynCatalog.getVersionedName( cm,
false );
+ if (vn==null) {
+ // for better legacy compatibiity, if id specified at root
use that
+ String id = (String) cm.get("id");
+ if (Strings.isNonBlank(id)) {
+ vn = VersionedName.fromString(id);
+ }
+ vn = new VersionedName(vn!=null &&
Strings.isNonBlank(vn.getSymbolicName()) ? vn.getSymbolicName() :
"brooklyn-catalog-bom-"+Identifiers.makeRandomId(8),
+ vn!=null && vn.getVersionString()!=null ?
vn.getVersionString() : getFirstAs(cm, String.class, "version").or(NO_VERSION));
+ }
+ log.debug("Wrapping supplied BOM as "+vn);
+ Manifest mf = new Manifest();
+ mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME,
vn.getSymbolicName());
+ mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION,
vn.getOsgiVersionString() );
+
mf.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(),
OSGI_MANIFEST_VERSION_VALUE);
+ mf.getMainAttributes().putValue(BROOKLYN_WRAPPED_BOM_BUNDLE,
Boolean.TRUE.toString());
+
+ BundleMaker bm = new BundleMaker(mgmt);
+ File bf = bm.createTempBundle(vn.getSymbolicName(), mf,
MutableMap.of(
+ new ZipEntry(CATALOG_BOM), (InputStream) new
ByteArrayInputStream(yaml.getBytes())) );
+
+ OsgiBundleInstallationResult result = null;
+ try {
+ result = osgiManager.get().install(null, new
FileInputStream(bf), true, true, forceUpdate).get();
+ } catch (FileNotFoundException e) {
+ throw Exceptions.propagate(e);
+ }
+ bf.delete();
+ uninstallEmptyWrapperBundles();
+ if (result.getCode().isError()) {
+ throw new IllegalStateException(result.getMessage());
+ }
+ return toLegacyCatalogItems(result.getCatalogItemsInstalled());
+
+ // if all items pertaining to an older anonymous catalog.bom
bundle have been overridden
+ // we delete those later; see list of wrapper bundles kept in
OsgiManager
+ }
+ // fallback to non-OSGi for tests and other environments
return addItems(yaml, null, forceUpdate);
}
- private List<? extends CatalogItem<?,?>> addItems(String yaml,
ManagedBundle bundle, boolean forceUpdate) {
- log.debug("Adding manual catalog item to "+mgmt+": "+yaml);
+ @SuppressWarnings("deprecation")
+ private List<CatalogItem<?,?>> toLegacyCatalogItems(Iterable<String>
itemIds) {
+ List<CatalogItem<?,?>> result = MutableList.of();
+ for (String id: itemIds) {
+ CatalogItem<?, ?> item =
CatalogUtils.getCatalogItemOptionalVersion(mgmt, id);
+ if (item==null) {
+ // using new Type Registry (OSGi addition);
+ result.add(RegisteredTypes.toPartialCatalogItem(
mgmt.getTypeRegistry().get(id) ));
+ } else {
+ result.add(item);
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public List<? extends CatalogItem<?, ?>> addItems(String yaml,
ManagedBundle bundle) {
+ return addItems(yaml, bundle, false);
+ }
+
+ @Override
+ public List<? extends CatalogItem<?,?>> addItems(String yaml,
ManagedBundle bundle, boolean forceUpdate) {
+ log.debug("Adding catalog item to "+mgmt+": "+yaml);
checkNotNull(yaml, "yaml");
- List<CatalogItemDtoAbstract<?, ?>> result =
collectCatalogItems(yaml);
+ List<CatalogItemDtoAbstract<?, ?>> result = MutableList.of();
+ collectCatalogItemsFromCatalogBomRoot(yaml, bundle, result, true,
ImmutableMap.of(), 0, false);
--- End diff --
Should this be passing `forceUpdate` as the last parameter instead of
explicit `false`? Would it be valid to have `forceUpdate == true` but still
call `collectCatalogItemsFromCatalogBomRoot` with `force == false`?
---
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.
---