Search in sources :

Example 1 with MulePluginModelJsonSerializer

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

the class MulePluginDescriptorGenerator method generateResource.

/**
 * {@inheritDoc}
 */
@Override
public Optional<GeneratedResource> generateResource(ExtensionModel extensionModel) {
    final Optional<ExtensionTypeDescriptorModelProperty> typeProperty = extensionModel.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
    if (!typeProperty.isPresent()) {
        return empty();
    }
    final ExportedArtifactsCollector exportCollector = new ExportedArtifactsCollector(extensionModel, new DefaultClassPackageFinder(processingEnvironment));
    final MulePluginModelBuilder builder = new MulePluginModelBuilder();
    // Set only for testing purposes, the value will be reset by the plugin packager.
    builder.setName(extensionModel.getName());
    builder.withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptorBuilder().setId(MULE_LOADER_ID).addProperty(EXPORTED_PACKAGES, exportCollector.getExportedPackages()).addProperty(EXPORTED_RESOURCES, exportCollector.getExportedResources()).addProperty(PRIVILEGED_EXPORTED_PACKAGES, exportCollector.getPrivilegedExportedPackages()).addProperty(PRIVILEGED_ARTIFACTS_IDS, exportCollector.getPrivilegedArtifacts()).build());
    builder.withExtensionModelDescriber().setId(getLoaderId(extensionModel)).addProperty(TYPE_PROPERTY_NAME, typeProperty.get().getType().getTypeName()).addProperty("version", extensionModel.getVersion());
    builder.withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    final Optional<LicenseModelProperty> licenseModelPropertyOptional = extensionModel.getModelProperty(LicenseModelProperty.class);
    builder.setRequiredProduct(extensionModel.getCategory().equals(COMMUNITY) ? MULE : MULE_EE);
    licenseModelPropertyOptional.ifPresent(licenseModelProperty -> {
        builder.setRequiredProduct(licenseModelProperty.requiresEeLicense() ? MULE_EE : MULE);
        if (licenseModelProperty.requiresEeLicense()) {
            builder.withLicenseModel().setAllowsEvaluationLicense(licenseModelProperty.isAllowsEvaluationLicense());
            licenseModelProperty.getRequiredEntitlement().ifPresent(requiredEntitlement -> builder.withLicenseModel().setProvider(extensionModel.getVendor()).setRequiredEntitlement(requiredEntitlement));
        }
    });
    final String descriptorJson = new MulePluginModelJsonSerializer().serialize(builder.build());
    return of(new GeneratedResource(AUTO_GENERATED_MULE_ARTIFACT_DESCRIPTOR, descriptorJson.getBytes()));
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) ExportedArtifactsCollector(org.mule.runtime.module.extension.internal.resources.manifest.ExportedArtifactsCollector) MuleArtifactLoaderDescriptorBuilder(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) MulePluginModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MulePluginModelJsonSerializer) DefaultClassPackageFinder(org.mule.runtime.module.extension.internal.resources.manifest.DefaultClassPackageFinder) GeneratedResource(org.mule.runtime.extension.api.resources.GeneratedResource) MulePluginModelBuilder(org.mule.runtime.api.deployment.meta.MulePluginModel.MulePluginModelBuilder) LicenseModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.LicenseModelProperty)

Example 2 with MulePluginModelJsonSerializer

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

the class PluginResourcesResolver method resolvePluginResourcesFor.

/**
 * Resolves for the given {@link PluginUrlClassification} the resources exported.
 *
 * @param pluginUrlClassification {@link PluginUrlClassification} to be resolved
 * @return {@link PluginUrlClassification} with the resources resolved
 */
public PluginUrlClassification resolvePluginResourcesFor(PluginUrlClassification pluginUrlClassification) {
    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(pluginUrlClassification.getUrls().toArray(new URL[0]), null)) {
        logger.debug("Loading plugin '{}' descriptor", pluginUrlClassification.getName());
        URL pluginJsonUrl = classLoader.getResource(MULE_ARTIFACT_PATH_INSIDE_JAR + "/" + MULE_ARTIFACT_JSON_DESCRIPTOR);
        if (pluginJsonUrl == null) {
            pluginJsonUrl = classLoader.getResource(MULE_AUTO_GENERATED_ARTIFACT_PATH_INSIDE_JAR);
            if (pluginJsonUrl == null) {
                throw new IllegalStateException(MULE_ARTIFACT_JSON_DESCRIPTOR + " couldn't be found for plugin: " + pluginUrlClassification.getName());
            }
        }
        MulePluginModel mulePluginModel;
        try (InputStream stream = pluginJsonUrl.openStream()) {
            mulePluginModel = new MulePluginModelJsonSerializer().deserialize(IOUtils.toString(stream));
        } catch (IOException e) {
            throw new IllegalArgumentException(format("Could not read extension describer on plugin '%s'", pluginJsonUrl), e);
        }
        Map<String, Object> attributes = mulePluginModel.getClassLoaderModelLoaderDescriptor().getAttributes();
        exportPackages.addAll((List<String>) attributes.getOrDefault(EXPORTED_PACKAGES, emptyList()));
        exportResources.addAll((List<String>) attributes.getOrDefault(EXPORTED_RESOURCES, emptyList()));
        privilegedExportedPackages.addAll((List<String>) attributes.getOrDefault(PRIVILEGED_EXPORTED_PACKAGES, emptyList()));
        privilegedArtifacts.addAll((List<String>) attributes.getOrDefault(PRIVILEGED_ARTIFACTS_IDS, emptyList()));
        return new PluginUrlClassification(pluginUrlClassification.getName(), pluginUrlClassification.getUrls(), pluginUrlClassification.getExportClasses(), pluginUrlClassification.getPluginDependencies(), exportPackages, exportResources, privilegedExportedPackages, privilegedArtifacts);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : MulePluginModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MulePluginModelJsonSerializer) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URL(java.net.URL) MulePluginModel(org.mule.runtime.api.deployment.meta.MulePluginModel) URLClassLoader(java.net.URLClassLoader)

Example 3 with MulePluginModelJsonSerializer

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

the class PluginResourcesResolverTestCase method resolvePluginResourcesForMulePlugin.

@Test
public void resolvePluginResourcesForMulePlugin() throws Exception {
    MulePluginModelBuilder builder = new MulePluginModelBuilder();
    builder.withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptorBuilder().setId("mule").addProperty(EXPORTED_PACKAGES, newArrayList(ORG_MULE_TEST_RUNNER, ORG_MULE_TEST_RUNNER_API)).addProperty(EXPORTED_RESOURCES, newArrayList(META_INF_RESOURCE_PROPERTIES, META_INF_ANOTHER_RESOURCE_PROPERTIES)).build());
    builder.withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    MulePluginModel mulePluginModel = builder.setName("samplePlugin").setMinMuleVersion("4.0.0").build();
    String mulePluginModelJson = new MulePluginModelJsonSerializer().serialize(mulePluginModel);
    File pluginPropertiesFile = new File(new File(new File(temporaryFolder.getRoot(), "META-INF"), "mule-artifact"), MULE_ARTIFACT_JSON_DESCRIPTOR);
    writeStringToFile(pluginPropertiesFile, mulePluginModelJson);
    URL classPathUrl = temporaryFolder.getRoot().toURI().toURL();
    List<URL> urls = newArrayList(classPathUrl);
    PluginUrlClassification mulePluginClassification = newPluginUrlClassification(urls);
    PluginResourcesResolver resolver = new PluginResourcesResolver();
    PluginUrlClassification result = resolver.resolvePluginResourcesFor(mulePluginClassification);
    assertResolvedResources(mulePluginClassification, result);
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) MuleArtifactLoaderDescriptorBuilder(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder) MulePluginModel(org.mule.runtime.api.deployment.meta.MulePluginModel) MulePluginModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MulePluginModelJsonSerializer) MulePluginModelBuilder(org.mule.runtime.api.deployment.meta.MulePluginModel.MulePluginModelBuilder) FileUtils.writeStringToFile(org.apache.commons.io.FileUtils.writeStringToFile) File(java.io.File) URL(java.net.URL) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 4 with MulePluginModelJsonSerializer

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

the class ArtifactPluginFileBuilder method getCustomResources.

@Override
protected List<ZipResource> getCustomResources() {
    final List<ZipResource> customResources = new LinkedList<>();
    if (!properties.isEmpty()) {
        final MulePluginModel.MulePluginModelBuilder builder = new MulePluginModel.MulePluginModelBuilder();
        builder.setName(getArtifactId()).setMinMuleVersion("4.0.0").setRequiredProduct(MULE);
        MuleArtifactLoaderDescriptorBuilder classLoaderModelDescriptorBuilder = new MuleArtifactLoaderDescriptorBuilder().setId(MULE_LOADER_ID);
        if (properties.containsKey(EXPORTED_CLASS_PACKAGES_PROPERTY)) {
            classLoaderModelDescriptorBuilder.addProperty(EXPORTED_PACKAGES, ((String) properties.get(EXPORTED_CLASS_PACKAGES_PROPERTY)).split(","));
        }
        if (properties.containsKey(PRIVILEGED_EXPORTED_CLASS_PACKAGES_PROPERTY)) {
            classLoaderModelDescriptorBuilder.addProperty(PRIVILEGED_EXPORTED_PACKAGES, ((String) properties.get(PRIVILEGED_EXPORTED_CLASS_PACKAGES_PROPERTY)).split(","));
        }
        if (properties.containsKey(PRIVILEGED_ARTIFACTS_PROPERTY)) {
            classLoaderModelDescriptorBuilder.addProperty(PRIVILEGED_ARTIFACTS_IDS, ((String) properties.get(PRIVILEGED_ARTIFACTS_PROPERTY)).split(","));
        }
        if (properties.containsKey(EXPORTED_RESOURCE_PROPERTY)) {
            classLoaderModelDescriptorBuilder.addProperty(EXPORTED_RESOURCES, ((String) properties.get(EXPORTED_RESOURCE_PROPERTY)).split(","));
        }
        builder.withClassLoaderModelDescriptorLoader(classLoaderModelDescriptorBuilder.build());
        builder.withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
        mulePluginModel = builder.build();
    }
    if (mulePluginModel != null) {
        final File jsonDescriptorFile = new File(getTempFolder(), MULE_ARTIFACT_FOLDER + separator + MULE_ARTIFACT_JSON_DESCRIPTOR);
        jsonDescriptorFile.deleteOnExit();
        String jsonDescriber = new MulePluginModelJsonSerializer().serialize(mulePluginModel);
        try {
            writeStringToFile(jsonDescriptorFile, jsonDescriber);
        } catch (IOException e) {
            throw new IllegalStateException("There was an issue generating the JSON file for " + this.getId(), e);
        }
        customResources.add(new ZipResource(jsonDescriptorFile.getAbsolutePath(), MULE_ARTIFACT_PATH_INSIDE_JAR + "/" + MULE_ARTIFACT_JSON_DESCRIPTOR));
    }
    return customResources;
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) MuleArtifactLoaderDescriptorBuilder(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder) MulePluginModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MulePluginModelJsonSerializer) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ZipResource(org.mule.tck.ZipUtils.ZipResource) MulePluginModel(org.mule.runtime.api.deployment.meta.MulePluginModel) FileUtils.writeStringToFile(org.apache.commons.io.FileUtils.writeStringToFile) File(java.io.File)

Aggregations

MulePluginModelJsonSerializer (org.mule.runtime.api.deployment.persistence.MulePluginModelJsonSerializer)4 MuleArtifactLoaderDescriptor (org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor)3 MuleArtifactLoaderDescriptorBuilder (org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder)3 MulePluginModel (org.mule.runtime.api.deployment.meta.MulePluginModel)3 File (java.io.File)2 IOException (java.io.IOException)2 URL (java.net.URL)2 FileUtils.writeStringToFile (org.apache.commons.io.FileUtils.writeStringToFile)2 MulePluginModelBuilder (org.mule.runtime.api.deployment.meta.MulePluginModel.MulePluginModelBuilder)2 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 URLClassLoader (java.net.URLClassLoader)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 GeneratedResource (org.mule.runtime.extension.api.resources.GeneratedResource)1 LicenseModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.LicenseModelProperty)1 ExtensionTypeDescriptorModelProperty (org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty)1 DefaultClassPackageFinder (org.mule.runtime.module.extension.internal.resources.manifest.DefaultClassPackageFinder)1 ExportedArtifactsCollector (org.mule.runtime.module.extension.internal.resources.manifest.ExportedArtifactsCollector)1 ZipResource (org.mule.tck.ZipUtils.ZipResource)1