File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\model\JenkinsLocationConfiguration.java.html

1 package jenkins.model;
2
3 import hudson.Extension;
4 import hudson.Util;
5 import hudson.XmlFile;
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;
11
12 import javax.mail.internet.AddressException;
13 import javax.mail.internet.InternetAddress;
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18
19 /**
20 * Stores the location of Jenkins (e-mail address and the HTTP URL.)
21 *
22 * @author Kohsuke Kawaguchi
23 */
24 @Extension
25 public class JenkinsLocationConfiguration extends GlobalConfiguration {
26 /**
27 * @deprecated
28 */
29 private transient String hudsonUrl;
30 private String adminAddress;
31 private String jenkinsUrl;
32
33 // just to suppress warnings
34 private transient String charset,useSsl;
35
36 public static JenkinsLocationConfiguration get() {
37 return GlobalConfiguration.all().get(JenkinsLocationConfiguration.class);
38 }
39
40 public JenkinsLocationConfiguration() {
41 load();
42 }
43
44 @Override
45 public synchronized void load() {
46 // for backward compatibility, if we don't have our own data yet, then
47 // load from Mailer.
48 XmlFile file = getConfigFile();
49 if(!file.exists()) {
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"));
53 if (file.exists()) {
54 try {
55 file.unmarshal(this);
56 if (jenkinsUrl==null)
57 jenkinsUrl = hudsonUrl;
58 } catch (IOException e) {
59 LOGGER.log(Level.WARNING, "Failed to load "+file, e);
60 }
61 }
62 } else {
63 super.load();
64 }
65 }
66
67 public String getAdminAddress() {
68 String v = adminAddress;
69 if(v==null) v = Messages.Mailer_Address_Not_Configured();
70 return v;
71 }
72
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);
78 }
79 this.adminAddress = adminAddress;
80 save();
81 }
82
83 public String getUrl() {
84 return jenkinsUrl;
85 }
86
87 public void setUrl(String hudsonUrl) {
88 String url = Util.nullify(hudsonUrl);
89 if(url!=null && !url.endsWith("/"))
90 url += '/';
91 this.jenkinsUrl = url;
92 save();
93 }
94
95 @Override
96 public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
97 req.bindJSON(this,json);
98 return true;
99 }
100
101 /**
102 * Checks the URL in <tt>global.jelly</tt>
103 */
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();
108 }
109
110 public FormValidation doCheckAdminAddress(@QueryParameter String value) {
111 try {
112 new InternetAddress(value);
113 return FormValidation.ok();
114 } catch (AddressException e) {
115 return FormValidation.error(e.getMessage());
116 }
117 }
118
119 private static final Logger LOGGER = Logger.getLogger(JenkinsLocationConfiguration.class.getName());
120 }
121