1 package org.apache.commons.jcs3.engine;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23
24 import org.apache.commons.jcs3.engine.behavior.ICache;
25 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
26 import org.apache.commons.jcs3.engine.behavior.ICacheListener;
27
28 /**
29 * Used for Cache-to-Cache messaging purposes. These are used in the balking
30 * facades in the lateral and remote caches.
31 */
32 public class CacheAdaptor<K, V>
33 implements ICacheListener<K, V>
34 {
35 /** The cache we are adapting. */
36 private final ICache<K, V> cache;
37
38 /** The unique id of this listener. */
39 private long listenerId;
40
41 /**
42 * Sets the listenerId attribute of the CacheAdaptor object
43 * <p>
44 * @param id
45 * The new listenerId value
46 * @throws IOException
47 */
48 @Override
49 public void setListenerId( final long id )
50 throws IOException
51 {
52 this.listenerId = id;
53 }
54
55 /**
56 * Gets the listenerId attribute of the CacheAdaptor object
57 * <p>
58 * @return The listenerId value
59 * @throws IOException
60 */
61 @Override
62 public long getListenerId()
63 throws IOException
64 {
65 return this.listenerId;
66 }
67
68 /**
69 * Constructor for the CacheAdaptor object
70 * <p>
71 * @param cache
72 */
73 public CacheAdaptor( final ICache<K, V> cache )
74 {
75 this.cache = cache;
76 }
77
78 /**
79 * Puts an item into the cache.
80 * <p>
81 * @param item
82 * @throws IOException
83 */
84 @Override
85 public void handlePut( final ICacheElement<K, V> item )
86 throws IOException
87 {
88 try
89 {
90 cache.update( item );
91 }
92 catch ( final IOException e )
93 {
94 // swallow
95 }
96 }
97
98 /**
99 * Removes an item.
100 * <p>
101 * @param cacheName
102 * @param key
103 * @throws IOException
104 */
105 @Override
106 public void handleRemove( final String cacheName, final K key )
107 throws IOException
108 {
109 cache.remove( key );
110 }
111
112 /**
113 * Clears the region.
114 * <p>
115 * @param cacheName
116 * @throws IOException
117 */
118 @Override
119 public void handleRemoveAll( final String cacheName )
120 throws IOException
121 {
122 cache.removeAll();
123 }
124
125 /**
126 * Shutdown call.
127 * <p>
128 * @param cacheName
129 * @throws IOException
130 */
131 @Override
132 public void handleDispose( final String cacheName )
133 throws IOException
134 {
135 cache.dispose();
136 }
137 }