All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ca.apim.gateway.cagatewayconfig.BuildEnvironmentBundleTask Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018 CA. All rights reserved.
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

package com.ca.apim.gateway.cagatewayconfig;

import com.ca.apim.gateway.cagatewayconfig.beans.BundleConfig;
import com.ca.apim.gateway.cagatewayconfig.environment.BundleCache;
import com.ca.apim.gateway.cagatewayconfig.environment.EnvironmentBundleCreator;
import com.ca.apim.gateway.cagatewayconfig.environment.MissingEnvironmentException;
import com.ca.apim.gateway.cagatewayconfig.util.environment.EnvironmentConfigurationUtils;
import com.ca.apim.gateway.cagatewayconfig.util.injection.InjectionRegistry;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.*;

import javax.inject.Inject;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.ca.apim.gateway.cagatewayconfig.environment.EnvironmentBundleCreationMode.PLUGIN;
import static com.ca.apim.gateway.cagatewayconfig.util.file.DocumentFileUtils.*;
import static com.ca.apim.gateway.cagatewayconfig.util.gateway.BuilderUtils.removeAllSpecialChars;
import static com.ca.apim.gateway.cagatewayconfig.util.injection.InjectionRegistry.getInstance;
import static org.apache.commons.lang3.StringUtils.EMPTY;

/**
 * The BuildEnvironmentBundle task will grab provided environment properties and build a bundle.
 */
public class BuildEnvironmentBundleTask extends DefaultTask {

    private final DirectoryProperty into;
    private final EnvironmentConfigurationUtils environmentConfigurationUtils;
    private final DirectoryProperty configFolder;
    private final Property configName;
    private final Property environmentConfig;
    private final Property envConfig;
    private GatewayDeveloperPluginConfig gatewaySourceConfig;

    @Inject
    public BuildEnvironmentBundleTask() {
        into = newOutputDirectory();
        environmentConfig = getProject().getObjects().property(Map.class);
        envConfig = getProject().getObjects().property(Map.class);
        environmentConfigurationUtils = getInstance(EnvironmentConfigurationUtils.class);
        configFolder = newInputDirectory();
        configName = getProject().getObjects().property(String.class);
        gatewaySourceConfig = getProject().getExtensions().findByType(GatewayDeveloperPluginConfig.class);
    }

    @OutputDirectory
    DirectoryProperty getInto() {
        return into;
    }

    @Input
    @Optional
    Property getEnvironmentConfig() {
        return environmentConfig;
    }

    @Input
    @Optional
    Property getEnvConfig() {
        return envConfig;
    }

    @InputDirectory
    @Optional
    DirectoryProperty getConfigFolder() {
        return configFolder;
    }

    @Input
    @Optional
    Property getConfigName() {
        return configName;
    }

    @TaskAction
    public void perform() {
        final BundleConfig bundleConfig = PluginUtils.toBundleConfig(gatewaySourceConfig);
        final EnvironmentBundleCreator environmentBundleCreator = getInstance(EnvironmentBundleCreator.class);
        File configuredFolder = configFolder.getAsFile().getOrNull();
        Map environmentEntities = java.util.Optional.ofNullable(envConfig.getOrNull()).orElse(environmentConfig.getOrNull());
        if (configuredFolder == null && environmentEntities == null) {
            throw new MissingEnvironmentException("EnvironmentConfig is not configured");
        }
        Map bundleEnvironmentValues = new HashMap<>();
        if(configuredFolder != null){
            bundleEnvironmentValues = environmentConfigurationUtils.loadConfigFolder(configuredFolder);
        }
        final String configurationName = configName != null ? removeAllSpecialChars(configName.get()) : EMPTY;
        ProjectInfo projectInfo = new ProjectInfo(getProject().getName(), getProject().getGroup().toString(), getProject().getVersion().toString(), configurationName);
        final String envBundleFileName = getEnvBundleFilename(projectInfo);

        //read environment properties from environmentConfig and merge it with config folder entities
        if(environmentEntities != null) {
            bundleEnvironmentValues.putAll(environmentConfigurationUtils.parseEnvironmentValues(environmentEntities));
        }

        // Invalidate the previously cached deployment bundle that is used to resolve the references inside the environment entities
        // Ex: Service reference inside the SSG Connector, JMS Destination or Active Connector, etc.
        final BundleCache cache = InjectionRegistry.getInjector().getInstance(BundleCache.class);
        cache.removeBundle(into.getAsFile().get().getPath());

        environmentBundleCreator.createEnvironmentBundle(
                bundleEnvironmentValues,
                into.getAsFile().get().getPath(),
                into.getAsFile().get().getPath(),
                configuredFolder != null ? configuredFolder.getPath() : EMPTY,
                PLUGIN,
                envBundleFileName, // Passing envBundleFileName
                projectInfo,
                bundleConfig
        );
    }

    /**
     * Generates the Environment install bundle filename in the format -environment--.install.bundle.
     * 

* Here is generated with the following preference: * 1) If "name" is provided in the EnvironmentConfig {} in build.gradle, "name" will be used after * removing all the special characters. For eg. if "name" is set "default", filename will have "-default" * * @param projectInfo ProjectInfo * @return Environment install bundle filename */ private String getEnvBundleFilename(final ProjectInfo projectInfo) { StringBuilder bundleNameBuilder = new StringBuilder(projectInfo.getName()); bundleNameBuilder.append("-").append(PREFIX_ENVIRONMENT); if (StringUtils.isNotBlank(projectInfo.getVersion())) { bundleNameBuilder.append("-").append(projectInfo.getVersion()); if (StringUtils.isNotBlank(projectInfo.getConfigName())) { bundleNameBuilder.append("-").append(projectInfo.getConfigName()); } } return bundleNameBuilder.append(INSTALL_BUNDLE_EXTENSION).toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy