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

1 package hudson;
2
3 import jenkins.model.Jenkins;
4 import jenkins.model.Jenkins.MasterComputer;
5
6 import javax.jmdns.JmDNS;
7 import javax.jmdns.ServiceInfo;
8 import java.io.Closeable;
9 import java.io.IOException;
10 import java.net.URL;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.concurrent.Callable;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16
17 /**
18 * Registers a DNS multi-cast service-discovery support.
19 *
20 * @author Kohsuke Kawaguchi
21 */
22 public class DNSMultiCast implements Closeable {
23 private JmDNS jmdns;
24
25 public DNSMultiCast(final Jenkins jenkins) {
26 if (disabled) return; // escape hatch
27
28 // the registerService call can be slow. run these asynchronously
29 MasterComputer.threadPoolForRemoting.submit(new Callable<Object>() {
30 public Object call() {
31 try {
32 jmdns = JmDNS.create();
33
34 Map<String,String> props = new HashMap<String, String>();
35 String rootURL = jenkins.getRootUrl();
36 if (rootURL==null) return null;
37
38 props.put("url", rootURL);
39 try {
40 props.put("version",String.valueOf(Jenkins.getVersion()));
41 } catch (IllegalArgumentException e) {
42 // failed to parse the version number
43 }
44
45 TcpSlaveAgentListener tal = jenkins.getTcpSlaveAgentListener();
46 if (tal!=null)
47 props.put("slave-port",String.valueOf(tal.getPort()));
48
49 props.put("server-id", jenkins.getLegacyInstanceId());
50
51 URL jenkins_url = new URL(rootURL);
52 int jenkins_port = jenkins_url.getPort();
53 if (jenkins_port == -1) {
54 jenkins_port = 80;
55 }
56 if (jenkins_url.getPath().length() > 0) {
57 props.put("path", jenkins_url.getPath());
58 }
59
60 jmdns.registerService(ServiceInfo.create("_hudson._tcp.local.","jenkins",
61 jenkins_port,0,0,props)); // for backward compatibility
62 jmdns.registerService(ServiceInfo.create("_jenkins._tcp.local.","jenkins",
63 jenkins_port,0,0,props));
64
65 // Make Jenkins appear in Safari's Bonjour bookmarks
66 jmdns.registerService(ServiceInfo.create("_http._tcp.local.","Jenkins",
67 jenkins_port,0,0,props));
68 } catch (IOException e) {
69 LOGGER.log(Level.WARNING,"Failed to advertise the service to DNS multi-cast",e);
70 }
71 return null;
72 }
73 });
74 }
75
76 public void close() {
77 if (jmdns!=null) {
78 // try {
79 jmdns.abort();
80 jmdns = null;
81 // } catch (final IOException e) {
82 // LOGGER.log(Level.WARNING,"Failed to close down JmDNS instance!",e);
83 // }
84 }
85 }
86
87 private static final Logger LOGGER = Logger.getLogger(DNSMultiCast.class.getName());
88
89 public static boolean disabled = Boolean.getBoolean(DNSMultiCast.class.getName()+".disabled");
90 }
91