001package org.apache.commons.jcs.engine; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.commons.jcs.engine.behavior.ICache; 023import org.apache.commons.jcs.engine.behavior.ICacheElement; 024import org.apache.commons.jcs.engine.behavior.ICacheListener; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027 028import java.io.IOException; 029 030/** 031 * Used for Cache-to-Cache messaging purposes. These are used in the balking 032 * facades in the lateral and remote caches. 033 */ 034public class CacheAdaptor<K, V> 035 implements ICacheListener<K, V> 036{ 037 /** The logger */ 038 private static final Log log = LogFactory.getLog( CacheAdaptor.class ); 039 040 /** The cache we are adapting. */ 041 private final ICache<K, V> cache; 042 043 /** The unique id of this listener. */ 044 private long listenerId = 0; 045 046 /** 047 * Sets the listenerId attribute of the CacheAdaptor object 048 * <p> 049 * @param id 050 * The new listenerId value 051 * @throws IOException 052 */ 053 @Override 054 public void setListenerId( long id ) 055 throws IOException 056 { 057 this.listenerId = id; 058 log.debug( "listenerId = " + id ); 059 } 060 061 /** 062 * Gets the listenerId attribute of the CacheAdaptor object 063 * <p> 064 * @return The listenerId value 065 * @throws IOException 066 */ 067 @Override 068 public long getListenerId() 069 throws IOException 070 { 071 return this.listenerId; 072 } 073 074 /** 075 * Constructor for the CacheAdaptor object 076 * <p> 077 * @param cache 078 */ 079 public CacheAdaptor( ICache<K, V> cache ) 080 { 081 this.cache = cache; 082 } 083 084 /** 085 * Puts an item into the cache. 086 * <p> 087 * @param item 088 * @throws IOException 089 */ 090 @Override 091 public void handlePut( ICacheElement<K, V> item ) 092 throws IOException 093 { 094 try 095 { 096 cache.update( item ); 097 } 098 catch ( Exception e ) 099 { 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}