KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > CompilerMediaCache


1 /* *****************************************************************************
2  * CompilerMediaCache.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

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 JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27
28 /**
29  * A class for maintaining a disk-backed cache of transcoded media
30  * files for the compiler.
31  *
32  * @author <a HREF="mailto:bloch@laszlosystems.com">Eric Bloch</a>
33  */

34 public class CompilerMediaCache extends Cache {
35
36     /** Logger. */
37     private static Logger mLogger = Logger.getLogger(CompilerMediaCache.class);
38
39     /** Properties */
40     private static Properties JavaDoc mProperties = null;
41     
42     /** See the constructor. */
43     protected File JavaDoc mCacheDirectory;
44
45     /**
46      * Creates a new <code>CompilerMediaCache</code> instance.
47      */

48     public CompilerMediaCache(File JavaDoc cacheDirectory, Properties JavaDoc props)
49         throws IOException JavaDoc {
50         super("cmcache", cacheDirectory, props);
51         this.mCacheDirectory = cacheDirectory;
52         if (props == null) {
53             this.mProperties = new Properties JavaDoc();
54         } else {
55             this.mProperties = props;
56         }
57
58     }
59
60     /**
61      * Return properties object
62      * There is one property <code>forcetranscode</code>
63      * which when set to <code>true</code> will force the
64      * cache to always transcode requests.
65      */

66     public Properties JavaDoc getProperties() {
67         return mProperties;
68     }
69
70     /**
71      * Transcode the given input file from the fromType to toType
72      * @return the transcoded file
73      * @param inputFile file to be transcoded
74      * @param fromType type of file to be transcoded
75      * @param toType type of file to transcode into
76      */

77     public synchronized File JavaDoc transcode(
78             File JavaDoc inputFile,
79             String JavaDoc fromType,
80             String JavaDoc toType)
81         throws TranscoderException,
82                FileNotFoundException JavaDoc,
83                IOException JavaDoc {
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 JavaDoc(inputFile.getPath());
92         }
93
94         // Key should be relative to webapp path and have
95
// consistent path separator
96
String JavaDoc key = FileUtils.relativePath(inputFile, LPS.HOME()) + ":" + toType;
97
98         /* we don't use the cache's encoding support; we do it ourselves */
99         String JavaDoc enc = null;
100         boolean lockit = false;
101         Item item = findItem(key, null, lockit);
102
103         String JavaDoc inputFilePath = inputFile.getAbsolutePath();
104         File JavaDoc cacheFile = item.getFile();
105         String JavaDoc cacheFilePath = cacheFile.getAbsolutePath();
106         mLogger.debug("transcoding input: " + inputFilePath +
107                 " output: " + cacheFilePath);
108
109         InputStream JavaDoc input = null;
110         FileOutputStream JavaDoc 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