Search in sources :

Example 21 with MuleRuntimeException

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

the class MuleProductArtifactDescriptorValidator method validate.

@Override
public void validate(ArtifactDescriptor descriptor) {
    Product requiredProduct = descriptor.getRequiredProduct();
    Product runtimeProduct = getProductByName(getProductName());
    if (!runtimeProduct.supports(requiredProduct)) {
        throw new MuleRuntimeException(createStaticMessage("The artifact %s requires a different runtime. The artifact required runtime is %s and the runtime is %s", descriptor.getName(), descriptor.getRequiredProduct().name(), runtimeProduct.name()));
    }
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Product(org.mule.runtime.api.deployment.meta.Product)

Example 22 with MuleRuntimeException

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

the class MavenUtils method getPomUrlFromJar.

/**
 * Finds the URL of the pom file within the artifact file.
 *
 * @param artifactFile the artifact file to search for the pom file.
 * @return the URL to the pom file.
 */
public static URL getPomUrlFromJar(File artifactFile) {
    String pomFilePath = MULE_ARTIFACT_PATH_INSIDE_JAR + "/" + MULE_PLUGIN_POM;
    URL possibleUrl;
    try {
        possibleUrl = getUrlWithinJar(artifactFile, pomFilePath);
        try (InputStream ignored = possibleUrl.openStream()) {
            return possibleUrl;
        } catch (Exception e) {
            List<URL> jarMavenUrls = getUrlsWithinJar(artifactFile, META_INF + "/" + "maven");
            Optional<URL> pomUrl = jarMavenUrls.stream().filter(url -> url.toString().endsWith("pom.xml")).findAny();
            if (!pomUrl.isPresent()) {
                throw new ArtifactDescriptorCreateException(format("The identifier '%s' requires the file '%s' within the artifact(error found while reading plugin '%s')", artifactFile.getName(), "pom.xml", artifactFile.getAbsolutePath()));
            }
            return pomUrl.get();
        }
    } catch (IOException e) {
        throw new MuleRuntimeException(e);
    }
}
Also used : Optional(java.util.Optional) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ArtifactDescriptorCreateException(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptorCreateException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) URL(java.net.URL) ArtifactDescriptorCreateException(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptorCreateException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IOException(java.io.IOException)

Example 23 with MuleRuntimeException

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

the class MuleExtensionModelLoaderManager method start.

/**
 * Will look through SPI every class that implements the {@code providerClass} and if there are repeated IDs, it will
 * collect them all to throw an exception with the detailed message.
 * <p/>
 * The exception, if thrown, will have the following message:
 * <pre>
 *   There are several loaders that return the same ID when looking up providers for 'org.mule.runtime.module.artifact.ExtensionModelLoader'. Full error list:
 *   ID [some-id] is being returned by the following classes [org.foo.FooLoader, org.bar.BarLoader]
 *   ID [another-id] is being returned by the following classes [org.foo2.FooLoader2, org.bar2.BarLoader2]
 * </pre>
 *
 * @throws IllegalStateException if there are loaders with repeated IDs.
 */
@Override
public void start() throws MuleException {
    final Class<ExtensionModelLoader> providerClass = ExtensionModelLoader.class;
    final SpiServiceRegistry spiServiceRegistry = new SpiServiceRegistry();
    final ClassLoader classLoader = containerClassLoader.getClassLoader();
    final Collection<ExtensionModelLoader> extensionModelLoaders = spiServiceRegistry.lookupProviders(providerClass, classLoader);
    final StringBuilder sb = new StringBuilder();
    extensionModelLoaders.stream().collect(groupingBy(ExtensionModelLoader::getId)).entrySet().stream().filter(entry -> entry.getValue().size() > 1).forEach(entry -> {
        // At this point we are sure there are at least 2 classes that return the same ID, we will append it to the builder
        final String classes = entry.getValue().stream().map(extensionModelLoader -> extensionModelLoader.getClass().getName()).collect(Collectors.joining(", "));
        sb.append(lineSeparator()).append("ID [").append(entry.getKey()).append("] is being returned by the following classes [").append(classes).append("]");
    });
    if (isNotBlank(sb.toString())) {
        throw new MuleRuntimeException(createStaticMessage(format("There are several loaders that return the same identifier when looking up providers for '%s'. Full error list: %s", providerClass.getName(), sb.toString())));
    }
    extensionModelLoaders.stream().forEach(extensionModelLoader -> this.extensionModelLoaders.put(extensionModelLoader.getId(), extensionModelLoader));
    if (logger.isDebugEnabled()) {
        logger.debug("ExtensionModelLoader registered identifiers: {}", printExtensionModelLoaderIDs());
    }
}
Also used : Optional.empty(java.util.Optional.empty) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) System.lineSeparator(java.lang.System.lineSeparator) Logger(org.slf4j.Logger) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Optional.of(java.util.Optional.of) Collection(java.util.Collection) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) LoggerFactory(org.slf4j.LoggerFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ExtensionModelLoaderManager(org.mule.runtime.module.extension.internal.loader.ExtensionModelLoaderManager) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) LoaderDescriber(org.mule.runtime.deployment.model.api.plugin.LoaderDescriber) MuleException(org.mule.runtime.api.exception.MuleException) Map(java.util.Map) Optional(java.util.Optional) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry)

Example 24 with MuleRuntimeException

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

the class ApplicationFileBuilder method createApplicationJsonDescriptorFile.

private File createApplicationJsonDescriptorFile(Optional<Boolean> redeploymentEnabled, Optional<String> configResources, Optional<String> exportedPackages, Optional<String> exportedResources) {
    File applicationDescriptor = new File(getTempFolder(), getArtifactId() + "application.json");
    applicationDescriptor.deleteOnExit();
    MuleApplicationModel.MuleApplicationModelBuilder muleApplicationModelBuilder = new MuleApplicationModel.MuleApplicationModelBuilder();
    muleApplicationModelBuilder.setName(getArtifactId()).setMinMuleVersion("4.0.0").setRequiredProduct(MULE);
    redeploymentEnabled.ifPresent(muleApplicationModelBuilder::setRedeploymentEnabled);
    configResources.ifPresent(configs -> {
        String[] configFiles = configs.split(",");
        muleApplicationModelBuilder.setConfigs(new HashSet<>(asList(configFiles)));
    });
    MuleArtifactLoaderDescriptorBuilder muleArtifactLoaderDescriptorBuilder = new MuleArtifactLoaderDescriptorBuilder().setId(MULE_LOADER_ID);
    exportedPackages.ifPresent(packages -> {
        muleArtifactLoaderDescriptorBuilder.addProperty(EXPORTED_PACKAGES, packages.split(","));
    });
    exportedResources.ifPresent(resources -> {
        muleArtifactLoaderDescriptorBuilder.addProperty(EXPORTED_RESOURCES, resources.split(","));
    });
    muleApplicationModelBuilder.withClassLoaderModelDescriptorLoader(muleArtifactLoaderDescriptorBuilder.build());
    muleApplicationModelBuilder.withBundleDescriptorLoader(muleArtifactLoaderDescriptorBuilder.build());
    String applicationDescriptorContent = new MuleApplicationModelJsonSerializer().serialize(muleApplicationModelBuilder.build());
    try (FileWriter fileWriter = new FileWriter(applicationDescriptor)) {
        fileWriter.write(applicationDescriptorContent);
    } catch (IOException e) {
        throw new MuleRuntimeException(e);
    }
    return applicationDescriptor;
}
Also used : MuleArtifactLoaderDescriptorBuilder(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder) FileWriter(java.io.FileWriter) IOException(java.io.IOException) MuleApplicationModel(org.mule.runtime.api.deployment.meta.MuleApplicationModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) File(java.io.File) MuleApplicationModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MuleApplicationModelJsonSerializer)

Example 25 with MuleRuntimeException

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

the class DomainFileBuilder method createDomainJsonDescriptorFile.

private File createDomainJsonDescriptorFile(Optional<Boolean> redeploymentEnabled, Optional<String> configResources, Optional<String> exportedResources) {
    File domainDescriptor = new File(getTempFolder(), getArtifactId() + "domain.json");
    domainDescriptor.deleteOnExit();
    MuleDomainModel.MuleDomainModelBuilder muleDomainModelBuilder = new MuleDomainModel.MuleDomainModelBuilder();
    muleDomainModelBuilder.setName(getArtifactId()).setMinMuleVersion("4.0.0").setRequiredProduct(MULE);
    redeploymentEnabled.ifPresent(muleDomainModelBuilder::setRedeploymentEnabled);
    configResources.ifPresent(configs -> {
        String[] configFiles = configs.split(",");
        muleDomainModelBuilder.setConfigs(new HashSet<>(asList(configFiles)));
    });
    MuleArtifactLoaderDescriptorBuilder muleArtifactClassLoaderDescriptorBuilder = new MuleArtifactLoaderDescriptorBuilder().setId(MULE_LOADER_ID);
    exportedResources.ifPresent(resources -> {
        muleArtifactClassLoaderDescriptorBuilder.addProperty(EXPORTED_RESOURCES, resources.split(","));
    });
    muleDomainModelBuilder.withClassLoaderModelDescriptorLoader(muleArtifactClassLoaderDescriptorBuilder.build());
    muleDomainModelBuilder.withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    String applicationDescriptorContent = new MuleDomainModelJsonSerializer().serialize(muleDomainModelBuilder.build());
    try (FileWriter fileWriter = new FileWriter(domainDescriptor)) {
        fileWriter.write(applicationDescriptorContent);
    } catch (IOException e) {
        throw new MuleRuntimeException(e);
    }
    return domainDescriptor;
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) MuleDomainModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MuleDomainModelJsonSerializer) MuleArtifactLoaderDescriptorBuilder(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder) FileWriter(java.io.FileWriter) IOException(java.io.IOException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) File(java.io.File) MuleDomainModel(org.mule.runtime.api.deployment.meta.MuleDomainModel)

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