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

org.kuali.maven.plugins.ingester.PropertiesUtils Maven / Gradle / Ivy

package org.kuali.maven.plugins.ingester;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class PropertiesUtils {

    public static void storeProperties(Properties props, File file) throws IOException {
        OutputStream out = null;
        try {
            FileUtils.touch(file);
            out = new FileOutputStream(file);
            props.store(out, "Maven Ingester Plugin");
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    public static void loadProperties(Properties props, InputStream in) throws IOException {
        try {
            props.load(in);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    public static void loadProperties(Properties props, String location) throws IOException {
        InputStream in = null;
        try {
            in = getInputStream(location);
            loadProperties(props, in);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    /**
     * Open an InputStream to the location indicated. If "location" is a File, a FileInputStream is returned, otherwise
     * the InputStream generated by Spring's resource loading logic is returned.
     */
    public static InputStream getInputStream(String location) throws IOException {
        File file = new File(location);
        if (file.exists()) {
            return new FileInputStream(file);
        }
        ResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource(location);
        return resource.getInputStream();
    }

    /**
     * Return true if the location provided to this method represents a file or a location Spring resource loading can
     * find, false otherwise.
     */
    public static boolean exists(String location) {
        if (StringUtils.isBlank(location)) {
            return false;
        }
        File file = new File(location);
        if (file.exists()) {
            return true;
        }
        ResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource(location);
        return resource.exists();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy