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

com.adobe.aio.util.FileUtil Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2017 Adobe. All rights reserved.
 * This file is licensed to you 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 REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.adobe.aio.util;

import com.adobe.aio.exception.AIOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;

public class FileUtil {

  private FileUtil() {
  }

  public static Map getMapFromProperties(final Properties properties) {
    Map map = new HashMap<>();
    for (final String name : properties.stringPropertyNames()) {
      map.put(name, properties.getProperty(name));
    }
    return map;
  }

  public static Optional readPropertiesFromFile(final String configFilePath) {
    if (StringUtils.isEmpty(configFilePath)) {
      return Optional.empty();
    } else {
      try (InputStream in = new FileInputStream(configFilePath)) {
        return Optional.of(read(in));
      } catch (FileNotFoundException e) {
        return Optional.empty();
      } catch (IOException e) {
        throw new AIOException("Unable to load your Properties from File " + configFilePath, e);
      }
    }
  }

  public static Properties readPropertiesFromClassPath(final String configClassPath) {
    try (InputStream in = FileUtil.class.getClassLoader().getResourceAsStream(configClassPath)) {
      return read(in);
    } catch (IOException e) {
      throw new AIOException("Unable to load your Properties from class path " + configClassPath, e);
    }
  }

  private static Properties read(InputStream in) throws IOException {
    Properties prop = new Properties();
    prop.load(in);
    in.close();
    return prop;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy