001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.jcs.jcache.extras.writer;
020
021import org.apache.commons.jcs.jcache.extras.closeable.Closeables;
022
023import javax.cache.Cache;
024import javax.cache.configuration.Factory;
025import javax.cache.integration.CacheWriter;
026import javax.cache.integration.CacheWriterException;
027import java.io.Closeable;
028import java.io.IOException;
029import java.util.Collection;
030
031public class CompositeCacheWriter<K, V> implements CacheWriter<K, V>, Closeable, Factory<CacheWriter<K, V>>
032{
033    private final CacheWriter<K, V>[] writers;
034
035    public CompositeCacheWriter(final CacheWriter<K, V>... writers)
036    {
037        this.writers = writers;
038    }
039
040    @Override
041    public void write(final Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException
042    {
043        CacheWriterException e = null;
044        for (final CacheWriter<K, V> writer : writers)
045        {
046            try
047            {
048                writer.write(entry);
049            }
050            catch (final CacheWriterException ex)
051            {
052                if (e == null)
053                {
054                    e = ex;
055                }
056            }
057        }
058        if (e != null)
059        {
060            throw e;
061        }
062    }
063
064    @Override
065    public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException
066    {
067        CacheWriterException e = null;
068        for (final CacheWriter<K, V> writer : writers)
069        {
070            try
071            {
072                writer.writeAll(entries);
073            }
074            catch (final CacheWriterException ex)
075            {
076                if (e == null)
077                {
078                    e = ex;
079                }
080            }
081        }
082        if (e != null)
083        {
084            throw e;
085        }
086    }
087
088    @Override
089    public void delete(final Object key) throws CacheWriterException
090    {
091        CacheWriterException e = null;
092        for (final CacheWriter<K, V> writer : writers)
093        {
094            try
095            {
096                writer.delete(key);
097            }
098            catch (final CacheWriterException ex)
099            {
100                if (e == null)
101                {
102                    e = ex;
103                }
104            }
105        }
106        if (e != null)
107        {
108            throw e;
109        }
110    }
111
112    @Override
113    public void deleteAll(final Collection<?> keys) throws CacheWriterException
114    {
115        CacheWriterException e = null;
116        for (final CacheWriter<K, V> writer : writers)
117        {
118            try
119            {
120                writer.deleteAll(keys);
121            }
122            catch (final CacheWriterException ex)
123            {
124                if (e == null)
125                {
126                    e = ex;
127                }
128            }
129        }
130        if (e != null)
131        {
132            throw e;
133        }
134    }
135
136    @Override
137    public CacheWriter<K, V> create()
138    {
139        return this;
140    }
141
142    @Override
143    public void close() throws IOException
144    {
145        Closeables.close(writers);
146    }
147}