Example usage for org.apache.commons.collections4 CollectionUtils isEmpty

List of usage examples for org.apache.commons.collections4 CollectionUtils isEmpty

Introduction

In this page you can find the example usage for org.apache.commons.collections4 CollectionUtils isEmpty.

Prototype

public static boolean isEmpty(final Collection<?> coll) 

Source Link

Document

Null-safe check if the specified collection is empty.

Usage

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

/**
 * Creates a list of {@link InstanceTypeConfig} from a given list of {@link EmrClusterDefinitionInstanceTypeConfig}.
 *
 * @param emrClusterDefinitionInstanceTypeConfigs the list of {@link EmrClusterDefinitionInstanceTypeConfig}
 *
 * @return the list of {@link InstanceTypeConfig}
 *//*w  w w. j a  va 2s  .c o m*/
protected List<InstanceTypeConfig> getInstanceTypeConfigs(
        List<EmrClusterDefinitionInstanceTypeConfig> emrClusterDefinitionInstanceTypeConfigs) {
    List<InstanceTypeConfig> instanceTypeConfigs = null;

    if (!CollectionUtils.isEmpty(emrClusterDefinitionInstanceTypeConfigs)) {
        instanceTypeConfigs = new ArrayList<>();

        for (EmrClusterDefinitionInstanceTypeConfig emrClusterDefinitionInstanceTypeConfig : emrClusterDefinitionInstanceTypeConfigs) {
            if (emrClusterDefinitionInstanceTypeConfig != null) {
                InstanceTypeConfig instanceTypeConfig = new InstanceTypeConfig();
                instanceTypeConfig.setInstanceType(emrClusterDefinitionInstanceTypeConfig.getInstanceType());
                instanceTypeConfig
                        .setWeightedCapacity(emrClusterDefinitionInstanceTypeConfig.getWeightedCapacity());
                instanceTypeConfig.setBidPrice(emrClusterDefinitionInstanceTypeConfig.getBidPrice());
                instanceTypeConfig.setBidPriceAsPercentageOfOnDemandPrice(
                        emrClusterDefinitionInstanceTypeConfig.getBidPriceAsPercentageOfOnDemandPrice());
                instanceTypeConfig.setEbsConfiguration(
                        getEbsConfiguration(emrClusterDefinitionInstanceTypeConfig.getEbsConfiguration()));
                instanceTypeConfig.setConfigurations(
                        getConfigurations(emrClusterDefinitionInstanceTypeConfig.getConfigurations()));

                instanceTypeConfigs.add(instanceTypeConfig);
            }
        }
    }

    return instanceTypeConfigs;
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

/**
 * Converts the given list of {@link Parameter} into a {@link Map} of {@link String}, {@link String}
 *
 * @param parameters List of {@link Parameter}
 *
 * @return {@link Map}/*from w  w  w  .  j av  a 2 s.c om*/
 */
protected Map<String, String> getMap(List<Parameter> parameters) {
    Map<String, String> map = null;

    if (!CollectionUtils.isEmpty(parameters)) {
        map = new HashMap<>();

        for (Parameter parameter : parameters) {
            if (parameter != null) {
                map.put(parameter.getName(), parameter.getValue());
            }
        }
    }

    return map;
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

private void addCustomBootstrapActionConfig(EmrClusterDefinition emrClusterDefinition,
        ArrayList<BootstrapActionConfig> bootstrapActions) {
    // Add Custom bootstrap script support if needed
    if (!CollectionUtils.isEmpty(emrClusterDefinition.getCustomBootstrapActionAll())) {
        for (ScriptDefinition scriptDefinition : emrClusterDefinition.getCustomBootstrapActionAll()) {
            BootstrapActionConfig customActionConfigAll = getBootstrapActionConfig(
                    scriptDefinition.getScriptName(), scriptDefinition.getScriptLocation());

            ArrayList<String> argList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(scriptDefinition.getScriptArguments())) {
                for (String argument : scriptDefinition.getScriptArguments()) {
                    // Trim the argument
                    argList.add(argument.trim());
                }//  ww  w.ja v a  2s.  co m
            }
            // Set arguments to bootstrap action
            customActionConfigAll.getScriptBootstrapAction().setArgs(argList);

            bootstrapActions.add(customActionConfigAll);
        }
    }
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

private void addCustomMasterBootstrapActionConfig(EmrClusterDefinition emrClusterDefinition,
        ArrayList<BootstrapActionConfig> bootstrapActions) {
    // Add Master custom bootstrap script support if needed
    if (!CollectionUtils.isEmpty(emrClusterDefinition.getCustomBootstrapActionMaster())) {
        for (ScriptDefinition scriptDefinition : emrClusterDefinition.getCustomBootstrapActionMaster()) {
            BootstrapActionConfig bootstrapActionConfig = getBootstrapActionConfig(
                    scriptDefinition.getScriptName(),
                    configurationHelper.getProperty(ConfigurationValue.EMR_CONDITIONAL_SCRIPT));

            // Add arguments to the bootstrap script
            ArrayList<String> argList = new ArrayList<>();

            // Execute this script only on the master node.
            argList.add(configurationHelper.getProperty(ConfigurationValue.EMR_NODE_CONDITION));
            argList.add(scriptDefinition.getScriptLocation());

            if (!CollectionUtils.isEmpty(scriptDefinition.getScriptArguments())) {
                for (String argument : scriptDefinition.getScriptArguments()) {
                    // Trim the argument
                    argList.add(argument.trim());
                }/*from  ww  w  .  ja v a 2 s.  c  o m*/
            }

            bootstrapActionConfig.getScriptBootstrapAction().setArgs(argList);
            bootstrapActions.add(bootstrapActionConfig);
        }
    }
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

private void addDaemonBootstrapActionConfig(EmrClusterDefinition emrClusterDefinition,
        ArrayList<BootstrapActionConfig> bootstrapActions) {
    // Add daemon Configuration support if needed
    if (!CollectionUtils.isEmpty(emrClusterDefinition.getDaemonConfigurations())) {
        BootstrapActionConfig daemonBootstrapActionConfig = getBootstrapActionConfig(
                ConfigurationValue.EMR_CONFIGURE_DAEMON.getKey(),
                configurationHelper.getProperty(ConfigurationValue.EMR_CONFIGURE_DAEMON));

        // Add arguments to the bootstrap script
        ArrayList<String> argList = new ArrayList<>();
        for (Parameter daemonConfig : emrClusterDefinition.getDaemonConfigurations()) {
            argList.add(daemonConfig.getName() + "=" + daemonConfig.getValue());
        }/*from   w w  w  .  ja  v  a  2s.  c  o m*/

        // Add the bootstrap action with arguments
        daemonBootstrapActionConfig.getScriptBootstrapAction().setArgs(argList);
        bootstrapActions.add(daemonBootstrapActionConfig);
    }
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

private void addHadoopBootstrapActionConfig(EmrClusterDefinition emrClusterDefinition,
        ArrayList<BootstrapActionConfig> bootstrapActions) {
    // Add hadoop Configuration support if needed
    if (!CollectionUtils.isEmpty(emrClusterDefinition.getHadoopConfigurations())) {
        ArrayList<String> argList = new ArrayList<>();
        BootstrapActionConfig hadoopBootstrapActionConfig = getBootstrapActionConfig(
                ConfigurationValue.EMR_CONFIGURE_HADOOP.getKey(),
                configurationHelper.getProperty(ConfigurationValue.EMR_CONFIGURE_HADOOP));
        // If config files are available, add them as arguments
        for (Object hadoopConfigObject : emrClusterDefinition.getHadoopConfigurations()) {
            // If the Config Files are available, add them as arguments
            if (hadoopConfigObject instanceof ConfigurationFiles) {
                for (ConfigurationFile configurationFile : ((ConfigurationFiles) hadoopConfigObject)
                        .getConfigurationFiles()) {
                    argList.add(configurationFile.getFileNameShortcut());
                    argList.add(configurationFile.getConfigFileLocation());
                }/*from   w ww  . j a v a  2s  . co m*/
            }

            // If the key value pairs are available, add them as arguments
            if (hadoopConfigObject instanceof KeyValuePairConfigurations) {
                for (KeyValuePairConfiguration keyValuePairConfiguration : ((KeyValuePairConfigurations) hadoopConfigObject)
                        .getKeyValuePairConfigurations()) {
                    argList.add(keyValuePairConfiguration.getKeyValueShortcut());
                    argList.add(keyValuePairConfiguration.getAttribKey() + "="
                            + keyValuePairConfiguration.getAttribVal());
                }
            }
        }

        // Add the bootstrap action with arguments
        hadoopBootstrapActionConfig.getScriptBootstrapAction().setArgs(argList);
        bootstrapActions.add(hadoopBootstrapActionConfig);
    }
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

/**
 * Create the run job flow request object.
 *
 * @param emrClusterDefinition the EMR definition name value
 * @param clusterName the EMR cluster name
 *
 * @return the run job flow request for the given configuration
 *//*from  w ww. j  a v  a2  s . c  om*/
private RunJobFlowRequest getRunJobFlowRequest(String clusterName, EmrClusterDefinition emrClusterDefinition) {
    // Create the object
    RunJobFlowRequest runJobFlowRequest = new RunJobFlowRequest(clusterName,
            getJobFlowInstancesConfig(emrClusterDefinition));

    // Set release label
    if (StringUtils.isNotBlank(emrClusterDefinition.getReleaseLabel())) {
        runJobFlowRequest.setReleaseLabel(emrClusterDefinition.getReleaseLabel());
    }

    // Set list of Applications
    List<EmrClusterDefinitionApplication> emrClusterDefinitionApplications = emrClusterDefinition
            .getApplications();
    if (!CollectionUtils.isEmpty(emrClusterDefinitionApplications)) {
        runJobFlowRequest.setApplications(getApplications(emrClusterDefinitionApplications));
    }

    // Set list of Configurations
    List<EmrClusterDefinitionConfiguration> emrClusterDefinitionConfigurations = emrClusterDefinition
            .getConfigurations();
    if (!CollectionUtils.isEmpty(emrClusterDefinitionConfigurations)) {
        runJobFlowRequest.setConfigurations(getConfigurations(emrClusterDefinitionConfigurations));
    }

    // Set the log bucket if specified
    if (StringUtils.isNotBlank(emrClusterDefinition.getLogBucket())) {
        runJobFlowRequest.setLogUri(emrClusterDefinition.getLogBucket());
    }

    // Set the visible to all flag
    if (emrClusterDefinition.isVisibleToAll() != null) {
        runJobFlowRequest.setVisibleToAllUsers(emrClusterDefinition.isVisibleToAll());
    }

    // Set the IAM profile for the nodes
    if (StringUtils.isNotBlank(emrClusterDefinition.getEc2NodeIamProfileName())) {
        runJobFlowRequest.setJobFlowRole(emrClusterDefinition.getEc2NodeIamProfileName());
    } else {
        runJobFlowRequest.setJobFlowRole(herdStringHelper
                .getRequiredConfigurationValue(ConfigurationValue.EMR_DEFAULT_EC2_NODE_IAM_PROFILE_NAME));
    }

    // Set the IAM profile for the service
    if (StringUtils.isNotBlank(emrClusterDefinition.getServiceIamRole())) {
        runJobFlowRequest.setServiceRole(emrClusterDefinition.getServiceIamRole());
    } else {
        runJobFlowRequest.setServiceRole(herdStringHelper
                .getRequiredConfigurationValue(ConfigurationValue.EMR_DEFAULT_SERVICE_IAM_ROLE_NAME));
    }

    // Set the AMI version if specified
    if (StringUtils.isNotBlank(emrClusterDefinition.getAmiVersion())) {
        runJobFlowRequest.setAmiVersion(emrClusterDefinition.getAmiVersion());
    }

    // Set the additionalInfo if specified
    if (StringUtils.isNotBlank(emrClusterDefinition.getAdditionalInfo())) {
        runJobFlowRequest.setAdditionalInfo(emrClusterDefinition.getAdditionalInfo());
    }

    // Set the bootstrap actions
    List<BootstrapActionConfig> bootstrapActionConfigList = getBootstrapActionConfigList(emrClusterDefinition);
    if (!bootstrapActionConfigList.isEmpty()) {
        runJobFlowRequest.setBootstrapActions(bootstrapActionConfigList);
    }

    // Set the app installation steps
    runJobFlowRequest.setSteps(getStepConfig(emrClusterDefinition));

    // Set the tags
    runJobFlowRequest.setTags(getEmrTags(emrClusterDefinition));

    // Assign supported products as applicable
    if (StringUtils.isNotBlank(emrClusterDefinition.getSupportedProduct())) {
        List<String> supportedProducts = new ArrayList<>();
        supportedProducts.add(emrClusterDefinition.getSupportedProduct());
        runJobFlowRequest.setSupportedProducts(supportedProducts);
    }

    // Assign security configuration.
    if (StringUtils.isNotBlank(emrClusterDefinition.getSecurityConfiguration())) {
        runJobFlowRequest.setSecurityConfiguration(emrClusterDefinition.getSecurityConfiguration());
    }

    // Assign scale down behavior.
    if (StringUtils.isNotBlank(emrClusterDefinition.getScaleDownBehavior())) {
        runJobFlowRequest.setScaleDownBehavior(emrClusterDefinition.getScaleDownBehavior());
    }

    // Assign Kerberos attributes.
    runJobFlowRequest
            .setKerberosAttributes(getKerberosAttributes(emrClusterDefinition.getKerberosAttributes()));

    // Return the object
    return runJobFlowRequest;
}

From source file:org.finra.herd.dao.impl.EmrDaoImpl.java

/**
 * Create the step config list of objects for hive/pig installation.
 *
 * @param emrClusterDefinition the EMR definition name value.
 *
 * @return list of step configuration that contains all the steps for the given configuration.
 *///from ww w  .j av  a  2  s  .c  om
private List<StepConfig> getStepConfig(EmrClusterDefinition emrClusterDefinition) {
    StepFactory stepFactory = new StepFactory();
    List<StepConfig> appSteps = new ArrayList<>();

    // Create install hive step and add to the StepConfig list
    if (StringUtils.isNotBlank(emrClusterDefinition.getHiveVersion())) {
        StepConfig installHive = new StepConfig().withName("Hive " + emrClusterDefinition.getHiveVersion())
                .withActionOnFailure(ActionOnFailure.TERMINATE_JOB_FLOW)
                .withHadoopJarStep(stepFactory.newInstallHiveStep(emrClusterDefinition.getHiveVersion()));
        appSteps.add(installHive);
    }

    // Create install Pig step and add to the StepConfig List
    if (StringUtils.isNotBlank(emrClusterDefinition.getPigVersion())) {
        StepConfig installPig = new StepConfig().withName("Pig " + emrClusterDefinition.getPigVersion())
                .withActionOnFailure(ActionOnFailure.TERMINATE_JOB_FLOW)
                .withHadoopJarStep(stepFactory.newInstallPigStep(emrClusterDefinition.getPigVersion()));
        appSteps.add(installPig);
    }

    // Add the hadoop jar steps that need to be added.
    if (!CollectionUtils.isEmpty(emrClusterDefinition.getHadoopJarSteps())) {
        for (HadoopJarStep hadoopJarStep : emrClusterDefinition.getHadoopJarSteps()) {
            StepConfig stepConfig = emrHelper.getEmrHadoopJarStepConfig(hadoopJarStep.getStepName(),
                    hadoopJarStep.getJarLocation(), hadoopJarStep.getMainClass(),
                    hadoopJarStep.getScriptArguments(), hadoopJarStep.isContinueOnError());

            appSteps.add(stepConfig);
        }
    }

    return appSteps;
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

@Override
public void restoreObjects(final S3FileTransferRequestParamsDto params, int expirationInDays,
        String archiveRetrievalOption) {
    LOGGER.info("Restoring a list of objects in S3... s3KeyPrefix=\"{}\" s3BucketName=\"{}\" s3KeyCount={}",
            params.getS3KeyPrefix(), params.getS3BucketName(), params.getFiles().size());

    if (!CollectionUtils.isEmpty(params.getFiles())) {
        // Initialize a key value pair for the error message in the catch block.
        String key = params.getFiles().get(0).getPath().replaceAll("\\\\", "/");

        try {//  w  ww.ja v  a2s.  co  m
            // Create an S3 client.
            AmazonS3Client s3Client = getAmazonS3(params);

            // Create a restore object request.
            RestoreObjectRequest requestRestore = new RestoreObjectRequest(params.getS3BucketName(), null,
                    expirationInDays);
            // Make Bulk the default archive retrieval option if the option is not provided
            requestRestore.setGlacierJobParameters(new GlacierJobParameters()
                    .withTier(StringUtils.isNotEmpty(archiveRetrievalOption) ? archiveRetrievalOption
                            : Tier.Bulk.toString()));

            try {
                for (File file : params.getFiles()) {
                    key = file.getPath().replaceAll("\\\\", "/");
                    ObjectMetadata objectMetadata = s3Operations.getObjectMetadata(params.getS3BucketName(),
                            key, s3Client);

                    // Request a restore for objects that are not already being restored.
                    if (BooleanUtils.isNotTrue(objectMetadata.getOngoingRestore())) {
                        requestRestore.setKey(key);
                        s3Operations.restoreObject(requestRestore, s3Client);
                    }
                }
            } finally {
                s3Client.shutdown();
            }
        } catch (Exception e) {
            throw new IllegalStateException(String.format(
                    "Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. Reason: %s", key,
                    params.getS3BucketName(), e.getMessage()), e);
        }
    }
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

@Override
public void tagObjects(final S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto,
        final S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto,
        final List<S3ObjectSummary> s3ObjectSummaries, final Tag tag) {
    LOGGER.info(//from   w ww  .  j  a  v  a2 s  .c  o m
            "Tagging objects in S3... s3BucketName=\"{}\" s3KeyPrefix=\"{}\" s3KeyCount={} s3ObjectTagKey=\"{}\" s3ObjectTagValue=\"{}\"",
            s3FileTransferRequestParamsDto.getS3BucketName(), s3FileTransferRequestParamsDto.getS3KeyPrefix(),
            CollectionUtils.size(s3ObjectSummaries), tag.getKey(), tag.getValue());

    if (!CollectionUtils.isEmpty(s3ObjectSummaries)) {
        // Convert a list of S3 object summaries to S3 version summaries without version identifiers.
        List<S3VersionSummary> s3VersionSummaries = new ArrayList<>();
        for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
            S3VersionSummary s3VersionSummary = new S3VersionSummary();
            s3VersionSummary.setBucketName(s3ObjectSummary.getBucketName());
            s3VersionSummary.setKey(s3ObjectSummary.getKey());
            s3VersionSummaries.add(s3VersionSummary);
        }

        // Tag S3 objects.
        tagVersionsHelper(s3FileTransferRequestParamsDto, s3ObjectTaggerParamsDto, s3VersionSummaries, tag);

        // Log a list of files tagged in the S3 bucket.
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Successfully tagged files in S3 bucket. "
                    + "s3BucketName=\"{}\" s3KeyPrefix=\"{}\" s3KeyCount={} s3ObjectTagKey=\"{}\" s3ObjectTagValue=\"{}\"",
                    s3FileTransferRequestParamsDto.getS3BucketName(),
                    s3FileTransferRequestParamsDto.getS3KeyPrefix(), CollectionUtils.size(s3ObjectSummaries),
                    tag.getKey(), tag.getValue());

            for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
                LOGGER.info("s3Key=\"{}\"", s3ObjectSummary.getKey());
            }
        }
    }
}