1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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
23 import java.io.IOException;
24 import java.net.InetAddress;
25 import java.util.Properties;
26 import java.util.StringTokenizer;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /***
32 * Builds a factory based on RMI
33 *
34 * @author Greg Luck
35 * @version $Id: RMICacheManagerPeerProviderFactory.html 13146 2011-08-01 17:12:39Z oletizi $
36 */
37 public class RMICacheManagerPeerProviderFactory extends CacheManagerPeerProviderFactory {
38
39 private static final Logger LOG = LoggerFactory.getLogger(RMICacheManagerPeerProviderFactory.class.getName());
40
41 private static final String HOST_NAME = "hostName";
42 private static final String PEER_DISCOVERY = "peerDiscovery";
43 private static final String AUTOMATIC_PEER_DISCOVERY = "automatic";
44 private static final String MANUALLY_CONFIGURED_PEER_DISCOVERY = "manual";
45 private static final String RMI_URLS = "rmiUrls";
46 private static final String MULTICAST_GROUP_PORT = "multicastGroupPort";
47 private static final String MULTICAST_GROUP_ADDRESS = "multicastGroupAddress";
48 private static final String MULTICAST_PACKET_TTL = "timeToLive";
49 private static final int MAXIMUM_TTL = 255;
50
51
52 /***
53 * @param properties implementation specific properties. These are configured as comma
54 * separated name value pairs in ehcache.xml
55 */
56 public CacheManagerPeerProvider createCachePeerProvider(CacheManager cacheManager, Properties properties)
57 throws CacheException {
58 String peerDiscovery = PropertyUtil.extractAndLogProperty(PEER_DISCOVERY, properties);
59 if (peerDiscovery == null || peerDiscovery.equalsIgnoreCase(AUTOMATIC_PEER_DISCOVERY)) {
60 try {
61 return createAutomaticallyConfiguredCachePeerProvider(cacheManager, properties);
62 } catch (IOException e) {
63 throw new CacheException("Could not create CacheManagerPeerProvider. Initial cause was " + e.getMessage(), e);
64 }
65 } else if (peerDiscovery.equalsIgnoreCase(MANUALLY_CONFIGURED_PEER_DISCOVERY)) {
66 return createManuallyConfiguredCachePeerProvider(properties);
67 } else {
68 return null;
69 }
70 }
71
72
73 /***
74 * peerDiscovery=manual, rmiUrls=//hostname:port/cacheName //hostname:port/cacheName //hostname:port/cacheName
75 */
76 protected CacheManagerPeerProvider createManuallyConfiguredCachePeerProvider(Properties properties) {
77 String rmiUrls = PropertyUtil.extractAndLogProperty(RMI_URLS, properties);
78 if (rmiUrls == null || rmiUrls.length() == 0) {
79 LOG.info("Starting manual peer provider with empty list of peers. " +
80 "No replication will occur unless peers are added.");
81 rmiUrls = "";
82 }
83 rmiUrls = rmiUrls.trim();
84 StringTokenizer stringTokenizer = new StringTokenizer(rmiUrls, PayloadUtil.URL_DELIMITER);
85 RMICacheManagerPeerProvider rmiPeerProvider = new ManualRMICacheManagerPeerProvider();
86 while (stringTokenizer.hasMoreTokens()) {
87 String rmiUrl = stringTokenizer.nextToken();
88 rmiUrl = rmiUrl.trim();
89 rmiPeerProvider.registerPeer(rmiUrl);
90
91 LOG.debug("Registering peer {}", rmiUrl);
92 }
93 return rmiPeerProvider;
94 }
95
96 /***
97 * peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, multicastPacketTimeToLive=255
98 */
99 protected CacheManagerPeerProvider createAutomaticallyConfiguredCachePeerProvider(CacheManager cacheManager,
100 Properties properties) throws IOException {
101 String hostName = PropertyUtil.extractAndLogProperty(HOST_NAME, properties);
102 InetAddress hostAddress = null;
103 if (hostName != null && hostName.length() != 0) {
104 hostAddress = InetAddress.getByName(hostName);
105 if (hostName.equals("localhost")) {
106 LOG.warn("Explicitly setting the multicast hostname to 'localhost' is not recommended. "
107 + "It will only work if all CacheManager peers are on the same machine.");
108 }
109 }
110
111
112 String groupAddressString = PropertyUtil.extractAndLogProperty(MULTICAST_GROUP_ADDRESS, properties);
113 InetAddress groupAddress = InetAddress.getByName(groupAddressString);
114 String multicastPortString = PropertyUtil.extractAndLogProperty(MULTICAST_GROUP_PORT, properties);
115 Integer multicastPort = Integer.valueOf(multicastPortString);
116 String packetTimeToLiveString = PropertyUtil.extractAndLogProperty(MULTICAST_PACKET_TTL, properties);
117 Integer timeToLive;
118 if (packetTimeToLiveString == null) {
119 timeToLive = Integer.valueOf(1);
120 LOG.debug("No TTL set. Setting it to the default of 1, which means packets are limited to the same subnet.");
121 } else {
122 timeToLive = Integer.valueOf(packetTimeToLiveString);
123 if (timeToLive.intValue() < 0 || timeToLive.intValue() > MAXIMUM_TTL) {
124 throw new CacheException("The TTL must be set to a value between 0 and 255");
125 }
126 }
127 return new MulticastRMICacheManagerPeerProvider(cacheManager, groupAddress, multicastPort, timeToLive, hostAddress);
128 }
129 }