Search in sources :

Example 1 with RequiredParameterNotSetException

use of org.mule.runtime.module.extension.internal.runtime.exception.RequiredParameterNotSetException in project mule by mulesoft.

the class ParametersResolver method resolveParameters.

/**
 * {@inheritDoc}
 */
@Override
public void resolveParameters(ObjectType objectType, DefaultObjectBuilder builder) {
    final Class<?> objectClass = getType(objectType);
    final boolean isParameterGroup = isFlattenedParameterGroup(objectType);
    objectType.getFields().forEach(field -> {
        final String key = getLocalPart(field);
        ValueResolver<?> valueResolver = null;
        Field objectField = getField(objectClass, key);
        if (parameters.containsKey(key)) {
            valueResolver = toValueResolver(parameters.get(key));
        } else if (!isParameterGroup) {
            valueResolver = getDefaultValue(field).isPresent() ? getFieldDefaultValueValueResolver(field, muleContext) : null;
        }
        Optional<NullSafeTypeAnnotation> nullSafe = field.getAnnotation(NullSafeTypeAnnotation.class);
        if (nullSafe.isPresent()) {
            ValueResolver<?> delegate = valueResolver != null ? valueResolver : new StaticValueResolver<>(null);
            MetadataType type = getMetadataType(nullSafe.get().getType(), ExtensionsTypeLoaderFactory.getDefault().createTypeLoader());
            valueResolver = NullSafeValueResolverWrapper.of(delegate, type, reflectionCache, muleContext, this);
        }
        if (field.getAnnotation(ConfigOverrideTypeAnnotation.class).isPresent()) {
            valueResolver = ConfigOverrideValueResolverWrapper.of(valueResolver != null ? valueResolver : new StaticValueResolver<>(null), key, reflectionCache, muleContext);
        }
        if (valueResolver != null) {
            try {
                initialiseIfNeeded(valueResolver, true, muleContext);
                builder.addPropertyResolver(objectField.getName(), valueResolver);
            } catch (InitialisationException e) {
                throw new MuleRuntimeException(e);
            }
        } else if (field.isRequired() && !isFlattenedParameterGroup(field) && !lazyInitEnabled) {
            throw new RequiredParameterNotSetException(objectField.getName());
        }
    });
}
Also used : RequiredParameterNotSetException(org.mule.runtime.module.extension.internal.runtime.exception.RequiredParameterNotSetException) MetadataType(org.mule.metadata.api.model.MetadataType) IntrospectionUtils.getMetadataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getMetadataType) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) Field(java.lang.reflect.Field) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigOverrideTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.ConfigOverrideTypeAnnotation)

Example 2 with RequiredParameterNotSetException

use of org.mule.runtime.module.extension.internal.runtime.exception.RequiredParameterNotSetException in project mule by mulesoft.

the class ParametersResolver method getResolverSet.

private ResolverSet getResolverSet(Optional<ParameterizedModel> model, List<ParameterGroupModel> groups, List<ParameterModel> parameterModels, MuleContext muleContext, ResolverSet resolverSet) throws ConfigurationException {
    Map<String, String> aliasedParameterNames = new HashMap<>();
    parameterModels.forEach(p -> {
        final String parameterName = getMemberName(p, p.getName());
        if (!parameterName.equals(p.getName())) {
            aliasedParameterNames.put(parameterName, p.getName());
        }
        ValueResolver<?> resolver = getParameterValueResolver(p, parameterName, muleContext);
        if (resolver != null) {
            resolverSet.add(parameterName, resolver);
        } else if (p.isRequired() && !lazyInitEnabled) {
            throw new RequiredParameterNotSetException(p);
        }
    });
    checkParameterGroupExclusiveness(model, groups, parameters.keySet().stream().map(k -> aliasedParameterNames.getOrDefault(k, k)).collect(Collectors.toSet()));
    return resolverSet;
}
Also used : RequiredParameterNotSetException(org.mule.runtime.module.extension.internal.runtime.exception.RequiredParameterNotSetException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

RequiredParameterNotSetException (org.mule.runtime.module.extension.internal.runtime.exception.RequiredParameterNotSetException)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 ConfigOverrideTypeAnnotation (org.mule.runtime.extension.api.declaration.type.annotation.ConfigOverrideTypeAnnotation)1 NullSafeTypeAnnotation (org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation)1 IntrospectionUtils.getMetadataType (org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getMetadataType)1