Search in sources :

Example 1 with DetectInfo

use of com.blackducksoftware.integration.hub.detect.DetectInfo in project hub-detect by blackducksoftware.

the class DetectConfigurationPrinter method print.

public void print(final PrintStream printStream, final DetectInfo detectInfo, final DetectConfiguration detectConfiguration, final List<DetectOption> detectOptions) throws IllegalArgumentException, IllegalAccessException {
    printStream.println("");
    printStream.println("Current property values:");
    printStream.println("--property = value [notes]");
    printStream.println(StringUtils.repeat("-", 60));
    List<Field> annotatedProperties = new ArrayList<>();
    final Field[] propertyFields = DetectConfiguration.class.getDeclaredFields();
    for (final Field propertyField : propertyFields) {
        final Optional<Annotation> foundField = Arrays.stream(propertyField.getAnnotations()).filter(annotation -> annotation.annotationType() == Value.class).findFirst();
        final int modifiers = propertyField.getModifiers();
        if (foundField.isPresent() && !Modifier.isStatic(modifiers) && Modifier.isPrivate(modifiers)) {
            annotatedProperties.add(propertyField);
        }
    }
    annotatedProperties = annotatedProperties.stream().sorted(new Comparator<Field>() {

        @Override
        public int compare(final Field field1, final Field field2) {
            return field1.getName().compareTo(field2.getName());
        }
    }).collect(Collectors.toList());
    for (final Field field : annotatedProperties) {
        field.setAccessible(true);
        final String fieldName = field.getName();
        Object rawFieldValue;
        rawFieldValue = field.get(detectConfiguration);
        String fieldValue = "";
        if (field.getType().isArray()) {
            fieldValue = String.join(", ", (String[]) rawFieldValue);
        } else {
            if (rawFieldValue != null) {
                fieldValue = rawFieldValue.toString();
            }
        }
        if (!StringUtils.isEmpty(fieldName) && !StringUtils.isEmpty(fieldValue) && "metaClass" != fieldName) {
            final boolean containsPassword = fieldName.toLowerCase().contains("password") || fieldName.toLowerCase().contains("apitoken");
            if (containsPassword) {
                fieldValue = StringUtils.repeat("*", fieldValue.length());
            }
            DetectOption option = null;
            for (final DetectOption opt : detectOptions) {
                if (opt.getFieldName().equals(fieldName)) {
                    option = opt;
                }
            }
            if (option != null && !option.getResolvedValue().equals(fieldValue) && !containsPassword) {
                if (option.interactiveValue != null) {
                    printStream.println(fieldName + " = " + fieldValue + " [interactive]");
                } else if (option.getResolvedValue().equals("latest")) {
                    printStream.println(fieldName + " = " + fieldValue + " [latest]");
                } else if (option.getResolvedValue().trim().length() == 0) {
                    printStream.println(fieldName + " = " + fieldValue + " [calculated]");
                } else {
                    printStream.println(fieldName + " = " + fieldValue + " + [" + option.getResolvedValue() + "]");
                }
            } else {
                printStream.println(fieldName + " = " + fieldValue);
            }
        }
        field.setAccessible(false);
    }
    printStream.println(StringUtils.repeat("-", 60));
    printStream.println("");
}
Also used : PrintStream(java.io.PrintStream) Arrays(java.util.Arrays) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Comparator(java.util.Comparator) DetectConfiguration(com.blackducksoftware.integration.hub.detect.DetectConfiguration) DetectInfo(com.blackducksoftware.integration.hub.detect.DetectInfo) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) DetectOption(com.blackducksoftware.integration.hub.detect.help.DetectOption) Field(java.lang.reflect.Field)

Aggregations

DetectConfiguration (com.blackducksoftware.integration.hub.detect.DetectConfiguration)1 DetectInfo (com.blackducksoftware.integration.hub.detect.DetectInfo)1 DetectOption (com.blackducksoftware.integration.hub.detect.help.DetectOption)1 PrintStream (java.io.PrintStream)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Value (org.springframework.beans.factory.annotation.Value)1