brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #482: DSL deferred execution
Date Tue, 13 Dec 2016 09:38:23 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/482#discussion_r92128816
  
    --- Diff: camp/camp-brooklyn/src/main/java/org/apache/brooklyn/camp/brooklyn/spi/dsl/DslDeferredFunctionCall.java
---
    @@ -0,0 +1,169 @@
    +/*
    + * Copyright 2016 The Apache Software Foundation.
    + *
    + * Licensed under the Apache License, Version 2.0 (the "License");
    + * you may not use this file except in compliance with the License.
    + * You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.brooklyn.camp.brooklyn.spi.dsl;
    +
    +import java.lang.reflect.InvocationTargetException;
    +import java.util.List;
    +import java.util.concurrent.Callable;
    +
    +import org.apache.brooklyn.api.mgmt.Task;
    +import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.BrooklynDslCommon;
    +import org.apache.brooklyn.core.entity.EntityInternal;
    +import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
    +import org.apache.brooklyn.util.core.task.Tasks;
    +import org.apache.brooklyn.util.exceptions.Exceptions;
    +import org.apache.brooklyn.util.guava.Maybe;
    +import org.apache.brooklyn.util.javalang.Reflections;
    +
    +import com.google.common.base.Joiner;
    +import com.google.common.base.Objects;
    +
    +public class DslDeferredFunctionCall extends BrooklynDslDeferredSupplier<Object>
{
    +
    +    private static final long serialVersionUID = 3243262633795112155L;
    +
    +    // TODO should this be some of the super types?
    +    private BrooklynDslDeferredSupplier<?> object;
    +    private String fnName;
    +    private List<?> args;
    +
    +    public DslDeferredFunctionCall(BrooklynDslDeferredSupplier<?> o, String fn,
List<Object> args) {
    +        this.object = o;
    +        this.fnName = fn;
    +        this.args = args;
    +    }
    +
    +    @Override
    +    public Maybe<Object> getImmediately() {
    +        Maybe<?> obj = resolveImmediate(object);
    +        if (obj.isPresent()) {
    +            if (obj.isNull()) {
    +                throw new IllegalArgumentException("Deferred function call, " + object
+ 
    +                        " evaluates to null (when calling " + fnName + "(" + toString(args)
+ "))");
    +            }
    +            return Maybe.of(invokeOn(obj.get()));
    +        }
    +        return Maybe.absent("Could not evaluate immediately " + object);
    +    }
    +
    +    @Override
    +    public Task<Object> newTask() {
    +        return Tasks.builder()
    +                .displayName("Deferred function call " + object + "." + fnName + "("
+ toString(args) + ")")
    +                .tag(BrooklynTaskTags.TRANSIENT_TASK_TAG)
    +                .dynamic(false)
    +                .body(new Callable<Object>() {
    +                    @Override
    +                    public Object call() throws Exception {
    +                        Object obj = DslDeferredFunctionCall.this.resolveBlocking(object).orNull();
    +                        if (obj == null) {
    +                            throw new IllegalArgumentException("Deferred function call,
" + object + 
    +                                    " evaluates to null (when calling " + fnName + "("
+ DslDeferredFunctionCall.toString(args) + "))");
    +                        }
    +                        return invokeOn(obj);
    +                    }
    +
    +                }).build();
    +    }
    +
    +    protected Maybe<?> resolveImmediate(Object object) {
    +        return resolve(object, true);
    +    }
    +    protected Maybe<?> resolveBlocking(Object object) {
    +        return resolve(object, false);
    +    }
    +    protected Maybe<?> resolve(Object object, boolean immediate) {
    +        if (object instanceof DslCallable || object == null) {
    +            return Maybe.of(object);
    +        }
    +        Maybe<?> resultMaybe = Tasks.resolving(object, Object.class)
    --- End diff --
    
    How is this code reachable?  This class is only constructed when `object` is `DslCallable`
in which case the above branch applies, isn't 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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message