Search in sources :

Example 1 with BundleDependency

use of org.mule.runtime.module.artifact.api.descriptor.BundleDependency in project mule by mulesoft.

the class DeployableMavenClassLoaderModelLoader method exportSharedLibrariesResourcesAndPackages.

private void exportSharedLibrariesResourcesAndPackages(File applicationFolder, ClassLoaderModelBuilder classLoaderModelBuilder, Set<BundleDependency> dependencies) {
    Model model = loadPomModel(applicationFolder);
    Build build = model.getBuild();
    if (build != null) {
        List<Plugin> plugins = build.getPlugins();
        if (plugins != null) {
            Optional<Plugin> packagingPluginOptional = plugins.stream().filter(plugin -> plugin.getArtifactId().equals(MULE_MAVEN_PLUGIN_ARTIFACT_ID) && plugin.getGroupId().equals(MULE_MAVEN_PLUGIN_GROUP_ID)).findFirst();
            packagingPluginOptional.ifPresent(packagingPlugin -> {
                Object configuration = packagingPlugin.getConfiguration();
                if (configuration != null) {
                    Xpp3Dom sharedLibrariesDom = ((Xpp3Dom) configuration).getChild("sharedLibraries");
                    if (sharedLibrariesDom != null) {
                        Xpp3Dom[] sharedLibraries = sharedLibrariesDom.getChildren("sharedLibrary");
                        if (sharedLibraries != null) {
                            FileJarExplorer fileJarExplorer = new FileJarExplorer();
                            for (Xpp3Dom sharedLibrary : sharedLibraries) {
                                String groupId = getSharedLibraryAttribute(applicationFolder, sharedLibrary, "groupId");
                                String artifactId = getSharedLibraryAttribute(applicationFolder, sharedLibrary, "artifactId");
                                Optional<BundleDependency> bundleDependencyOptional = dependencies.stream().filter(bundleDependency -> bundleDependency.getDescriptor().getArtifactId().equals(artifactId) && bundleDependency.getDescriptor().getGroupId().equals(groupId)).findFirst();
                                bundleDependencyOptional.map(bundleDependency -> {
                                    JarInfo jarInfo = fileJarExplorer.explore(bundleDependency.getBundleUri());
                                    classLoaderModelBuilder.exportingPackages(jarInfo.getPackages());
                                    classLoaderModelBuilder.exportingResources(jarInfo.getResources());
                                    return bundleDependency;
                                }).orElseThrow(() -> new MuleRuntimeException(I18nMessageFactory.createStaticMessage(format("Dependency %s:%s could not be found within the artifact %s. It must be declared within the maven dependencies of the artifact.", groupId, artifactId, applicationFolder.getName()))));
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : FileJarExplorer(org.mule.runtime.module.artifact.internal.util.FileJarExplorer) LoggerFactory(org.slf4j.LoggerFactory) BundleDescriptor(org.mule.runtime.module.artifact.api.descriptor.BundleDescriptor) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) MULE_MAVEN_PLUGIN_ARTIFACT_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Map(java.util.Map) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) DOMAIN(org.mule.runtime.core.api.config.bootstrap.ArtifactType.DOMAIN) POLICY(org.mule.runtime.core.api.config.bootstrap.ArtifactType.POLICY) MULE_LOADER_ID(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.MULE_LOADER_ID) MavenClient(org.mule.maven.client.api.MavenClient) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) INCLUDE_TEST_DEPENDENCIES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.INCLUDE_TEST_DEPENDENCIES) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) AbstractMavenClassLoaderModelLoader(org.mule.runtime.module.deployment.impl.internal.maven.AbstractMavenClassLoaderModelLoader) Set(java.util.Set) LocalRepositorySupplierFactory(org.mule.maven.client.api.LocalRepositorySupplierFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) File(java.io.File) I18nMessageFactory(org.mule.runtime.api.i18n.I18nMessageFactory) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) List(java.util.List) Plugin(org.apache.maven.model.Plugin) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) MULE_MAVEN_PLUGIN_GROUP_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_GROUP_ID) MavenUtils.getPomModelFolder(org.mule.runtime.module.deployment.impl.internal.maven.MavenUtils.getPomModelFolder) Optional(java.util.Optional) ClassLoaderModelBuilder(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel.ClassLoaderModelBuilder) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) Model(org.apache.maven.model.Model) FileJarExplorer(org.mule.runtime.module.artifact.internal.util.FileJarExplorer) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) Build(org.apache.maven.model.Build) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Model(org.apache.maven.model.Model) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Plugin(org.apache.maven.model.Plugin)

Example 2 with BundleDependency

use of org.mule.runtime.module.artifact.api.descriptor.BundleDependency in project mule by mulesoft.

the class AbstractMavenClassLoaderModelLoader method createHeavyPackageClassLoaderModel.

private ClassLoaderModel createHeavyPackageClassLoaderModel(File artifactFile, Map<String, Object> attributes) {
    File classLoaderModelDescriptor = getClassLoaderModelDescriptor(artifactFile);
    org.mule.tools.api.classloader.model.ClassLoaderModel packagerClassLoaderModel = deserialize(classLoaderModelDescriptor);
    File deployableArtifactRepositoryFolder = getDeployableArtifactRepositoryFolder(artifactFile);
    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)));
    Set<BundleDependency> patchBundleDependencies = getPatchedBundledDependencies(artifactFile, deployableArtifactRepositoryFolder);
    Set<BundleDependency> bundleDependencies = packagerClassLoaderModel.getDependencies().stream().map(artifact -> {
        Optional<BundleDependency> patchedBundledDependency = patchBundleDependencies.stream().filter(bundleDependency -> bundleDependency.getDescriptor().getGroupId().equals(artifact.getArtifactCoordinates().getGroupId()) && bundleDependency.getDescriptor().getArtifactId().equals(artifact.getArtifactCoordinates().getArtifactId())).findAny();
        return patchedBundledDependency.orElse(createBundleDependencyFromPackagerDependency(deployableArtifactRepositoryFolder).apply(artifact));
    }).collect(toSet());
    loadUrls(artifactFile, classLoaderModelBuilder, bundleDependencies);
    classLoaderModelBuilder.dependingOn(bundleDependencies);
    return classLoaderModelBuilder.build();
}
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) Optional(java.util.Optional) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) File(java.io.File) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) HashSet(java.util.HashSet)

Example 3 with BundleDependency

use of org.mule.runtime.module.artifact.api.descriptor.BundleDependency in project mule by mulesoft.

the class AbstractMavenClassLoaderModelLoader method getPatchedBundledDependencies.

private Set<BundleDependency> getPatchedBundledDependencies(File artifactFile, File deployableArtifactRepositoryFolder) {
    Set<BundleDependency> patchBundleDependencies = new HashSet<>();
    File classLoaderModelPatchDescriptor = getClassLoaderModelPatchDescriptor(artifactFile);
    if (classLoaderModelPatchDescriptor.exists()) {
        org.mule.tools.api.classloader.model.ClassLoaderModel packagerClassLoaderModelPatch = deserialize(classLoaderModelPatchDescriptor);
        patchBundleDependencies.addAll(packagerClassLoaderModelPatch.getDependencies().stream().map(artifact -> createBundleDependencyFromPackagerDependency(deployableArtifactRepositoryFolder).apply(artifact)).collect(toSet()));
    }
    return patchBundleDependencies;
}
Also used : BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with BundleDependency

use of org.mule.runtime.module.artifact.api.descriptor.BundleDependency in project mule by mulesoft.

the class DeployableMavenClassLoaderModelLoaderTestCase method testPatchedDependency.

private void testPatchedDependency(String application, int totalExpectedDependencies, String patchedArtifactId, String patchedArtifactVersion) throws InvalidDescriptorLoaderException {
    DeployableMavenClassLoaderModelLoader deployableMavenClassLoaderModelLoader = new DeployableMavenClassLoaderModelLoader(mockMavenClient, mockLocalRepository);
    URL patchedAppUrl = getClass().getClassLoader().getResource(Paths.get(APPS_FOLDER, application).toString());
    ClassLoaderModel classLoaderModel = deployableMavenClassLoaderModelLoader.load(FileUtils.toFile(patchedAppUrl), emptyMap(), APP);
    Set<BundleDependency> dependencies = classLoaderModel.getDependencies();
    assertThat(dependencies, hasSize(totalExpectedDependencies));
    List<BundleDependency> connectorsFound = dependencies.stream().filter(bundleDependency -> bundleDependency.getDescriptor().getArtifactId().equals(patchedArtifactId)).collect(Collectors.toList());
    assertThat(connectorsFound, hasSize(1));
    assertThat(connectorsFound.get(0).getDescriptor().getVersion(), is(patchedArtifactVersion));
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) MavenClient(org.mule.maven.client.api.MavenClient) URL(java.net.URL) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) Set(java.util.Set) LocalRepositorySupplierFactory(org.mule.maven.client.api.LocalRepositorySupplierFactory) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) Rule(org.junit.Rule) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Paths(java.nio.file.Paths) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) Is.is(org.hamcrest.core.Is.is) Matchers.hasSize(org.hamcrest.Matchers.hasSize) InvalidDescriptorLoaderException(org.mule.runtime.module.artifact.api.descriptor.InvalidDescriptorLoaderException) TemporaryFolder(org.junit.rules.TemporaryFolder) Mockito.mock(org.mockito.Mockito.mock) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) URL(java.net.URL) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel)

Example 5 with BundleDependency

use of org.mule.runtime.module.artifact.api.descriptor.BundleDependency in project mule by mulesoft.

the class DeployableArtifactDescriptorFactoryTestCase method classLoaderModelWithSingleDependency.

@Test
public void classLoaderModelWithSingleDependency() throws Exception {
    D desc = createArtifactDescriptor(getArtifactRootFolder() + "single-dependency");
    ClassLoaderModel classLoaderModel = desc.getClassLoaderModel();
    assertThat(classLoaderModel.getDependencies(), hasSize(1));
    BundleDependency commonsCollectionDependency = classLoaderModel.getDependencies().iterator().next();
    assertThat(commonsCollectionDependency, commonsCollectionDependencyMatcher());
    assertThat(classLoaderModel.getUrls().length, is(2));
    assertThat(asList(classLoaderModel.getUrls()), hasItem(commonsCollectionDependency.getBundleUri().toURL()));
}
Also used : BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Test(org.junit.Test)

Aggregations

BundleDependency (org.mule.runtime.module.artifact.api.descriptor.BundleDependency)13 File (java.io.File)7 ClassLoaderModel (org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)5 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4 Test (org.junit.Test)4 LocalRepositorySupplierFactory (org.mule.maven.client.api.LocalRepositorySupplierFactory)4 MavenClient (org.mule.maven.client.api.MavenClient)4 String.format (java.lang.String.format)3 MalformedURLException (java.net.MalformedURLException)3 Paths (java.nio.file.Paths)3 Optional (java.util.Optional)3 Model (org.apache.maven.model.Model)3 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)3 I18nMessageFactory (org.mule.runtime.api.i18n.I18nMessageFactory)3 ArtifactType (org.mule.runtime.core.api.config.bootstrap.ArtifactType)3