brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorbett <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Config constraints
Date Thu, 08 Oct 2015 10:44:04 GMT
Github user sjcorbett commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/890#discussion_r41499155
  
    --- Diff: core/src/main/java/org/apache/brooklyn/core/config/ConfigConstraints.java ---
    @@ -0,0 +1,165 @@
    +/*
    + * 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 org.apache.brooklyn.core.config;
    +
    +import java.util.Iterator;
    +import java.util.List;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.objs.BrooklynObject;
    +import org.apache.brooklyn.api.objs.EntityAdjunct;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.objs.AbstractEntityAdjunct;
    +import org.apache.brooklyn.core.objs.BrooklynObjectPredicate;
    +import org.apache.brooklyn.core.objs.BrooklynObjectInternal;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.base.Predicate;
    +import com.google.common.collect.Iterables;
    +import com.google.common.collect.Lists;
    +
    +public abstract class ConfigConstraints<T extends BrooklynObject> {
    +
    +    public static final Logger LOG = LoggerFactory.getLogger(ConfigConstraints.class);
    +
    +    private final T brooklynObject;
    +
    +    /**
    +     * Checks all constraints of all config keys available to an entity.
    +     * <p>
    +     * If a constraint is a {@link BrooklynObjectPredicate} then
    +     * {@link BrooklynObjectPredicate#apply(Object, BrooklynObject)} will be used.
    +     */
    +    public static void assertValid(Entity entity) {
    +        Iterable<ConfigKey<?>> violations = new EntityConfigConstraints(entity).getViolations();
    +        if (!Iterables.isEmpty(violations)) {
    +            throw new ConstraintViolationException(errorMessage(entity, violations));
    +        }
    +    }
    +
    +    /**
    +     * Checks all constraints of all config keys available to an entity adjunct.
    +     * <p>
    +     * If a constraint is a {@link BrooklynObjectPredicate} then
    +     * {@link BrooklynObjectPredicate#apply(Object, BrooklynObject)} will be used.
    +     */
    +    public static void assertValid(EntityAdjunct adjunct) {
    +        Iterable<ConfigKey<?>> violations = new EntityAdjunctConstraints(adjunct).getViolations();
    +        if (!Iterables.isEmpty(violations)) {
    +            throw new ConstraintViolationException(errorMessage(adjunct, violations));
    +        }
    +    }
    +
    +    private static String errorMessage(BrooklynObject object, Iterable<ConfigKey<?>>
violations) {
    +        StringBuilder message = new StringBuilder("Error configuring ")
    +                .append(object.getDisplayName())
    +                .append(": [");
    +        Iterator<ConfigKey<?>> it = violations.iterator();
    +        while (it.hasNext()) {
    +            ConfigKey<?> config = it.next();
    +            message.append(config.getName())
    +                    .append(":")
    +                    .append(config.getConstraint());
    +            if (it.hasNext()) {
    +                message.append(", ");
    +            }
    +        }
    +        return message.append("]").toString();
    +    }
    +
    +    public ConfigConstraints(T brooklynObject) {
    +        this.brooklynObject = brooklynObject;
    +    }
    +
    +    abstract Iterable<ConfigKey<?>> getBrooklynObjectTypeConfigKeys();
    +
    +    public boolean isValid() {
    +        return Iterables.isEmpty(getViolations());
    +    }
    +
    +    public Iterable<ConfigKey<?>> getViolations() {
    +        return validateAll();
    +    }
    +
    +    @SuppressWarnings("unchecked")
    +    private Iterable<ConfigKey<?>> validateAll() {
    +        List<ConfigKey<?>> violating = Lists.newArrayList();
    +        BrooklynObjectInternal.ConfigurationSupportInternal configInternal = getConfigurationSupportInternal();
    +
    +        Iterable<ConfigKey<?>> configKeys = getBrooklynObjectTypeConfigKeys();
    +        LOG.trace("Checking config keys on {}: {}", getBrooklynObject(), configKeys);
    +        for (ConfigKey<?> configKey : configKeys) {
    +            // getRaw returns null if explicitly set and absent if config key was unset.
    +            Object value = configInternal.getRaw(configKey).or(configKey.getDefaultValue());
    +
    +            if (value == null || value.getClass().isAssignableFrom(configKey.getType()))
{
    --- End diff --
    
    @neykov Is that what @ahgittin suggests? None of the methods in `ConfigurationSupportInternal`
do any coercion.


---
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