001package co.codewizards.cloudstore.core.oio; 002 003import java.util.Iterator; 004import java.util.ServiceLoader; 005 006import org.slf4j.Logger; 007import org.slf4j.LoggerFactory; 008 009 010/** 011 * @author Sebastian Schefczyk 012 */ 013public class OioRegistry { 014 015 private static final Logger logger = LoggerFactory.getLogger(OioRegistry.class); 016 017 private final FileFactory fileFactory; 018 019 private static class OioProviderHolder { 020 public static final OioRegistry instance = new OioRegistry(); 021 } 022 023 private OioRegistry() { 024 this.fileFactory = getPrioritizedService(FileFactory.class); 025 logger.info("Preferred implementation '{}' for fileFactory", this.fileFactory.getClass().getSimpleName()); 026 } 027 028 private <N extends FileFactory> N getPrioritizedService(final Class<N> n) { 029 final Iterator<N> it = ServiceLoader.load(n).iterator(); 030 N highPrio = null; 031 032 while (it.hasNext()) { 033 final N i = it.next(); 034 if (highPrio == null) 035 highPrio = i; 036 else if (highPrio.getPriority() < i.getPriority()) 037 highPrio = i; 038 } 039 040 if (highPrio == null) 041 throw new IllegalStateException("Could not get an implementation for interface: " + n.getName()); 042 043 return highPrio; 044 } 045 046 public static OioRegistry getInstance() { 047 return OioProviderHolder.instance; 048 } 049 050 public FileFactory getFileFactory() { 051 return fileFactory; 052 } 053 054}