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.jcs3.jcache.extras.writer; 020 021import javax.cache.Cache; 022import javax.cache.configuration.Factory; 023import javax.cache.integration.CacheWriter; 024import javax.cache.integration.CacheWriterException; 025 026import org.apache.commons.jcs3.jcache.extras.closeable.Closeables; 027 028import java.io.Closeable; 029import java.io.IOException; 030import java.util.Collection; 031 032public class CompositeCacheWriter<K, V> implements CacheWriter<K, V>, Closeable, Factory<CacheWriter<K, V>> 033{ 034 private final CacheWriter<K, V>[] writers; 035 036 public CompositeCacheWriter(final CacheWriter<K, V>... writers) 037 { 038 this.writers = writers; 039 } 040 041 @Override 042 public void write(final Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException 043 { 044 CacheWriterException e = null; 045 for (final CacheWriter<K, V> writer : writers) 046 { 047 try 048 { 049 writer.write(entry); 050 } 051 catch (final CacheWriterException ex) 052 { 053 if (e == null) 054 { 055 e = ex; 056 } 057 } 058 } 059 if (e != null) 060 { 061 throw e; 062 } 063 } 064 065 @Override 066 public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException 067 { 068 CacheWriterException e = null; 069 for (final CacheWriter<K, V> writer : writers) 070 { 071 try 072 { 073 writer.writeAll(entries); 074 } 075 catch (final CacheWriterException ex) 076 { 077 if (e == null) 078 { 079 e = ex; 080 } 081 } 082 } 083 if (e != null) 084 { 085 throw e; 086 } 087 } 088 089 @Override 090 public void delete(final Object key) throws CacheWriterException 091 { 092 CacheWriterException e = null; 093 for (final CacheWriter<K, V> writer : writers) 094 { 095 try 096 { 097 writer.delete(key); 098 } 099 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}