Search in sources :

Example 1 with ParentMuleContextAwareConfigurationBuilder

use of org.mule.runtime.core.internal.config.ParentMuleContextAwareConfigurationBuilder in project mule by mulesoft.

the class AutoConfigurationBuilder method autoConfigure.

protected void autoConfigure(MuleContext muleContext, ConfigResource[] resources) throws ConfigurationException {
    Map<String, List<ConfigResource>> configsMap = new LinkedHashMap<String, List<ConfigResource>>();
    for (ConfigResource resource : resources) {
        String configExtension = substringAfterLast(resource.getUrl().getPath(), ".");
        List<ConfigResource> configs = configsMap.get(configExtension);
        if (configs == null) {
            configs = new ArrayList<ConfigResource>();
            configsMap.put(configExtension, configs);
        }
        configs.add(resource);
    }
    try {
        Properties props = loadProperties(getResource("configuration-builders.properties", this.getClass()).openStream());
        for (Map.Entry<String, List<ConfigResource>> e : configsMap.entrySet()) {
            String extension = e.getKey();
            List<ConfigResource> configs = e.getValue();
            String className = (String) props.get(extension);
            if (className == null || !ClassUtils.isClassOnPath(className, this.getClass())) {
                throw new ConfigurationException(configurationBuilderNoMatching(createConfigResourcesString()));
            }
            ConfigurationBuilder cb = (ConfigurationBuilder) ClassUtils.instantiateClass(className, new Object[] { configs.stream().map(ConfigResource::getResourceName).toArray(String[]::new), getArtifactProperties(), artifactType });
            if (parentContext != null && cb instanceof ParentMuleContextAwareConfigurationBuilder) {
                ((ParentMuleContextAwareConfigurationBuilder) cb).setParentContext(parentContext);
            } else if (parentContext != null) {
                throw new MuleRuntimeException(createStaticMessage(format("ConfigurationBuilder %s does not support domain context", cb.getClass().getCanonicalName())));
            }
            cb.configure(muleContext);
        }
    } catch (ConfigurationException e) {
        throw e;
    } catch (Exception e) {
        throw new ConfigurationException(e);
    }
}
Also used : ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) ParentMuleContextAwareConfigurationBuilder(org.mule.runtime.core.internal.config.ParentMuleContextAwareConfigurationBuilder) AbstractResourceConfigurationBuilder(org.mule.runtime.core.api.config.builders.AbstractResourceConfigurationBuilder) Properties(java.util.Properties) PropertiesUtils.loadProperties(org.mule.runtime.core.api.util.PropertiesUtils.loadProperties) ConfigResource(org.mule.runtime.core.api.config.ConfigResource) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) LinkedHashMap(java.util.LinkedHashMap) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) ParentMuleContextAwareConfigurationBuilder(org.mule.runtime.core.internal.config.ParentMuleContextAwareConfigurationBuilder) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 ConfigResource (org.mule.runtime.core.api.config.ConfigResource)1 ConfigurationBuilder (org.mule.runtime.core.api.config.ConfigurationBuilder)1 ConfigurationException (org.mule.runtime.core.api.config.ConfigurationException)1 AbstractResourceConfigurationBuilder (org.mule.runtime.core.api.config.builders.AbstractResourceConfigurationBuilder)1 PropertiesUtils.loadProperties (org.mule.runtime.core.api.util.PropertiesUtils.loadProperties)1 ParentMuleContextAwareConfigurationBuilder (org.mule.runtime.core.internal.config.ParentMuleContextAwareConfigurationBuilder)1