package ameba.i18n;

import com.google.common.base.Charsets;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * @author icode
 */
public class MultiResourceBundleControl extends ResourceBundle.Control {

    boolean noCache = false;

    @Override
    public long getTimeToLive(String baseName, Locale locale) {
        return noCache ? ResourceBundle.Control.TTL_DONT_CACHE : super.getTimeToLive(baseName, locale);
    }

    @Override
    public ResourceBundle newBundle(
            String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
            throws IllegalAccessException, InstantiationException, IOException {
        String bundleName = toBundleName(baseName, locale);
        ResourceBundle bundle = null;
        switch (format) {
            case "java.class":
                try {
                    Class<? extends ResourceBundle> bundleClass
                            = (Class<? extends ResourceBundle>) loader.loadClass(bundleName);

                    // If the class isn't a ResourceBundle subclass, throw a
                    // ClassCastException.
                    if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
                        bundle = bundleClass.newInstance();
                    } else {
                        throw new ClassCastException(bundleClass.getName()
                                + " cannot be cast to ResourceBundle");
                    }
                } catch (ClassNotFoundException e) {
                    // no op
                }
                break;
            case "java.properties":
                final String resourceName = toResourceName(bundleName, "properties");
                final ClassLoader classLoader = loader;
                final boolean reloadFlag = reload;
                Properties properties;
                try {
                    properties = AccessController.doPrivileged(
                            new PrivilegedExceptionAction<Properties>() {
                                public Properties run() throws IOException {
                                    Properties properties = null;
                                    Enumeration<URL> urls = classLoader.getResources(resourceName);
                                    if (urls != null && urls.hasMoreElements()) {
                                        properties = new Properties();
                                        while (urls.hasMoreElements()) {
                                            URLConnection connection = urls.nextElement().openConnection();
                                            if (connection != null) {
                                                // Disable caches to get fresh data for
                                                // reloading.
                                                if (reloadFlag) {
                                                    connection.setUseCaches(false);
                                                }
                                                properties.load(
                                                        new InputStreamReader(
                                                                connection.getInputStream(),
                                                                Charsets.UTF_8)
                                                );
                                            }
                                        }
                                    }
                                    return properties;
                                }
                            });
                } catch (PrivilegedActionException e) {
                    throw (IOException) e.getException();
                }
                if (properties != null) {
                    bundle = new PropertiesResourceBundle(properties);
                }
                break;
            default:
                throw new IllegalArgumentException("unknown format: " + format);
        }
        return bundle;
    }
}
@@ -14,16 +14,43 @@
 .i18n;%0A%0A
+import ameba.util.IOUtils;%0A
 import c
@@ -102,32 +102,60 @@
 io.IOException;%0A
+import java.io.InputStream;%0A
 import java.io.I
@@ -2826,32 +2826,216 @@
 reElements()) %7B%0A
+                                            URL url = urls.nextElement();%0A                                            if (url.getPath().endsWith(%22/classes/%22 + resourceName)) continue;%0A
                 
@@ -3092,31 +3092,16 @@
 on = url
-s.nextElement()
 .openCon
@@ -3583,33 +3583,31 @@
     
-properties.load(%0A        
+InputStream in = null;%0A
     
@@ -3650,20 +3650,16 @@
         
-new 
 InputStr
@@ -3667,17 +3667,31 @@
 amReader
-(
+ reader = null;
 %0A       
@@ -3715,32 +3715,46 @@
                 
+        try %7B%0A
                 
@@ -3752,32 +3752,65 @@
                 
+                             in =
  connection.getI
@@ -3825,14 +3825,158 @@
 am()
-,
+;
 %0A    
+                                                reader = new InputStreamReader(%0A                                                            in,%0A
     
@@ -4046,16 +4046,17 @@
 s.UTF_8)
+;
 %0A       
@@ -4096,18 +4096,314 @@
         
-);
+    properties.load(reader);%0A                                                %7D finally %7B%0A                                                    IOUtils.closeQuietly(reader);%0A                                                    IOUtils.closeQuietly(in);%0A                                                %7D
 %0A       

排除 classes 下的 i18n 配置