1 package org.apache.commons.jcs.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 org.apache.commons.jcs.engine.behavior.ICache;
23 import org.apache.commons.jcs.engine.behavior.ICacheElement;
24 import org.apache.commons.jcs.engine.behavior.ICacheListener;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import java.io.IOException;
29
30 /**
31 * Used for Cache-to-Cache messaging purposes. These are used in the balking
32 * facades in the lateral and remote caches.
33 */
34 public class CacheAdaptor<K, V>
35 implements ICacheListener<K, V>
36 {
37 /** The logger */
38 private static final Log log = LogFactory.getLog( CacheAdaptor.class );
39
40 /** The cache we are adapting. */
41 private final ICache<K, V> cache;
42
43 /** The unique id of this listener. */
44 private long listenerId = 0;
45
46 /**
47 * Sets the listenerId attribute of the CacheAdaptor object
48 * <p>
49 * @param id
50 * The new listenerId value
51 * @throws IOException
52 */
53 @Override
54 public void setListenerId( long id )
55 throws IOException
56 {
57 this.listenerId = id;
58 log.debug( "listenerId = " + id );
59 }
60
61 /**
62 * Gets the listenerId attribute of the CacheAdaptor object
63 * <p>
64 * @return The listenerId value
65 * @throws IOException
66 */
67 @Override
68 public long getListenerId()
69 throws IOException
70 {
71 return this.listenerId;
72 }
73
74 /**
75 * Constructor for the CacheAdaptor object
76 * <p>
77 * @param cache
78 */
79 public CacheAdaptor( ICache<K, V> cache )
80 {
81 this.cache = cache;
82 }
83
84 /**
85 * Puts an item into the cache.
86 * <p>
87 * @param item
88 * @throws IOException
89 */
90 @Override
91 public void handlePut( ICacheElement<K, V> item )
92 throws IOException
93 {
94 try
95 {
96 cache.update( item );
97 }
98 catch ( Exception e )
99 {
100 // swallow
101 }
102 }
103
104 /**
105 * Removes an item.
106 * <p>
107 * @param cacheName
108 * @param key
109 * @throws IOException
110 */
111 @Override
112 public void handleRemove( String cacheName, K key )
113 throws IOException
114 {
115 cache.remove( key );
116 }
117
118 /**
119 * Clears the region.
120 * <p>
121 * @param cacheName
122 * @throws IOException
123 */
124 @Override
125 public void handleRemoveAll( String cacheName )
126 throws IOException
127 {
128 cache.removeAll();
129 }
130
131 /**
132 * Shutdown call.
133 * <p>
134 * @param cacheName
135 * @throws IOException
136 */
137 @Override
138 public void handleDispose( String cacheName )
139 throws IOException
140 {
141 cache.dispose();
142 }
143 }