1 26 27 package org.objectweb.jonas.ant.jonasbase; 28 29 import java.io.File ; 30 import java.util.Properties ; 31 32 import org.apache.tools.ant.BuildException; 33 import org.objectweb.jonas.ant.JOnASBaseTask; 34 35 39 public class Mail extends JTask implements BaseTaskItf { 40 41 44 private static final String INFO = "[Mail] "; 45 46 49 private static final String MAILFACTORY_PROPERTY = "jonas.service.mail.factories"; 50 51 54 private static final String SESSION_FACTORY = "Session"; 55 56 59 private static final String MIMEPARTDATASOURCE_FACTORY = "MimePartDataSource"; 60 61 64 private static final String SESSION_FACTORY_CLASS = "javax.mail.Session"; 65 66 69 private static final String MIMEPARTDATASOURCE_FACTORY_CLASS = "javax.mail.internet.MimePartDataSource"; 70 71 74 private String type = null; 75 76 79 private String name = null; 80 81 84 private String mailTo = null; 85 86 89 private String subject = null; 90 91 95 public void setMailTo(String mailTo) { 96 this.mailTo = mailTo; 97 } 98 99 103 public void setName(String name) { 104 this.name = name; 105 } 106 107 111 public void setSubject(String subject) { 112 this.subject = subject; 113 } 114 115 119 public void setType(String type) { 120 this.type = type; 121 } 122 123 126 private void checkProperties() { 127 if (name == null) { 128 throw new BuildException(INFO + "Property 'name' is missing."); 129 } else if (type == null) { 130 throw new BuildException(INFO + "Property 'type' is missing."); 131 } 132 } 133 134 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 + "' and name '" + name + "'"; 145 146 if (type.equalsIgnoreCase(SESSION_FACTORY)) { 147 className = SESSION_FACTORY_CLASS; 148 } else if (type.equalsIgnoreCase(MIMEPARTDATASOURCE_FACTORY)) { 149 className = MIMEPARTDATASOURCE_FACTORY_CLASS; 150 if (mailTo != null) { 152 props.put("mail.to", mailTo); 153 infoTxt += ", mailTo field '" + mailTo + "'"; 154 } 155 if (subject != null) { 157 props.put("mail.subject", subject); 158 infoTxt += ", subject '" + subject + "'"; 159 } 160 161 } else { 162 throw new BuildException(INFO + "Invalid type '" + type + "'."); 163 } 164 165 log(INFO + infoTxt + "..."); 167 168 props.put("mail.factory.type", className); 170 171 String jBaseConf = getDestDir().getPath() + File.separator + "conf"; 173 174 String propsFileName = jBaseConf + File.separator + name + ".properties"; 175 File tmpFile = new File (propsFileName); 176 177 writePropsToFile(INFO, props, tmpFile); 178 179 changeValueForKey(INFO, jBaseConf, JOnASBaseTask.JONAS_CONF_FILE, MAILFACTORY_PROPERTY, name, true); 181 } 182 183 } | Popular Tags |