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

com.covisint.platform.oauth.client.token.sdk.AbstractClasspathPropertyLoader Maven / Gradle / Ivy

/* 
 * Copyright 2015 Covisint
 * 
 * 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 com.covisint.platform.oauth.client.token.sdk;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/** Abstract property loader. */
abstract class AbstractClasspathPropertyLoader {

    /** The loaded properties. */
    private final Properties properties;

    /** Initializing constructor. */
    public AbstractClasspathPropertyLoader() {

        final String filename = getPropertyFileName();

        InputStream input = getResource(filename);

        if (input == null) {
            // Retry previous attempts, but this time, from the root of the classpath.
            input = getResource("/" + filename);
        }

        if (input == null) {
            throw new IllegalStateException("Could not locate resource in classpath: " + filename);
        }

        properties = new Properties();

        try {
            properties.load(input);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Attempt to read an inputstream into the given resource.
     * 
     * @param resource The resource to locate.
     * @return Returns an inputstream into the given resource, or null if the resource couldn't be located.
     */
    private InputStream getResource(String resource) {

        // First try and load it from the current thread's classloaded.
        InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);

        if (input == null) {
            // Next, attempt to get it from the classloader of this static class.
            input = AbstractClasspathPropertyLoader.class.getResourceAsStream(resource);
        }

        if (input == null) {
            // Finally, try the classloader of the runtime instance of this class.
            input = getClass().getResourceAsStream(resource);
        }

        return input;
    }

    /**
     * Returns the name of the property file to load. Must exist in the classpath.
     * 
     * @return Returns the name of the property file to load.
     */
    protected abstract String getPropertyFileName();

    /**
     * Gets the properties loaded by this loader.
     * 
     * @return Returns the loaded properties.
     */
    protected final Properties getProperties() {
        return properties;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy