View Javadoc
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  
21  import javax.cache.Cache;
22  import javax.cache.configuration.Factory;
23  import javax.cache.integration.CacheWriter;
24  import javax.cache.integration.CacheWriterException;
25  
26  import org.apache.commons.jcs3.jcache.extras.closeable.Closeables;
27  
28  import java.io.Closeable;
29  import java.io.IOException;
30  import java.util.Collection;
31  
32  public class CompositeCacheWriter<K, V> implements CacheWriter<K, V>, Closeable, Factory<CacheWriter<K, V>>
33  {
34      private final CacheWriter<K, V>[] writers;
35  
36      public CompositeCacheWriter(final CacheWriter<K, V>... writers)
37      {
38          this.writers = writers;
39      }
40  
41      @Override
42      public void write(final Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException
43      {
44          CacheWriterException e = null;
45          for (final CacheWriter<K, V> writer : writers)
46          {
47              try
48              {
49                  writer.write(entry);
50              }
51              catch (final CacheWriterException ex)
52              {
53                  if (e == null)
54                  {
55                      e = ex;
56                  }
57              }
58          }
59          if (e != null)
60          {
61              throw e;
62          }
63      }
64  
65      @Override
66      public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException
67      {
68          CacheWriterException e = null;
69          for (final CacheWriter<K, V> writer : writers)
70          {
71              try
72              {
73                  writer.writeAll(entries);
74              }
75              catch (final CacheWriterException ex)
76              {
77                  if (e == null)
78                  {
79                      e = ex;
80                  }
81              }
82          }
83          if (e != null)
84          {
85              throw e;
86          }
87      }
88  
89      @Override
90      public void delete(final Object key) throws CacheWriterException
91      {
92          CacheWriterException e = null;
93          for (final CacheWriter<K, V> writer : writers)
94          {
95              try
96              {
97                  writer.delete(key);
98              }
99              catch (final CacheWriterException ex)
100             {
101                 if (e == null)
102                 {
103                     e = ex;
104                 }
105             }
106         }
107         if (e != null)
108         {
109             throw e;
110         }
111     }
112 
113     @Override
114     public void deleteAll(final Collection<?> keys) throws CacheWriterException
115     {
116         CacheWriterException e = null;
117         for (final CacheWriter<K, V> writer : writers)
118         {
119             try
120             {
121                 writer.deleteAll(keys);
122             }
123             catch (final CacheWriterException ex)
124             {
125                 if (e == null)
126                 {
127                     e = ex;
128                 }
129             }
130         }
131         if (e != null)
132         {
133             throw e;
134         }
135     }
136 
137     @Override
138     public CacheWriter<K, V> create()
139     {
140         return this;
141     }
142 
143     @Override
144     public void close() throws IOException
145     {
146         Closeables.close(writers);
147     }
148 }