001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jca.cfg;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.log.Log;
033: import com.caucho.util.L10N;
034: import com.caucho.webbeans.cfg.*;
035:
036: import java.lang.reflect.Method;
037: import java.util.Properties;
038: import java.util.logging.Logger;
039:
040: import javax.annotation.*;
041: import javax.mail.*;
042:
043: /**
044: * Configuration for a javamail.
045: */
046: public class JavaMailConfig extends AbstractBeanConfig {
047: private static final L10N L = new L10N(JavaMailConfig.class);
048: private static final Logger log = Logger
049: .getLogger(JavaMailConfig.class.getName());
050:
051: private Properties _props = new Properties();
052: private Authenticator _auth;
053: private Session _session;
054:
055: public JavaMailConfig() {
056: }
057:
058: /**
059: * Sets the authenticator
060: */
061: public void setAuthenticator(Authenticator auth) {
062: _auth = auth;
063: }
064:
065: //
066: // well-known attributes
067: //
068:
069: /**
070: * mail.from
071: */
072: public void setFrom(String from) {
073: setProperty("mail.from", from);
074: }
075:
076: /**
077: * mail.host
078: */
079: public void setHost(String host) {
080: setProperty("mail.host", host);
081: }
082:
083: /**
084: * mail.imap.host
085: */
086: public void setImapHost(String host) {
087: setProperty("mail.imap.host", host);
088: }
089:
090: /**
091: * mail.imap.user
092: */
093: public void setImapUser(String user) {
094: setProperty("mail.imap.user", user);
095: }
096:
097: /**
098: * mail.pop3.host
099: */
100: public void setPop3Host(String host) {
101: setProperty("mail.pop3.host", host);
102: }
103:
104: /**
105: * mail.pop3.user
106: */
107: public void setPop3User(String user) {
108: setProperty("mail.pop3.user", user);
109: }
110:
111: /**
112: * mail.smtp.host
113: */
114: public void setSmtpHost(String host) {
115: setProperty("mail.smtp.host", host);
116: }
117:
118: /**
119: * mail.smtp.ssl
120: */
121: public void setSmtpSsl(boolean ssl) {
122: setProperty("mail.smtp.ssl", String.valueOf(ssl));
123: }
124:
125: /**
126: * mail.smtp.port
127: */
128: public void setSmtpPort(int port) {
129: setProperty("mail.smtp.port", String.valueOf(port));
130: }
131:
132: /**
133: * mail.smtp.user
134: */
135: public void setSmtpUser(String user) {
136: setProperty("mail.smtp.user", user);
137: }
138:
139: /**
140: * mail.store.protocol
141: */
142: public void setStoreProtocol(String protocol) {
143: setProperty("mail.store.protocol", protocol);
144: }
145:
146: /**
147: * mail.transport.protocol
148: */
149: public void setTransportProtocol(String protocol) {
150: setProperty("mail.transport.protocol", protocol);
151: }
152:
153: /**
154: * mail.user
155: */
156: public void setUser(String user) {
157: setProperty("mail.user", user);
158: }
159:
160: /**
161: * Sets an attribute.
162: */
163: public void setProperty(String name, String value) {
164: _props.put(name, value);
165: }
166:
167: public void setProperties(Properties props) {
168: _props.putAll(props);
169: }
170:
171: public void setValue(Properties props) {
172: _props.putAll(props);
173: }
174:
175: @PostConstruct
176: public void init() throws ConfigException {
177: try {
178: if (getInit() != null)
179: getInit().configure(this );
180:
181: if (_auth != null)
182: _session = Session.getInstance(_props, _auth);
183: else
184: _session = Session.getInstance(_props);
185:
186: register(_session);
187: } catch (Exception e) {
188: throw ConfigException.create(e);
189: }
190: }
191:
192: public Object replaceObject() {
193: return _session;
194: }
195: }
|