Search in sources :

Example 1 with ConfigurationWrongPolymorphicTypeIdException

use of org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException in project ignite-3 by apache.

the class ConfigurationUtil method findEx.

/**
 * Finds a node or a leaf by the given path.
 *
 * @param path     Path.
 * @param rootNode Root node.
 * @param <T>      Arbitrary result type.
 * @return Node or leaf.
 */
@Nullable
public static <T> T findEx(List<KeyPathNode> path, InnerNode rootNode) {
    try {
        var visitor = new ConfigurationVisitor<T>() {

            private final int pathSize = path.size();

            /**
             * Current index of the key in the {@code path}.
             */
            private int idx;

            /**
             * {@inheritDoc}
             */
            @Override
            public T visitLeafNode(String key, Serializable val) {
                if (idx != pathSize) {
                    throw new KeyNotFoundException("Configuration value '" + joinPath(path.subList(0, idx)) + "' is a leaf");
                } else {
                    return (T) val;
                }
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public T visitInnerNode(String key, InnerNode node) {
                if (node == null) {
                    throw new KeyNotFoundException("Configuration node '" + joinPath(path.subList(0, idx)) + "' is null");
                } else if (idx == pathSize) {
                    return (T) node;
                } else {
                    try {
                        KeyPathNode pathNode = path.get(idx++);
                        assert !pathNode.unresolvedName;
                        if (INTERNAL_ID.equals(pathNode.key)) {
                            // It's impossible to get this value with a regular traversal. Just call a method.
                            return (T) node.internalId();
                        } else if (INJECTED_NAME.equals(pathNode.key)) {
                            // It's impossible to get this value with a regular traversal. Just call a method.
                            return (T) node.getInjectedNameFieldValue();
                        }
                        return node.traverseChild(pathNode.key, this, true);
                    } catch (NoSuchElementException e) {
                        throw new KeyNotFoundException("Configuration value '" + joinPath(path.subList(0, idx)) + "' has not been found");
                    } catch (ConfigurationWrongPolymorphicTypeIdException e) {
                        assert false : e;
                        return null;
                    }
                }
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public T visitNamedListNode(String key, NamedListNode<?> node) {
                if (idx == pathSize) {
                    return (T) node;
                } else {
                    KeyPathNode pathNode = path.get(idx++);
                    assert pathNode.namedListEntry;
                    String name = pathNode.unresolvedName ? pathNode.key : node.keyByInternalId(UUID.fromString(pathNode.key));
                    return visitInnerNode(name, node.getInnerNode(name));
                }
            }
        };
        return rootNode.accept(null, visitor);
    } catch (KeyNotFoundException e) {
        throw new NoSuchElementException(joinPath(path));
    }
}
Also used : Serializable(java.io.Serializable) NamedListNode(org.apache.ignite.internal.configuration.tree.NamedListNode) ConfigurationWrongPolymorphicTypeIdException(org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException) ConfigurationVisitor(org.apache.ignite.internal.configuration.tree.ConfigurationVisitor) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) NoSuchElementException(java.util.NoSuchElementException) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ConfigurationWrongPolymorphicTypeIdException

use of org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException in project ignite-3 by apache.

the class HoconObjectConfigurationSource method descend.

/**
 * {@inheritDoc}
 */
@Override
public void descend(ConstructableTreeNode node) {
    for (Map.Entry<String, ConfigValue> entry : hoconCfgObject.entrySet()) {
        String key = entry.getKey();
        if (key.equals(ignoredKey)) {
            continue;
        }
        ConfigValue hoconCfgValue = entry.getValue();
        try {
            switch(hoconCfgValue.valueType()) {
                case NULL:
                    node.construct(key, null, false);
                    break;
                case OBJECT:
                    {
                        List<String> path = appendKey(this.path, key);
                        node.construct(key, new HoconObjectConfigurationSource(null, path, (ConfigObject) hoconCfgValue), false);
                        break;
                    }
                case LIST:
                    {
                        List<String> path = appendKey(this.path, key);
                        node.construct(key, new HoconListConfigurationSource(path, (ConfigList) hoconCfgValue), false);
                        break;
                    }
                default:
                    {
                        List<String> path = appendKey(this.path, key);
                        node.construct(key, new HoconPrimitiveConfigurationSource(path, hoconCfgValue), false);
                    }
            }
        } catch (NoSuchElementException e) {
            if (path.isEmpty()) {
                throw new IllegalArgumentException(format("'%s' configuration root doesn't exist", key), e);
            } else {
                throw new IllegalArgumentException(format("'%s' configuration doesn't have the '%s' sub-configuration", join(path), key), e);
            }
        } catch (ConfigurationWrongPolymorphicTypeIdException e) {
            throw new IllegalArgumentException("Polymorphic configuration type is not correct: " + e.getMessage());
        }
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) ConfigurationWrongPolymorphicTypeIdException(org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException) ConfigList(com.typesafe.config.ConfigList) List(java.util.List) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with ConfigurationWrongPolymorphicTypeIdException

use of org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException in project ignite-3 by apache.

the class ConfigurationAsmGenerator method addCfgImplPolymorphicInstanceConfigTypeMethod.

/**
 * Add {@link DynamicConfiguration#polymorphicInstanceConfigType} method implementation to the class.
 *
 * <p>It looks like the following code:
 * <pre><code>
 * public Class polymorphicInstanceConfigType() {
 *      InnerNode val = this.isRemovedFromNamedList() ? this.currentValue() : this.refreshValue();
 *      String typeId = val.polymorphicTypeId;
 *      switch(typeId) {
 *          case "hash":
 *              return HashIndexConfiguration.class;
 *          case "sorted"
 *              return SortedIndexConfiguration.class;
 *          default:
 *              throw new ConfigurationWrongPolymorphicTypeIdException(typeId);
 *      }
 * }
 * </code></pre>
 *
 * @param classDef Class definition.
 * @param schemaClass Polymorphic configuration schema (parent).
 * @param polymorphicExtensions Polymorphic configuration instance schemas (children).
 * @param polymorphicTypeIdFieldDef Identification field for the polymorphic configuration instance.
 */
private static void addCfgImplPolymorphicInstanceConfigTypeMethod(ClassDefinition classDef, Class<?> schemaClass, Set<Class<?>> polymorphicExtensions, FieldDefinition polymorphicTypeIdFieldDef) {
    MethodDefinition polymorphicInstanceConfigTypeMtd = classDef.declareMethod(of(PUBLIC), "polymorphicInstanceConfigType", type(Class.class));
    // String tmpStr;
    Variable tmpStrVar = polymorphicInstanceConfigTypeMtd.getScope().createTempVariable(String.class);
    StringSwitchBuilder switchBuilder = new StringSwitchBuilder(polymorphicInstanceConfigTypeMtd.getScope()).expression(tmpStrVar).defaultCase(throwException(ConfigurationWrongPolymorphicTypeIdException.class, tmpStrVar));
    for (Class<?> polymorphicExtension : polymorphicExtensions) {
        switchBuilder.addCase(polymorphicInstanceId(polymorphicExtension), constantClass(typeFromJavaClassName(configurationClassName(polymorphicExtension))).ret());
    }
    // ConfigNode
    ParameterizedType nodeType = typeFromJavaClassName(nodeClassName(schemaClass));
    // Object tmpObj;
    Variable tmpObjVar = polymorphicInstanceConfigTypeMtd.getScope().createTempVariable(Object.class);
    // this;
    Variable thisVar = polymorphicInstanceConfigTypeMtd.getThis();
    // tmpObj = this.isRemovedFromNamedList() ? this.currentValue() : this.refreshValue();
    // tmpStr = ((ConfigNode) tmpObj).typeId;
    // switch(tmpStr) ...
    polymorphicInstanceConfigTypeMtd.getBody().append(tmpObjVar.set(inlineIf(thisVar.invoke(IS_REMOVED_FROM_NAMED_LIST_MTD), thisVar.invoke(CURRENT_VALUE_MTD), thisVar.invoke(REFRESH_VALUE_MTD)))).append(tmpStrVar.set(tmpObjVar.cast(nodeType).getField(polymorphicTypeIdFieldDef.getName(), String.class))).append(switchBuilder.build()).ret();
}
Also used : ParameterizedType(com.facebook.presto.bytecode.ParameterizedType) Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) ConfigurationWrongPolymorphicTypeIdException(org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException) BytecodeExpressions.constantClass(com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass)

Aggregations

ConfigurationWrongPolymorphicTypeIdException (org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException)3 NoSuchElementException (java.util.NoSuchElementException)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 ParameterizedType (com.facebook.presto.bytecode.ParameterizedType)1 Variable (com.facebook.presto.bytecode.Variable)1 BytecodeExpressions.constantClass (com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass)1 ConfigList (com.typesafe.config.ConfigList)1 ConfigValue (com.typesafe.config.ConfigValue)1 Serializable (java.io.Serializable)1 List (java.util.List)1 Map (java.util.Map)1 KeyPathNode (org.apache.ignite.internal.configuration.direct.KeyPathNode)1 ConfigurationVisitor (org.apache.ignite.internal.configuration.tree.ConfigurationVisitor)1 InnerNode (org.apache.ignite.internal.configuration.tree.InnerNode)1 NamedListNode (org.apache.ignite.internal.configuration.tree.NamedListNode)1 Nullable (org.jetbrains.annotations.Nullable)1