Search in sources :

Example 1 with HeteroDescribableConfigurator

use of io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator in project configuration-as-code-plugin by jenkinsci.

the class DefaultConfiguratorRegistry method internalLookup.

private Configurator internalLookup(Type type) {
    Class clazz = Types.erasure(type);
    final Jenkins jenkins = Jenkins.get();
    final ExtensionList<Configurator> l = jenkins.getExtensionList(Configurator.class);
    for (Configurator c : l) {
        if (c.canConfigure(clazz)) {
            // this type has a dedicated Configurator implementation
            return c;
        }
    }
    // TODO: Only try to cast if we can actually get the parameterized type
    if (Collection.class.isAssignableFrom(clazz) && type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type actualType = pt.getActualTypeArguments()[0];
        if (actualType instanceof WildcardType) {
            actualType = ((WildcardType) actualType).getUpperBounds()[0];
        }
        if (actualType instanceof ParameterizedType) {
            actualType = ((ParameterizedType) actualType).getRawType();
        }
        if (!(actualType instanceof Class)) {
            throw new IllegalStateException("Can't handle " + type);
        }
        // cache is not reëntrant
        return internalLookup(actualType);
    }
    if (Configurable.class.isAssignableFrom(clazz)) {
        return new ConfigurableConfigurator(clazz);
    }
    if (Descriptor.class.isAssignableFrom(clazz)) {
        ExtensionList extensions = jenkins.getExtensionList(clazz);
        if (!extensions.isEmpty()) {
            return new DescriptorConfigurator((Descriptor) extensions.get(0));
        }
    }
    if (DataBoundConfigurator.getDataBoundConstructor(clazz) != null) {
        return new DataBoundConfigurator(clazz);
    }
    if (Modifier.isAbstract(clazz.getModifiers()) && Describable.class.isAssignableFrom(clazz)) {
        // this is a jenkins Describable component, with various implementations
        return new HeteroDescribableConfigurator(clazz);
    }
    if (Extension.class.isAssignableFrom(clazz)) {
        return new ExtensionConfigurator(clazz);
    }
    if (Stapler.lookupConverter(clazz) != null) {
        return new PrimitiveConfigurator(clazz);
    }
    if (clazz.isEnum()) {
        return new EnumConfigurator(clazz);
    }
    LOGGER.warning("Configuration-as-Code can't handle type " + type);
    return null;
}
Also used : ConfigurableConfigurator(io.jenkins.plugins.casc.impl.configurators.ConfigurableConfigurator) EnumConfigurator(io.jenkins.plugins.casc.impl.configurators.EnumConfigurator) Describable(hudson.model.Describable) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) RootElementConfigurator(io.jenkins.plugins.casc.RootElementConfigurator) PrimitiveConfigurator(io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator) HeteroDescribableConfigurator(io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator) DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) Configurator(io.jenkins.plugins.casc.Configurator) EnumConfigurator(io.jenkins.plugins.casc.impl.configurators.EnumConfigurator) ConfigurableConfigurator(io.jenkins.plugins.casc.impl.configurators.ConfigurableConfigurator) ExtensionConfigurator(io.jenkins.plugins.casc.impl.configurators.ExtensionConfigurator) DataBoundConfigurator(io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) Jenkins(jenkins.model.Jenkins) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ExtensionConfigurator(io.jenkins.plugins.casc.impl.configurators.ExtensionConfigurator) HeteroDescribableConfigurator(io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator) ExtensionList(hudson.ExtensionList) Collection(java.util.Collection) PrimitiveConfigurator(io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator)

Example 2 with HeteroDescribableConfigurator

use of io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator in project configuration-as-code-plugin by jenkinsci.

the class SchemaGeneration method generateSchema.

public static JSONObject generateSchema() {
    JSONObject schemaObject = new JSONObject(schemaTemplateObject.toString());
    DefaultConfiguratorRegistry registry = new DefaultConfiguratorRegistry();
    final ConfigurationContext context = new ConfigurationContext(registry);
    JSONObject rootConfiguratorProperties = new JSONObject();
    for (RootElementConfigurator rootElementConfigurator : RootElementConfigurator.all()) {
        JSONObject schemaConfiguratorObjects = new JSONObject();
        Set<Object> elements = new LinkedHashSet<>();
        listElements(elements, rootElementConfigurator.describe(), context, true);
        for (Object configuratorObject : elements) {
            if (configuratorObject instanceof BaseConfigurator) {
                BaseConfigurator baseConfigurator = (BaseConfigurator) configuratorObject;
                List<Attribute> baseConfigAttributeList = baseConfigurator.getAttributes();
                if (baseConfigAttributeList.size() == 0) {
                    String key = baseConfigurator.getName();
                    schemaConfiguratorObjects.put(key, new JSONObject().put("additionalProperties", false).put("type", "object").put("properties", new JSONObject()));
                } else {
                    JSONObject attributeSchema = new JSONObject();
                    for (Attribute attribute : baseConfigAttributeList) {
                        if (attribute.multiple) {
                            generateMultipleAttributeSchema(attributeSchema, attribute, context, baseConfigurator);
                        } else {
                            if (attribute.type.isEnum()) {
                                generateEnumAttributeSchema(attributeSchema, attribute, baseConfigurator);
                            } else {
                                attributeSchema.put(attribute.getName(), generateNonEnumAttributeObject(attribute, baseConfigurator));
                                String key = baseConfigurator.getName();
                                schemaConfiguratorObjects.put(key, new JSONObject().put("additionalProperties", false).put("type", "object").put("properties", attributeSchema));
                            }
                        }
                    }
                }
            } else if (configuratorObject instanceof HeteroDescribableConfigurator) {
                HeteroDescribableConfigurator heteroDescribableConfigurator = (HeteroDescribableConfigurator) configuratorObject;
                String key = heteroDescribableConfigurator.getName();
                schemaConfiguratorObjects.put(key, generateHeteroDescribableConfigObject(heteroDescribableConfigurator));
            } else if (configuratorObject instanceof Attribute) {
                Attribute attribute = (Attribute) configuratorObject;
                if (attribute.type.isEnum()) {
                    generateEnumAttributeSchema(schemaConfiguratorObjects, attribute, null);
                } else {
                    schemaConfiguratorObjects.put(attribute.getName(), generateNonEnumAttributeObject(attribute, null));
                }
            }
        }
        rootConfiguratorProperties.put(rootElementConfigurator.getName(), new JSONObject().put("type", "object").put("additionalProperties", false).put("properties", schemaConfiguratorObjects).put("title", "Configuration base for the " + rootElementConfigurator.getName() + " classifier"));
    }
    schemaObject.put("properties", rootConfiguratorProperties);
    return schemaObject;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JSONObject(org.json.JSONObject) HeteroDescribableConfigurator(io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator) DescribableAttribute(io.jenkins.plugins.casc.impl.attributes.DescribableAttribute) DefaultConfiguratorRegistry(io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry) JSONObject(org.json.JSONObject)

Aggregations

HeteroDescribableConfigurator (io.jenkins.plugins.casc.impl.configurators.HeteroDescribableConfigurator)2 ExtensionList (hudson.ExtensionList)1 Describable (hudson.model.Describable)1 Configurator (io.jenkins.plugins.casc.Configurator)1 RootElementConfigurator (io.jenkins.plugins.casc.RootElementConfigurator)1 DefaultConfiguratorRegistry (io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry)1 DescribableAttribute (io.jenkins.plugins.casc.impl.attributes.DescribableAttribute)1 ConfigurableConfigurator (io.jenkins.plugins.casc.impl.configurators.ConfigurableConfigurator)1 DataBoundConfigurator (io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator)1 DescriptorConfigurator (io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator)1 EnumConfigurator (io.jenkins.plugins.casc.impl.configurators.EnumConfigurator)1 ExtensionConfigurator (io.jenkins.plugins.casc.impl.configurators.ExtensionConfigurator)1 PrimitiveConfigurator (io.jenkins.plugins.casc.impl.configurators.PrimitiveConfigurator)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 Jenkins (jenkins.model.Jenkins)1 JSONObject (org.json.JSONObject)1