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

software.amazon.jdbc.util.PropertyUtils Maven / Gradle / Ivy

There is a newer version: 2.5.2
Show newest version
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package software.amazon.jdbc.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import software.amazon.awssdk.services.rds.endpoints.internal.Value.Bool;
import software.amazon.jdbc.AwsWrapperProperty;
import software.amazon.jdbc.PropertyDefinition;

public class PropertyUtils {
  private static final Logger LOGGER = Logger.getLogger(PropertyUtils.class.getName());
  private static final Set SECRET_PROPERTIES = Collections.unmodifiableSet(
      new HashSet<>(Collections.singletonList(PropertyDefinition.PASSWORD.name))
  );

  public static void applyProperties(final Object target, final Properties properties) {
    if (target == null || properties == null) {
      return;
    }

    final List methods = Arrays.asList(target.getClass().getMethods());
    final Enumeration propertyNames = properties.propertyNames();
    while (propertyNames.hasMoreElements()) {
      final Object key = propertyNames.nextElement();
      final String propName = key.toString();
      Object propValue = properties.getProperty(propName);
      if (propValue == null) {
        propValue = properties.get(key);
      }

      setPropertyOnTarget(target, propName, propValue, methods);
    }
  }

  public static void setPropertyOnTarget(
      final Object target,
      final String propName,
      final Object propValue,
      final List methods) {
    Method writeMethod = null;
    String methodName = "set" + propName.substring(0, 1).toUpperCase() + propName.substring(1);

    for (final Method method : methods) {
      if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
        writeMethod = method;
        break;
      }
    }

    if (writeMethod == null) {
      methodName = "set" + propName.toUpperCase();
      for (final Method method : methods) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
          writeMethod = method;
          break;
        }
      }
    }

    if (writeMethod == null) {
      LOGGER.finest(
          () ->
              Messages.get(
                  "PropertyUtils.setMethodDoesNotExistOnTarget",
                  new Object[] {propName, target.getClass()}));
      return;
    }

    try {
      final Class paramClass = writeMethod.getParameterTypes()[0];
      if (paramClass == String.class) {
        writeMethod.invoke(target, propValue.toString());
      } else if (paramClass == int.class) {
        writeMethod.invoke(target, Integer.parseInt(propValue.toString()));
      } else if (paramClass == long.class) {
        writeMethod.invoke(target, Long.parseLong(propValue.toString()));
      } else if (paramClass == boolean.class || paramClass == Boolean.class) {
        writeMethod.invoke(target, Boolean.parseBoolean(propValue.toString()));
      } else {
        writeMethod.invoke(target, propValue);
      }
      Object cleanPropValue = isSecretProperty(propName) ? "***" : propValue;
      LOGGER.finest(() -> String.format("Set property '%s' with value: %s", propName, cleanPropValue));

    } catch (final InvocationTargetException ex) {
      LOGGER.warning(
          () ->
              Messages.get(
                  "PropertyUtils.failedToSetPropertyWithReason",
                  new Object[] {propName, target.getClass(), ex.getCause().getMessage()}));
      throw new RuntimeException(ex.getCause());
    } catch (final Exception e) {
      LOGGER.warning(
          () ->
              Messages.get(
                  "PropertyUtils.failedToSetProperty", new Object[] {propName, target.getClass()}));
      throw new RuntimeException(e);
    }
  }

  public static @NonNull Properties copyProperties(final Properties props) {
    final Properties copy = new Properties();

    if (props == null) {
      return copy;
    }

    return addProperties(copy, props);
  }

  public static @NonNull Properties addProperties(
      final Properties dest, final Properties propsToAdd) {

    if (propsToAdd == null) {
      return dest;
    }

    for (final Map.Entry entry : propsToAdd.entrySet()) {
      dest.setProperty(entry.getKey().toString(), entry.getValue().toString());
    }
    return dest;
  }

  private static boolean isSecretProperty(final Object propertyKey) {
    return SECRET_PROPERTIES.contains(propertyKey);
  }

  public static @NonNull Properties maskProperties(final Properties props) {
    final Properties maskedProperties = copyProperties(props);
    if (maskedProperties.containsKey(PropertyDefinition.PASSWORD.name)) {
      maskedProperties.setProperty(PropertyDefinition.PASSWORD.name, "***");
    }
    return maskedProperties;
  }

  public static String logProperties(final Properties props, final String caption) {
    final StringBuilder sb = new StringBuilder();
    for (final Object key : props.keySet()) {
      if (sb.length() > 0) {
        sb.append("\n");
      }
      sb.append("[").append(key.toString()).append("] ").append(props.get(key).toString());
    }
    if (sb.length() == 0) {
      sb.append("");
    }
    if (caption != null) {
      sb.insert(0, caption);
    }
    return sb.toString();
  }

  public static Integer getIntegerPropertyValue(
      final @NonNull Properties props,
      final @NonNull AwsWrapperProperty wrapperProperty) {

    Integer result = null;
    if (!StringUtils.isNullOrEmpty(wrapperProperty.getString(props))) {
      result = wrapperProperty.getInteger(props);
    }
    return result;
  }

  public static Boolean getBooleanPropertyValue(
      final @NonNull Properties props,
      final @NonNull AwsWrapperProperty wrapperProperty) {

    Boolean result = null;
    if (!StringUtils.isNullOrEmpty(wrapperProperty.getString(props))) {
      result = wrapperProperty.getBoolean(props);
    }
    return result;
  }
}