1 package org.apache.commons.jcs.utils.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
26 import org.apache.commons.jcs.engine.behavior.IProvideScheduler;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33
34
35
36 public class UDPDiscoveryManager
37 {
38
39 private static final Log log = LogFactory.getLog( UDPDiscoveryManager.class );
40
41
42 private static UDPDiscoveryManager INSTANCE = new UDPDiscoveryManager();
43
44
45 private final Map<String, UDPDiscoveryService> services = new HashMap<String, UDPDiscoveryService>();
46
47
48 private UDPDiscoveryManager()
49 {
50
51 }
52
53
54
55
56
57
58 public static UDPDiscoveryManager getInstance()
59 {
60 return INSTANCE;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public synchronized UDPDiscoveryService getService( String discoveryAddress, int discoveryPort, int servicePort,
76 ICompositeCacheManager cacheMgr )
77 {
78 String key = discoveryAddress + ":" + discoveryPort + ":" + servicePort;
79
80 UDPDiscoveryService service = services.get( key );
81 if ( service == null )
82 {
83 if ( log.isInfoEnabled() )
84 {
85 log.info( "Creating service for address:port:servicePort [" + key + "]" );
86 }
87
88 UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
89 attributes.setUdpDiscoveryAddr( discoveryAddress );
90 attributes.setUdpDiscoveryPort( discoveryPort );
91 attributes.setServicePort( servicePort );
92
93 service = new UDPDiscoveryService( attributes );
94
95
96 cacheMgr.registerShutdownObserver( service );
97
98
99 if ( cacheMgr instanceof IProvideScheduler)
100 {
101 service.setScheduledExecutorService(((IProvideScheduler)cacheMgr).getScheduledExecutorService());
102 }
103
104 service.startup();
105 services.put( key, service );
106 }
107
108 if ( log.isDebugEnabled() )
109 {
110 log.debug( "Returning service [" + service + "] for key [" + key + "]" );
111 }
112
113 return service;
114 }
115 }