Java Properties Load from File loadProvidersConfiguration(Class base, String confPropertiesName)

Here you can find the source of loadProvidersConfiguration(Class base, String confPropertiesName)

Description

Build the provider map for the specified base class.

License

LGPL

Parameter

Parameter Description
base Base class type of the provider
confPropertiesName Name of the properties file holding the providers configuration

Return

Providers configuration

Declaration

public static <E> Map<String, Class<? extends E>> loadProvidersConfiguration(Class<E> base,
        String confPropertiesName) 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

import java.util.HashMap;
import java.util.Map;

import java.util.Properties;

public class Main {
    /**/*from   w  w  w  . ja  v  a 2s .  co  m*/
     * Build the provider map for the specified base class. <br>
     * First tries to read the built-in configuration located in the same package of the base
     * class. Then tries to load external configuration located in the root of the classpath. <br>
     * <br>
     * The properties file must respect the following format: <br>
     * <code>provider-name=fully.qualified.provider.class</code>
     *
     * @param base Base class type of the provider
     * @param confPropertiesName Name of the properties file holding the providers configuration
     * @return Providers configuration
     */
    public static <E> Map<String, Class<? extends E>> loadProvidersConfiguration(Class<E> base,
            String confPropertiesName) {
        HashMap<String, Class<? extends E>> providers;
        providers = new HashMap<String, Class<? extends E>>();
        InputStream in;

        /* Try to read built-in provider configuration */
        in = base.getResourceAsStream(confPropertiesName);
        if (in != null) {
            providers.putAll(readProvidersConfiguration(base, in, "built-in"));
        } else
            System.err.format("Built-in configuration not found for %s\n", base.getName());

        /* Try to read external provider configuration */
        in = base.getResourceAsStream("/" + confPropertiesName);
        if (in != null) {
            providers.putAll(readProvidersConfiguration(base, in, "external"));
        }

        return providers;
    }

    /**
     * Read providers configuration from input stream
     *
     * @param base Name of the base class
     * @param in InputStream pointing to a providers configuration properties file
     * @param resource Name on the resource to read
     * @return Providers configuration
     */
    private static <E> HashMap<String, Class<? extends E>> readProvidersConfiguration(Class<E> base, InputStream in,
            String resource) {
        HashMap<String, Class<? extends E>> providers = new HashMap<String, Class<? extends E>>();
        Properties conf = new Properties();
        try {
            conf.load(in);
        } catch (Exception e) {
            System.err.format("Error while reading configuration for %s (%s)\n", base.getName(), resource);
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
            }
        }

        for (Map.Entry entry : conf.entrySet()) {
            String key = (String) entry.getKey();
            String className = (String) entry.getValue();

            try {
                Class<? extends E> clazz = (Class<? extends E>) Class.forName(className);
                providers.put(key, clazz);
            } catch (ClassNotFoundException e) {
                System.err.format("Provider implementation not found for %s (%s): %s\n", base.getName(), resource,
                        className);
            } catch (ClassCastException e) {
                System.err.format("Invalid provider implementation for %s (%s): %s\n", base.getName(), resource,
                        className);
            }
        }

        return providers;
    }
}

Related

  1. loadPropertiesFromFile(String filePath)
  2. loadPropertiesFromFile(String fullFileName)
  3. loadPropertiesFromFile(String pathname)
  4. loadPropertiesFromPath(String propsFilePath)
  5. loadPropertiesInClassPath(String propertiesFileName)
  6. loadPythonScript(String pythonScriptName)
  7. loadReader(Reader stream)
  8. loadRecursively(String propertiesFilename)
  9. loadRQGProperties()