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.Ehcache;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import java.util.ArrayList;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: /**
028: * A provider of Peer RMI addresses based off manual configuration.
029: * <p/>
030: * Because there is no monitoring of whether a peer is actually there, the list of peers is dynamically
031: * looked up and verified each time a lookup request is made.
032: * <p/>
033: *
034: * @author Greg Luck
035: * @version $Id: ManualRMICacheManagerPeerProvider.java 552 2007-08-26 04:20:49Z gregluck $
036: */
037: public final class ManualRMICacheManagerPeerProvider extends
038: RMICacheManagerPeerProvider {
039:
040: private static final Log LOG = LogFactory
041: .getLog(ManualRMICacheManagerPeerProvider.class.getName());
042:
043: /**
044: * Empty constructor.
045: */
046: public ManualRMICacheManagerPeerProvider() {
047: super ();
048: }
049:
050: /**
051: * {@inheritDoc}
052: */
053: public final void init() {
054: //nothing to do here
055: }
056:
057: /**
058: * Time for a cluster to form. This varies considerably, depending on the implementation.
059: *
060: * @return the time in ms, for a cluster to form
061: */
062: public long getTimeForClusterToForm() {
063: return 0;
064: }
065:
066: /**
067: * Register a new peer.
068: *
069: * @param rmiUrl
070: */
071: public final synchronized void registerPeer(String rmiUrl) {
072: peerUrls.put(rmiUrl, new Date());
073: }
074:
075: /**
076: * @return a list of {@link CachePeer} peers, excluding the local peer.
077: */
078: public final synchronized List listRemoteCachePeers(Ehcache cache)
079: throws CacheException {
080: List remoteCachePeers = new ArrayList();
081: List staleList = new ArrayList();
082: for (Iterator iterator = peerUrls.keySet().iterator(); iterator
083: .hasNext();) {
084: String rmiUrl = (String) iterator.next();
085: String rmiUrlCacheName = extractCacheName(rmiUrl);
086:
087: if (!rmiUrlCacheName.equals(cache.getName())) {
088: continue;
089: }
090: Date date = (Date) peerUrls.get(rmiUrl);
091: if (!stale(date)) {
092: CachePeer cachePeer = null;
093: try {
094: cachePeer = lookupRemoteCachePeer(rmiUrl);
095: remoteCachePeers.add(cachePeer);
096: } catch (Exception e) {
097: if (LOG.isDebugEnabled()) {
098: LOG
099: .debug(
100: "Looking up rmiUrl "
101: + rmiUrl
102: + " through exception "
103: + e.getMessage()
104: + ". This may be normal if a node has gone offline. Or it may indicate network connectivity"
105: + " difficulties", e);
106: }
107: }
108: } else {
109: if (LOG.isDebugEnabled()) {
110: LOG
111: .debug("rmiUrl "
112: + rmiUrl
113: + " should never be stale for a manually configured cluster.");
114: }
115: staleList.add(rmiUrl);
116: }
117:
118: }
119:
120: //Remove any stale remote peers. Must be done here to avoid concurrent modification exception.
121: for (int i = 0; i < staleList.size(); i++) {
122: String rmiUrl = (String) staleList.get(i);
123: peerUrls.remove(rmiUrl);
124: }
125: return remoteCachePeers;
126: }
127:
128: /**
129: * Whether the entry should be considered stale.
130: * <p/>
131: * Manual RMICacheManagerProviders use a static list of urls and are therefore never stale.
132: *
133: * @param date the date the entry was created
134: * @return true if stale
135: */
136: protected final boolean stale(Date date) {
137: return false;
138: }
139:
140: }
|