001: /* *****************************************************************************
002: * CompilerMediaCache.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.compiler;
011:
012: import org.openlaszlo.media.Transcoder;
013: import org.openlaszlo.media.TranscoderException;
014: import org.openlaszlo.cache.Cache;
015: import org.openlaszlo.cache.CachedInfo;
016: import org.openlaszlo.utils.FileUtils;
017: import org.openlaszlo.server.LPS;
018:
019: import org.apache.log4j.*;
020:
021: import java.util.Properties;
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.FileNotFoundException;
025: import java.io.InputStream;
026: import java.io.FileOutputStream;
027:
028: /**
029: * A class for maintaining a disk-backed cache of transcoded media
030: * files for the compiler.
031: *
032: * @author <a href="mailto:bloch@laszlosystems.com">Eric Bloch</a>
033: */
034: public class CompilerMediaCache extends Cache {
035:
036: /** Logger. */
037: private static Logger mLogger = Logger
038: .getLogger(CompilerMediaCache.class);
039:
040: /** Properties */
041: private static Properties mProperties = null;
042:
043: /** See the constructor. */
044: protected File mCacheDirectory;
045:
046: /**
047: * Creates a new <code>CompilerMediaCache</code> instance.
048: */
049: public CompilerMediaCache(File cacheDirectory, Properties props)
050: throws IOException {
051: super ("cmcache", cacheDirectory, props);
052: this .mCacheDirectory = cacheDirectory;
053: if (props == null) {
054: this .mProperties = new Properties();
055: } else {
056: this .mProperties = props;
057: }
058:
059: }
060:
061: /**
062: * Return properties object
063: * There is one property <code>forcetranscode</code>
064: * which when set to <code>true</code> will force the
065: * cache to always transcode requests.
066: */
067: public Properties getProperties() {
068: return mProperties;
069: }
070:
071: /**
072: * Transcode the given input file from the fromType to toType
073: * @return the transcoded file
074: * @param inputFile file to be transcoded
075: * @param fromType type of file to be transcoded
076: * @param toType type of file to transcode into
077: */
078: public synchronized File transcode(File inputFile, String fromType,
079: String toType) throws TranscoderException,
080: FileNotFoundException, IOException {
081:
082: mLogger.debug(
083: /* (non-Javadoc)
084: * @i18n.test
085: * @org-mes="transcoding from " + p[0] + " to " + p[1]
086: */
087: org.openlaszlo.i18n.LaszloMessages.getMessage(
088: CompilerMediaCache.class.getName(), "051018-90",
089: new Object[] { fromType, toType }));
090: if (fromType.equalsIgnoreCase(toType)) {
091: return inputFile;
092: }
093:
094: if (!inputFile.exists()) {
095: throw new FileNotFoundException(inputFile.getPath());
096: }
097:
098: // Key should be relative to webapp path and have
099: // consistent path separator
100: String key = FileUtils.relativePath(inputFile, LPS.HOME())
101: + ":" + toType;
102:
103: /* we don't use the cache's encoding support; we do it ourselves */
104: String enc = null;
105: boolean lockit = false;
106: Item item = findItem(key, null, lockit);
107:
108: String inputFilePath = inputFile.getAbsolutePath();
109: File cacheFile = item.getFile();
110: String cacheFilePath = cacheFile.getAbsolutePath();
111: mLogger.debug(
112: /* (non-Javadoc)
113: * @i18n.test
114: * @org-mes="transcoding input: " + p[0] + " output: " + p[1]
115: */
116: org.openlaszlo.i18n.LaszloMessages.getMessage(
117: CompilerMediaCache.class.getName(), "051018-118",
118: new Object[] { inputFilePath, cacheFilePath }));
119:
120: InputStream input = null;
121: FileOutputStream output = null;
122:
123: if (!cacheFile.exists()
124: || !inputFile.canRead()
125: || inputFile.lastModified() > cacheFile.lastModified()
126: || mProperties.getProperty("forcetranscode", "false") == "true") {
127:
128: item.markDirty();
129:
130: mLogger.debug(
131: /* (non-Javadoc)
132: * @i18n.test
133: * @org-mes="transcoding..."
134: */
135: org.openlaszlo.i18n.LaszloMessages.getMessage(
136: CompilerMediaCache.class.getName(), "051018-135"));
137:
138: CachedInfo info = item.getInfo();
139: try {
140: input = Transcoder.transcode(inputFile, fromType,
141: toType);
142: mLogger.debug(
143: /* (non-Javadoc)
144: * @i18n.test
145: * @org-mes="done transcoding"
146: */
147: org.openlaszlo.i18n.LaszloMessages.getMessage(
148: CompilerMediaCache.class.getName(),
149: "051018-147"));
150: item.update(input, null);
151: info.setLastModified(cacheFile.lastModified());
152: item.updateInfo();
153: item.markClean();
154: } finally {
155: FileUtils.close(input);
156: }
157: } else {
158: mLogger.debug(
159: /* (non-Javadoc)
160: * @i18n.test
161: * @org-mes="using cached transcode"
162: */
163: org.openlaszlo.i18n.LaszloMessages.getMessage(
164: CompilerMediaCache.class.getName(), "051018-163"));
165: }
166:
167: updateCache(item);
168:
169: return cacheFile;
170: }
171: }
|