001package org.apache.commons.jcs3.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 java.io.IOException;
023
024import org.apache.commons.jcs3.engine.behavior.ICache;
025import org.apache.commons.jcs3.engine.behavior.ICacheElement;
026import org.apache.commons.jcs3.engine.behavior.ICacheListener;
027
028/**
029 * Used for Cache-to-Cache messaging purposes. These are used in the balking
030 * facades in the lateral and remote caches.
031 */
032public class CacheAdaptor<K, V>
033    implements ICacheListener<K, V>
034{
035    /** The cache we are adapting. */
036    private final ICache<K, V> cache;
037
038    /** The unique id of this listener. */
039    private long listenerId;
040
041    /**
042     * Sets the listenerId attribute of the CacheAdaptor object
043     * <p>
044     * @param id
045     *            The new listenerId value
046     * @throws IOException
047     */
048    @Override
049    public void setListenerId( final long id )
050        throws IOException
051    {
052        this.listenerId = id;
053    }
054
055    /**
056     * Gets the listenerId attribute of the CacheAdaptor object
057     * <p>
058     * @return The listenerId value
059     * @throws IOException
060     */
061    @Override
062    public long getListenerId()
063        throws IOException
064    {
065        return this.listenerId;
066    }
067
068    /**
069     * Constructor for the CacheAdaptor object
070     * <p>
071     * @param cache
072     */
073    public CacheAdaptor( final ICache<K, V> cache )
074    {
075        this.cache = cache;
076    }
077
078    /**
079     * Puts an item into the cache.
080     * <p>
081     * @param item
082     * @throws IOException
083     */
084    @Override
085    public void handlePut( final ICacheElement<K, V> item )
086        throws IOException
087    {
088        try
089        {
090            cache.update( item );
091        }
092        catch ( final IOException e )
093        {
094            // swallow
095        }
096    }
097
098    /**
099     * 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}