Java URL Load getBundle(String basename, Locale locale, ClassLoader cl, URL url)

Here you can find the source of getBundle(String basename, Locale locale, ClassLoader cl, URL url)

Description

Get a resource bundle via the network class loader given the resource name and the locale.

License

Open Source License

Parameter

Parameter Description
basename a parameter
locale a parameter
cl a parameter
url url

Exception

Parameter Description
MissingResourceException if bundle cannot be found for any reason

Return

resource bundle

Declaration

public static ResourceBundle getBundle(String basename, Locale locale, ClassLoader cl, URL url)
        throws MissingResourceException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.net.URL;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class Main {
    /**//  ww w. j a  v a 2 s  .  c  o m
     * Get a resource bundle via the network class loader given the resource
     * name and the locale. This is required for 1.1 compatibility.
     * 
     * @param basename
     * @param locale
     * @param cl
     * @param url url
     * @return resource bundle
     * @throws MissingResourceException if bundle cannot be found for any reason
     */
    public static ResourceBundle getBundle(String basename, Locale locale, ClassLoader cl, URL url)
            throws MissingResourceException {

        ResourceBundle resourceBundle = null;

        // ResourceBundle by default looks for loads of different ways of providing the 
        // resource bundle. We do not want it to go to the server unless a different locale is
        // specified so must provide our own locating and loading

        // Create the resource name
        String resource = basename.replace('.', '/') + ".properties";
        System.out.println("Looking for '" + resource + "' in locale " + locale + " (display name = "
                + locale.getDisplayName() + "'");

        // If the language is not specified and not "en" or "en_GB" then load from the network 
        if (locale != null && !"en".equalsIgnoreCase(locale.toString())
                && !"en_GB".equalsIgnoreCase(locale.toString())) {
            try {
                URL resourceUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(),
                        url.getFile() + resource);
                System.out.println("Non GB resource, so trying server '" + resourceUrl.toExternalForm() + "'");
                InputStream in = resourceUrl.openStream();
                try {
                    resourceBundle = new PropertyResourceBundle(in);
                } finally {
                    closeStream(in);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

        // If no resource bundle has yet been loaded, look for it in the class loader      
        if (resourceBundle == null && cl != null) {
            System.out.println("Must GB resource, so trying class load");
            InputStream in = cl.getResourceAsStream(resource);
            try {
                resourceBundle = new PropertyResourceBundle(in);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                closeStream(in);
            }
        }

        // Cannot find
        if (resourceBundle == null) {
            throw new MissingResourceException("No such bundle could be located for " + basename, basename, "");
        }

        return resourceBundle;
    }

    /**
     * 
     * 
     * @param in
     * 
     * @return
     */
    public static boolean closeStream(InputStream in) {
        try {
            if (in != null) {
                in.close();
            }

            return true;
        } catch (IOException ioe) {
            return false;
        }
    }

    /**
     * 
     * 
     * @param out
     * 
     * @return
     */
    public static boolean closeStream(OutputStream out) {
        try {
            if (out != null) {
                out.close();
            }

            return true;
        } catch (IOException ioe) {
            return false;
        }
    }
}

Related

  1. findResourcesFromJar(Collection urls, URL url, String resource, ClassLoader cl)
  2. getManifestAsString(URLClassLoader cl, String jarBaseName)
  3. load(Iterable urls)
  4. load(String fileOrURL)
  5. load(URL pUrl)