1 17 package org.alfresco.repo.content.transform; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.service.cmr.repository.ContentAccessor; 26 import org.alfresco.service.cmr.repository.ContentIOException; 27 import org.alfresco.service.cmr.repository.ContentReader; 28 import org.alfresco.service.cmr.repository.ContentWriter; 29 import org.alfresco.service.cmr.repository.MimetypeService; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 42 public abstract class AbstractContentTransformer implements ContentTransformer 43 { 44 private static final Log logger = LogFactory.getLog(AbstractContentTransformer.class); 45 46 private MimetypeService mimetypeService; 47 private ContentTransformerRegistry registry; 48 private List <ContentTransformerRegistry.TransformationKey> explicitTransformations; 49 private double averageTime = 0.0; 50 private long count = 0L; 51 52 55 protected AbstractContentTransformer() 56 { 57 averageTime = 0.0; 58 explicitTransformations = new ArrayList <ContentTransformerRegistry.TransformationKey>(0); 59 } 60 61 66 public void setRegistry(ContentTransformerRegistry registry) 67 { 68 this.registry = registry; 69 } 70 71 76 public void setMimetypeService(MimetypeService mimetypeService) 77 { 78 this.mimetypeService = mimetypeService; 79 } 80 81 84 protected MimetypeService getMimetypeService() 85 { 86 return mimetypeService; 87 } 88 89 92 protected List <ContentTransformerRegistry.TransformationKey> getExplicitTransformations() 93 { 94 return explicitTransformations; 95 } 96 97 103 public void setExplicitTransformations(List <ContentTransformerRegistry.TransformationKey> explicitTransformations) 104 { 105 this.explicitTransformations = explicitTransformations; 106 } 107 108 @Override 109 public String toString() 110 { 111 StringBuilder sb = new StringBuilder (); 112 sb.append(this.getClass().getSimpleName()) 113 .append("[ average=").append((long)averageTime).append("ms") 114 .append("]"); 115 return sb.toString(); 116 } 117 118 122 public void register() 123 { 124 if (registry == null) 125 { 126 if (registry == null) 127 { 128 logger.warn("Property 'registry' has not been set. Ignoring auto-registration: \n" + 129 " transformer: " + this); 130 return; 131 } 132 return; 133 } 134 if (explicitTransformations != null) 136 { 137 for (ContentTransformerRegistry.TransformationKey key : explicitTransformations) 138 { 139 registry.addExplicitTransformer(key, this); 140 } 141 } 142 registry.addTransformer(this); 144 } 145 146 153 protected String getMimetype(ContentAccessor content) 154 { 155 String mimetype = content.getMimetype(); 156 if (mimetype == null) 157 { 158 throw new AlfrescoRuntimeException("Mimetype is mandatory for transformation: " + content); 159 } 160 return mimetype; 162 } 163 164 171 protected void checkReliability(ContentReader reader, ContentWriter writer) 172 { 173 String sourceMimetype = getMimetype(reader); 174 String targetMimetype = getMimetype(writer); 175 double reliability = getReliability(sourceMimetype, targetMimetype); 176 if (reliability <= 0.0) 177 { 178 throw new AlfrescoRuntimeException("Zero scoring transformation attempted: \n" + 179 " reader: " + reader + "\n" + 180 " writer: " + writer); 181 } 182 } 184 185 195 protected abstract void transformInternal( 196 ContentReader reader, 197 ContentWriter writer, 198 Map <String , Object > options) throws Exception ; 199 200 204 public final void transform(ContentReader reader, ContentWriter writer) throws ContentIOException 205 { 206 transform(reader, writer, null); 207 } 208 209 222 public final void transform( 223 ContentReader reader, 224 ContentWriter writer, 225 Map <String , Object > options) throws ContentIOException 226 { 227 long before = System.currentTimeMillis(); 229 230 checkReliability(reader, writer); 232 233 if (options == null) 235 { 236 options = Collections.emptyMap(); 237 } 238 239 try 240 { 241 transformInternal(reader, writer, options); 242 } 243 catch (Throwable e) 244 { 245 recordTime(10000); 250 throw new ContentIOException("Content conversion failed: \n" + 251 " reader: " + reader + "\n" + 252 " writer: " + writer + "\n" + 253 " options: " + options, 254 e); 255 } 256 finally 257 { 258 if (!reader.isClosed()) 260 { 261 logger.error("Content reader not closed by transformer: \n" + 262 " reader: " + reader + "\n" + 263 " transformer: " + this); 264 } 265 if (!writer.isClosed()) 266 { 267 logger.error("Content writer not closed by transformer: \n" + 268 " writer: " + writer + "\n" + 269 " transformer: " + this); 270 } 271 } 272 273 long after = System.currentTimeMillis(); 275 recordTime(after - before); 276 277 if (logger.isDebugEnabled()) 279 { 280 logger.debug("Completed transformation: \n" + 281 " reader: " + reader + "\n" + 282 " writer: " + writer + "\n" + 283 " options: " + options + "\n" + 284 " transformer: " + this); 285 } 286 } 287 288 291 public synchronized long getTransformationTime() 292 { 293 return (long) averageTime; 294 } 295 296 308 protected final synchronized void recordTime(long transformationTime) 309 { 310 if (count == Long.MAX_VALUE) 311 { 312 count /= 2L; 315 } 316 count++; 318 double diffTime = ((double) transformationTime) - averageTime; 319 averageTime += diffTime / (double) count; 320 } 321 } 322 | Popular Tags |