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

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

/**
 * Copyright 2011-2012 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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 org.kuali.maven.plugins.ingester;

import java.io.File;
import java.io.FileInputStream;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class PropertyUtils {
    private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);

    public static void store(Properties props, File file) throws IOException {
        OutputStream out = null;
        try {
            out = FileUtils.openOutputStream(file);
            props.store(out, "");
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    public static void load(Properties props, String location) throws IOException {
        InputStream in = null;
        try {
            in = getInputStream(location);
            logger.info("Loading " + location);
            props.load(in);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    /**
     * Get an InputStream from 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 is an existing file on the file system
     * 
     * @param location
     * @return
     */
    public static boolean isExistingFile(String location) {
        File file = new File(location);
        return file.exists();
    }

    /**
     * Return true if the location is a resource Spring can find
     * 
     * @param location
     * @return
     */
    public static boolean isExistingResource(String location) {
        ResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource(location);
        return resource.exists();
    }

    /**
     * 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;
        } else {
            return isExistingFile(location) || isExistingResource(location);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy