1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.distribution;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.CacheManager;
21 import net.sf.ehcache.util.PropertyUtil;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.util.Properties;
28 import java.util.StringTokenizer;
29
30
31
32
33
34
35
36 public class RMICacheManagerPeerProviderFactory extends CacheManagerPeerProviderFactory {
37
38 private static final Log LOG = LogFactory.getLog(RMICacheManagerPeerProviderFactory.class.getName());
39
40 private static final String PEER_DISCOVERY = "peerDiscovery";
41 private static final String AUTOMATIC_PEER_DISCOVERY = "automatic";
42 private static final String MANUALLY_CONFIGURED_PEER_DISCOVERY = "manual";
43 private static final String RMI_URLS = "rmiUrls";
44 private static final String MULTICAST_GROUP_PORT = "multicastGroupPort";
45 private static final String MULTICAST_GROUP_ADDRESS = "multicastGroupAddress";
46 private static final String MULTICAST_PACKET_TTL = "timeToLive";
47 private static final int MAXIMUM_TTL = 255;
48
49
50
51
52
53
54 public CacheManagerPeerProvider createCachePeerProvider(CacheManager cacheManager, Properties properties)
55 throws CacheException {
56 String peerDiscovery = PropertyUtil.extractAndLogProperty(PEER_DISCOVERY, properties);
57 if (peerDiscovery == null || peerDiscovery.equalsIgnoreCase(AUTOMATIC_PEER_DISCOVERY)) {
58 try {
59 return createAutomaticallyConfiguredCachePeerProvider(cacheManager, properties);
60 } catch (IOException e) {
61 throw new CacheException("Could not create CacheManagerPeerProvider. Initial cause was " + e.getMessage(), e);
62 }
63 } else if (peerDiscovery.equalsIgnoreCase(MANUALLY_CONFIGURED_PEER_DISCOVERY)) {
64 return createManuallyConfiguredCachePeerProvider(properties);
65 } else {
66 return null;
67 }
68 }
69
70
71
72
73
74 protected CacheManagerPeerProvider createManuallyConfiguredCachePeerProvider(Properties properties) {
75 String rmiUrls = PropertyUtil.extractAndLogProperty(RMI_URLS, properties);
76 if (rmiUrls == null || rmiUrls.length() == 0) {
77 LOG.info("Starting manual peer provider with empty list of peers. No replication will occur unless peers are added.");
78 rmiUrls = new String();
79 }
80 rmiUrls = rmiUrls.trim();
81 StringTokenizer stringTokenizer = new StringTokenizer(rmiUrls, PayloadUtil.URL_DELIMITER);
82 RMICacheManagerPeerProvider rmiPeerProvider = new ManualRMICacheManagerPeerProvider();
83 while (stringTokenizer.hasMoreTokens()) {
84 String rmiUrl = stringTokenizer.nextToken();
85 rmiUrl = rmiUrl.trim();
86 rmiPeerProvider.registerPeer(rmiUrl);
87 if (LOG.isDebugEnabled()) {
88 LOG.debug("Registering peer " + rmiUrl);
89 }
90 }
91 return rmiPeerProvider;
92 }
93
94
95
96
97 protected CacheManagerPeerProvider createAutomaticallyConfiguredCachePeerProvider(CacheManager cacheManager,
98 Properties properties) throws IOException {
99 String groupAddressString = PropertyUtil.extractAndLogProperty(MULTICAST_GROUP_ADDRESS, properties);
100 InetAddress groupAddress = InetAddress.getByName(groupAddressString);
101 String multicastPortString = PropertyUtil.extractAndLogProperty(MULTICAST_GROUP_PORT, properties);
102 Integer multicastPort = new Integer(multicastPortString);
103 String packetTimeToLiveString = PropertyUtil.extractAndLogProperty(MULTICAST_PACKET_TTL, properties);
104 Integer timeToLive;
105 if (packetTimeToLiveString == null) {
106 timeToLive = new Integer(1);
107 LOG.debug("No TTL set. Setting it to the default of 1, which means packets are limited to the same subnet.");
108 } else {
109 timeToLive = new Integer(packetTimeToLiveString);
110 if (timeToLive.intValue() < 0 || timeToLive.intValue() > MAXIMUM_TTL) {
111 throw new CacheException("The TTL must be set to a value between 0 and 255");
112 }
113 }
114 return new MulticastRMICacheManagerPeerProvider(cacheManager, groupAddress, multicastPort, timeToLive);
115 }
116 }