Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.lang.ref.SoftReference;
import java.util.HashMap;

public class Main {
    private static HashMap<String, SoftReference<ResourceBundle>> sRefMap = null;

    public synchronized static ResourceBundle loadBundle(String resource) {
        if (sRefMap == null) {
            sRefMap = new HashMap<String, SoftReference<ResourceBundle>>();
        }
        SoftReference<ResourceBundle> bundleRef = sRefMap.get(resource);
        if (bundleRef == null || bundleRef.get() == null) {
            // Attempt to load the messages.
            try {
                ResourceBundle bundle = setLocale(Locale.getDefault(), resource);
                bundleRef = new SoftReference<ResourceBundle>(bundle);
                sRefMap.put(resource, bundleRef);
                return bundle;
            } catch (Throwable e) {
                //Logger.global.warning("Got Throwable " + e + " loading messages");
                return null;
            }
        } else {
            return bundleRef.get();
        }
    }

    /**
     * Changes the locale of the messages.
     * 
     * @param locale
     *            Locale the locale to change to.
     * @param resource
     *            the name of the bundle resource
     */
    static public ResourceBundle setLocale(final Locale locale, final String resource) {
        try {
            // BEGIN android-removed
            // final ClassLoader loader = VM.bootCallerClassLoader();
            // END android-removed
            return (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    // BEGIN android-changed
                    return ResourceBundle.getBundle(resource, locale, ClassLoader.getSystemClassLoader());
                    // END android-changed
                }
            });
        } catch (MissingResourceException e) {
        }
        return null;
    }
}