1 4 5 9 10 package org.openlaszlo.compiler; 11 12 import org.openlaszlo.media.Transcoder; 13 import org.openlaszlo.media.TranscoderException; 14 import org.openlaszlo.cache.Cache; 15 import org.openlaszlo.cache.CachedInfo; 16 import org.openlaszlo.utils.FileUtils; 17 import org.openlaszlo.server.LPS; 18 19 import org.apache.log4j.*; 20 21 import java.util.Properties ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.FileNotFoundException ; 25 import java.io.InputStream ; 26 import java.io.FileOutputStream ; 27 28 34 public class CompilerMediaCache extends Cache { 35 36 37 private static Logger mLogger = Logger.getLogger(CompilerMediaCache.class); 38 39 40 private static Properties mProperties = null; 41 42 43 protected File mCacheDirectory; 44 45 48 public CompilerMediaCache(File cacheDirectory, Properties props) 49 throws IOException { 50 super("cmcache", cacheDirectory, props); 51 this.mCacheDirectory = cacheDirectory; 52 if (props == null) { 53 this.mProperties = new Properties (); 54 } else { 55 this.mProperties = props; 56 } 57 58 } 59 60 66 public Properties getProperties() { 67 return mProperties; 68 } 69 70 77 public synchronized File transcode( 78 File inputFile, 79 String fromType, 80 String toType) 81 throws TranscoderException, 82 FileNotFoundException , 83 IOException { 84 85 mLogger.debug("transcoding from " + fromType + " to " + toType); 86 if (fromType.equalsIgnoreCase(toType)) { 87 return inputFile; 88 } 89 90 if (!inputFile.exists()) { 91 throw new FileNotFoundException (inputFile.getPath()); 92 } 93 94 String key = FileUtils.relativePath(inputFile, LPS.HOME()) + ":" + toType; 97 98 99 String enc = null; 100 boolean lockit = false; 101 Item item = findItem(key, null, lockit); 102 103 String inputFilePath = inputFile.getAbsolutePath(); 104 File cacheFile = item.getFile(); 105 String cacheFilePath = cacheFile.getAbsolutePath(); 106 mLogger.debug("transcoding input: " + inputFilePath + 107 " output: " + cacheFilePath); 108 109 InputStream input = null; 110 FileOutputStream output = null; 111 112 if (!cacheFile.exists() || !inputFile.canRead() || 113 inputFile.lastModified() > cacheFile.lastModified() || 114 mProperties.getProperty("forcetranscode", "false") == "true") { 115 116 item.markDirty(); 117 118 mLogger.debug("transcoding..."); 119 120 CachedInfo info = item.getInfo(); 121 try { 122 input = Transcoder.transcode(inputFile, fromType, toType); 123 mLogger.debug("done transcoding"); 124 item.update(input, null); 125 info.setLastModified(cacheFile.lastModified()); 126 item.updateInfo(); 127 item.markClean(); 128 } finally { 129 FileUtils.close(input); 130 } 131 } else { 132 mLogger.debug("using cached transcode"); 133 } 134 135 updateCache(item); 136 137 return cacheFile; 138 } 139 } 140 | Popular Tags |