1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.File ; 20 import java.util.LinkedList ; 21 import java.util.Map ; 22 23 import org.alfresco.error.AlfrescoRuntimeException; 24 import org.alfresco.repo.content.filestore.FileContentWriter; 25 import org.alfresco.service.cmr.repository.ContentIOException; 26 import org.alfresco.service.cmr.repository.ContentReader; 27 import org.alfresco.service.cmr.repository.ContentWriter; 28 import org.alfresco.util.TempFileProvider; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 41 public class CompoundContentTransformer implements ContentTransformer 42 { 43 private static final Log logger = LogFactory.getLog(CompoundContentTransformer.class); 44 45 46 private LinkedList <Transformation> chain; 47 48 private double reliability; 49 50 public CompoundContentTransformer() 51 { 52 chain = new LinkedList <Transformation>(); 53 reliability = 1.0; 54 } 55 56 65 public void addTransformation(String sourceMimetype, String targetMimetype, ContentTransformer transformer) 66 { 67 Transformation transformation = new Transformation( 69 transformer, 70 sourceMimetype, 71 targetMimetype); 72 chain.add(transformation); 74 double transformerReliability = transformer.getReliability(sourceMimetype, targetMimetype); 76 if (transformerReliability <= 0.0 || transformerReliability > 1.0) 77 { 78 throw new AlfrescoRuntimeException( 79 "Reliability of transformer must be between 0.0 and 1.0: \n" + 80 " transformer: " + transformer + "\n" + 81 " source: " + sourceMimetype + "\n" + 82 " target: " + targetMimetype + "\n" + 83 " reliability: " + transformerReliability); 84 } 85 this.reliability *= transformerReliability; 86 } 87 88 96 public double getReliability(String sourceMimetype, String targetMimetype) 97 { 98 if (chain.size() == 0) 99 { 100 return 0.0; 102 } 103 Transformation first = chain.getFirst(); 104 Transformation last = chain.getLast(); 105 if (!first.getSourceMimetype().equals(sourceMimetype) 106 && last.getTargetMimetype().equals(targetMimetype)) 107 { 108 return 0.0; 111 } 112 return reliability; 113 } 114 115 119 public long getTransformationTime() 120 { 121 long transformationTime = 0L; 122 for (Transformation transformation : chain) 123 { 124 ContentTransformer transformer = transformation.transformer; 125 transformationTime += transformer.getTransformationTime(); 126 } 127 return transformationTime; 128 } 129 130 133 public void transform(ContentReader reader, ContentWriter writer) throws ContentIOException 134 { 135 transform(reader, writer, null); 136 } 137 138 141 public void transform(ContentReader reader, ContentWriter writer, Map <String , Object > options) 142 throws ContentIOException 143 { 144 if (chain.size() == 0) 145 { 146 throw new AlfrescoRuntimeException("No transformations present in chain"); 147 } 148 149 String sourceMimetype = reader.getMimetype(); 151 String targetMimetype = writer.getMimetype(); 152 Transformation firstTransformation = chain.getFirst(); 153 Transformation lastTransformation = chain.getLast(); 154 if (!firstTransformation.getSourceMimetype().equals(sourceMimetype) 155 && lastTransformation.getTargetMimetype().equals(targetMimetype)) 156 { 157 throw new AlfrescoRuntimeException("Attempting to perform unreliable transformation: \n" + 158 " reader: " + reader + "\n" + 159 " writer: " + writer); 160 } 161 162 ContentReader currentReader = reader; 163 ContentWriter currentWriter = null; 164 int currentIndex = 0; 165 for (Transformation transformation : chain) 166 { 167 boolean last = (currentIndex == chain.size() - 1); 168 if (last) 169 { 170 currentWriter = writer; 172 } 173 else 174 { 175 File tempFile = TempFileProvider.createTempFile("transform", ".tmp"); 177 currentWriter = new FileContentWriter(tempFile); 178 currentWriter.setMimetype(transformation.getTargetMimetype()); 180 } 181 transformation.execute(currentReader, currentWriter, options); 183 if (!currentWriter.isClosed()) 184 { 185 throw new AlfrescoRuntimeException("Writer not closed by transformation: \n" + 186 " transformation: " + transformation + "\n" + 187 " writer: " + currentWriter); 188 } 189 if (!last) 192 { 193 currentReader = currentWriter.getReader(); 194 } 195 } 196 if (logger.isDebugEnabled()) 198 { 199 logger.debug("Executed complex transformation: \n" + 200 " chain: " + chain + "\n" + 201 " reader: " + reader + "\n" + 202 " writer: " + writer); 203 } 204 } 205 206 210 public static class Transformation extends ContentTransformerRegistry.TransformationKey 211 { 212 private ContentTransformer transformer; 213 public Transformation(ContentTransformer transformer, String sourceMimetype, String targetMimetype) 214 { 215 super(sourceMimetype, targetMimetype); 216 this.transformer = transformer; 217 } 218 219 227 public void execute(ContentReader reader, ContentWriter writer, Map <String , Object > options) 228 throws ContentIOException 229 { 230 String sourceMimetype = getSourceMimetype(); 231 String targetMimetype = getTargetMimetype(); 232 if (!sourceMimetype.equals(reader.getMimetype())) 234 { 235 throw new AlfrescoRuntimeException("The source mimetype doesn't match the reader's mimetype: \n" + 236 " source mimetype: " + sourceMimetype + "\n" + 237 " reader: " + reader); 238 } 239 if (!targetMimetype.equals(writer.getMimetype())) 240 { 241 throw new AlfrescoRuntimeException("The target mimetype doesn't match the writer's mimetype: \n" + 242 " target mimetype: " + targetMimetype + "\n" + 243 " writer: " + writer); 244 } 245 transformer.transform(reader, writer, options); 246 } 247 } 248 } | Popular Tags |