Search in sources :

Example 1 with MuleServiceModelJsonSerializer

use of org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer in project mule by mulesoft.

the class ServiceFileBuilder method createServiceJsonDescriptorFile.

private File createServiceJsonDescriptorFile() {
    File serviceDescriptor = new File(getTempFolder(), getArtifactId() + "service.json");
    serviceDescriptor.deleteOnExit();
    MuleServiceModelBuilder serviceModelBuilder = new MuleServiceModelBuilder();
    serviceModelBuilder.setName(getArtifactId()).setMinMuleVersion("4.0.0").setRequiredProduct(MULE);
    serviceModelBuilder.withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    serviceModelBuilder.withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    serviceModelBuilder.withServiceProviderClassName(serviceProviderClassName);
    String serviceDescriptorContent = new MuleServiceModelJsonSerializer().serialize(serviceModelBuilder.build());
    try (FileWriter fileWriter = new FileWriter(serviceDescriptor)) {
        fileWriter.write(serviceDescriptorContent);
    } catch (IOException e) {
        throw new MuleRuntimeException(e);
    }
    return serviceDescriptor;
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) MuleServiceModelBuilder(org.mule.runtime.api.deployment.meta.MuleServiceModel.MuleServiceModelBuilder) FileWriter(java.io.FileWriter) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IOException(java.io.IOException) MuleServiceModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer) File(java.io.File)

Example 2 with MuleServiceModelJsonSerializer

use of org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer in project mule by mulesoft.

the class ServiceResourcesResolver method resolveServiceResourcesFor.

/**
 * Resolves for the given {@link ArtifactUrlClassification} the resources exported.
 *
 * @param serviceUrlClassification {@link ArtifactUrlClassification} to be resolved
 * @return {@link ArtifactUrlClassification} with the resources resolved
 */
public ArtifactUrlClassification resolveServiceResourcesFor(ArtifactUrlClassification serviceUrlClassification) {
    final Set<String> exportPackages = newHashSet();
    final Set<String> exportResources = newHashSet();
    final Set<String> privilegedExportedPackages = newHashSet();
    final Set<String> privilegedArtifacts = newHashSet();
    try (URLClassLoader classLoader = new URLClassLoader(serviceUrlClassification.getUrls().toArray(new URL[0]), null)) {
        logger.debug("Loading service '{}' descriptor", serviceUrlClassification.getName());
        URL artifactJsonUrl = classLoader.findResource(META_INF + "/" + MULE_ARTIFACT_JSON_DESCRIPTOR);
        if (artifactJsonUrl == null) {
            artifactJsonUrl = classLoader.getResource(MULE_ARTIFACT_PATH_INSIDE_JAR + "/" + MULE_ARTIFACT_JSON_DESCRIPTOR);
            if (artifactJsonUrl == null) {
                throw new IllegalStateException(MULE_ARTIFACT_JSON_DESCRIPTOR + " couldn't be found for service: " + serviceUrlClassification.getName());
            }
        }
        MuleServiceModel muleServiceModel;
        try (InputStream stream = artifactJsonUrl.openStream()) {
            muleServiceModel = new MuleServiceModelJsonSerializer().deserialize(IOUtils.toString(stream));
        } catch (IOException e) {
            throw new IllegalArgumentException(format("Could not read extension describer on service '%s'", artifactJsonUrl), e);
        }
        MuleArtifactLoaderDescriptor classLoaderModelDescriptor = muleServiceModel.getClassLoaderModelLoaderDescriptor();
        exportPackages.addAll((List<String>) classLoaderModelDescriptor.getAttributes().getOrDefault(EXPORTED_PACKAGES, new ArrayList<>()));
        exportResources.addAll((List<String>) classLoaderModelDescriptor.getAttributes().getOrDefault(EXPORTED_RESOURCES, new ArrayList<>()));
        privilegedExportedPackages.addAll((List<String>) classLoaderModelDescriptor.getAttributes().getOrDefault(PRIVILEGED_EXPORTED_PACKAGES, new ArrayList<>()));
        privilegedArtifacts.addAll((List<String>) classLoaderModelDescriptor.getAttributes().getOrDefault(PRIVILEGED_ARTIFACTS_IDS, new ArrayList<>()));
        return new ArtifactUrlClassification(serviceUrlClassification.getArtifactId(), muleServiceModel.getServiceProviderClassName(), serviceUrlClassification.getUrls());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) MuleServiceModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer) MuleServiceModel(org.mule.runtime.api.deployment.meta.MuleServiceModel)

Example 3 with MuleServiceModelJsonSerializer

use of org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer in project mule by mulesoft.

the class AetherClassPathClassifierTestCase method serviceClassification.

@Test
public void serviceClassification() throws Exception {
    this.directDependencies = newArrayList(fooCoreDep, fooToolsArtifactDep, fooTestsSupportDep, derbyDriverDep, guavaDep, fooServiceDep);
    when(dependencyResolver.getDirectDependencies(rootArtifact)).thenReturn(directDependencies);
    ArtifactDescriptorResult artifactDescriptorResult = mock(ArtifactDescriptorResult.class);
    List<Dependency> managedDependencies = newArrayList(guavaDep);
    when(artifactDescriptorResult.getManagedDependencies()).thenReturn(managedDependencies);
    when(dependencyResolver.readArtifactDescriptor(any(Artifact.class), any(List.class))).thenReturn(artifactDescriptorResult);
    when(dependencyResolver.resolveArtifact(any(Artifact.class), any(List.class))).thenAnswer(invocation -> {
        Artifact artifact = (Artifact) invocation.getArguments()[0];
        artifact = artifact.setFile(temporaryFolder.newFile());
        return new ArtifactResult(new ArtifactRequest(artifact, null, null)).setArtifact(artifact);
    });
    when(artifactClassificationTypeResolver.resolveArtifactClassificationType(rootArtifact)).thenReturn(APPLICATION);
    File fooServiceArtifactFile = temporaryFolder.newFolder();
    File metaInfFolder = new File(fooServiceArtifactFile, META_INF);
    metaInfFolder.delete();
    metaInfFolder.mkdir();
    File descriptor = new File(metaInfFolder, MULE_ARTIFACT_JSON_DESCRIPTOR);
    MuleServiceModel muleServiceModel = new MuleServiceModelBuilder().setName("Foo").setMinMuleVersion("4.0.0").withServiceProviderClassName("org.foo.ServiceProvider").withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap())).withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap())).build();
    String model = new MuleServiceModelJsonSerializer().serialize(muleServiceModel);
    FileUtils.write(descriptor, model);
    List<File> fooServiceUrls = newArrayList(fooServiceArtifactFile);
    when(dependencyResolver.resolveDependencies(argThat(equalTo(new Dependency(fooServiceDep.getArtifact(), COMPILE))), argThat(equalTo(emptyList())), argThat(equalTo(emptyList())), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())))).thenReturn(fooServiceUrls);
    File rootArtifactFile = temporaryFolder.newFile();
    Artifact jarRootArtifact = rootArtifact.setFile(rootArtifactFile);
    ArtifactResult rootArtifactResult = mock(ArtifactResult.class);
    when(rootArtifactResult.getArtifact()).thenReturn(jarRootArtifact);
    when(dependencyResolver.resolveArtifact(argThat(new ArtifactMatcher(rootArtifact.getGroupId(), rootArtifact.getArtifactId())))).thenReturn(rootArtifactResult);
    File serviceArtifactFile = temporaryFolder.newFile();
    ArtifactResult serviceArtifactResult = mock(ArtifactResult.class);
    Artifact jarServiceArtifact = serviceArtifact.setFile(serviceArtifactFile);
    when(serviceArtifactResult.getArtifact()).thenReturn(jarServiceArtifact);
    when(dependencyResolver.resolveArtifact(argThat(new ArtifactMatcher(serviceArtifact.getGroupId(), serviceArtifact.getArtifactId())))).thenReturn(serviceArtifactResult);
    ArtifactDescriptorResult defaultArtifactDescriptorResult = noManagedDependencies();
    Dependency compileMuleCoreDep = fooCoreDep.setScope(COMPILE);
    Dependency compileMuleArtifactDep = fooToolsArtifactDep.setScope(COMPILE);
    File fooCoreArtifactFile = temporaryFolder.newFile();
    File fooToolsArtifactFile = temporaryFolder.newFile();
    when(dependencyResolver.resolveDependencies(argThat(nullValue(Dependency.class)), (List<Dependency>) and((argThat(hasItems(equalTo(compileMuleCoreDep), equalTo(compileMuleArtifactDep)))), argThat(hasSize(3))), (List<Dependency>) argThat(hasItems(equalTo(guavaDep))), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())))).thenReturn(newArrayList(fooCoreArtifactFile, fooToolsArtifactFile));
    ArtifactsUrlClassification classification = classifier.classify(context);
    assertThat(classification.getTestRunnerLibUrls(), hasSize(1));
    assertThat(classification.getTestRunnerLibUrls(), hasItem(rootArtifactFile.toURI().toURL()));
    assertThat(classification.getPluginUrlClassifications(), is(empty()));
    assertThat(classification.getApplicationSharedLibUrls(), is(empty()));
    assertThat(classification.getContainerUrls(), hasSize(2));
    assertThat(classification.getServiceUrlClassifications(), hasSize(1));
    assertThat(classification.getServiceUrlClassifications().get(0).getUrls(), hasItem(fooServiceArtifactFile.toURI().toURL()));
    verify(defaultArtifactDescriptorResult, atLeastOnce()).getManagedDependencies();
    verify(dependencyResolver, atLeastOnce()).readArtifactDescriptor(any(Artifact.class), anyObject());
    verify(dependencyResolver, atLeastOnce()).resolveDependencies(argThat(equalTo(new Dependency(fooServiceDep.getArtifact(), COMPILE))), argThat(equalTo(emptyList())), argThat(equalTo(emptyList())), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())));
    verify(artifactClassificationTypeResolver).resolveArtifactClassificationType(rootArtifact);
    verify(dependencyResolver, atLeastOnce()).resolveDependencies(argThat(nullValue(Dependency.class)), (List<Dependency>) and((argThat(hasItems(equalTo(compileMuleCoreDep), equalTo(compileMuleArtifactDep)))), argThat(hasSize(3))), (List<Dependency>) argThat(hasItems(equalTo(guavaDep))), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())));
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) Dependency(org.eclipse.aether.graph.Dependency) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MuleServiceModelBuilder(org.mule.runtime.api.deployment.meta.MuleServiceModel.MuleServiceModelBuilder) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) MuleServiceModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) MuleServiceModel(org.mule.runtime.api.deployment.meta.MuleServiceModel) SmallTest(org.mule.tck.size.SmallTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MuleArtifactLoaderDescriptor (org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor)3 MuleServiceModelJsonSerializer (org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer)3 File (java.io.File)2 IOException (java.io.IOException)2 MuleServiceModel (org.mule.runtime.api.deployment.meta.MuleServiceModel)2 MuleServiceModelBuilder (org.mule.runtime.api.deployment.meta.MuleServiceModel.MuleServiceModelBuilder)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 FileWriter (java.io.FileWriter)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Artifact (org.eclipse.aether.artifact.Artifact)1 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)1 Dependency (org.eclipse.aether.graph.Dependency)1 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)1 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)1