private static Map loadAddressMap()

in geronimo-mail_2.1_spec/src/main/java/jakarta/mail/Session.java [740:846]


    private static Map loadAddressMap(final ClassLoader cl) {
        // we create a merged map from reading all of the potential address map entries.  The locations
        // searched are:
        //   0. java.home/conf/javamail.address.map
        //   1. java.home/lib/javamail.address.map
        //   2. META-INF/javamail.address.map
        //   3. META-INF/javamail.default.address.map
        // 
        // JDK 1.9 adds a new <java.home>/conf directory to hold configuration
        // files that were previously stored in <java.home>/lib.  When using
        // JavaMail on JDK 1.9, it should look for its (optional) configuration
        // files in the <java.home>/conf directory.
        //
        // if all of the above searches fail, we just set up some "default" defaults.

        // the format of the address.map file is defined as a property file.  We can cheat and
        // just use Properties.load() to read in the files.
        final Properties addressMap = new Properties();

        // add this to the tracking map.
        addressMapsByClassLoader.put(cl, addressMap);

        // NOTE:  We are reading these resources in reverse order of what's cited above.  This allows
        // user defined entries to overwrite default entries if there are similarly named items.

        try {
            final Enumeration e = cl.getResources("META-INF/javamail.default.address.map");
            while (e.hasMoreElements()) {
                final URL url = (URL) e.nextElement();
                final InputStream is = url.openStream();
                try {
                    // load as a property file
                    addressMap.load(is);
                } finally{
                    is.close();
                }
            }
        } catch (final SecurityException e) {
            // ignore
        } catch (final IOException e) {
            // ignore
        }


        try {
            final Enumeration e = cl.getResources("META-INF/javamail.address.map");
            while (e.hasMoreElements()) {
                final URL url = (URL) e.nextElement();
                final InputStream is = url.openStream();
                try {
                    // load as a property file
                    addressMap.load(is);
                } finally{
                    is.close();
                }
            }
        } catch (final SecurityException e) {
            // ignore
        } catch (final IOException e) {
            // ignore
        }


        try {
            File file = new File(System.getProperty("java.home"), "conf/javamail.address.map");
            if (!file.exists()) {
                file = new File(System.getProperty("java.home"), "lib/javamail.address.map");
            }
            final InputStream is = new FileInputStream(file);
            try {
                // load as a property file
                addressMap.load(is);
            } finally{
                is.close();
            }
        } catch (final SecurityException e) {
            // ignore
        } catch (final IOException e) {
            // ignore
        }

        try {
            final Enumeration e = cl.getResources("META-INF/javamail.address.map");
            while (e.hasMoreElements()) {
                final URL url = (URL) e.nextElement();
                final InputStream is = url.openStream();
                try {
                    // load as a property file
                    addressMap.load(is);
                } finally{
                    is.close();
                }
            }
        } catch (final SecurityException e) {
            // ignore
        } catch (final IOException e) {
            // ignore
        }


        // if unable to load anything, at least create the MimeMessage-smtp protocol mapping.
        if (addressMap.isEmpty()) {
            addressMap.put("rfc822", "smtp");
        }

        return addressMap;
    }