KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > transform > magick > AbstractImageMagickContentTransformer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.transform.magick;
18
19 import java.io.File JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.error.AlfrescoRuntimeException;
25 import org.alfresco.repo.content.MimetypeMap;
26 import org.alfresco.repo.content.filestore.FileContentWriter;
27 import org.alfresco.repo.content.transform.AbstractContentTransformer;
28 import org.alfresco.service.cmr.repository.ContentIOException;
29 import org.alfresco.service.cmr.repository.ContentReader;
30 import org.alfresco.service.cmr.repository.ContentWriter;
31 import org.alfresco.util.TempFileProvider;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * Abstract helper for transformations based on <b>ImageMagick</b>
37  *
38  * @author Derek Hulley
39  */

40 public abstract class AbstractImageMagickContentTransformer extends AbstractContentTransformer
41 {
42     /** the prefix for mimetypes supported by the transformer */
43     public static final String JavaDoc MIMETYPE_IMAGE_PREFIX = "image/";
44     
45     private static final Log logger = LogFactory.getLog(AbstractImageMagickContentTransformer.class);
46     
47     private boolean available;
48     
49     public AbstractImageMagickContentTransformer()
50     {
51         this.available = false;
52     }
53     
54     /**
55      * @return Returns true if the transformer is functioning otherwise false
56      */

57     public boolean isAvailable()
58     {
59         return available;
60     }
61
62     /**
63      * Make the transformer available
64      * @param available
65      */

66     protected void setAvailable(boolean available)
67     {
68         this.available = available;
69     }
70
71     /**
72      * Checks for the JMagick and ImageMagick dependencies, using the common
73      * {@link #transformInternal(File, File) transformation method} to check
74      * that the sample image can be converted.
75      * <p>
76      * If initialization is successful, then autoregistration takes place.
77      */

78     public void init()
79     {
80         if (getMimetypeService() == null)
81         {
82             throw new AlfrescoRuntimeException("MimetypeMap not present");
83         }
84         try
85         {
86             // load, into memory the sample gif
87
String JavaDoc resourcePath = "org/alfresco/repo/content/transform/magick/alfresco.gif";
88             InputStream JavaDoc imageStream = getClass().getClassLoader().getResourceAsStream(resourcePath);
89             if (imageStream == null)
90             {
91                 throw new AlfrescoRuntimeException("Sample image not found: " + resourcePath);
92             }
93             // dump to a temp file
94
File JavaDoc inputFile = TempFileProvider.createTempFile(
95                     getClass().getSimpleName() + "_init_source_",
96                     ".gif");
97             FileContentWriter writer = new FileContentWriter(inputFile);
98             writer.putContent(imageStream);
99             
100             // create the output file
101
File JavaDoc outputFile = TempFileProvider.createTempFile(
102                     getClass().getSimpleName() + "_init_target_",
103                     ".png");
104             
105             // execute it
106
Map JavaDoc<String JavaDoc, Object JavaDoc> options = Collections.emptyMap();
107             transformInternal(inputFile, outputFile, options);
108             
109             // check that the file exists
110
if (!outputFile.exists())
111             {
112                 throw new Exception JavaDoc("Image conversion failed: \n" +
113                         " from: " + inputFile + "\n" +
114                         " to: " + outputFile);
115             }
116             // we can be sure that it works
117
setAvailable(true);
118             
119             // register
120
super.register();
121         }
122         catch (Throwable JavaDoc e)
123         {
124             logger.error(
125                     getClass().getSimpleName() + " not available: " +
126                     (e.getMessage() != null ? e.getMessage() : ""));
127             // debug so that we can trace the issue if required
128
logger.debug(e);
129         }
130     }
131     
132     /**
133      * Some image formats are not supported by ImageMagick, or at least appear not to work.
134      *
135      * @param mimetype the mimetype to check
136      * @return Returns true if ImageMagic can handle the given image format
137      */

138     public static boolean isSupported(String JavaDoc mimetype)
139     {
140         if (!mimetype.startsWith(MIMETYPE_IMAGE_PREFIX))
141         {
142             return false; // not an image
143
}
144         else if (mimetype.equals(MimetypeMap.MIMETYPE_IMAGE_RGB))
145         {
146             return false; // rgb extension doesn't work
147
}
148         else
149         {
150             return true;
151         }
152     }
153     
154     /**
155      * Supports image to image conversion, but only if the JMagick library and required
156      * libraries are available.
157      */

158     public double getReliability(String JavaDoc sourceMimetype, String JavaDoc targetMimetype)
159     {
160         if (!available)
161         {
162             return 0.0;
163         }
164         if (!AbstractImageMagickContentTransformer.isSupported(sourceMimetype) ||
165                 !AbstractImageMagickContentTransformer.isSupported(targetMimetype))
166         {
167             // only support IMAGE -> IMAGE (excl. RGB)
168
return 0.0;
169         }
170         else
171         {
172             return 1.0;
173         }
174     }
175     
176     /**
177      * @see #transformInternal(File, File)
178      */

179     protected final void transformInternal(
180             ContentReader reader,
181             ContentWriter writer,
182             Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws Exception JavaDoc
183     {
184         // get mimetypes
185
String JavaDoc sourceMimetype = getMimetype(reader);
186         String JavaDoc targetMimetype = getMimetype(writer);
187         
188         // get the extensions to use
189
String JavaDoc sourceExtension = getMimetypeService().getExtension(sourceMimetype);
190         String JavaDoc targetExtension = getMimetypeService().getExtension(targetMimetype);
191         if (sourceExtension == null || targetExtension == null)
192         {
193             throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" +
194                     " source mimetype: " + sourceMimetype + "\n" +
195                     " source extension: " + sourceExtension + "\n" +
196                     " target mimetype: " + targetMimetype + "\n" +
197                     " target extension: " + targetExtension);
198         }
199         
200         // create required temp files
201
File JavaDoc sourceFile = TempFileProvider.createTempFile(
202                 getClass().getSimpleName() + "_source_",
203                 "." + sourceExtension);
204         File JavaDoc targetFile = TempFileProvider.createTempFile(
205                 getClass().getSimpleName() + "_target_",
206                 "." + targetExtension);
207         
208         // pull reader file into source temp file
209
reader.getContent(sourceFile);
210         
211         // transform the source temp file to the target temp file
212
transformInternal(sourceFile, targetFile, options);
213         
214         // check that the file was created
215
if (!targetFile.exists())
216         {
217             throw new ContentIOException("JMagick transformation failed to write output file");
218         }
219         // upload the output image
220
writer.putContent(targetFile);
221         // done
222
if (logger.isDebugEnabled())
223         {
224             logger.debug("Transformation completed: \n" +
225                     " source: " + reader + "\n" +
226                     " target: " + writer + "\n" +
227                     " options: " + options);
228         }
229     }
230     
231     /**
232      * Transform the image content from the source file to the target file
233      *
234      * @param sourceFile the source of the transformation
235      * @param targetFile the target of the transformation
236      * @param options the transformation options supported by ImageMagick
237      * @throws Exception
238      */

239     protected abstract void transformInternal(
240             File JavaDoc sourceFile,
241             File JavaDoc targetFile,
242             Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws Exception JavaDoc;
243 }
244
Popular Tags