Search in sources :

Example 11 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException 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)

Example 12 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class PropertiesUtils method loadAllProperties.

/**
 * Load all properties files in the classpath with the given properties file name.
 */
public static Properties loadAllProperties(String fileName, ClassLoader classLoader) {
    Properties p = new Properties();
    List<URL> resourcesUrl = new ArrayList<>();
    Enumeration<URL> resources;
    try {
        resources = classLoader.getResources(fileName);
        while (resources.hasMoreElements()) {
            resourcesUrl.add(resources.nextElement());
        }
        Collections.sort(resourcesUrl, (url, url1) -> {
            if ("file".equals(url.getProtocol())) {
                return 1;
            }
            return -1;
        });
        for (URL resourceUrl : resourcesUrl) {
            InputStream in = resourceUrl.openStream();
            p.load(in);
            in.close();
        }
    } catch (IOException e) {
        throw new MuleRuntimeException(CoreMessages.createStaticMessage("Failed to load resource: " + fileName), e);
    }
    return p;
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IOException(java.io.IOException) Properties(java.util.Properties) OrderedProperties(org.mule.runtime.core.internal.util.OrderedProperties) URL(java.net.URL)

Example 13 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class StringMessageUtils method getBoilerPlate.

public static String getBoilerPlate(List<String> messages, char c, int maxlength) {
    int size;
    StringBuilder buf = new StringBuilder(messages.size() * maxlength);
    int trimLength = maxlength - (c == ' ' ? 2 : 4);
    for (int i = 0; i < messages.size(); i++) {
        size = messages.get(i).toString().length();
        if (size > trimLength) {
            String temp = messages.get(i).toString();
            int k = i;
            int x;
            int len;
            messages.remove(i);
            while (temp.length() > 0) {
                len = (trimLength <= temp.length() ? trimLength : temp.length());
                String msg = temp.substring(0, len);
                x = msg.indexOf(lineSeparator());
                if (x > -1) {
                    msg = msg.substring(0, x);
                    len = x + 1;
                } else {
                    x = msg.lastIndexOf(' ');
                    if (x > -1 && len == trimLength) {
                        msg = msg.substring(0, x);
                        len = x + 1;
                    }
                }
                if (msg.startsWith(" ")) {
                    msg = msg.substring(1);
                }
                temp = temp.substring(len);
                messages.add(k, msg);
                k++;
            }
        }
    }
    buf.append(lineSeparator());
    if (c != ' ') {
        buf.append(StringUtils.repeat(c, maxlength));
    }
    for (int i = 0; i < messages.size(); i++) {
        buf.append(lineSeparator());
        if (c != ' ') {
            buf.append(c);
        }
        buf.append(" ");
        buf.append(messages.get(i));
        String osEncoding = Charset.defaultCharset().name();
        int padding;
        try {
            padding = trimLength - messages.get(i).toString().getBytes(osEncoding).length;
        } catch (UnsupportedEncodingException ueex) {
            throw new MuleRuntimeException(failedToConvertStringUsingEncoding(osEncoding), ueex);
        }
        if (padding > 0) {
            buf.append(StringUtils.repeat(' ', padding));
        }
        buf.append(' ');
        if (c != ' ') {
            buf.append(c);
        }
    }
    buf.append(lineSeparator());
    if (c != ' ') {
        buf.append(StringUtils.repeat(c, maxlength));
    }
    return buf.toString();
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 14 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class AbstractAggregator method initEventGroupsObjectStore.

protected void initEventGroupsObjectStore() throws InitialisationException {
    try {
        if (eventGroupsObjectStore == null) {
            // TODO: Delete ProvidedObjectStoreWrapper if not needed when moving this to compatibility
            eventGroupsObjectStore = new ProvidedPartitionableObjectStoreWrapper(null, internalEventsGroupsObjectStoreSupplier());
        }
        eventGroupsObjectStore.open(storePrefix + ".expiredAndDispatchedGroups");
        eventGroupsObjectStore.open(storePrefix + ".eventGroups");
    } catch (MuleRuntimeException | ObjectStoreException e) {
        throw new InitialisationException(e, this);
    }
}
Also used : ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) ProvidedPartitionableObjectStoreWrapper(org.mule.runtime.core.internal.util.store.ProvidedPartitionableObjectStoreWrapper) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException)

Example 15 with MuleRuntimeException

use of org.mule.runtime.api.exception.MuleRuntimeException in project mule by mulesoft.

the class MuleObjectStoreManager method getMonitorablePartition.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T extends ObjectStore<? extends Serializable>> T getMonitorablePartition(String name, ObjectStore baseStore, T store, ObjectStoreSettings settings) {
    if (baseStore instanceof PartitionableExpirableObjectStore) {
        Scheduler scheduler = schedulerService.customScheduler(muleContext.getSchedulerBaseConfig().withName("ObjectStoreManager-Monitor-" + name).withMaxConcurrentTasks(1));
        scheduler.scheduleWithFixedDelay(new Monitor(name, (PartitionableExpirableObjectStore) baseStore, settings.getEntryTTL().orElse(0L), settings.getMaxEntries().orElse(UNBOUNDED)), 0, settings.getExpirationInterval(), MILLISECONDS);
        expirationSchedulers.put(name, scheduler);
        return store;
    } else {
        MonitoredObjectStoreWrapper monObjectStore;
        // or putting an uninitialised ObjectStore
        synchronized (this) {
            monObjectStore = new MonitoredObjectStoreWrapper(store, settings);
            monObjectStore.setMuleContext(muleContext);
            try {
                monObjectStore.initialise();
            } catch (InitialisationException e) {
                throw new MuleRuntimeException(e);
            }
        }
        return (T) monObjectStore;
    }
}
Also used : Scheduler(org.mule.runtime.api.scheduler.Scheduler) PartitionableExpirableObjectStore(org.mule.runtime.api.store.PartitionableExpirableObjectStore) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException)

Aggregations

MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)123 IOException (java.io.IOException)22 List (java.util.List)22 MuleException (org.mule.runtime.api.exception.MuleException)22 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)22 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)22 Map (java.util.Map)20 Optional (java.util.Optional)20 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)18 ArrayList (java.util.ArrayList)17 String.format (java.lang.String.format)16 File (java.io.File)15 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)13 Set (java.util.Set)13 Collectors.toList (java.util.stream.Collectors.toList)12 ConfigurationException (org.mule.runtime.core.api.config.ConfigurationException)12 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)10 Collections.emptyMap (java.util.Collections.emptyMap)9 Optional.empty (java.util.Optional.empty)9