Java Properties Load loadProperties(String resourceName, ClassLoader classLoader)

Here you can find the source of loadProperties(String resourceName, ClassLoader classLoader)

Description

Load all properties from the specified class path resource (in ISO-8859-1 encoding), using the given class loader.

License

Apache License

Parameter

Parameter Description
resourceName the name of the class path resource
classLoader the class loader

Exception

Parameter Description
IOException if loading failed

Return

the Properties instance

Declaration

public static synchronized Properties loadProperties(String resourceName, ClassLoader classLoader)
        throws IOException 

Method Source Code

//package com.java2s;
/**/*from   ww  w .  j  a  va2 s . c om*/
 * Copyright 2008-2016 Juho Jeong
 *
 * 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.
 */

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;

public class Main {
    private static final String XML_FILE_EXTENSION = ".xml";
    /**
     * Indicates whether properties should be cached for improved performance.
     * <p>
     * Note that when this class is deployed via a shared classloader in
     * a container, this will affect all webapps. However making this
     * configurable per webapp would mean having a map keyed by context classloader
     * which may introduce memory-leak problems.
     */
    private static volatile boolean cacheEnabled = true;
    /**
     * Stores a cache of Properties in a WeakHashMap.
     * <p>
     * The keys into this map only ever exist as temporary variables within
     * methods of this class, and are never exposed to users of this class.
     * This means that the WeakHashMap is used only as a mechanism for
     * limiting the size of the cache, ie a way to tell the garbage collector
     * that the contents of the cache can be completely garbage-collected
     * whenever it needs the memory. Whether this is a good approach to
     * this problem is doubtful; something like the commons-collections
     * LRUMap may be more appropriate (though of course selecting an
     * appropriate size is an issue).
     */
    private static final Map<String, Reference<Properties>> cache = Collections
            .synchronizedMap(new WeakHashMap<>());

    /**
     * Load all properties from the specified class path resource
    * (in ISO-8859-1 encoding), using the given class loader.
    * <p>Merges properties if more than one resource of the same name
    * found in the class path.
     *
     * @param resourceName the name of the class path resource
     * @param classLoader the class loader
     * @return the Properties instance
     * @throws IOException if loading failed
     */
    public static synchronized Properties loadProperties(String resourceName, ClassLoader classLoader)
            throws IOException {
        Properties props = getCachedProperties(resourceName);
        if (props != null)
            return props;

        Enumeration<URL> urls = classLoader.getResources(resourceName);
        props = new Properties();

        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            URLConnection con = url.openConnection();
            InputStream is = con.getInputStream();
            try {
                if (resourceName.endsWith(XML_FILE_EXTENSION)) {
                    props.loadFromXML(is);
                } else {
                    props.load(is);
                }
            } finally {
                is.close();
            }
        }

        cacheMethod(resourceName, props);

        return props;
    }

    /**
     * Return the Properties from the cache, if present.
     *
     * @param resourceName The resource name
     * @return The cached Properties
     */
    private static Properties getCachedProperties(String resourceName) {
        if (cacheEnabled) {
            Reference<Properties> ref = cache.get(resourceName);
            if (ref != null) {
                return ref.get();
            }
        }
        return null;
    }

    /**
     * Add a Properties to the cache.
     *
     * @param resourceName The resource name
     * @param props The Properties to cache
     */
    private static void cacheMethod(String resourceName, Properties props) {
        if (cacheEnabled) {
            if (props != null) {
                cache.put(resourceName, new WeakReference<>(props));
            }
        }
    }
}

Related

  1. loadProperties(String properties)
  2. loadProperties(String properties)
  3. loadProperties(String propertyName)
  4. loadProperties(String resource)
  5. loadProperties(String resourceName)
  6. loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)
  7. loadPropertiesFromResource(String name)
  8. loadPropertiesFromResources(Class cls, String[] resources)
  9. loadPropertiesFromString(String propertiesString)