3
import hudson.Extension;
6
import hudson.util.FormValidation;
7
import hudson.util.XStream2;
8
import net.sf.json.JSONObject;
9
import org.kohsuke.stapler.QueryParameter;
10
import org.kohsuke.stapler.StaplerRequest;
12
import javax.mail.internet.AddressException;
13
import javax.mail.internet.InternetAddress;
15
import java.io.IOException;
16
import java.util.logging.Level;
17
import java.util.logging.Logger;
20
* Stores the location of Jenkins (e-mail address and the HTTP URL.)
22
* @author Kohsuke Kawaguchi
25
public class JenkinsLocationConfiguration extends GlobalConfiguration {
29
private transient String hudsonUrl;
30
private String adminAddress;
31
private String jenkinsUrl;
33
// just to suppress warnings
34
private transient String charset,useSsl;
36
public static JenkinsLocationConfiguration get() {
37
return GlobalConfiguration.all().get(JenkinsLocationConfiguration.class);
40
public JenkinsLocationConfiguration() {
45
public synchronized void load() {
46
// for backward compatibility, if we don't have our own data yet, then
48
XmlFile file = getConfigFile();
50
XStream2 xs = new XStream2();
51
xs.addCompatibilityAlias("hudson.tasks.Mailer$DescriptorImpl",JenkinsLocationConfiguration.class);
52
file = new XmlFile(xs,new File(Jenkins.getInstance().getRootDir(),"hudson.tasks.Mailer.xml"));
57
jenkinsUrl = hudsonUrl;
58
} catch (IOException e) {
59
LOGGER.log(Level.WARNING, "Failed to load "+file, e);
67
public String getAdminAddress() {
68
String v = adminAddress;
69
if(v==null) v = Messages.Mailer_Address_Not_Configured();
73
public void setAdminAddress(String adminAddress) {
74
if(adminAddress.startsWith("\"") && adminAddress.endsWith("\"")) {
75
// some users apparently quote the whole thing. Don't konw why
76
// anyone does this, but it's a machine's job to forgive human mistake
77
adminAddress = adminAddress.substring(1,adminAddress.length()-1);
79
this.adminAddress = adminAddress;
83
public String getUrl() {
87
public void setUrl(String hudsonUrl) {
88
String url = Util.nullify(hudsonUrl);
89
if(url!=null && !url.endsWith("/"))
91
this.jenkinsUrl = url;
96
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
97
req.bindJSON(this,json);
102
* Checks the URL in <tt>global.jelly</tt>
104
public FormValidation doCheckUrl(@QueryParameter String value) {
105
if(value.startsWith("http://localhost"))
106
return FormValidation.warning(Messages.Mailer_Localhost_Error());
107
return FormValidation.ok();
110
public FormValidation doCheckAdminAddress(@QueryParameter String value) {
112
new InternetAddress(value);
113
return FormValidation.ok();
114
} catch (AddressException e) {
115
return FormValidation.error(e.getMessage());
119
private static final Logger LOGGER = Logger.getLogger(JenkinsLocationConfiguration.class.getName());