View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
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  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   * Builds a factory based on RMI
32   *
33   * @author Greg Luck
34   * @version $Id: RMICacheManagerPeerProviderFactory.java 512 2007-07-10 09:18:45Z gregluck $
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       * @param properties implementation specific properties. These are configured as comma
52       *                   separated name value pairs in ehcache.xml
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       * peerDiscovery=manual, rmiUrls=//hostname:port/cacheName //hostname:port/cacheName //hostname:port/cacheName
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       * peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, multicastPacketTimeToLive=255
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 }