1 package hudson.tasks; 2 3 import hudson.Launcher; 4 import hudson.model.Build; 5 import hudson.model.BuildListener; 6 import hudson.model.Descriptor; 7 import hudson.model.Project; 8 import hudson.model.User; 9 import hudson.model.UserPropertyDescriptor; 10 import org.apache.tools.ant.types.selectors.SelectorUtils; 11 import org.kohsuke.stapler.StaplerRequest; 12 13 import javax.mail.Authenticator ; 14 import javax.mail.PasswordAuthentication ; 15 import javax.mail.Session ; 16 import java.io.File ; 17 import java.util.Map ; 18 import java.util.Properties ; 19 import java.util.logging.Level ; 20 import java.util.logging.Logger ; 21 22 27 public class Mailer extends Publisher { 28 private static final Logger LOGGER = Logger.getLogger(Mailer.class.getName()); 29 30 33 public String recipients; 34 35 38 public boolean dontNotifyEveryUnstableBuild; 39 40 43 public boolean sendToIndividuals; 44 45 private transient String from; 48 private transient String subject; 49 private transient boolean failureOnly; 50 51 public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException { 52 if(debug) 53 listener.getLogger().println("Running mailer"); 54 return new MailSender<Project,Build>(recipients,dontNotifyEveryUnstableBuild,sendToIndividuals) { 55 56 @Override 57 public boolean artifactMatches(String path, Build build) { 58 ArtifactArchiver aa = (ArtifactArchiver) build.getProject().getPublishers().get(ArtifactArchiver.DESCRIPTOR); 59 if (aa == null) { 60 LOGGER.finer("No ArtifactArchiver found"); 61 return false; 62 } 63 String artifacts = aa.getArtifacts(); 64 for (String include : artifacts.split("[, ]+")) { 65 String pattern = include.replace(File.separatorChar, '/'); 66 if (pattern.endsWith("/")) { 67 pattern += "**"; 68 } 69 if (SelectorUtils.matchPath(pattern, path)) { 70 LOGGER.log(Level.FINER, "DescriptorImpl.artifactMatches true for {0} against {1}", new Object [] {path, pattern}); 71 return true; 72 } 73 } 74 LOGGER.log(Level.FINER, "DescriptorImpl.artifactMatches for {0} matched none of {1}", new Object [] {path, artifacts}); 75 return false; 76 } 77 }.execute(build,listener); 78 } 79 80 public Descriptor<Publisher> getDescriptor() { 81 return DESCRIPTOR; 82 } 83 84 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 85 86 public static final class DescriptorImpl extends Descriptor<Publisher> { 87 91 private String defaultSuffix; 92 93 96 private String hudsonUrl; 97 98 101 private String smtpAuthPassword,smtpAuthUsername; 102 103 107 private String adminAddress; 108 109 113 private String smtpHost; 114 115 public DescriptorImpl() { 116 super(Mailer.class); 117 load(); 118 } 119 120 123 protected void convert(Map <String , Object > oldPropertyBag) { 124 defaultSuffix = (String )oldPropertyBag.get("mail.default.suffix"); 125 hudsonUrl = (String )oldPropertyBag.get("mail.hudson.url"); 126 smtpAuthUsername = (String )oldPropertyBag.get("mail.hudson.smtpauth.username"); 127 smtpAuthPassword = (String )oldPropertyBag.get("mail.hudson.smtpauth.password"); 128 adminAddress = (String )oldPropertyBag.get("mail.admin.address"); 129 smtpHost = (String )oldPropertyBag.get("mail.smtp.host"); 130 } 131 132 public String getDisplayName() { 133 return "E-mail Notification"; 134 } 135 136 public String getHelpFile() { 137 return "/help/project-config/mailer.html"; 138 } 139 140 public String getDefaultSuffix() { 141 return defaultSuffix; 142 } 143 144 145 public Session createSession() { 146 Properties props = new Properties (System.getProperties()); 147 if(smtpHost!=null) 148 props.put("mail.smtp.host",smtpHost); 149 150 return Session.getInstance(props,getAuthenticator()); 151 } 152 153 private Authenticator getAuthenticator() { 154 final String un = getSmtpAuthUserName(); 155 if(un==null) return null; 156 return new Authenticator () { 157 protected PasswordAuthentication getPasswordAuthentication() { 158 return new PasswordAuthentication (getSmtpAuthUserName(),getSmtpAuthPassword()); 159 } 160 }; 161 } 162 163 public boolean configure(StaplerRequest req) throws FormException { 164 smtpHost = nullify(req.getParameter("mailer_smtp_server")); 166 adminAddress = req.getParameter("mailer_admin_address"); 167 defaultSuffix = nullify(req.getParameter("mailer_default_suffix")); 168 String url = nullify(req.getParameter("mailer_hudson_url")); 169 if(url!=null && !url.endsWith("/")) 170 url += '/'; 171 hudsonUrl = url; 172 173 if(req.getParameter("mailer.useSMTPAuth")!=null) { 174 smtpAuthUsername = nullify(req.getParameter("mailer.SMTPAuth.userName")); 175 smtpAuthPassword = nullify(req.getParameter("mailer.SMTPAuth.password")); 176 } else { 177 smtpAuthUsername = smtpAuthPassword = null; 178 } 179 180 save(); 181 return super.configure(req); 182 } 183 184 private String nullify(String v) { 185 if(v!=null && v.length()==0) v=null; 186 return v; 187 } 188 189 public String getSmtpServer() { 190 return smtpHost; 191 } 192 193 public String getAdminAddress() { 194 String v = adminAddress; 195 if(v==null) v = "address not configured yet <nobody>"; 196 return v; 197 } 198 199 public String getUrl() { 200 return hudsonUrl; 201 } 202 203 public String getSmtpAuthUserName() { 204 return smtpAuthUsername; 205 } 206 207 public String getSmtpAuthPassword() { 208 return smtpAuthPassword; 209 } 210 211 public Publisher newInstance(StaplerRequest req) { 212 Mailer m = new Mailer(); 213 req.bindParameters(m,"mailer_"); 214 return m; 215 } 216 } 217 218 221 public static class UserProperty extends hudson.model.UserProperty { 222 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 223 224 228 private final String emailAddress; 229 230 public UserProperty(String emailAddress) { 231 this.emailAddress = emailAddress; 232 } 233 234 public String getAddress() { 235 if(emailAddress!=null) 236 return emailAddress; 237 238 String ds = Mailer.DESCRIPTOR.getDefaultSuffix(); 239 if(ds!=null) 240 return user.getId()+ds; 241 else 242 return null; 243 } 244 245 public DescriptorImpl getDescriptor() { 246 return DESCRIPTOR; 247 } 248 249 public static final class DescriptorImpl extends UserPropertyDescriptor { 250 public DescriptorImpl() { 251 super(UserProperty.class); 252 } 253 254 public String getDisplayName() { 255 return "E-mail"; 256 } 257 258 public UserProperty newInstance(User user) { 259 return new UserProperty(null); 260 } 261 262 public UserProperty newInstance(StaplerRequest req) throws FormException { 263 return new UserProperty(req.getParameter("email.address")); 264 } 265 } 266 } 267 268 271 public static boolean debug = false; 272 } 273 | Popular Tags |