1 package org.apache.commons.jcs.utils.discovery;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * This manages UDPDiscovery Services. We should end up with one service per Lateral Cache Manager
32 * Instance. One service works for multiple regions. We don't want a connection for each region.
33 * <p>
34 * @author Aaron Smuts
35 */
36 public class UDPDiscoveryManager
37 {
38 /** The logger */
39 private static final Log log = LogFactory.getLog( UDPDiscoveryManager.class );
40
41 /** Singleton instance */
42 private static UDPDiscoveryManager INSTANCE = new UDPDiscoveryManager();
43
44 /** Known services */
45 private final Map<String, UDPDiscoveryService> services = new HashMap<String, UDPDiscoveryService>();
46
47 /** private for singleton */
48 private UDPDiscoveryManager()
49 {
50 // noopt
51 }
52
53 /**
54 * Singleton
55 * <p>
56 * @return UDPDiscoveryManager
57 */
58 public static UDPDiscoveryManager getInstance()
59 {
60 return INSTANCE;
61 }
62
63 /**
64 * Creates a service for the address and port if one doesn't exist already.
65 * <p>
66 * We need to key this using the listener port too. TODO think of making one discovery service
67 * work for multiple types of clients.
68 * <p>
69 * @param discoveryAddress
70 * @param discoveryPort
71 * @param servicePort
72 * @param cacheMgr
73 * @return UDPDiscoveryService
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 // register for shutdown notification
96 cacheMgr.registerShutdownObserver( service );
97
98 // inject scheduler
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 }