KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > property > ThumbnailPropertyProcessor


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.property;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.riotfamily.common.image.Thumbnailer;
33 import org.riotfamily.common.web.file.FileStore;
34
35
36 /**
37  * @author Felix Gnass [fgnass at neteye dot de]
38  * @since 6.4
39  */

40 public class ThumbnailPropertyProcessor extends PropertyProcessorAdapter {
41
42     private static final Log log = LogFactory.getLog(ThumbnailPropertyProcessor.class);
43     
44     private static final String JavaDoc MTIME_SUFFIX = "-mtime";
45     
46     private String JavaDoc source;
47     
48     private String JavaDoc dest;
49     
50     private int width;
51     
52     private int height;
53     
54     private String JavaDoc format = "jpg";
55     
56     private FileStore fileStore;
57     
58     private Thumbnailer thumbnailer;
59     
60     private File JavaDoc tempDir;
61     
62     public void setSource(String JavaDoc source) {
63         this.source = source;
64     }
65     
66     public void setDest(String JavaDoc dest) {
67         this.dest = dest;
68     }
69
70     public void setFileStore(FileStore fileStore) {
71         this.fileStore = fileStore;
72     }
73
74     public void setThumbnailer(Thumbnailer thumbnailer) {
75         this.thumbnailer = thumbnailer;
76     }
77
78     public void setFormat(String JavaDoc format) {
79         this.format = format;
80     }
81
82     public void setWidth(int width) {
83         this.width = width;
84     }
85     
86     public void setHeight(int height) {
87         this.height = height;
88     }
89
90     public void convertToStrings(Map JavaDoc map) {
91         try {
92             String JavaDoc sourceUri = (String JavaDoc) map.get(source);
93             File JavaDoc sourceFile = fileStore.retrieve(sourceUri);
94             
95             String JavaDoc mtime = (String JavaDoc) map.get(dest + MTIME_SUFFIX);
96             if (mtime == null || Long.parseLong(mtime) < sourceFile.lastModified()) {
97                 File JavaDoc tempFile = File.createTempFile("thumb", "." + format, tempDir);
98                 thumbnailer.renderThumbnail(sourceFile, tempFile, width, height);
99                 String JavaDoc destUri = (String JavaDoc) map.get(dest);
100                 if (destUri != null) {
101                     fileStore.delete(destUri);
102                 }
103                 destUri = fileStore.store(tempFile, null);
104                 map.put(dest, destUri);
105                 map.put(dest + MTIME_SUFFIX, String.valueOf(System.currentTimeMillis()));
106             }
107         }
108         catch (IOException JavaDoc e) {
109             log.error(e);
110         }
111     }
112     
113 }
114
Popular Tags