This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 2609ad5  if cannot create as a spec then fall back to trying as a bean
2609ad5 is described below

commit 2609ad5202a6a87dbe4d540cd4f57a0f8808341b
Author: Alex Heneveld <[email protected]>
AuthorDate: Wed Jan 13 15:18:59 2021 +0000

    if cannot create as a spec then fall back to trying as a bean
---
 .../catalog/internal/BasicBrooklynCatalog.java     | 63 +++++++++++++++-------
 .../core/resolve/jackson/BeanWithTypeUtils.java    |  1 +
 2 files changed, 45 insertions(+), 19 deletions(-)

diff --git 
a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java
 
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java
index bb62920..c5d64e4 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/catalog/internal/BasicBrooklynCatalog.java
@@ -1348,7 +1348,7 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
         boolean suspicionOfABean = false;
 
         private Maybe<Object> attemptPlanTranformer() {
-            Exception errorInBean = null;
+            MutableSet<Throwable> exceptions = MutableSet.<Throwable>of();
             try {
                 suspicionOfABean = false;
 
@@ -1377,43 +1377,68 @@ public class BasicBrooklynCatalog implements 
BrooklynCatalog {
                 }
 
                 Object t = null;
+                boolean triedBean = false;
+                // try as bean first if signs are auspicious
                 if (catalogItemType == CatalogItemType.BEAN || 
suspicionOfABean) {
                     try {
+                        triedBean = true;
                         t = mgmt.getTypeRegistry().createBeanFromPlan(format, 
itemYaml, constraint, null);
                         catalogItemType = CatalogItemType.BEAN;
                     } catch (Exception e) {
                         Exceptions.propagateIfFatal(e);
-                        // if we were speculatively trying bean then retry as 
plan
-                        errorInBean = e;
+                        exceptions.add(e);
                     }
                 }
 
+                // then try as spec unless known to be a bean
                 if (catalogItemType != CatalogItemType.BEAN && t==null) {
-                    t = mgmt.getTypeRegistry().createSpecFromPlan(format, 
itemYaml, constraint,
-                            
BrooklynObjectType.of(catalogItemType).getSpecType());
-                    if (catalogItemType == null) {
-                        catalogItemType = 
CatalogItemType.ofSpecClass(BrooklynObjectType.of(t.getClass()).getSpecType());
+                    try {
+                        t = mgmt.getTypeRegistry().createSpecFromPlan(format, 
itemYaml, constraint,
+                                
BrooklynObjectType.of(catalogItemType).getSpecType());
+                        if (catalogItemType == null) {
+                            catalogItemType = 
CatalogItemType.ofSpecClass(BrooklynObjectType.of(t.getClass()).getSpecType());
+                        }
+                    } catch (Exception e) {
+                        Exceptions.propagateIfFatal(e);
+                        exceptions.add(e);
                     }
                 }
 
-                if (t==null) {
-                    if (errorInBean!=null) throw errorInBean;
-                    throw new IllegalStateException("Type registry creation 
returned null");
+                // lastly try as bean if we haven't already and it is not 
known to be a spec (ie if item type is unknown)
+                if (catalogItemType==null && t==null && !triedBean) {
+                    try {
+                        triedBean = true;
+                        t = mgmt.getTypeRegistry().createBeanFromPlan(format, 
itemYaml, constraint, null);
+                        if (format==null && (t instanceof Map || t instanceof 
Collection)) {
+                            // doesn't look like a bean
+                        } else {
+                            catalogItemType = CatalogItemType.BEAN;
+                        }
+                    } catch (Exception e) {
+                        Exceptions.propagateIfFatal(e);
+                        exceptions.add(e);
+                    }
+                }
+
+                if (t!=null) {
+                    resolved = true;
+                    return Maybe.of(t);
                 }
 
-                resolved = true;
-                return Maybe.of(t);
 
             } catch (Exception e) {
                 Exceptions.propagateIfFatal(e);
-                MutableList<Throwable> exceptions = 
MutableList.<Throwable>of().appendIfNotNull(errorInBean, e);
-                return Maybe.absent(
-                        () ->
-                            Exceptions.create("Unable to transform definition 
of "+
-                                (itemId!=null ? itemId : 
"plan:\n"+itemYaml+"\n"),
-                                exceptions)
-                );
+                exceptions.add(e);
             }
+
+            if (exceptions.isEmpty()) exceptions.add(new 
IllegalStateException("Type registry creation returned null"));
+
+            return Maybe.absent(
+                    () ->
+                        Exceptions.create("Unable to transform definition of "+
+                            (itemId!=null ? itemId : "plan:\n"+itemYaml+"\n"),
+                            exceptions)
+            );
         }
 
         public boolean isResolved() { return resolved; }
diff --git 
a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java
 
b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java
index 9edda36..fa8051c 100644
--- 
a/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java
+++ 
b/core/src/main/java/org/apache/brooklyn/core/resolve/jackson/BeanWithTypeUtils.java
@@ -51,6 +51,7 @@ public class BeanWithTypeUtils {
 
     public static final String FORMAT = "bean-with-type";
 
+    /** also see {@link 
org.apache.brooklyn.util.core.json.BrooklynObjectsJsonMapper#newMapper(ManagementContext)}
 */
     public static ObjectMapper newMapper(ManagementContext mgmt, boolean 
allowRegisteredTypes, BrooklynClassLoadingContext loader, boolean 
allowBasicJavaTypes) {
         JsonMapper mapper = newSimpleMapper();
 

Reply via email to