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

com.vmware.ovsdb.util.PropertyManager Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018 VMware, Inc. All Rights Reserved.
 *
 * This product is licensed to you under the BSD-2 license (the "License").
 * You may not use this product except in compliance with the BSD-2 License.
 *
 * This product may include a number of subcomponents with separate copyright
 * notices and license terms. Your use of these subcomponents is subject to the
 * terms and conditions of the subcomponent's license, as noted in the LICENSE
 * file.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

package com.vmware.ovsdb.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.util.Properties;
import java.util.function.Function;

public class PropertyManager {

  private static final Logger LOGGER = LoggerFactory.getLogger(
      MethodHandles.lookup().lookupClass());

  private static final Properties properties = new Properties();

  private static final String PROPERTY_FILE_NAME = "ovsdb-client.properties";

  static {
    ClassLoader classLoader = PropertyManager.class.getClassLoader();
    if (classLoader != null) {
      InputStream is = classLoader.getResourceAsStream(PROPERTY_FILE_NAME);
      try {
        properties.load(is);
      } catch (IOException ex) {
        LOGGER.error("Failed to load properties", ex);
      } finally {
        try {
          is.close();
        } catch (IOException ex) {
          LOGGER.error("Failed to close {}", PROPERTY_FILE_NAME);
        }
      }
    }
  }

  private static  T getProperty(String propertyName, T defaultValue,
      Function converter) {
    String value = properties.getProperty(propertyName);
    try {
      return converter.apply(value);
    } catch (Throwable ex) {
      LOGGER.error("Failed to get property: " + propertyName, ex);
    }
    return defaultValue;
  }

  public static int getIntProperty(String propertyName, int defaultValue) {
    return getProperty(propertyName, defaultValue, Integer::valueOf);
  }

  public static long getLongProperty(String propertyName, long defaultValue) {
    return getProperty(propertyName, defaultValue, Long::valueOf);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy