Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4097807

ResourceBundle.getResource catches Exception but not SecurityException

XMLWordPrintable



      Name: joT67522 Date: 12/08/97


      Under Netscape, call
      ResourceBundle.getBundle("myProperties", Locale.getDefault());

      where the server has a file called
         myProperties.class
      but no file called
         myProperties_EN_us.class or myProperties_EN.class

      After failing the loadClass, ResourceBundle tries to
      getResourceAsStream(), which causes a SecurityException
      under Netscape (because it tries to do a local disk
      access). However, because ResourceBundle catches
      Exception (instead of Throwable), failure to load
      myProperties_EN_us bubbles up to the application without
      looking for either myProperties_EN or myProperties.

      //---------Base.java-----------
      import java.util.ResourceBundle;
      import java.util.MissingResourceException;
      public class Base {
         public Base() {
            ResourceBundle bundle = null;
            try {
               bundle = ResourceBundle.getBundle("MyResource");
            }
            catch(MissingResourceException e) {
               System.out.println("Application got exception!");
               e.printStackTrace();
            }
            finally {
               System.out.println("****** LOADED Bundle=" + bundle);
            }
         }
      }



      //-----------MyResource.java---------
      public class MyResource extends java.util.ListResourceBundle {
         public Object[][]getContents() {
            return CONTENTS;
         }
         static private final Object [][] CONTENTS = {
            { "Hello", "hello" },
            { "World", "world" }
         };
      }


      //---------Test.java-------------
      import java.io.*;
      import java.util.Vector;
      public class Test
      {
         public static void main(String [] args)
         {
            ClassLoader loader = new MyClassLoader();
            try {
               Class baseClass = loader.loadClass("Base");
               baseClass.newInstance();
            }
            catch(Exception e) {
               e.printStackTrace();
               return;
            }
            System.out.println("Done!");
        }
      }
      class MyClassLoader extends ClassLoader {
         protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
      System.out.println("loadClass:" + name);
            try {
               InputStream is = new FileInputStream(name + ".class");
               byte [] array = new byte[10000];
               int len = is.read(array);
               return defineClass(name, array, 0, len);
            }
            catch(FileNotFoundException fne) {
               return findSystemClass( name );
            }
            catch(Exception e) {
               e.printStackTrace();
               return findSystemClass( name );
            }
         }
          public InputStream getResourceAsStream(String name) {
      System.out.println("getResourceAsStream:"+name);
      /***********************************************************************
       * BUG: The security manager doesn't allow property files to be loaded
       * This exception gets bubbled up through the
       * ResourceBundle.getBundle() method, so it cannot try looking for
       * the next MyResource*.class file based on the locale
       * securityManager.checkAccess();
       ***********************************************************************/

              throw new SecurityException();

      /***********************************************************************
       * BUG: It works just fine if we actually allow access to .properties
       * files and try loading them, as below
       ***********************************************************************/
      // return ClassLoader.getSystemResourceAsStream(name);
          }
          public java.net.URL getResource(String name) {
      System.out.println("getResource:"+name);
              return ClassLoader.getSystemResource(name);
          }
      }



      (Review ID: 21439)
      ======================================================================

            rgillamsunw Richard Gillam (Inactive)
            johsunw Joon Oh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: