CompositeCacheWriter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.jcs3.jcache.extras.writer;

  20. import javax.cache.Cache;
  21. import javax.cache.configuration.Factory;
  22. import javax.cache.integration.CacheWriter;
  23. import javax.cache.integration.CacheWriterException;

  24. import org.apache.commons.jcs3.jcache.extras.closeable.Closeables;

  25. import java.io.Closeable;
  26. import java.io.IOException;
  27. import java.util.Collection;

  28. public class CompositeCacheWriter<K, V> implements CacheWriter<K, V>, Closeable, Factory<CacheWriter<K, V>>
  29. {
  30.     private final CacheWriter<K, V>[] writers;

  31.     public CompositeCacheWriter(final CacheWriter<K, V>... writers)
  32.     {
  33.         this.writers = writers;
  34.     }

  35.     @Override
  36.     public void write(final Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException
  37.     {
  38.         CacheWriterException e = null;
  39.         for (final CacheWriter<K, V> writer : writers)
  40.         {
  41.             try
  42.             {
  43.                 writer.write(entry);
  44.             }
  45.             catch (final CacheWriterException ex)
  46.             {
  47.                 if (e == null)
  48.                 {
  49.                     e = ex;
  50.                 }
  51.             }
  52.         }
  53.         if (e != null)
  54.         {
  55.             throw e;
  56.         }
  57.     }

  58.     @Override
  59.     public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException
  60.     {
  61.         CacheWriterException e = null;
  62.         for (final CacheWriter<K, V> writer : writers)
  63.         {
  64.             try
  65.             {
  66.                 writer.writeAll(entries);
  67.             }
  68.             catch (final CacheWriterException ex)
  69.             {
  70.                 if (e == null)
  71.                 {
  72.                     e = ex;
  73.                 }
  74.             }
  75.         }
  76.         if (e != null)
  77.         {
  78.             throw e;
  79.         }
  80.     }

  81.     @Override
  82.     public void delete(final Object key) throws CacheWriterException
  83.     {
  84.         CacheWriterException e = null;
  85.         for (final CacheWriter<K, V> writer : writers)
  86.         {
  87.             try
  88.             {
  89.                 writer.delete(key);
  90.             }
  91.             catch (final CacheWriterException ex)
  92.             {
  93.                 if (e == null)
  94.                 {
  95.                     e = ex;
  96.                 }
  97.             }
  98.         }
  99.         if (e != null)
  100.         {
  101.             throw e;
  102.         }
  103.     }

  104.     @Override
  105.     public void deleteAll(final Collection<?> keys) throws CacheWriterException
  106.     {
  107.         CacheWriterException e = null;
  108.         for (final CacheWriter<K, V> writer : writers)
  109.         {
  110.             try
  111.             {
  112.                 writer.deleteAll(keys);
  113.             }
  114.             catch (final CacheWriterException ex)
  115.             {
  116.                 if (e == null)
  117.                 {
  118.                     e = ex;
  119.                 }
  120.             }
  121.         }
  122.         if (e != null)
  123.         {
  124.             throw e;
  125.         }
  126.     }

  127.     @Override
  128.     public CacheWriter<K, V> create()
  129.     {
  130.         return this;
  131.     }

  132.     @Override
  133.     public void close() throws IOException
  134.     {
  135.         Closeables.close(writers);
  136.     }
  137. }