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.Cache;
20 import net.sf.ehcache.CacheException;
21 import net.sf.ehcache.CacheManager;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.net.MalformedURLException;
26 import java.rmi.Naming;
27 import java.rmi.NotBoundException;
28 import java.rmi.RemoteException;
29 import java.util.Collections;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36 * A provider of Peer RMI addresses.
37 *
38 * @author Greg Luck
39 * @version $Id: RMICacheManagerPeerProvider.java 52 2006-04-24 14:50:03Z gregluck $
40 */
41 public abstract class RMICacheManagerPeerProvider implements CacheManagerPeerProvider {
42
43 private static final Log LOG = LogFactory.getLog(RMICacheManagerPeerProvider.class.getName());
44
45 /**
46 * Contains a RMI URLs of the form: "//" + hostName + ":" + port + "/" + cacheName;
47 */
48 protected final Map peerUrls = Collections.synchronizedMap(new HashMap());
49
50 /**
51 * The CacheManager this peer provider is associated with.
52 */
53 private CacheManager cacheManager;
54
55
56 /**
57 * Constructor
58 *
59 * @param cacheManager
60 */
61 public RMICacheManagerPeerProvider(CacheManager cacheManager) {
62 this.cacheManager = cacheManager;
63 }
64
65 /**
66 * Empty constructor
67 */
68 public RMICacheManagerPeerProvider() {
69
70 }
71
72
73 /**
74 * {@inheritDoc}
75 */
76 public abstract void init();
77
78
79 /**
80 * Register a new peer
81 *
82 * @param rmiUrl
83 */
84 public abstract void registerPeer(String rmiUrl);
85
86
87
88 /**
89 * Gets the cache name out of the url
90 * @param rmiUrl
91 * @return the cache name as it would appear in ehcache.xml
92 */
93 static String extractCacheName(String rmiUrl) {
94 return rmiUrl.substring(rmiUrl.lastIndexOf('/') + 1);
95 }
96
97 /**
98 * Unregisters a peer
99 *
100 * @param rmiUrl
101 */
102 public final synchronized void unregisterPeer(String rmiUrl) {
103 peerUrls.remove(rmiUrl);
104 }
105
106 /**
107 * @return a list of {@link net.sf.ehcache.distribution.CachePeer} peers for the given cache, excluding the local peer.
108 */
109 public abstract List listRemoteCachePeers(Cache cache) throws CacheException;
110
111 /**
112 * Whether the entry should be considered stale. This will depend on the type of RMICacheManagerPeerProvider.
113 * <p/>
114 * @param date the date the entry was created
115 * @return true if stale
116 */
117 protected abstract boolean stale(Date date);
118
119
120 /**
121 * The use of one-time registry creation and Naming.rebind should mean we can create as many listeneres as we like.
122 * They will simply replace the ones that were there.
123 */
124 public static CachePeer lookupRemoteCachePeer(String url) throws MalformedURLException, NotBoundException, RemoteException {
125 return (CachePeer) Naming.lookup(url);
126 }
127
128 /**
129 * Providers may be doing all sorts of exotic things and need to be able to clean up on dispose.
130 *
131 * @throws net.sf.ehcache.CacheException
132 */
133 public void dispose() throws CacheException {
134
135 }
136
137 /**
138 * The cacheManager this provider is bound to
139 */
140 public final CacheManager getCacheManager() {
141 return cacheManager;
142 }
143
144
145
146
147 }