All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dihedron.patterns.cache.handlers.MultiCacheMissHandler Maven / Gradle / Ivy

Go to download

Base set of functionalities, including simple utility classes and more complex patterns.

The newest version!
/**
 * Copyright (c) 2012-2014, Andrea Funto'. All rights reserved. See LICENSE for details.
 */ 


package org.dihedron.patterns.cache.handlers;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.dihedron.core.License;
import org.dihedron.patterns.cache.CacheException;
import org.dihedron.patterns.cache.CacheMissHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles multiple cache miss handlers at one.
 * 
 * @author Andrea Funto'
 */
@License
public class MultiCacheMissHandler implements CacheMissHandler {

	/** 
	 * The logger. 
	 */
	private static final Logger logger = LoggerFactory.getLogger(MultiCacheMissHandler.class);
	
	/** 
	 * The list of handlers. 
	 */
	private List handlers;
	
	/**
	 * Constructor.
	 */
	public MultiCacheMissHandler() {
		logger.debug("instantiating empty multi-provider CacheMissHandler");
		this.handlers = new ArrayList();
	}
	
	/**
	 * Constructor.
	 * 
	 * @param handlers
	 *   a list of CacheMissHandlers.
	 */
	public MultiCacheMissHandler(List handlers) {
		if(handlers != null) {
			logger.debug("instantiating multi-provider CacheMissHandler, initial list has {} elements", handlers.size());
			this.handlers = new ArrayList(handlers);
		} else {
			logger.debug("instantiating empty multi-provider CacheMissHandler");
			this.handlers = new ArrayList();
		}
	}

	/**
	 * Constructor.
	 * 
	 * @param array
	 *   an array of CacheMissHandlers.
	 */
	public MultiCacheMissHandler(CacheMissHandler [] array) {
		logger.debug("instantiating multi-provider CacheMissHandler");
		this.handlers = new ArrayList();
		// copy handlers into array
		if(array != null) {
			logger.debug("adding {} elements to handlers", array.length);
			for(CacheMissHandler handler : array) {
				logger.debug("adding handler of class '{}'", handler.getClass());
				this.handlers.add(handler);
			}
		}
	}
	
	/**
	 * Appends a CacheMissHandler to the internal list of handlers.
	 * 
	 * @param handler
	 *   the CacheMissHandler to be added.
	 */
	public void addHandler(CacheMissHandler handler) {
		if(handler != null) {
			logger.debug("adding handler of class '{}'", handler.getClass());
			handlers.add(handler);
		}
	}
	
	/**
	 * Attempts a resource retrieval from each of the provided 
	 * CacheMissHandlers; as soon as one succeeds, it returns it as
	 * an InputStream.
	 * 
	 * @return
	 *   the resource as an InputStream, as soon as any of the 
	 *   CacheMissHandlers succeeds, null otherwise.
	 */
	public InputStream getAsStream() throws CacheException {
		List exceptions = new ArrayList(); 
		for (CacheMissHandler handler : handlers) {
			try {
				logger.debug("trying handler of class '{}'", handler.getClass());
				InputStream stream = handler.getAsStream();
				if(stream != null) {
					logger.debug("success, returning stream");
					return stream;
				}
			} catch(Exception e) {
				logger.error("error during resource retrieval", e);
				exceptions.add(e);
			}
		}
		
		if(!exceptions.isEmpty()) {
			logger.warn("failure, resource not found, and {} exceptions occurred", exceptions.size());
			throw new CacheException("Resource not found", exceptions);
		}
		logger.warn("failure, resource not found");
		return null;
	}	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy