
org.dihedron.patterns.cache.handlers.MultiCacheMissHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dihedron-patterns Show documentation
Show all versions of dihedron-patterns Show documentation
b Library of more complex base functionalities (micro-patterns).
The newest version!
/**
* Copyright (c) 2012-2014, Andrea Funto'. All rights reserved.
*
* This file is part of the Dihedron Common Utilities library ("Commons").
*
* "Commons" is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* "Commons" is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with "Commons". If not, see .
*/
package org.dihedron.patterns.cache.handlers;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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'
*/
public class MultiCacheMissHandler implements CacheMissHandler {
/**
* The logger.
*/
private static 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 CacheMissHandler
s.
*/
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 CacheMissHandler
s.
*/
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
* CacheMissHandler
s; as soon as one succeeds, it returns it as
* an InputStream
.
*
* @return
* the resource as an InputStream
, as soon as any of the
* CacheMissHandler
s 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.size() > 0) {
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