View Javadoc

1   /**
2    *  Copyright 2003-2006 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.CacheManager;
20  import net.sf.ehcache.CacheException;
21  
22  import java.util.Properties;
23  import java.util.StringTokenizer;
24  import java.net.InetAddress;
25  import java.io.IOException;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * Builds a factory based on RMI
32   * @author Greg Luck
33   * @version $Id: RMICacheManagerPeerProviderFactory.java 52 2006-04-24 14:50:03Z gregluck $
34   */
35  public final class RMICacheManagerPeerProviderFactory extends CacheManagerPeerProviderFactory {
36  
37      private static final Log LOG = LogFactory.getLog(RMICacheManagerPeerProviderFactory.class.getName());
38  
39      private static final String PEER_DISCOVERY = "peerDiscovery";
40      private static final String AUTOMATIC_PEER_DISCOVERY = "automatic";
41      private static final String MANUALLY_CONFIGURED_PEER_DISCOVERY = "manual";
42      private static final String RMI_URLS = "rmiUrls";
43      private static final String MULTICAST_GROUP_PORT = "multicastGroupPort";
44      private static final String MULTICAST_GROUP_ADDRESS = "multicastGroupAddress";
45  
46  
47      /**
48       * @param properties implementation specific properties. These are configured as comma
49       *                   separated name value pairs in ehcache.xml
50       */
51      public final CacheManagerPeerProvider createCachePeerProvider(CacheManager cacheManager, Properties properties)
52      throws CacheException {
53          String peerDiscovery = extractAndLogProperty(PEER_DISCOVERY, properties);
54          if (peerDiscovery.equalsIgnoreCase(AUTOMATIC_PEER_DISCOVERY)) {
55              try {
56                  return createAutomaticallyConfiguredCachePeerProvider(cacheManager, properties);
57              } catch (IOException e) {
58                  throw new CacheException("Could not create CacheManagerPeerProvider. Initial cause was " + e.getMessage(), e);
59              }
60          } else if (peerDiscovery.equalsIgnoreCase(MANUALLY_CONFIGURED_PEER_DISCOVERY)) {
61              return createManuallyConfiguredCachePeerProvider(properties);
62          } else {
63              return null;
64          }
65      }
66  
67  
68      /**
69       * peerDiscovery=manual, rmiUrls=//hostname:port/cacheName //hostname:port/cacheName //hostname:port/cacheName
70       */
71      private static CacheManagerPeerProvider createManuallyConfiguredCachePeerProvider(Properties properties) {
72          String rmiUrls = extractAndLogProperty(RMI_URLS, properties);
73          if (rmiUrls == null || rmiUrls.length() == 0) {
74              throw new CacheException("rmiUrls must be specified when peerDiscovery is manual");
75          }
76          rmiUrls = rmiUrls.trim();
77          StringTokenizer stringTokenizer = new StringTokenizer(rmiUrls, PayloadUtil.URL_DELIMITER);
78          RMICacheManagerPeerProvider rmiPeerProvider = new ManualRMICacheManagerPeerProvider();
79          while (stringTokenizer.hasMoreTokens()) {
80              String rmiUrl = stringTokenizer.nextToken();
81              rmiUrl = rmiUrl.trim();
82              rmiPeerProvider.registerPeer(rmiUrl);
83              if (LOG.isDebugEnabled()) {
84                  LOG.debug("Registering peer " + rmiUrl);
85              }
86          }
87          return rmiPeerProvider;
88      }
89  
90      /**
91       * peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446
92       */
93      private static CacheManagerPeerProvider createAutomaticallyConfiguredCachePeerProvider(CacheManager cacheManager,
94                                                                               Properties properties) throws IOException {
95          String groupAddressString = extractAndLogProperty(MULTICAST_GROUP_ADDRESS, properties);
96          InetAddress groupAddress = InetAddress.getByName(groupAddressString);
97          String multicastPortString = extractAndLogProperty(MULTICAST_GROUP_PORT, properties);
98          Integer multicastPort = new Integer(multicastPortString);
99          return new MulticastRMICacheManagerPeerProvider(cacheManager, groupAddress, multicastPort);
100     }
101 
102 
103     /**
104      * @return null is their is not property for the key
105      */
106     private static String extractAndLogProperty(String name, Properties properties) {
107         String foundValue = (String) properties.get(name);
108         if (LOG.isDebugEnabled()) {
109             LOG.debug(new StringBuffer().append("Value found for ").append(name).append(": ")
110                     .append(foundValue).toString());
111         }
112         return foundValue;
113 
114     }
115 
116 
117 }