Ejemplo n.º 1
0
  private static <T> Maybe<T> tryValidateBean(
      T object, RegisteredType type, final RegisteredTypeLoadingContext context) {
    if (object == null) return Maybe.absentNull("object is null");

    if (type != null) {
      if (type.getKind() != RegisteredTypeKind.BEAN)
        return Maybe.absent("Validating a bean when type is " + type.getKind() + " " + type);
      if (!isSubtypeOf(object.getClass(), type))
        return Maybe.absent(object + " does not have all the java supertypes of " + type);
    }

    if (context != null) {
      if (context.getExpectedKind() != null && context.getExpectedKind() != RegisteredTypeKind.BEAN)
        return Maybe.absent(
            "Validating a bean when constraint expected " + context.getExpectedKind());
      if (context.getExpectedJavaSuperType() != null
          && !context.getExpectedJavaSuperType().isInstance(object))
        return Maybe.absent(
            object
                + " is not of the expected java supertype "
                + context.getExpectedJavaSuperType());
    }

    return Maybe.of(object);
  }
Ejemplo n.º 2
0
 /**
  * Returns a wrapped map, if the object is YAML which parses as a map; otherwise returns absent
  * capable of throwing an error with more details
  */
 @SuppressWarnings("unchecked")
 public static Maybe<Map<?, ?>> getAsYamlMap(Object planData) {
   if (!(planData instanceof String)) return Maybe.absent("not a string");
   Iterable<Object> result;
   try {
     result = Yamls.parseAll((String) planData);
   } catch (Exception e) {
     Exceptions.propagateIfFatal(e);
     return Maybe.absent(e);
   }
   Iterator<Object> ri = result.iterator();
   if (!ri.hasNext()) return Maybe.absent("YAML has no elements in it");
   Object r1 = ri.next();
   if (ri.hasNext()) return Maybe.absent("YAML has multiple elements in it");
   if (r1 instanceof Map) return (Maybe<Map<?, ?>>) (Maybe<?>) Maybe.of(r1);
   return Maybe.absent("YAML does not contain a map");
 }
    private WaitInTaskForAttributeReady(
        Entity source,
        AttributeSensor<T> sensor,
        Predicate<? super T> ready,
        List<AttributeAndSensorCondition<?>> abortConditions,
        String blockingDetails) {
      this.source = source;
      this.sensor = sensor;
      this.ready = ready;
      this.abortSensorConditions = abortConditions;
      this.blockingDetails = blockingDetails;

      this.timeout = Duration.PRACTICALLY_FOREVER;
      this.onTimeout = Maybe.absent();
      this.ignoreUnmanaged = DEFAULT_IGNORE_UNMANAGED;
      this.onUnmanaged = Maybe.absent();
      this.postProcess = null;
    }
Ejemplo n.º 4
0
  /**
   * Validates that the given type matches the context (if supplied); if not satisfied. returns an
   * {@link Absent} if failed with details of the error, with {@link Absent#isNull()} true if the
   * object is null.
   */
  public static Maybe<RegisteredType> tryValidate(
      RegisteredType item, final RegisteredTypeLoadingContext constraint) {
    // kept as a Maybe in case someone wants a wrapper around item validity;
    // unclear what the contract should be, as this can return Maybe.Present(null)
    // which is suprising, but it is more natural to callers otherwise they'll likely do a separate
    // null check on the item
    // (often handling null different to errors) so the Maybe.get() is redundant as they have an
    // object for the input anyway.

    if (item == null || constraint == null) return Maybe.ofDisallowingNull(item);
    if (constraint.getExpectedKind() != null
        && !constraint.getExpectedKind().equals(item.getKind()))
      return Maybe.absent(item + " is not the expected kind " + constraint.getExpectedKind());
    if (constraint.getExpectedJavaSuperType() != null) {
      if (!isSubtypeOf(item, constraint.getExpectedJavaSuperType())) {
        return Maybe.absent(
            item + " is not for the expected type " + constraint.getExpectedJavaSuperType());
      }
    }
    return Maybe.of(item);
  }
Ejemplo n.º 5
0
  private static <T> Maybe<T> tryValidateSpec(
      T object, RegisteredType rType, final RegisteredTypeLoadingContext constraint) {
    if (object == null) return Maybe.absentNull("object is null");

    if (!(object instanceof AbstractBrooklynObjectSpec)) {
      Maybe.absent("Found " + object + " when expecting a spec");
    }
    Class<?> targetType = ((AbstractBrooklynObjectSpec<?, ?>) object).getType();

    if (targetType == null) {
      Maybe.absent("Spec " + object + " does not have a target type");
    }

    if (rType != null) {
      if (rType.getKind() != RegisteredTypeKind.SPEC)
        Maybe.absent("Validating a spec when type is " + rType.getKind() + " " + rType);
      if (!isSubtypeOf(targetType, rType))
        Maybe.absent(object + " does not have all the java supertypes of " + rType);
    }

    if (constraint != null) {
      if (constraint.getExpectedJavaSuperType() != null) {
        if (!constraint.getExpectedJavaSuperType().isAssignableFrom(targetType)) {
          Maybe.absent(
              object
                  + " does not target the expected java supertype "
                  + constraint.getExpectedJavaSuperType());
        }
        if (constraint.getExpectedJavaSuperType().isAssignableFrom(BrooklynObjectInternal.class)) {
          // don't check spec type; any spec is acceptable
        } else {
          @SuppressWarnings("unchecked")
          Class<? extends AbstractBrooklynObjectSpec<?, ?>> specType =
              RegisteredTypeLoadingContexts.lookupSpecTypeForTarget(
                  (Class<? extends BrooklynObject>) constraint.getExpectedJavaSuperType());
          if (specType == null) {
            // means a problem in our classification of spec types!
            Maybe.absent(
                object
                    + " is returned as spec for unexpected java supertype "
                    + constraint.getExpectedJavaSuperType());
          }
          if (!specType.isAssignableFrom(object.getClass())) {
            Maybe.absent(
                object
                    + " is not a spec of the expected java supertype "
                    + constraint.getExpectedJavaSuperType());
          }
        }
      }
    }
    return Maybe.of(object);
  }
 public Builder<T, V> onUnmanagedThrow() {
   onUnmanaged = Maybe.<V>absent();
   return this;
 }
 public Builder<T, V> onTimeoutThrow() {
   onTimeout = Maybe.<V>absent();
   return this;
 }
 @Override
 @Deprecated
 public Maybe<Object> getConfigRaw(ConfigKey<?> key, boolean includeInherited) {
   Maybe<Object> result = delegate.getConfigRaw(key, includeInherited);
   return (result.isPresent()) ? Maybe.of(transform(key, result.get())) : Maybe.absent();
 }