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
18 package net.sf.ehcache.distribution;
19
20 import net.sf.ehcache.Cache;
21 import net.sf.ehcache.CacheException;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.rmi.NotBoundException;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /**
32 * A provider of Peer RMI addresses based off manual configuration.
33 * <p/>
34 * Because there is no monitoring of whether a peer is actually there, the list of peers is dynamically
35 * looked up and verified each time a lookup request is made.
36 * <p/>
37 * @author Greg Luck
38 * @version $Id: ManualRMICacheManagerPeerProvider.java 52 2006-04-24 14:50:03Z gregluck $
39 */
40 public final class ManualRMICacheManagerPeerProvider extends RMICacheManagerPeerProvider {
41
42 private static final Log LOG = LogFactory.getLog(ManualRMICacheManagerPeerProvider.class.getName());
43
44 /**
45 * Empty constructor.
46 */
47 public ManualRMICacheManagerPeerProvider() {
48 super();
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 public final void init() {
55
56 }
57
58 /**
59 * Register a new peer.
60 *
61 * @param rmiUrl
62 */
63 public final synchronized void registerPeer(String rmiUrl) {
64 peerUrls.put(rmiUrl, new Date());
65 }
66
67
68 /**
69 * @return a list of {@link CachePeer} peers, excluding the local peer.
70 */
71 public final synchronized List listRemoteCachePeers(Cache cache) throws CacheException {
72 List remoteCachePeers = new ArrayList();
73 List staleList = new ArrayList();
74 for (Iterator iterator = peerUrls.keySet().iterator(); iterator.hasNext();) {
75 String rmiUrl = (String) iterator.next();
76 String rmiUrlCacheName = extractCacheName(rmiUrl);
77 try {
78 if (!rmiUrlCacheName.equals(cache.getName())) {
79 continue;
80 }
81 Date date = (Date) peerUrls.get(rmiUrl);
82 if (!stale(date)) {
83 CachePeer cachePeer = lookupRemoteCachePeer(rmiUrl);
84 remoteCachePeers.add(cachePeer);
85 } else {
86 if (LOG.isDebugEnabled()) {
87 LOG.debug("rmiUrl " + rmiUrl + " is stale. Either the remote peer is shutdown or the " +
88 "network connectivity has been interrupted. Will be removed from list of remote cache peers");
89 }
90 staleList.add(rmiUrl);
91 }
92 } catch (NotBoundException e) {
93 if (LOG.isDebugEnabled()) {
94 LOG.debug("No cache peer bound to URL at " + rmiUrl
95 + ". It must have disappeared since the last heartbeat");
96 }
97 } catch (Exception exception) {
98 LOG.error(exception.getMessage(), exception);
99 throw new CacheException("Unable to list remote cache peers. Error was " + exception.getMessage());
100 }
101 }
102
103
104 for (int i = 0; i < staleList.size(); i++) {
105 String rmiUrl = (String) staleList.get(i);
106 peerUrls.remove(rmiUrl);
107 }
108 return remoteCachePeers;
109 }
110
111
112 /**
113 * Whether the entry should be considered stale.
114 * <p/>
115 * Manual RMICacheManagerProviders use a static list of urls and are therefore never stale.
116 *
117 * @param date the date the entry was created
118 * @return true if stale
119 */
120 protected final boolean stale(Date date) {
121 return false;
122 }
123
124 }