1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.File ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.repo.content.transform.ContentTransformerRegistry.TransformationKey; 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.util.TempFileProvider; 30 import org.alfresco.util.exec.RuntimeExec; 31 import org.alfresco.util.exec.RuntimeExec.ExecutionResult; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 62 public class RuntimeExecutableContentTransformer extends AbstractContentTransformer 63 { 64 public static final String VAR_SOURCE = "source"; 65 public static final String VAR_TARGET = "target"; 66 67 private static Log logger = LogFactory.getLog(RuntimeExecutableContentTransformer.class); 68 69 private boolean available; 70 private RuntimeExec checkCommand; 71 private RuntimeExec transformCommand; 72 73 public RuntimeExecutableContentTransformer() 74 { 75 } 76 77 @Override 78 public String toString() 79 { 80 StringBuilder sb = new StringBuilder (); 81 sb.append(this.getClass().getSimpleName()) 82 .append("[ transform=").append(transformCommand).append("\n") 83 .append("]"); 84 return sb.toString(); 85 } 86 87 96 public void setCheckCommand(RuntimeExec checkCommand) 97 { 98 this.checkCommand = checkCommand; 99 } 100 101 106 public void setTransformCommand(RuntimeExec transformCommand) 107 { 108 this.transformCommand = transformCommand; 109 } 110 111 117 public void setErrorCodes(String errCodesStr) 118 { 119 throw new AlfrescoRuntimeException("content.runtime_exec.property_moved"); 120 } 121 122 127 @Override 128 public void register() 129 { 130 if (transformCommand == null) 131 { 132 throw new AlfrescoRuntimeException("Mandatory property 'transformCommand' not set"); 133 } 134 135 if (checkCommand != null) 137 { 138 ExecutionResult result = checkCommand.execute(); 139 available = result.getSuccess(); 141 } 142 else 143 { 144 available = true; 146 } 147 super.register(); 149 } 150 151 159 public double getReliability(String sourceMimetype, String targetMimetype) 160 { 161 if (!available) 162 { 163 return 0.0; 164 } 165 TransformationKey transformationKey = new TransformationKey(sourceMimetype, targetMimetype); 167 List <TransformationKey> explicitTransformations = getExplicitTransformations(); 168 if (explicitTransformations.size() == 0) 169 { 170 logger.warn( 171 "Property 'explicitTransformations' should be set to enable this transformer: \n" + 172 " transformer: " + this); 173 } 174 if (explicitTransformations.contains(transformationKey)) 175 { 176 return 1.0; 177 } 178 else 179 { 180 return 0.0; 181 } 182 } 183 184 190 protected final void transformInternal( 191 ContentReader reader, 192 ContentWriter writer, 193 Map <String , Object > options) throws Exception 194 { 195 String sourceMimetype = getMimetype(reader); 197 String targetMimetype = getMimetype(writer); 198 199 String sourceExtension = getMimetypeService().getExtension(sourceMimetype); 201 String targetExtension = getMimetypeService().getExtension(targetMimetype); 202 if (sourceExtension == null || targetExtension == null) 203 { 204 throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" + 205 " source mimetype: " + sourceMimetype + "\n" + 206 " source extension: " + sourceExtension + "\n" + 207 " target mimetype: " + targetMimetype + "\n" + 208 " target extension: " + targetExtension); 209 } 210 211 File sourceFile = TempFileProvider.createTempFile( 213 getClass().getSimpleName() + "_source_", 214 "." + sourceExtension); 215 File targetFile = TempFileProvider.createTempFile( 216 getClass().getSimpleName() + "_target_", 217 "." + targetExtension); 218 219 Map <String , String > properties = new HashMap <String , String >(5); 220 for (Map.Entry <String , Object > entry : options.entrySet()) 222 { 223 String key = entry.getKey(); 224 Object value = entry.getValue(); 225 properties.put(key, (value == null ? null : value.toString())); 226 } 227 properties.put(VAR_SOURCE, sourceFile.getAbsolutePath()); 229 properties.put(VAR_TARGET, targetFile.getAbsolutePath()); 230 231 reader.getContent(sourceFile); 233 234 ExecutionResult result = null; 236 try 237 { 238 result = transformCommand.execute(properties); 239 } 240 catch (Throwable e) 241 { 242 throw new ContentIOException("Transformation failed during command execution: \n" + transformCommand, e); 243 } 244 245 if (!result.getSuccess()) 247 { 248 throw new ContentIOException("Transformation failed - status indicates an error: \n" + result); 249 } 250 251 if (!targetFile.exists()) 253 { 254 throw new ContentIOException("Transformation failed - target file doesn't exist: \n" + result); 255 } 256 writer.putContent(targetFile); 258 259 if (logger.isDebugEnabled()) 261 { 262 logger.debug("Transformation completed: \n" + 263 " source: " + reader + "\n" + 264 " target: " + writer + "\n" + 265 " options: " + options + "\n" + 266 " result: \n" + result); 267 } 268 } 269 } 270 | Popular Tags |