1 /**
2  *  Copyright 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 package net.sf.ehcache.writer.writethrough;
17
18 import net.sf.ehcache.Cache;
19 import net.sf.ehcache.CacheEntry;
20 import net.sf.ehcache.CacheException;
21 import net.sf.ehcache.Element;
22 import net.sf.ehcache.writer.CacheWriter;
23 import net.sf.ehcache.writer.CacheWriterManager;
24 import net.sf.ehcache.writer.CacheWriterManagerException;
25
26 /**
27  * Implements a {@code WriterManager} that writes elements directly through to the underlying store.
28  *
29  * @author Geert Bevin
30  * @version $Id: WriteThroughManager.java 5631 2012-05-10 08:31:33Z teck $
31  */

32 public class WriteThroughManager implements CacheWriterManager {
33     private volatile Cache cache;
34
35     /**
36      * {@inheritDoc}
37      */

38     public void init(Cache cache) throws CacheException {
39         this.cache = cache;
40     }
41
42     /**
43      * {@inheritDoc}
44      */

45     public void put(Element element) throws CacheException {
46         try {
47             CacheWriter writer = cache.getRegisteredCacheWriter();
48             if (writer != null) {
49                 writer.write(element);
50             }
51         } catch (RuntimeException e) {
52             throw new CacheWriterManagerException(e);
53         }
54     }
55
56     /**
57      * {@inheritDoc}
58      */

59     public void remove(CacheEntry entry) throws CacheException {
60         try {
61             CacheWriter writer = cache.getRegisteredCacheWriter();
62             if (writer != null) {
63                 writer.delete(entry);
64             }
65         } catch (RuntimeException e) {
66             throw new CacheWriterManagerException(e);
67         }
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public void dispose() {
74         // nothing to do
75     }
76 }
77