001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: Mail.java 4943 2004-06-11 09:27:47Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ant.jonasbase;
026:
027: import java.io.File;
028: import java.util.Properties;
029:
030: import org.apache.tools.ant.BuildException;
031: import org.objectweb.jonas.ant.JOnASBaseTask;
032:
033: /**
034: * Allow to create mail factory
035: * @author Florent Benoit
036: */
037: public class Mail extends JTask implements BaseTaskItf {
038:
039: /**
040: * Info for the logger
041: */
042: private static final String INFO = "[Mail] ";
043:
044: /**
045: * Property for Mail factories
046: */
047: private static final String MAILFACTORY_PROPERTY = "jonas.service.mail.factories";
048:
049: /**
050: * Session factory
051: */
052: private static final String SESSION_FACTORY = "Session";
053:
054: /**
055: * MimePartDataSource factory
056: */
057: private static final String MIMEPARTDATASOURCE_FACTORY = "MimePartDataSource";
058:
059: /**
060: * Session factory class
061: */
062: private static final String SESSION_FACTORY_CLASS = "javax.mail.Session";
063:
064: /**
065: * MimePartDataSource factory class
066: */
067: private static final String MIMEPARTDATASOURCE_FACTORY_CLASS = "javax.mail.internet.MimePartDataSource";
068:
069: /**
070: * Type of factory (Session or MimePartDataSource)
071: */
072: private String type = null;
073:
074: /**
075: * Name of the factory
076: */
077: private String name = null;
078:
079: /**
080: * Recipient (TO) of MimePartDataSource factory
081: */
082: private String mailTo = null;
083:
084: /**
085: * Subject of MimePartDataSource factory
086: */
087: private String subject = null;
088:
089: /**
090: * Sets the recipient (MimePartDataSource)
091: * @param mailTo recipient.
092: */
093: public void setMailTo(String mailTo) {
094: this .mailTo = mailTo;
095: }
096:
097: /**
098: * Sets the name
099: * @param name name of the factory
100: */
101: public void setName(String name) {
102: this .name = name;
103: }
104:
105: /**
106: * Sets the subject (MimePartDataSource)
107: * @param subject of the mail
108: */
109: public void setSubject(String subject) {
110: this .subject = subject;
111: }
112:
113: /**
114: * Sets the type of factory
115: * @param type of factory
116: */
117: public void setType(String type) {
118: this .type = type;
119: }
120:
121: /**
122: * Check the properties
123: */
124: private void checkProperties() {
125: if (name == null) {
126: throw new BuildException(INFO
127: + "Property 'name' is missing.");
128: } else if (type == null) {
129: throw new BuildException(INFO
130: + "Property 'type' is missing.");
131: }
132: }
133:
134: /**
135: * Execute this task
136: */
137: public void execute() {
138: checkProperties();
139:
140: Properties props = new Properties();
141: props.put("mail.factory.name", name);
142: String className = null;
143:
144: String infoTxt = "Generating a MailFactory with type '" + type
145: + "' and name '" + name + "'";
146:
147: if (type.equalsIgnoreCase(SESSION_FACTORY)) {
148: className = SESSION_FACTORY_CLASS;
149: } else if (type.equalsIgnoreCase(MIMEPARTDATASOURCE_FACTORY)) {
150: className = MIMEPARTDATASOURCE_FACTORY_CLASS;
151: // Set mailTo
152: if (mailTo != null) {
153: props.put("mail.to", mailTo);
154: infoTxt += ", mailTo field '" + mailTo + "'";
155: }
156: // Subject
157: if (subject != null) {
158: props.put("mail.subject", subject);
159: infoTxt += ", subject '" + subject + "'";
160: }
161:
162: } else {
163: throw new BuildException(INFO + "Invalid type '" + type
164: + "'.");
165: }
166:
167: // Display info
168: log(INFO + infoTxt + "...");
169:
170: // Set type
171: props.put("mail.factory.type", className);
172:
173: // Build new mail factory
174: String jBaseConf = getDestDir().getPath() + File.separator
175: + "conf";
176:
177: String propsFileName = jBaseConf + File.separator + name
178: + ".properties";
179: File tmpFile = new File(propsFileName);
180:
181: writePropsToFile(INFO, props, tmpFile);
182:
183: // Now add name to the existing list for the property
184: changeValueForKey(INFO, jBaseConf,
185: JOnASBaseTask.JONAS_CONF_FILE, MAILFACTORY_PROPERTY,
186: name, true);
187: }
188:
189: }
|