View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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.CacheException;
21  import net.sf.ehcache.Ehcache;
22  
23  import java.util.ArrayList;
24  import java.util.Date;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
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   *
38   * @author Greg Luck
39   * @version $Id: ManualRMICacheManagerPeerProvider.html 13146 2011-08-01 17:12:39Z oletizi $
40   */
41  public final class ManualRMICacheManagerPeerProvider extends RMICacheManagerPeerProvider {
42  
43      private static final Logger LOG = LoggerFactory.getLogger(ManualRMICacheManagerPeerProvider.class.getName());
44  
45      /***
46       * Empty constructor.
47       */
48      public ManualRMICacheManagerPeerProvider() {
49          super();
50      }
51  
52      /***
53       * {@inheritDoc}
54       */
55      public final void init() {
56          //nothing to do here
57      }
58  
59      /***
60       * Time for a cluster to form. This varies considerably, depending on the implementation.
61       *
62       * @return the time in ms, for a cluster to form
63       */
64      public long getTimeForClusterToForm() {
65          return 0;
66      }
67  
68      /***
69       * Register a new peer.
70       *
71       * @param rmiUrl
72       */
73      public final synchronized void registerPeer(String rmiUrl) {
74          peerUrls.put(rmiUrl, new Date());
75      }
76  
77  
78      /***
79       * @return a list of {@link CachePeer} peers, excluding the local peer.
80       */
81      public final synchronized List listRemoteCachePeers(Ehcache cache) throws CacheException {
82          List remoteCachePeers = new ArrayList();
83          List staleList = new ArrayList();
84          for (Iterator iterator = peerUrls.keySet().iterator(); iterator.hasNext();) {
85              String rmiUrl = (String) iterator.next();
86              String rmiUrlCacheName = extractCacheName(rmiUrl);
87  
88              if (!rmiUrlCacheName.equals(cache.getName())) {
89                  continue;
90              }
91              Date date = (Date) peerUrls.get(rmiUrl);
92              if (!stale(date)) {
93                  CachePeer cachePeer = null;
94                  try {
95                      cachePeer = lookupRemoteCachePeer(rmiUrl);
96                      remoteCachePeers.add(cachePeer);
97                  } catch (Exception e) {
98                      if (LOG.isDebugEnabled()) {
99                          LOG.debug("Looking up rmiUrl " + rmiUrl + " through exception " + e.getMessage()
100                                 + ". This may be normal if a node has gone offline. Or it may indicate network connectivity"
101                                 + " difficulties", e);
102                     }
103                 }
104             } else {
105                     LOG.debug("rmiUrl {} should never be stale for a manually configured cluster.", rmiUrl);
106                 staleList.add(rmiUrl);
107             }
108 
109         }
110 
111         //Remove any stale remote peers. Must be done here to avoid concurrent modification exception.
112         for (int i = 0; i < staleList.size(); i++) {
113             String rmiUrl = (String) staleList.get(i);
114             peerUrls.remove(rmiUrl);
115         }
116         return remoteCachePeers;
117     }
118 
119 
120     /***
121      * Whether the entry should be considered stale.
122      * <p/>
123      * Manual RMICacheManagerProviders use a static list of urls and are therefore never stale.
124      *
125      * @param date the date the entry was created
126      * @return true if stale
127      */
128     protected final boolean stale(Date date) {
129         return false;
130     }
131 
132 }