better error message when can't find referenced yaml resource
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/df971f5a Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/df971f5a Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/df971f5a Branch: refs/heads/master Commit: df971f5a0972fb3e73e483489ef7e121162ae87f Parents: c6d6cc4 Author: Alex Heneveld <[email protected]> Authored: Mon Sep 1 16:45:59 2014 +0100 Committer: Alex Heneveld <[email protected]> Committed: Mon Sep 1 17:07:01 2014 +0100 ---------------------------------------------------------------------- .../JavaBrooklynClassLoadingContext.java | 2 +- .../BrooklynComponentTemplateResolver.java | 6 ++- .../guava/IllegalStateExceptionSupplier.java | 41 ++++++++++++++++++++ .../main/java/brooklyn/util/guava/Maybe.java | 40 ++++++++++++------- 4 files changed, 71 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/df971f5a/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java b/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java index 7783b25..ac58c79 100644 --- a/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java +++ b/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java @@ -48,7 +48,7 @@ public class JavaBrooklynClassLoadingContext extends AbstractBrooklynClassLoadin return (Maybe) Maybe.of(loader.loadClass(className)); } catch (Exception e) { Exceptions.propagateIfFatal(e); - return Maybe.absent(e); + return Maybe.absent("Invalid class: "+className, e); } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/df971f5a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java index 39ec1c7..cc0eb0e 100644 --- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java +++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java @@ -195,7 +195,10 @@ public class BrooklynComponentTemplateResolver { /** returns the entity class, if needed in contexts which scan its statics for example */ public Class<? extends Entity> loadEntityClass() { - return tryLoadEntityClass().get(); + Maybe<Class<? extends Entity>> result = tryLoadEntityClass(); + if (result.isAbsent()) + throw new IllegalStateException("Could not find "+getBrooklynType(), ((Maybe.Absent<?>)result).getException()); + return result.get(); } /** tries to load the Java entity class */ @@ -239,7 +242,6 @@ public class BrooklynComponentTemplateResolver { } else { // If this is a concrete class, particularly for an Application class, we want the proxy // to expose all interfaces it implements. - @SuppressWarnings("rawtypes") Class interfaceclazz = (Application.class.isAssignableFrom(type)) ? Application.class : Entity.class; List<Class<?>> additionalInterfaceClazzes = Reflections.getAllInterfaces(type); spec = EntitySpec.create(interfaceclazz).impl(type).additionalInterfaces(additionalInterfaceClazzes); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/df971f5a/utils/common/src/main/java/brooklyn/util/guava/IllegalStateExceptionSupplier.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/brooklyn/util/guava/IllegalStateExceptionSupplier.java b/utils/common/src/main/java/brooklyn/util/guava/IllegalStateExceptionSupplier.java new file mode 100644 index 0000000..4fc8546 --- /dev/null +++ b/utils/common/src/main/java/brooklyn/util/guava/IllegalStateExceptionSupplier.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 brooklyn.util.guava; + +import com.google.common.base.Supplier; + +public class IllegalStateExceptionSupplier implements Supplier<RuntimeException> { + + protected final String message; + protected final Throwable cause; + + public IllegalStateExceptionSupplier() { this(null, null); } + public IllegalStateExceptionSupplier(String message) { this(message, null); } + public IllegalStateExceptionSupplier(Throwable cause) { this(null, cause); } + public IllegalStateExceptionSupplier(String message, Throwable cause) { + this.message = message; + this.cause = cause; + } + + @Override + public RuntimeException get() { + return new IllegalStateException(message, cause); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/df971f5a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java b/utils/common/src/main/java/brooklyn/util/guava/Maybe.java index 9a3ebdb..fb60bf8 100644 --- a/utils/common/src/main/java/brooklyn/util/guava/Maybe.java +++ b/utils/common/src/main/java/brooklyn/util/guava/Maybe.java @@ -29,6 +29,7 @@ import brooklyn.util.javalang.JavaClassNames; import com.google.common.base.Function; import com.google.common.base.Optional; +import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; @@ -44,25 +45,24 @@ public abstract class Maybe<T> implements Serializable, Supplier<T> { /** Creates an absent whose get throws an {@link IllegalStateException} with the indicated message. * Both stack traces (the cause and the callers) are provided, which can be quite handy. */ public static <T> Maybe<T> absent(final String message) { - return new Absent<T>() { - private static final long serialVersionUID = 1L; - @Override - public T get() { - throw new IllegalStateException(message); - } - }; + return absent(new IllegalStateExceptionSupplier(message)); } /** Creates an absent whose get throws an {@link IllegalStateException} with the indicated cause. * Both stack traces (the cause and the callers) are provided, which can be quite handy. */ public static <T> Maybe<T> absent(final Throwable cause) { - return new Absent<T>() { - private static final long serialVersionUID = 1L; - @Override - public T get() { - throw new IllegalStateException(cause); - } - }; + return absent(new IllegalStateExceptionSupplier(cause)); + } + + /** Creates an absent whose get throws an {@link IllegalStateException} with the indicated message and underlying cause. + * Both stack traces (the cause and the callers) are provided, which can be quite handy. */ + public static <T> Maybe<T> absent(final String message, final Throwable cause) { + return absent(new IllegalStateExceptionSupplier(message, cause)); + } + + /** Creates an absent whose get throws an {@link RuntimeException} generated on demand from the given supplier */ + public static <T> Maybe<T> absent(final Supplier<RuntimeException> exceptionSupplier) { + return new Absent<T>(Preconditions.checkNotNull(exceptionSupplier)); } public static <T> Maybe<T> of(@Nullable T value) { @@ -151,13 +151,23 @@ public abstract class Maybe<T> implements Serializable, Supplier<T> { public static class Absent<T> extends Maybe<T> { private static final long serialVersionUID = -757170462010887057L; + private final Supplier<RuntimeException> exception; + public Absent() { + this(new IllegalStateExceptionSupplier()); + } + public Absent(Supplier<RuntimeException> exception) { + this.exception = exception; + } @Override public boolean isPresent() { return false; } @Override public T get() { - throw new IllegalStateException(); + throw getException(); + } + public RuntimeException getException() { + return exception.get(); } }
