00001
00027 package org.objectweb.jonas.ant.jonasbase;
00028
00029 import java.io.File;
00030 import java.util.Properties;
00031
00032 import org.apache.tools.ant.BuildException;
00033 import org.objectweb.jonas.ant.JOnASBaseTask;
00034
00039 public class Mail extends JTask implements BaseTaskItf {
00040
00044 private static final String INFO = "[Mail] ";
00045
00049 private static final String MAILFACTORY_PROPERTY = "jonas.service.mail.factories";
00050
00054 private static final String SESSION_FACTORY = "Session";
00055
00059 private static final String MIMEPARTDATASOURCE_FACTORY = "MimePartDataSource";
00060
00064 private static final String SESSION_FACTORY_CLASS = "javax.mail.Session";
00065
00069 private static final String MIMEPARTDATASOURCE_FACTORY_CLASS = "javax.mail.internet.MimePartDataSource";
00070
00074 private String type = null;
00075
00079 private String name = null;
00080
00084 private String mailTo = null;
00085
00089 private String subject = null;
00090
00095 public void setMailTo(String mailTo) {
00096 this.mailTo = mailTo;
00097 }
00098
00103 public void setName(String name) {
00104 this.name = name;
00105 }
00106
00111 public void setSubject(String subject) {
00112 this.subject = subject;
00113 }
00114
00119 public void setType(String type) {
00120 this.type = type;
00121 }
00122
00126 private void checkProperties() {
00127 if (name == null) {
00128 throw new BuildException(INFO + "Property 'name' is missing.");
00129 } else if (type == null) {
00130 throw new BuildException(INFO + "Property 'type' is missing.");
00131 }
00132 }
00133
00137 public void execute() {
00138 checkProperties();
00139
00140 Properties props = new Properties();
00141 props.put("mail.factory.name", name);
00142 String className = null;
00143
00144 String infoTxt = "Generating a MailFactory with type '" + type + "' and name '" + name + "'";
00145
00146 if (type.equalsIgnoreCase(SESSION_FACTORY)) {
00147 className = SESSION_FACTORY_CLASS;
00148 } else if (type.equalsIgnoreCase(MIMEPARTDATASOURCE_FACTORY)) {
00149 className = MIMEPARTDATASOURCE_FACTORY_CLASS;
00150
00151 if (mailTo != null) {
00152 props.put("mail.to", mailTo);
00153 infoTxt += ", mailTo field '" + mailTo + "'";
00154 }
00155
00156 if (subject != null) {
00157 props.put("mail.subject", subject);
00158 infoTxt += ", subject '" + subject + "'";
00159 }
00160
00161 } else {
00162 throw new BuildException(INFO + "Invalid type '" + type + "'.");
00163 }
00164
00165
00166 log(INFO + infoTxt + "...");
00167
00168
00169 props.put("mail.factory.type", className);
00170
00171
00172 String jBaseConf = getDestDir().getPath() + File.separator + "conf";
00173
00174 String propsFileName = jBaseConf + File.separator + name + ".properties";
00175 File tmpFile = new File(propsFileName);
00176
00177 writePropsToFile(INFO, props, tmpFile);
00178
00179
00180 changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, MAILFACTORY_PROPERTY, name, true);
00181 }
00182
00183 }