You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2020/11/17 17:50:37 UTC

[brooklyn-server] branch master updated (51762ba -> d39dfb5)

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

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


    from 51762ba  Merge pull request #1125 from ahgittin/misc-serial
     new ff24c90  correct DSL serialization problem and allow $brooklyn:object to load registered types
     new 5ebc7d5  fix bug by distinguishing DslFuctionSupplier and DeferredSupplier
     new d39dfb5  Merge branch 'master' of https://gitbox.apache.org/repos/asf/brooklyn-server

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../brooklyn/spi/dsl/DslDeferredFunctionCall.java  | 27 ++++++++++++++--------
 .../spi/dsl/methods/BrooklynDslCommon.java         | 22 +++++++++++++++++-
 .../spi/dsl/methods/DslToStringHelpers.java        |  2 +-
 .../camp/brooklyn/spi/dsl/DslYamlTest.java         |  4 +++-
 .../brooklyn/spi/dsl/methods/DslTestObjects.java   | 15 +++++++-----
 .../brooklyn/util/core/ClassLoaderUtils.java       | 10 ++++++--
 6 files changed, 60 insertions(+), 20 deletions(-)


[brooklyn-server] 01/03: correct DSL serialization problem and allow $brooklyn:object to load registered types

Posted by he...@apache.org.
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

commit ff24c90745b8e94657e095539f3c8c0fe01cdccb
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Tue Nov 17 17:25:43 2020 +0000

    correct DSL serialization problem and allow $brooklyn:object to load registered types
---
 .../brooklyn/spi/dsl/DslDeferredFunctionCall.java  | 27 ++++++++++++++--------
 .../spi/dsl/methods/BrooklynDslCommon.java         | 22 +++++++++++++++++-
 .../spi/dsl/methods/DslToStringHelpers.java        |  2 +-
 .../brooklyn/util/core/ClassLoaderUtils.java       | 10 ++++++--
 4 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslDeferredFunctionCall.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslDeferredFunctionCall.java
index 30b8416..9ef74d0 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslDeferredFunctionCall.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslDeferredFunctionCall.java
@@ -30,6 +30,7 @@ import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslToStringHelpers;
 import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
 import org.apache.brooklyn.util.core.task.ImmediateSupplier;
 import org.apache.brooklyn.util.core.task.Tasks;
+import org.apache.brooklyn.util.core.task.ValueResolver;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.javalang.Reflections;
@@ -84,7 +85,7 @@ public class DslDeferredFunctionCall extends BrooklynDslDeferredSupplier<Object>
     }
 
     protected Maybe<Object> invokeOnDeferred(Object obj, boolean immediate) {
-        Maybe<?> resolvedMaybe = resolve(obj, immediate);
+        Maybe<?> resolvedMaybe = resolve(obj, immediate, true);
         if (resolvedMaybe.isPresent()) {
             Object instance = resolvedMaybe.get();
 
@@ -93,7 +94,11 @@ public class DslDeferredFunctionCall extends BrooklynDslDeferredSupplier<Object>
                         object + " evaluates to null (wanting to call " + toStringF(fnName, args) + ")");
             }
 
-            return invokeOn(instance);
+            Maybe<Object> tentative = invokeOn(instance);
+            if (tentative.isAbsent()) {
+                return tentative;
+            }
+            return (Maybe) resolve(tentative.get(), immediate, false);
         } else {
             if (immediate) {
                 return Maybe.absent(new ImmediateSupplier.ImmediateValueNotAvailableException("Could not evaluate immediately: " + obj));
@@ -183,13 +188,17 @@ public class DslDeferredFunctionCall extends BrooklynDslDeferredSupplier<Object>
         }
     }
     
-    protected Maybe<?> resolve(Object object, boolean immediate) {
-        return Tasks.resolving(object, Object.class)
-            .context(entity().getExecutionContext())
-            .deep()
-            .immediately(immediate)
-            .iterator()
-            .nextOrLast(DslFunctionSource.class);
+    protected Maybe<?> resolve(Object object, boolean immediate, boolean dslFunctionSource) {
+        ValueResolver<Object> r = Tasks.resolving(object, Object.class)
+                .context(entity().getExecutionContext())
+                .deep()
+                .immediately(immediate);
+        if (dslFunctionSource) {
+            return r.iterator()
+                    .nextOrLast(DslFunctionSource.class);
+        } else {
+            return r.getMaybe();
+        }
     }
 
     private static void checkCallAllowed(Method m) {
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
index d027b30..fa96c84 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/BrooklynDslCommon.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import java.util.*;
+import org.apache.brooklyn.api.typereg.RegisteredType;
 import org.apache.brooklyn.camp.brooklyn.spi.dsl.DslUtils;
 import static org.apache.brooklyn.camp.brooklyn.spi.dsl.DslUtils.resolved;
 
@@ -45,6 +46,7 @@ import org.apache.brooklyn.core.config.external.ExternalConfigSupplier;
 import org.apache.brooklyn.core.entity.EntityDynamicType;
 import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
+import org.apache.brooklyn.core.mgmt.classloading.OsgiBrooklynClassLoadingContext;
 import org.apache.brooklyn.core.mgmt.internal.ExternalConfigSupplierRegistry;
 import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
 import org.apache.brooklyn.core.mgmt.persist.DeserializingClassRenamesProvider;
@@ -52,6 +54,7 @@ import org.apache.brooklyn.core.objs.AbstractConfigurationSupportInternal;
 import org.apache.brooklyn.core.objs.BrooklynObjectInternal;
 import org.apache.brooklyn.core.resolve.jackson.BrooklynJacksonSerializationUtils;
 import org.apache.brooklyn.core.sensor.DependentConfiguration;
+import org.apache.brooklyn.core.typereg.RegisteredTypeLoadingContexts;
 import org.apache.brooklyn.util.collections.Jsonya;
 import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableMap;
@@ -68,6 +71,7 @@ import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.javalang.Reflections;
 import org.apache.brooklyn.util.javalang.coerce.TypeCoercer;
 import org.apache.brooklyn.util.net.Urls;
+import org.apache.brooklyn.util.os.Os;
 import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes;
 import org.apache.brooklyn.util.yaml.Yamls;
 import org.apache.commons.beanutils.BeanUtils;
@@ -328,7 +332,7 @@ public class BrooklynDslCommon {
         try {
             type = new ClassLoaderUtils(BrooklynDslCommon.class).loadClass(mappedTypeName);
         } catch (ClassNotFoundException e) {
-            LOG.debug("Cannot load class " + typeName + " for DLS object; assuming it is in OSGi bundle; will defer its loading");
+            LOG.debug("Cannot load class " + typeName + " for DSL 'object'; assuming it is in OSGi bundle; will defer its loading");
             return new DslObject(mappedTypeName, constructorArgs, objectFields, brooklynConfig);
         }
 
@@ -667,6 +671,9 @@ public class BrooklynDslCommon {
 
         @Override @JsonIgnore
         public Maybe<Object> getImmediately() {
+            // TODO reconcile with "bean-with-type" usage and constructors;
+            // for now, if it's a registered type we use that to get the java instance but we ignore fields on it
+
             final Class<?> clazz = getOrLoadType();
             final ExecutionContext executionContext = entity().getExecutionContext();
 
@@ -720,6 +727,8 @@ public class BrooklynDslCommon {
                     .body(new Callable<Object>() {
                         @Override
                         public Object call() throws Exception {
+                            // TODO de-dupe with getImmediately
+
                             Map<String, Object> resolvedFields = MutableMap.copyOf(Maps.transformValues(fields, resolver));
                             Map<String, Object> resolvedConfig = MutableMap.copyOf(Maps.transformValues(config, resolver));
                             List<Object> resolvedConstructorArgs = MutableList.copyOf(Lists.transform(constructorArgs, resolver));
@@ -737,12 +746,23 @@ public class BrooklynDslCommon {
         @JsonIgnore
         protected Class<?> getOrLoadType() {
             Class<?> type = this.type;
+
             if (type == null) {
                 EntityInternal entity = entity();
                 try {
                     if (entity==null) {
                         throw new IllegalStateException("Cannot invoke without a Task running the context of an entity");
                     }
+                    RegisteredType rt = managementContext().getTypeRegistry().get(typeName, RegisteredTypeLoadingContexts.loader(new OsgiBrooklynClassLoadingContext(entity)));
+                    if (rt!=null) {
+                        Object inst = managementContext().getTypeRegistry().create(rt, null, null);
+                        if (inst!=null) {
+                            // we ignore the actually instance for now; see comments at start of this class
+                            return inst.getClass();
+                        }
+                    }
+
+                    // fall back to trying to load as a class
                     type = new ClassLoaderUtils(BrooklynDslCommon.class, entity).loadClass(typeName);
                 } catch (ClassNotFoundException e) {
                     throw Exceptions.propagate(e);
diff --git a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslToStringHelpers.java b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslToStringHelpers.java
index 03b1a33..8bf4460 100644
--- a/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslToStringHelpers.java
+++ b/camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslToStringHelpers.java
@@ -44,7 +44,7 @@ public class DslToStringHelpers {
     }
 
     /** convenience for functions, inserting parentheses and commas */
-    public static String fn(String functionName, Iterable<Object> args) {
+    public static String fn(String functionName, Iterable<?> args) {
         StringBuilder out = new StringBuilder();
         out.append(BrooklynDslCommon.PREFIX);
         out.append(functionName);
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java b/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
index 91dc577..db80482 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
@@ -287,8 +287,14 @@ public class ClassLoaderUtils {
                 CatalogItem<?, ?> item = CatalogUtils.getCatalogItemOptionalVersion(mgmt, catalogItemId);
                 if (item != null) {
                     BrooklynClassLoadingContextSequential loader = new BrooklynClassLoadingContextSequential(mgmt);
-                    loader.add(newClassLoadingContextForCatalogItems(mgmt, item.getCatalogItemId(),
-                        item.getCatalogItemIdSearchPath()));
+                    try {
+                        loader.add(newClassLoadingContextForCatalogItems(mgmt, item.getCatalogItemId(),
+                                item.getCatalogItemIdSearchPath()));
+                    } catch (UnsupportedOperationException e) {
+                        // normal if item comes from the type; could suppress load attempt
+                    } catch (Exception e) {
+                        log.warn("Error accessing looking up "+className+" relative to "+catalogItemId+" (ignoring, will try loading other ways but may fail): "+e, e);
+                    }
                     cls = dispatcher.tryLoadFrom(loader, className);
                     if (cls.isPresent()) {
                         return cls;


[brooklyn-server] 03/03: Merge branch 'master' of https://gitbox.apache.org/repos/asf/brooklyn-server

Posted by he...@apache.org.
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

commit d39dfb5ad40ab123381763c9f9629637ebbcfb67
Merge: 5ebc7d5 51762ba
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Tue Nov 17 17:50:06 2020 +0000

    Merge branch 'master' of https://gitbox.apache.org/repos/asf/brooklyn-server



[brooklyn-server] 02/03: fix bug by distinguishing DslFuctionSupplier and DeferredSupplier

Posted by he...@apache.org.
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

commit 5ebc7d5351f05be3b0164be85ae5c23d0196f4d2
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Tue Nov 17 17:49:26 2020 +0000

    fix bug by distinguishing DslFuctionSupplier and DeferredSupplier
---
 .../brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java       |  4 +++-
 .../camp/brooklyn/spi/dsl/methods/DslTestObjects.java     | 15 +++++++++------
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java
index a2922a1..342729c 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslYamlTest.java
@@ -768,6 +768,9 @@ public class DslYamlTest extends AbstractYamlTest {
         assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
     }
 
+    // TODO this and the next method used to work with DslTestCallableAlsoSupplier; but now if it is a supplier we get it when resolving,
+    // otherwise we don't get the values we want in certain cases; things should be DslFunctionSource or DeferredSupplier but not both.
+    // that might however cause problems when we try to evaluate $brooklyn:component("xxx").attributeWhenReady() if we get the entity from the first one
     @Test
     public void testDeferredDslChainingWithCustomCallable() throws Exception {
         final Entity app = createAndStartApplication(
@@ -779,7 +782,6 @@ public class DslYamlTest extends AbstractYamlTest {
         app.config().set(customCallableWrapperKey, new DslTestSupplierWrapper(new DslTestCallable()));
         assertEquals(getConfigEventually(app, DEST), Boolean.TRUE);
     }
-
     @Test
     public void testDeferredDslChainingWithNestedEvaluation() throws Exception {
         final Entity app = createAndStartApplication(
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslTestObjects.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslTestObjects.java
index 18c0c16..0646cdd 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslTestObjects.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/methods/DslTestObjects.java
@@ -66,8 +66,15 @@ public class DslTestObjects {
         }
     }
 
-    public static class DslTestCallable implements DslFunctionSource, DeferredSupplier<TestDslSupplier>, ImmediateSupplier<TestDslSupplier> {
+    public static class DslTestCallable implements DslFunctionSource {
+        @DslAccessible
+        public boolean isSupplierCallable() {
+            return true;
+        }
+    }
 
+    // see comments in DslYamlTest.testDeferredDslChainingWithCustomCallable
+    public static class DslTestCallableAlsoSupplier extends DslTestCallable implements DeferredSupplier<TestDslSupplier>, ImmediateSupplier<TestDslSupplier> {
         @Override @JsonIgnore
         public Maybe<TestDslSupplier> getImmediately() {
             throw new IllegalStateException("Not to be called");
@@ -75,13 +82,9 @@ public class DslTestObjects {
 
         @Override
         public TestDslSupplier get() {
-            throw new IllegalStateException("Not to be called");
+            return getImmediately().get();
         }
 
-        @DslAccessible
-        public boolean isSupplierCallable() {
-            return true;
-        }
     }
 
 }