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  package net.sf.ehcache.event;
18  
19  import java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ConcurrentMap;
21  
22  import net.sf.ehcache.CacheException;
23  import net.sf.ehcache.Ehcache;
24  import net.sf.ehcache.Element;
25  import net.sf.ehcache.Status;
26  import net.sf.ehcache.distribution.CacheReplicator;
27  
28  /***
29   * Creates a wrapper for sending out cache events through the Terracotta cluster
30   *
31   * @author Geert Bevin
32   * @version $Id: TerracottaCacheEventReplication.html 13146 2011-08-01 17:12:39Z oletizi $
33   */
34  public class TerracottaCacheEventReplication implements CacheReplicator {
35      private Status status = Status.STATUS_ALIVE;
36      
37      private final ConcurrentMap<Ehcache, CacheEventListener> replicators = new ConcurrentHashMap<Ehcache, CacheEventListener>();
38  
39      /***
40       * {@inheritDoc}
41       */
42      public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
43          createCacheEventReplicator(cache).notifyElementRemoved(cache, element);
44      }
45  
46      /***
47       * {@inheritDoc}
48       */
49      public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
50          createCacheEventReplicator(cache).notifyElementPut(cache, element);
51      }
52  
53      /***
54       * {@inheritDoc}
55       */
56      public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
57          createCacheEventReplicator(cache).notifyElementUpdated(cache, element);
58      }
59  
60      /***
61       * {@inheritDoc}
62       */
63      public void notifyElementExpired(Ehcache cache, Element element) {
64          createCacheEventReplicator(cache).notifyElementExpired(cache, element);
65      }
66  
67      /***
68       * {@inheritDoc}
69       */
70      public void notifyElementEvicted(Ehcache cache, Element element) {
71          createCacheEventReplicator(cache).notifyElementEvicted(cache, element);
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public void notifyRemoveAll(Ehcache cache) {
78          createCacheEventReplicator(cache).notifyRemoveAll(cache);
79      }
80  
81      private CacheEventListener createCacheEventReplicator(Ehcache cache) {
82          // the race is not a problem here, since the event replicator will only be created once in the clustered instance factory
83          // this replicator map is simply a locally cached version, several puts for the same cache will result in the same value being put
84          CacheEventListener replicator = replicators.get(cache);
85          if (null == replicator) {
86              replicator = cache.getCacheManager().createTerracottaEventReplicator(cache);
87              replicators.put(cache, replicator);
88          }
89          
90          return replicator;
91      }
92  
93      /***
94       * {@inheritDoc}
95       */
96      @Override
97      public TerracottaCacheEventReplication clone() throws CloneNotSupportedException {
98          return (TerracottaCacheEventReplication) super.clone();
99      }
100 
101     /***
102      * {@inheritDoc}
103      */
104     public boolean isReplicateUpdatesViaCopy() {
105         return false;
106     }
107 
108     /***
109      * {@inheritDoc}
110      */
111     public final boolean notAlive() {
112         return !alive();
113     }
114 
115     /***
116      * {@inheritDoc}
117      */
118     public final boolean alive() {
119         return status != null && (status.equals(Status.STATUS_ALIVE));
120     }
121 
122     /***
123      * {@inheritDoc}
124      */
125     public void dispose() {
126         status = Status.STATUS_SHUTDOWN;
127     }
128 }