001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.distribution;
016:
017: import net.sf.ehcache.CacheException;
018: import net.sf.ehcache.CacheManager;
019: import net.sf.ehcache.util.PropertyUtil;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import java.io.IOException;
024: import java.net.InetAddress;
025: import java.util.Properties;
026: import java.util.StringTokenizer;
027:
028: /**
029: * Builds a factory based on RMI
030: *
031: * @author Greg Luck
032: * @version $Id: RMICacheManagerPeerProviderFactory.java 519 2007-07-27 07:11:45Z gregluck $
033: */
034: public class RMICacheManagerPeerProviderFactory extends
035: CacheManagerPeerProviderFactory {
036:
037: private static final Log LOG = LogFactory
038: .getLog(RMICacheManagerPeerProviderFactory.class.getName());
039:
040: private static final String PEER_DISCOVERY = "peerDiscovery";
041: private static final String AUTOMATIC_PEER_DISCOVERY = "automatic";
042: private static final String MANUALLY_CONFIGURED_PEER_DISCOVERY = "manual";
043: private static final String RMI_URLS = "rmiUrls";
044: private static final String MULTICAST_GROUP_PORT = "multicastGroupPort";
045: private static final String MULTICAST_GROUP_ADDRESS = "multicastGroupAddress";
046: private static final String MULTICAST_PACKET_TTL = "timeToLive";
047: private static final int MAXIMUM_TTL = 255;
048:
049: /**
050: * @param properties implementation specific properties. These are configured as comma
051: * separated name value pairs in ehcache.xml
052: */
053: public CacheManagerPeerProvider createCachePeerProvider(
054: CacheManager cacheManager, Properties properties)
055: throws CacheException {
056: String peerDiscovery = PropertyUtil.extractAndLogProperty(
057: PEER_DISCOVERY, properties);
058: if (peerDiscovery == null
059: || peerDiscovery
060: .equalsIgnoreCase(AUTOMATIC_PEER_DISCOVERY)) {
061: try {
062: return createAutomaticallyConfiguredCachePeerProvider(
063: cacheManager, properties);
064: } catch (IOException e) {
065: throw new CacheException(
066: "Could not create CacheManagerPeerProvider. Initial cause was "
067: + e.getMessage(), e);
068: }
069: } else if (peerDiscovery
070: .equalsIgnoreCase(MANUALLY_CONFIGURED_PEER_DISCOVERY)) {
071: return createManuallyConfiguredCachePeerProvider(properties);
072: } else {
073: return null;
074: }
075: }
076:
077: /**
078: * peerDiscovery=manual, rmiUrls=//hostname:port/cacheName //hostname:port/cacheName //hostname:port/cacheName
079: */
080: protected CacheManagerPeerProvider createManuallyConfiguredCachePeerProvider(
081: Properties properties) {
082: String rmiUrls = PropertyUtil.extractAndLogProperty(RMI_URLS,
083: properties);
084: if (rmiUrls == null || rmiUrls.length() == 0) {
085: LOG
086: .info("Starting manual peer provider with empty list of peers. No replication will occur unless peers are added.");
087: rmiUrls = new String();
088: }
089: rmiUrls = rmiUrls.trim();
090: StringTokenizer stringTokenizer = new StringTokenizer(rmiUrls,
091: PayloadUtil.URL_DELIMITER);
092: RMICacheManagerPeerProvider rmiPeerProvider = new ManualRMICacheManagerPeerProvider();
093: while (stringTokenizer.hasMoreTokens()) {
094: String rmiUrl = stringTokenizer.nextToken();
095: rmiUrl = rmiUrl.trim();
096: rmiPeerProvider.registerPeer(rmiUrl);
097: if (LOG.isDebugEnabled()) {
098: LOG.debug("Registering peer " + rmiUrl);
099: }
100: }
101: return rmiPeerProvider;
102: }
103:
104: /**
105: * peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, multicastPacketTimeToLive=255
106: */
107: protected CacheManagerPeerProvider createAutomaticallyConfiguredCachePeerProvider(
108: CacheManager cacheManager, Properties properties)
109: throws IOException {
110: String groupAddressString = PropertyUtil.extractAndLogProperty(
111: MULTICAST_GROUP_ADDRESS, properties);
112: InetAddress groupAddress = InetAddress
113: .getByName(groupAddressString);
114: String multicastPortString = PropertyUtil
115: .extractAndLogProperty(MULTICAST_GROUP_PORT, properties);
116: Integer multicastPort = new Integer(multicastPortString);
117: String packetTimeToLiveString = PropertyUtil
118: .extractAndLogProperty(MULTICAST_PACKET_TTL, properties);
119: Integer timeToLive;
120: if (packetTimeToLiveString == null) {
121: timeToLive = new Integer(1);
122: LOG
123: .debug("No TTL set. Setting it to the default of 1, which means packets are limited to the same subnet.");
124: } else {
125: timeToLive = new Integer(packetTimeToLiveString);
126: if (timeToLive.intValue() < 0
127: || timeToLive.intValue() > MAXIMUM_TTL) {
128: throw new CacheException(
129: "The TTL must be set to a value between 0 and 255");
130: }
131: }
132: return new MulticastRMICacheManagerPeerProvider(cacheManager,
133: groupAddress, multicastPort, timeToLive);
134: }
135: }
|