📄 session.java
字号:
} /** * Returns the Properties object associated with this Session * * @return Properties object */ public Properties getProperties() { return props; } /** * Returns the value of the specified property. Returns null * if this property does not exist. * * @return String that is the property value */ public String getProperty(String name) { return props.getProperty(name); } /** * Load the protocol providers config files. */ private void loadProviders(Class cl) { StreamLoader loader = new StreamLoader() { public void load(InputStream is) throws IOException { loadProvidersFromStream(is); } }; // load system-wide javamail.providers from the <java.home>/lib dir try { String res = System.getProperty("java.home") + File.separator + "lib" + File.separator + "javamail.providers"; loadFile(res, loader); } catch (SecurityException sex) { if (debug) pr("DEBUG: can't get java.home: " + sex); } // load the META-INF/javamail.providers file supplied by an application loadAllResources("META-INF/javamail.providers", cl, loader); // load default META-INF/javamail.default.providers from mail.jar file loadResource("/META-INF/javamail.default.providers", cl, loader); if (providers.size() == 0) { if (debug) pr("DEBUG: failed to load any providers, using defaults"); // failed to load any providers, initialize with our defaults addProvider(new Provider(Provider.Type.STORE, "imap", "com.sun.mail.imap.IMAPStore", "Sun Microsystems, Inc.", Version.version)); addProvider(new Provider(Provider.Type.STORE, "imaps", "com.sun.mail.imap.IMAPSSLStore", "Sun Microsystems, Inc.", Version.version)); addProvider(new Provider(Provider.Type.STORE, "pop3", "com.sun.mail.pop3.POP3Store", "Sun Microsystems, Inc.", Version.version)); addProvider(new Provider(Provider.Type.STORE, "pop3s", "com.sun.mail.pop3.POP3SSLStore", "Sun Microsystems, Inc.", Version.version)); addProvider(new Provider(Provider.Type.TRANSPORT, "smtp", "com.sun.mail.smtp.SMTPTransport", "Sun Microsystems, Inc.", Version.version)); addProvider(new Provider(Provider.Type.TRANSPORT, "smtps", "com.sun.mail.smtp.SMTPSSLTransport", "Sun Microsystems, Inc.", Version.version)); } if (debug) { // dump the output of the tables for debugging pr("DEBUG: Tables of loaded providers"); pr("DEBUG: Providers Listed By Class Name: " + providersByClassName.toString()); pr("DEBUG: Providers Listed By Protocol: " + providersByProtocol.toString()); } } private void loadProvidersFromStream(InputStream is) throws IOException { if (is != null) { LineInputStream lis = new LineInputStream(is); String currLine; // load and process one line at a time using LineInputStream while ((currLine = lis.readLine()) != null) { if (currLine.startsWith("#")) continue; Provider.Type type = null; String protocol = null, className = null; String vendor = null, version = null; // separate line into key-value tuples StringTokenizer tuples = new StringTokenizer(currLine,";"); while (tuples.hasMoreTokens()) { String currTuple = tuples.nextToken().trim(); // set the value of each attribute based on its key int sep = currTuple.indexOf("="); if (currTuple.startsWith("protocol=")) { protocol = currTuple.substring(sep+1); } else if (currTuple.startsWith("type=")) { String strType = currTuple.substring(sep+1); if (strType.equalsIgnoreCase("store")) { type = Provider.Type.STORE; } else if (strType.equalsIgnoreCase("transport")) { type = Provider.Type.TRANSPORT; } } else if (currTuple.startsWith("class=")) { className = currTuple.substring(sep+1); } else if (currTuple.startsWith("vendor=")) { vendor = currTuple.substring(sep+1); } else if (currTuple.startsWith("version=")) { version = currTuple.substring(sep+1); } } // check if a valid Provider; else, continue if (type == null || protocol == null || className == null || protocol.length() <= 0 || className.length() <= 0) { if (debug) pr("DEBUG: Bad provider entry: " + currLine); continue; } Provider provider = new Provider(type, protocol, className, vendor, version); // add the newly-created Provider to the lookup tables addProvider(provider); } } } /** * Add a provider to the session. * * @param provider the provider to add * @since JavaMail 1.4 */ public synchronized void addProvider(Provider provider) { providers.addElement(provider); providersByClassName.put(provider.getClassName(), provider); if (!providersByProtocol.containsKey(provider.getProtocol())) providersByProtocol.put(provider.getProtocol(), provider); } // load maps in reverse order of preference so that the preferred // map is loaded last since its entries will override the previous ones private void loadAddressMap(Class cl) { StreamLoader loader = new StreamLoader() { public void load(InputStream is) throws IOException { addressMap.load(is); } }; // load default META-INF/javamail.default.address.map from mail.jar loadResource("/META-INF/javamail.default.address.map", cl, loader); // load the META-INF/javamail.address.map file supplied by an app loadAllResources("META-INF/javamail.address.map", cl, loader); // load system-wide javamail.address.map from the <java.home>/lib dir try { String res = System.getProperty("java.home") + File.separator + "lib" + File.separator + "javamail.address.map"; loadFile(res, loader); } catch (SecurityException sex) { if (debug) pr("DEBUG: can't get java.home: " + sex); } if (addressMap.isEmpty()) { if (debug) pr("DEBUG: failed to load address map, using defaults"); addressMap.put("rfc822", "smtp"); } } /** * Set the default transport protocol to use for addresses of * the specified type. Normally the default is set by the * <code>javamail.default.address.map</code> or * <code>javamail.address.map</code> files or resources. * * @param addresstype type of address * @param protocol name of protocol * @see #getTransport(Address) * @since JavaMail 1.4 */ public synchronized void setProtocolForAddress(String addresstype, String protocol) { if (protocol == null) addressMap.remove(addresstype); else addressMap.put(addresstype, protocol); } /** * Load from the named file. */ private void loadFile(String name, StreamLoader loader) { InputStream clis = null; try { clis = new BufferedInputStream(new FileInputStream(name)); loader.load(clis); if (debug) pr("DEBUG: successfully loaded file: " + name); } catch (IOException e) { if (debug) { pr("DEBUG: not loading file: " + name); pr("DEBUG: " + e); } } catch (SecurityException sex) { if (debug) { pr("DEBUG: not loading file: " + name); pr("DEBUG: " + sex); } } finally { try { if (clis != null) clis.close(); } catch (IOException ex) { } // ignore it } } /** * Load from the named resource. */ private void loadResource(String name, Class cl, StreamLoader loader) { InputStream clis = null; try { clis = getResourceAsStream(cl, name); if (clis != null) { loader.load(clis); if (debug) pr("DEBUG: successfully loaded resource: " + name); } else { if (debug) pr("DEBUG: not loading resource: " + name); } } catch (IOException e) { if (debug) pr("DEBUG: " + e); } catch (SecurityException sex) { if (debug) pr("DEBUG: " + sex); } finally { try { if (clis != null) clis.close(); } catch (IOException ex) { } // ignore it } } /** * Load all of the named resource. */ private void loadAllResources(String name, Class cl, StreamLoader loader) { boolean anyLoaded = false; try { URL[] urls; ClassLoader cld = null; // First try the "application's" class loader. cld = getContextClassLoader(); if (cld == null) cld = cl.getClassLoader(); if (cld != null) urls = getResources(cld, name); else urls = getSystemResources(name); if (urls != null) { for (int i = 0; i < urls.length; i++) { URL url = urls[i]; InputStream clis = null; if (debug) pr("DEBUG: URL " + url); try { clis = openStream(url); if (clis != null) { loader.load(clis); anyLoaded = true; if (debug) pr("DEBUG: successfully loaded resource: " + url); } else { if (debug) pr("DEBUG: not loading resource: " + url); } } catch (IOException ioex) { if (debug) pr("DEBUG: " + ioex); } catch (SecurityException sex) { if (debug) pr("DEBUG: " + sex); } finally { try { if (clis != null) clis.close(); } catch (IOException cex) { } } } } } catch (Exception ex) { if (debug) pr("DEBUG: " + ex); } // if failed to load anything, fall back to old technique, just in case if (!anyLoaded) { if (debug) pr("DEBUG: !anyLoaded"); loadResource("/" + name, cl, loader); } } private void pr(String str) { getDebugOut().println(str); } /* * Following are security related methods that work on JDK 1.2 or newer. */ private static ClassLoader getContextClassLoader() { return (ClassLoader) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { ClassLoader cl = null; try { cl = Thread.currentThread().getContextClassLoader(); } catch (SecurityException ex) { } return cl; } }); } private static InputStream getResourceAsStream(final Class c, final String name) throws IOException { try { return (InputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws IOException { return c.getResourceAsStream(name); } }); } catch (PrivilegedActionException e) { throw (IOException)e.getException(); } } private static URL[] getResources(final ClassLoader cl, final String name) { return (URL[]) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { URL[] ret = null; try { Vector v = new Vector(); Enumeration e = cl.getResources(name); while (e != null && e.hasMoreElements()) { URL url = (URL)e.nextElement(); if (url != null) v.addElement(url); } if (v.size() > 0) { ret = new URL[v.size()]; v.copyInto(ret); } } catch (IOException ioex) { } catch (SecurityException ex) { } return ret; } }); } private static URL[] getSystemResources(final String name) { return (URL[]) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { URL[] ret = null; try { Vector v = new Vector(); Enumeration e = ClassLoader.getSystemResources(name); while (e != null && e.hasMoreElements()) { URL url = (URL)e.nextElement(); if (url != null) v.addElement(url); } if (v.size() > 0) { ret = new URL[v.size()]; v.copyInto(ret); } } catch (IOException ioex) { } catch (SecurityException ex) { } return ret; } }); } private static InputStream openStream(final URL url) throws IOException { try { return (InputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws IOException { return url.openStream(); } }); } catch (PrivilegedActionException e) { throw (IOException)e.getException(); } }}/** * Support interface to generalize * code that loads resources from stream. */interface StreamLoader { public void load(InputStream is) throws IOException;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -