KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > registry


1 /*
2  * @(#)registry.java 1.9 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38 import java.util.*;
39
40 import javax.mail.*;
41 import javax.mail.internet.*;
42
43 /**
44  * This class demonstrates how to query the registry for available
45  * Providers, set default providers, etc. See section 5.2 in the
46  * JavaMail 1.0 Spec for details on how to use the registry.
47  *
48  * See the comments inline for what's happening.
49  *
50  * @author Max Spivak
51  */

52
53 public class registry {
54     // let's remember a few providers
55
static Provider _aProvider, _bProvider, _sunSMTP, _sunIMAP;
56
57     public static void main(String JavaDoc[] args) {
58     Properties props = new Properties();
59
60     // set smtp and imap to be our default
61
// transport and store protocols, respectively
62
props.put("mail.transport.protocol", "smtp");
63     props.put("mail.store.protocol", "imap");
64
65     //
66
props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport");
67     props.put("mail.imap.class", "com.sun.mail.imap.IMAPStore");
68     
69     Session session = Session.getInstance(props, null);
70     //session.setDebug(true);
71

72     // Retrieve all configured providers from the Session
73
System.out.println("\n------ getProviders()----------");
74     Provider[] providers = session.getProviders();
75     for (int i = 0; i < providers.length; i++) {
76         System.out.println("** " + providers[i]);
77
78         // let's remember some providers so that we can use them later
79
// (I'm explicitly showing multiple ways of testing Providers)
80
// BTW, no Provider "ACME Corp" will be found in the default
81
// setup b/c its not in any javamail.providers resource files
82
String JavaDoc s = null;
83         if (((s = providers[i].getVendor()) != null) &&
84         s.startsWith("ACME Corp")) {
85         _aProvider = providers[i];
86         }
87
88         // this Provider won't be found by default either
89
if (providers[i].getClassName().endsWith("application.smtp"))
90         _bProvider = providers[i];
91
92         // this Provider will be found since com.sun.mail.imap.IMAPStore
93
// is configured in javamail.default.providers
94
if (providers[i].getClassName().equals("com.sun.mail.imap.IMAPStore")){
95         _sunIMAP = providers[i];
96         }
97
98         // this Provider will be found as well since there is a
99
// Sun Microsystems SMTP transport configured by
100
// default in javamail.default.providers
101
if (((s = providers[i].getVendor()) != null) &&
102         s.startsWith("Sun Microsystems") &&
103         providers[i].getType() == Provider.Type.TRANSPORT &&
104         providers[i].getProtocol().equalsIgnoreCase("smtp")) {
105         _sunSMTP = providers[i];
106         }
107     }
108     
109     System.out.println("\n------ initial protocol defaults -------");
110     try {
111         System.out.println("imap: " + session.getProvider("imap"));
112         System.out.println("smtp: " + session.getProvider("smtp"));
113         // the NNTP provider will fail since we don't have one configured
114
System.out.println("nntp: " + session.getProvider("nntp"));
115     } catch (NoSuchProviderException mex) {
116         System.out.println(">> This exception is OK since there is no NNTP Provider configured by default");
117         mex.printStackTrace();
118     }
119
120     System.out.println("\n------ set new protocol defaults ------");
121     // set some new defaults
122
try {
123         // since _aProvider isn't configured, this will fail
124
session.setProvider(_aProvider); // will fail
125
} catch (NoSuchProviderException mex) {
126         System.out.println(">> Exception expected: _aProvider is null");
127         mex.printStackTrace();
128     }
129     try {
130         // _sunIMAP provider should've configured correctly; should work
131
session.setProvider(_sunIMAP);
132     } catch (NoSuchProviderException mex) { mex.printStackTrace(); }
133     try {
134         System.out.println("imap: " + session.getProvider("imap"));
135         System.out.println("smtp: " + session.getProvider("smtp"));
136     } catch (NoSuchProviderException mex) { mex.printStackTrace(); }
137
138
139     System.out.println("\n\n----- get some stores ---------");
140     // multiple ways to retrieve stores. these will print out the
141
// string "imap:" since its the URLName for the store
142
try {
143         System.out.println("getStore(): " + session.getStore());
144         System.out.println("getStore(Provider): " +
145                    session.getStore(_sunIMAP));
146     } catch (NoSuchProviderException mex) {
147         mex.printStackTrace();
148     }
149
150     try {
151         System.out.println("getStore(imap): " + session.getStore("imap"));
152         // pop3 will fail since it doesn't exist
153
System.out.println("getStore(pop3): " + session.getStore("pop3"));
154     } catch (NoSuchProviderException mex) {
155         System.out.println(">> Exception expected: no pop3 provider");
156         mex.printStackTrace();
157     }
158
159
160     System.out.println("\n\n----- now for transports/addresses ---------");
161     // retrieve transports; these will print out "smtp:" (like stores did)
162
try {
163         System.out.println("getTransport(): " + session.getTransport());
164         System.out.println("getTransport(Provider): " +
165                    session.getTransport(_sunSMTP));
166         System.out.println("getTransport(smtp): " +
167                    session.getTransport("smtp"));
168         System.out.println("getTransport(Address): " +
169                    session.getTransport(new InternetAddress("mspivak@apilon")));
170         // News will fail since there's no news provider configured
171
System.out.println("getTransport(News): " +
172                    session.getTransport(new NewsAddress("rec.humor")));
173     } catch (MessagingException mex) {
174         System.out.println(">> Exception expected: no news provider configured");
175         mex.printStackTrace();
176     }
177     }
178 }
179
Popular Tags