1 17 package org.alfresco.repo.content.metadata; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.concurrent.locks.Lock ; 24 import java.util.concurrent.locks.ReadWriteLock ; 25 import java.util.concurrent.locks.ReentrantReadWriteLock ; 26 27 import org.alfresco.error.AlfrescoRuntimeException; 28 import org.alfresco.repo.content.MimetypeMap; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 41 public class MetadataExtracterRegistry 42 { 43 private static final Log logger = LogFactory.getLog(MetadataExtracterRegistry.class); 44 45 private List <MetadataExtracter> extracters; 46 private Map <String , MetadataExtracter> extracterCache; 47 48 private MimetypeMap mimetypeMap; 49 50 private Lock extracterCacheReadLock; 51 52 private Lock extracterCacheWriteLock; 53 54 public MetadataExtracterRegistry() 55 { 56 extracters = new ArrayList <MetadataExtracter>(10); 58 extracterCache = new HashMap <String , MetadataExtracter>(17); 59 60 ReadWriteLock extractionCacheLock = new ReentrantReadWriteLock (); 62 extracterCacheReadLock = extractionCacheLock.readLock(); 63 extracterCacheWriteLock = extractionCacheLock.writeLock(); 64 } 65 66 71 public void setMimetypeMap(MimetypeMap mimetypeMap) 72 { 73 this.mimetypeMap = mimetypeMap; 74 } 75 76 81 public void register(MetadataExtracter extracter) 82 { 83 if (logger.isDebugEnabled()) 84 { 85 logger.debug("Registering metadata extracter: " + extracter); 86 } 87 88 extracterCacheWriteLock.lock(); 89 try 90 { 91 extracters.add(extracter); 92 extracterCache.clear(); 93 } 94 finally 95 { 96 extracterCacheWriteLock.unlock(); 97 } 98 } 99 100 110 public MetadataExtracter getExtracter(String sourceMimetype) 111 { 112 if (!mimetypeMap.getMimetypes().contains(sourceMimetype)) 114 { 115 throw new AlfrescoRuntimeException("Unknown extraction source mimetype: " + sourceMimetype); 116 } 117 118 MetadataExtracter extracter = null; 119 extracterCacheReadLock.lock(); 120 try 121 { 122 if (extracterCache.containsKey(sourceMimetype)) 123 { 124 return extracterCache.get(sourceMimetype); 127 } 128 } 129 finally 130 { 131 extracterCacheReadLock.unlock(); 132 } 133 134 extracterCacheWriteLock.lock(); 138 try 139 { 140 extracter = findBestExtracter(sourceMimetype); 142 extracterCache.put(sourceMimetype, extracter); 144 return extracter; 145 } 146 finally 147 { 148 extracterCacheWriteLock.unlock(); 149 } 150 } 151 152 157 private MetadataExtracter findBestExtracter(String sourceMimetype) 158 { 159 double bestReliability = -1; 160 long bestTime = Long.MAX_VALUE; 161 logger.debug("Finding best extracter for " + sourceMimetype); 162 163 MetadataExtracter bestExtracter = null; 164 165 for (MetadataExtracter ext : extracters) 166 { 167 double r = ext.getReliability(sourceMimetype); 168 if (r <= 0.0) 169 { 170 continue; 172 } 173 else if (r == bestReliability) 174 { 175 long time = ext.getExtractionTime(); 176 if (time < bestTime) 177 { 178 bestExtracter = ext; 179 bestTime = time; 180 } 181 } 182 else if (r > bestReliability) 183 { 184 bestExtracter = ext; 185 bestReliability = r; 186 bestTime = ext.getExtractionTime(); 187 } 188 } 189 return bestExtracter; 190 } 191 } | Popular Tags |