Search in sources :

Example 81 with MuleRuntimeException

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

the class DeployableMavenClassLoaderModelLoader method addArtifactSpecificClassloaderConfiguration.

@Override
protected void addArtifactSpecificClassloaderConfiguration(File artifactFile, ClassLoaderModelBuilder classLoaderModelBuilder, Set<BundleDependency> dependencies) {
    try {
        classLoaderModelBuilder.containing(artifactFile.toURL());
        exportSharedLibrariesResourcesAndPackages(artifactFile, classLoaderModelBuilder, dependencies);
    } catch (MalformedURLException e) {
        throw new MuleRuntimeException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 82 with MuleRuntimeException

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

the class ArtifactContextBuilder method build.

/**
 * @return the {@code MuleContext} created with the provided configuration
 * @throws ConfigurationException when there's a problem creating the {@code MuleContext}
 * @throws InitialisationException when a certain configuration component failed during initialisation phase
 */
public ArtifactContext build() throws InitialisationException, ConfigurationException {
    checkState(executionClassLoader != null, EXECUTION_CLASSLOADER_WAS_NOT_SET);
    checkState(classLoaderRepository != null, CLASS_LOADER_REPOSITORY_WAS_NOT_SET);
    checkState(POLICY.equals(artifactType) || APP.equals(artifactType) || parentArtifact == null, ONLY_APPLICATIONS_OR_POLICIES_ARE_ALLOWED_TO_HAVE_A_PARENT_ARTIFACT);
    try {
        return withContextClassLoader(executionClassLoader, () -> {
            List<ConfigurationBuilder> builders = new LinkedList<>();
            builders.addAll(additionalBuilders);
            builders.add(new ArtifactBootstrapServiceDiscovererConfigurationBuilder(artifactPlugins));
            if (extensionManagerFactory == null) {
                if (parentArtifact == null) {
                    extensionManagerFactory = new ArtifactExtensionManagerFactory(artifactPlugins, extensionModelLoaderRepository, new DefaultExtensionManagerFactory());
                } else {
                    extensionManagerFactory = new CompositeArtifactExtensionManagerFactory(parentArtifact, extensionModelLoaderRepository, artifactPlugins, new DefaultExtensionManagerFactory());
                }
            }
            builders.add(new ArtifactExtensionManagerConfigurationBuilder(artifactPlugins, extensionManagerFactory));
            builders.add(createConfigurationBuilderFromApplicationProperties());
            // TODO MULE-14289 (elrodro83) pass this object to the builder instead of looking it up here
            ArtifactConfigurationProcessor artifactConfigurationProcessor = ArtifactConfigurationProcessor.discover();
            AtomicReference<ArtifactContext> artifactContext = new AtomicReference<>();
            builders.add(new ConfigurationBuilder() {

                @Override
                public void configure(MuleContext muleContext) throws ConfigurationException {
                    if (serviceRepository != null) {
                        serviceConfigurators.add(new ContainerServicesMuleContextConfigurator(serviceRepository));
                    }
                    if (classLoaderRepository != null) {
                        serviceConfigurators.add(customizationService -> customizationService.registerCustomServiceImpl(OBJECT_CLASSLOADER_REPOSITORY, classLoaderRepository));
                    }
                    if (policyProvider != null) {
                        serviceConfigurators.add(customizationService -> customizationService.registerCustomServiceImpl(OBJECT_POLICY_PROVIDER, policyProvider));
                    }
                    ArtifactContextConfiguration.ArtifactContextConfigurationBuilder artifactContextConfigurationBuilder = ArtifactContextConfiguration.builder().setMuleContext(muleContext).setConfigResources(configurationFiles).setArtifactDeclaration(artifactDeclaration).setArtifactProperties(merge(artifactProperties, muleContext.getDeploymentProperties())).setArtifactType(artifactType).setEnableLazyInitialization(enableLazyInit).setDisableXmlValidations(disableXmlValidations).setServiceConfigurators(serviceConfigurators);
                    if (parentArtifact != null) {
                        artifactContextConfigurationBuilder.setParentContext(parentArtifact.getRegistry().lookupByType(MuleContext.class).get());
                    }
                    artifactContext.set(artifactConfigurationProcessor.createArtifactContext(artifactContextConfigurationBuilder.build()));
                    ((DefaultMuleConfiguration) muleContext.getConfiguration()).setDataFolderName(dataFolderName);
                }

                @Override
                public void addServiceConfigurator(ServiceConfigurator serviceConfigurator) {
                // Nothing to do
                }
            });
            DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
            if (muleContextListener != null) {
                muleContextFactory.addListener(muleContextListener);
            }
            if (APP.equals(artifactType)) {
                muleContextBuilder = new ApplicationMuleContextBuilder(artifactName, artifactProperties, defaultEncoding);
            } else if (POLICY.equals(artifactType)) {
                muleContextBuilder = new PolicyMuleContextBuilder(artifactName, artifactProperties, defaultEncoding);
            } else {
                muleContextBuilder = new DomainMuleContextBuilder(artifactName);
            }
            muleContextBuilder.setExecutionClassLoader(this.executionClassLoader);
            ArtifactObjectSerializer objectSerializer = new ArtifactObjectSerializer(classLoaderRepository);
            muleContextBuilder.setObjectSerializer(objectSerializer);
            muleContextBuilder.setDeploymentProperties(properties);
            if (parentArtifact != null) {
                builders.add(new ConnectionManagerConfigurationBuilder(parentArtifact));
                muleContextBuilder.setErrorTypeRepository(createCompositeErrorTypeRepository(parentArtifact.getRegistry().lookupByType(MuleContext.class).get().getErrorTypeRepository()));
            } else {
                builders.add(new ConnectionManagerConfigurationBuilder());
            }
            try {
                muleContextFactory.createMuleContext(builders, muleContextBuilder);
                return artifactContext.get();
            } catch (InitialisationException e) {
                throw new ConfigurationException(e);
            }
        });
    } catch (MuleRuntimeException e) {
        // We need this exception to be thrown as they are since the are possible causes of connectivity errors
        if (e.getCause() instanceof InitialisationException) {
            throw (InitialisationException) e.getCause();
        }
        if (e.getCause() instanceof ConfigurationException) {
            throw (ConfigurationException) e.getCause();
        }
        throw e;
    }
}
Also used : ServiceConfigurator(org.mule.runtime.api.config.custom.ServiceConfigurator) APP_HOME_DIRECTORY_PROPERTY(org.mule.runtime.core.api.config.MuleProperties.APP_HOME_DIRECTORY_PROPERTY) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DomainMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.domain.DomainMuleContextBuilder) ArtifactContextConfiguration(org.mule.runtime.deployment.model.api.artifact.ArtifactContextConfiguration) OBJECT_CLASSLOADER_REPOSITORY(org.mule.runtime.core.api.config.MuleProperties.OBJECT_CLASSLOADER_REPOSITORY) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DefaultExtensionManagerFactory(org.mule.runtime.module.extension.api.manager.DefaultExtensionManagerFactory) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) ErrorTypeRepositoryFactory.createCompositeErrorTypeRepository(org.mule.runtime.core.internal.exception.ErrorTypeRepositoryFactory.createCompositeErrorTypeRepository) ApplicationMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.ApplicationMuleContextBuilder) POLICY(org.mule.runtime.core.api.config.bootstrap.ArtifactType.POLICY) Collections.emptyList(java.util.Collections.emptyList) ArtifactPlugin(org.mule.runtime.deployment.model.api.plugin.ArtifactPlugin) PolicyMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.PolicyMuleContextBuilder) DefaultMuleConfiguration(org.mule.runtime.core.api.config.DefaultMuleConfiguration) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) List(java.util.List) Domain(org.mule.runtime.deployment.model.api.domain.Domain) ArtifactConfigurationProcessor(org.mule.runtime.deployment.model.api.artifact.ArtifactConfigurationProcessor) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) Optional(java.util.Optional) OBJECT_POLICY_PROVIDER(org.mule.runtime.core.api.config.MuleProperties.OBJECT_POLICY_PROVIDER) ArtifactExtensionManagerFactory(org.mule.runtime.module.deployment.impl.internal.policy.ArtifactExtensionManagerFactory) ServiceRepository(org.mule.runtime.api.service.ServiceRepository) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) Optional.empty(java.util.Optional.empty) ClassLoaderRepository(org.mule.runtime.module.artifact.api.classloader.ClassLoaderRepository) ExtensionModelLoaderRepository(org.mule.runtime.module.extension.internal.loader.ExtensionModelLoaderRepository) ArtifactContext(org.mule.runtime.deployment.model.api.artifact.ArtifactContext) MuleContextListener(org.mule.runtime.core.api.context.notification.MuleContextListener) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) HashMap(java.util.HashMap) APP_NAME_PROPERTY(org.mule.runtime.core.api.config.MuleProperties.APP_NAME_PROPERTY) Location(org.mule.runtime.api.component.location.Location) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) UUID.getUUID(org.mule.runtime.core.api.util.UUID.getUUID) ArtifactObjectSerializer(org.mule.runtime.module.artifact.api.serializer.ArtifactObjectSerializer) MuleContext(org.mule.runtime.core.api.MuleContext) ArtifactDeclaration(org.mule.runtime.app.declaration.api.ArtifactDeclaration) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) LinkedList(java.util.LinkedList) Properties(java.util.Properties) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) File(java.io.File) MuleContextBuilder(org.mule.runtime.core.api.context.MuleContextBuilder) ConnectivityTestingService(org.mule.runtime.api.connectivity.ConnectivityTestingService) PolicyProvider(org.mule.runtime.core.api.policy.PolicyProvider) DeployableArtifact(org.mule.runtime.deployment.model.api.DeployableArtifact) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) ExtensionManagerFactory(org.mule.runtime.module.extension.api.manager.ExtensionManagerFactory) Collections(java.util.Collections) SimpleConfigurationBuilder(org.mule.runtime.core.api.config.builders.SimpleConfigurationBuilder) ConfigurationBuilder(org.mule.runtime.core.api.config.ConfigurationBuilder) ArtifactExtensionManagerFactory(org.mule.runtime.module.deployment.impl.internal.policy.ArtifactExtensionManagerFactory) ArtifactContext(org.mule.runtime.deployment.model.api.artifact.ArtifactContext) ArtifactObjectSerializer(org.mule.runtime.module.artifact.api.serializer.ArtifactObjectSerializer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleContext(org.mule.runtime.core.api.MuleContext) ConfigurationException(org.mule.runtime.core.api.config.ConfigurationException) ArtifactConfigurationProcessor(org.mule.runtime.deployment.model.api.artifact.ArtifactConfigurationProcessor) DomainMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.domain.DomainMuleContextBuilder) DefaultExtensionManagerFactory(org.mule.runtime.module.extension.api.manager.DefaultExtensionManagerFactory) PolicyMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.PolicyMuleContextBuilder) ServiceConfigurator(org.mule.runtime.api.config.custom.ServiceConfigurator) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationMuleContextBuilder(org.mule.runtime.module.deployment.impl.internal.application.ApplicationMuleContextBuilder) LinkedList(java.util.LinkedList) DefaultMuleContextFactory(org.mule.runtime.core.api.context.DefaultMuleContextFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 83 with MuleRuntimeException

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

the class AbstractMavenClassLoaderModelLoader method createLightPackageClassLoaderModel.

private ClassLoaderModel createLightPackageClassLoaderModel(File artifactFile, Map<String, Object> attributes, ArtifactType artifactType) {
    File containerRepository;
    if (isStandalone() && !getBoolean("mule.mode.embedded")) {
        containerRepository = new File(getMuleHomeFolder(), "repository");
        if (!containerRepository.exists()) {
            if (!containerRepository.mkdirs()) {
                // check again since it may have been created already.
                if (!containerRepository.exists()) {
                    throw new MuleRuntimeException(I18nMessageFactory.createStaticMessage("Failure creating repository folder in MULE_HOME folder " + containerRepository.getAbsolutePath()));
                }
            }
        }
    }
    File localMavenRepositoryLocation = mavenClient.getMavenConfiguration().getLocalMavenRepositoryLocation();
    Supplier<File> compositeRepoLocationSupplier = localRepositorySupplierFactory.composeSuppliers(localRepositorySupplierFactory.artifactFolderRepositorySupplier(artifactFile, localMavenRepositoryLocation), localRepositorySupplierFactory.fixedFolderSupplier(localMavenRepositoryLocation));
    File mavenRepository = compositeRepoLocationSupplier.get();
    File temporaryDirectory = createTempDir();
    try {
        List<org.mule.maven.client.api.model.BundleDependency> dependencies = mavenClient.resolveArtifactDependencies(artifactFile, includeTestDependencies(attributes), includeProvidedDependencies(artifactType), of(mavenRepository), ofNullable((MavenReactorResolver) attributes.get(CLASSLOADER_MODEL_MAVEN_REACTOR_RESOLVER)), of(temporaryDirectory));
        final ClassLoaderModel.ClassLoaderModelBuilder classLoaderModelBuilder = new ClassLoaderModel.ClassLoaderModelBuilder();
        classLoaderModelBuilder.exportingPackages(new HashSet<>(getAttribute(attributes, EXPORTED_PACKAGES))).exportingPrivilegedPackages(new HashSet<>(getAttribute(attributes, PRIVILEGED_EXPORTED_PACKAGES)), new HashSet<>(getAttribute(attributes, PRIVILEGED_ARTIFACTS_IDS))).exportingResources(new HashSet<>(getAttribute(attributes, EXPORTED_RESOURCES))).includeTestDependencies(valueOf(getSimpleAttribute(attributes, INCLUDE_TEST_DEPENDENCIES, "false")));
        Set<BundleDependency> bundleDependencies = dependencies.stream().filter(mavenClientDependency -> !mavenClientDependency.getScope().equals(PROVIDED)).map(mavenClientDependency -> convertBundleDependency(mavenClientDependency)).collect(toSet());
        loadUrls(artifactFile, classLoaderModelBuilder, bundleDependencies);
        Set<BundleDependency> allBundleDependencies = dependencies.stream().map(mavenClientDependency -> convertBundleDependency(mavenClientDependency)).collect(toSet());
        classLoaderModelBuilder.dependingOn(allBundleDependencies);
        return classLoaderModelBuilder.build();
    } finally {
        deleteQuietly(temporaryDirectory);
    }
}
Also used : PRIVILEGED_EXPORTED_PACKAGES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.PRIVILEGED_EXPORTED_PACKAGES) Optional.of(java.util.Optional.of) LoggerFactory(org.slf4j.LoggerFactory) MULE_PLUGIN_CLASSIFIER(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_PLUGIN_CLASSIFIER) Map(java.util.Map) InvalidDescriptorLoaderException(org.mule.runtime.module.artifact.api.descriptor.InvalidDescriptorLoaderException) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactDescriptorConstants(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants) MULE_LOADER_ID(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.MULE_LOADER_ID) Artifact(org.mule.tools.api.classloader.model.Artifact) Files.createTempDir(com.google.common.io.Files.createTempDir) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) PROVIDED(org.mule.maven.client.api.model.BundleScope.PROVIDED) Set(java.util.Set) LocalRepositorySupplierFactory(org.mule.maven.client.api.LocalRepositorySupplierFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Boolean.getBoolean(java.lang.Boolean.getBoolean) String.format(java.lang.String.format) I18nMessageFactory(org.mule.runtime.api.i18n.I18nMessageFactory) ClassLoaderModelJsonSerializer.deserialize(org.mule.tools.api.classloader.ClassLoaderModelJsonSerializer.deserialize) List(java.util.List) Optional(java.util.Optional) FileUtils.deleteQuietly(org.apache.commons.io.FileUtils.deleteQuietly) Model(org.apache.maven.model.Model) MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) EXPORTED_RESOURCES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.EXPORTED_RESOURCES) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) BundleDescriptor(org.mule.runtime.module.artifact.api.descriptor.BundleDescriptor) BundleScope(org.mule.runtime.module.artifact.api.descriptor.BundleScope) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) MavenClient(org.mule.maven.client.api.MavenClient) PRIVILEGED_ARTIFACTS_IDS(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.PRIVILEGED_ARTIFACTS_IDS) MuleFoldersUtil.getMuleHomeFolder(org.mule.runtime.container.api.MuleFoldersUtil.getMuleHomeFolder) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Optional.ofNullable(java.util.Optional.ofNullable) INCLUDE_TEST_DEPENDENCIES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.INCLUDE_TEST_DEPENDENCIES) REPOSITORY_FOLDER(org.mule.runtime.deployment.model.api.application.ApplicationDescriptor.REPOSITORY_FOLDER) Boolean.valueOf(java.lang.Boolean.valueOf) MavenReactorResolver(org.mule.maven.client.api.MavenReactorResolver) File(java.io.File) EXPORTED_PACKAGES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.EXPORTED_PACKAGES) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) ClassLoaderModelLoader(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModelLoader) Paths(java.nio.file.Paths) MuleContainerBootstrapUtils.isStandalone(org.mule.runtime.module.reboot.api.MuleContainerBootstrapUtils.isStandalone) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) File(java.io.File) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) MavenReactorResolver(org.mule.maven.client.api.MavenReactorResolver) HashSet(java.util.HashSet)

Example 84 with MuleRuntimeException

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

the class MavenUtils method updateArtifactPom.

/**
 * Updates the pom file from an artifact.
 *
 * @param artifactFolder the artifact folder
 * @param model the new pom model
 */
public static void updateArtifactPom(File artifactFolder, Model model) {
    final File mulePluginPom = lookupPomFromMavenLocation(artifactFolder);
    MavenXpp3Writer writer = new MavenXpp3Writer();
    try (FileOutputStream outputStream = new FileOutputStream(mulePluginPom)) {
        writer.write(outputStream, model);
    } catch (IOException e) {
        throw new MuleRuntimeException(e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IOException(java.io.IOException) FileUtils.createFile(org.mule.runtime.core.api.util.FileUtils.createFile) File(java.io.File) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 85 with MuleRuntimeException

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

the class DefaultMuleApplicationStatusTestCase method deploymentFailedOnStart.

@Test
public void deploymentFailedOnStart() throws Exception {
    MuleContext mockedMuleContext = mock(MuleContext.class);
    when(mockArtifactContext.getMuleContext()).thenReturn(mockedMuleContext);
    mockedMuleContext.start();
    doThrow(new MuleRuntimeException(createStaticMessage("error")));
    try {
        application.start();
        fail("Was expecting start to fail");
    } catch (Exception e) {
        assertStatus(ApplicationStatus.DEPLOYMENT_FAILED);
    }
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Test(org.junit.Test)

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