1 25 package org.archive.io; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.NoSuchElementException ; 30 import java.util.concurrent.atomic.AtomicInteger ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 34 import org.apache.commons.pool.BasePoolableObjectFactory; 35 import org.apache.commons.pool.impl.FairGenericObjectPool; 36 import org.apache.commons.pool.impl.GenericObjectPool; 37 38 46 public abstract class WriterPool { 47 final Logger logger = Logger.getLogger(this.getClass().getName()); 48 49 52 final private AtomicInteger serialNo; 53 54 58 protected static final int NO_MAX_IDLE = -1; 59 60 65 private final int arbitraryRetryMax = 10; 66 67 70 public static final int DEFAULT_MAX_ACTIVE = 5; 71 72 75 public static final int DEFAULT_MAXIMUM_WAIT = 1000 * 60 * 5; 76 77 80 private GenericObjectPool pool = null; 81 82 86 private final WriterPoolSettings settings; 87 88 91 private WriterPool() { 92 this(null, null, null, -1, -1); 93 } 94 95 103 public WriterPool(final AtomicInteger serial, 104 final BasePoolableObjectFactory factory, 105 final WriterPoolSettings settings, 106 final int poolMaximumActive, final int poolMaximumWait) { 107 logger.info("Initial configuration:" + 108 " prefix=" + settings.getPrefix() + 109 ", suffix=" + settings.getSuffix() + 110 ", compress=" + settings.isCompressed() + 111 ", maxSize=" + settings.getMaxSize() + 112 ", maxActive=" + poolMaximumActive + 113 ", maxWait=" + poolMaximumWait); 114 this.settings = settings; 115 this.pool = new FairGenericObjectPool(factory, poolMaximumActive, 116 GenericObjectPool.WHEN_EXHAUSTED_BLOCK, poolMaximumWait, 117 NO_MAX_IDLE); 118 this.serialNo = serial; 119 } 120 121 133 public WriterPoolMember borrowFile() 134 throws IOException { 135 WriterPoolMember f = null; 136 for (int i = 0; f == null; i++) { 137 long waitStart = System.currentTimeMillis(); 138 try { 139 f = (WriterPoolMember)this.pool.borrowObject(); 140 if (logger.getLevel() == Level.FINE) { 141 logger.fine("Borrowed " + f + " (Pool State: " 142 + getPoolState(waitStart) + ")."); 143 } 144 } catch (NoSuchElementException e) { 145 logger.warning(e.getMessage() + ": Retry #" + i + " of " 148 + " max of " + arbitraryRetryMax 149 + ": NSEE Pool State: " + getPoolState(waitStart)); 150 if (i >= arbitraryRetryMax) { 151 logger.log(Level.SEVERE, 152 "maximum retries exceeded; rethrowing",e); 153 throw e; 154 } 155 } catch (Exception e) { 156 logger.severe(e.getMessage() + ": E Pool State: " + 158 getPoolState(waitStart)); 159 throw new IOException ("Failed getting writer from pool: " + 160 e.getMessage()); 161 } 162 } 163 return f; 164 } 165 166 170 public void returnFile(WriterPoolMember writer) 171 throws IOException { 172 try { 173 if (logger.getLevel() == Level.FINE) { 174 logger.fine("Returned " + writer); 175 } 176 this.pool.returnObject(writer); 177 } 178 catch(Exception e) 179 { 180 throw new IOException ("Failed restoring writer to pool: " + 181 e.getMessage()); 182 } 183 } 184 185 public void invalidateFile(WriterPoolMember f) 186 throws IOException { 187 try { 188 this.pool.invalidateObject(f); 189 } catch (Exception e) { 190 throw new IOException (e.getMessage()); 192 } 193 File file = f.getFile(); 196 file.renameTo(new File (file.getAbsoluteFile() + 197 WriterPoolMember.INVALID_SUFFIX)); 198 } 199 200 204 public int getNumActive() 205 throws UnsupportedOperationException { 206 return this.pool.getNumActive(); 207 } 208 209 213 public int getNumIdle() 214 throws UnsupportedOperationException { 215 return this.pool.getNumIdle(); 216 } 217 218 221 public void close() { 222 this.pool.clear(); 223 } 224 225 228 public WriterPoolSettings getSettings() { 229 return this.settings; 230 } 231 232 235 protected String getPoolState() { 236 return getPoolState(-1); 237 } 238 239 245 protected String getPoolState(long startTime) { 246 StringBuffer buffer = new StringBuffer ("Active "); 247 buffer.append(getNumActive()); 248 buffer.append(" of max "); 249 buffer.append(this.pool.getMaxActive()); 250 buffer.append(", idle "); 251 buffer.append(this.pool.getNumIdle()); 252 if (startTime != -1) { 253 buffer.append(", time "); 254 buffer.append(System.currentTimeMillis() - startTime); 255 buffer.append("ms of max "); 256 buffer.append(this.pool.getMaxWait()); 257 buffer.append("ms"); 258 } 259 return buffer.toString(); 260 } 261 262 268 public AtomicInteger getSerialNo() { 269 return serialNo; 270 } 271 } | Popular Tags |