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.Ehcache;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import java.net.MalformedURLException;
024: import java.rmi.Naming;
025: import java.rmi.NotBoundException;
026: import java.rmi.RemoteException;
027: import java.util.Collections;
028: import java.util.Date;
029: import java.util.HashMap;
030: import java.util.List;
031: import java.util.Map;
032:
033: /**
034: * A provider of Peer RMI addresses.
035: *
036: * @author Greg Luck
037: * @version $Id: RMICacheManagerPeerProvider.java 519 2007-07-27 07:11:45Z gregluck $
038: */
039: public abstract class RMICacheManagerPeerProvider implements
040: CacheManagerPeerProvider {
041:
042: private static final Log LOG = LogFactory
043: .getLog(RMICacheManagerPeerProvider.class.getName());
044:
045: /**
046: * Contains a RMI URLs of the form: "//" + hostName + ":" + port + "/" + cacheName;
047: */
048: protected final Map peerUrls = Collections
049: .synchronizedMap(new HashMap());
050:
051: /**
052: * The CacheManager this peer provider is associated with.
053: */
054: protected CacheManager cacheManager;
055:
056: /**
057: * Constructor
058: *
059: * @param cacheManager
060: */
061: public RMICacheManagerPeerProvider(CacheManager cacheManager) {
062: this .cacheManager = cacheManager;
063: }
064:
065: /**
066: * Empty constructor
067: */
068: public RMICacheManagerPeerProvider() {
069: //nothing to do
070: }
071:
072: /**
073: * {@inheritDoc}
074: */
075: public abstract void init();
076:
077: /**
078: * Register a new peer
079: *
080: * @param rmiUrl
081: */
082: public abstract void registerPeer(String rmiUrl);
083:
084: /**
085: * Gets the cache name out of the url
086: * @param rmiUrl
087: * @return the cache name as it would appear in ehcache.xml
088: */
089: static String extractCacheName(String rmiUrl) {
090: return rmiUrl.substring(rmiUrl.lastIndexOf('/') + 1);
091: }
092:
093: /**
094: * Unregisters a peer
095: *
096: * @param rmiUrl
097: */
098: public final synchronized void unregisterPeer(String rmiUrl) {
099: peerUrls.remove(rmiUrl);
100: }
101:
102: /**
103: * @return a list of {@link net.sf.ehcache.distribution.CachePeer} peers for the given cache, excluding the local peer.
104: */
105: public abstract List listRemoteCachePeers(Ehcache cache)
106: throws CacheException;
107:
108: /**
109: * Whether the entry should be considered stale. This will depend on the type of RMICacheManagerPeerProvider.
110: * <p/>
111: * @param date the date the entry was created
112: * @return true if stale
113: */
114: protected abstract boolean stale(Date date);
115:
116: /**
117: * The use of one-time registry creation and Naming.rebind should mean we can create as many listeneres as we like.
118: * They will simply replace the ones that were there.
119: */
120: public CachePeer lookupRemoteCachePeer(String url)
121: throws MalformedURLException, NotBoundException,
122: RemoteException {
123: return (CachePeer) Naming.lookup(url);
124: }
125:
126: /**
127: * Providers may be doing all sorts of exotic things and need to be able to clean up on dispose.
128: *
129: * @throws net.sf.ehcache.CacheException
130: */
131: public void dispose() throws CacheException {
132: //nothing to do.
133: }
134:
135: /**
136: * The cacheManager this provider is bound to
137: */
138: public final CacheManager getCacheManager() {
139: return cacheManager;
140: }
141:
142: }
|