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