KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.error.AlfrescoRuntimeException;
24 import org.alfresco.service.cmr.repository.ContentIOException;
25 import org.alfresco.util.exec.RuntimeExec;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * Executes a statement to implement
31  *
32  * @author Derek Hulley
33  */

34 public class ImageMagickContentTransformer extends AbstractImageMagickContentTransformer
35 {
36     /** the command options, such as <b>--resize</b>, etc. */
37     public static final String JavaDoc KEY_OPTIONS = "options";
38     /** source variable name */
39     public static final String JavaDoc VAR_OPTIONS = "options";
40     /** source variable name */
41     public static final String JavaDoc VAR_SOURCE = "source";
42     /** target variable name */
43     public static final String JavaDoc VAR_TARGET = "target";
44     
45     private static final Log logger = LogFactory.getLog(ImageMagickContentTransformer.class);
46
47     /** the system command executer */
48     private RuntimeExec executer;
49     
50     public ImageMagickContentTransformer()
51     {
52     }
53     
54     /**
55      * Set the runtime command executer that must be executed in order to run
56      * <b>ImageMagick</b>. Whether or not this is the full path to the convertCommand
57      * or just the convertCommand itself depends the environment setup.
58      * <p>
59      * The command must contain the variables <code>${source}</code> and
60      * <code>${target}</code>, which will be replaced by the names of the file to
61      * be transformed and the name of the output file respectively.
62      * <pre>
63      * convert ${source} ${target}
64      * </pre>
65      *
66      * @param executer the system command executer
67      */

68     public void setExecuter(RuntimeExec executer)
69     {
70         this.executer = executer;
71     }
72
73     /**
74      * Checks for the JMagick and ImageMagick dependencies, using the common
75      * {@link #transformInternal(File, File) transformation method} to check
76      * that the sample image can be converted.
77      */

78     public void init()
79     {
80         if (executer == null)
81         {
82             throw new AlfrescoRuntimeException("System runtime executer not set");
83         }
84         super.init();
85     }
86     
87     /**
88      * Transform the image content from the source file to the target file
89      */

90     protected void transformInternal(File JavaDoc sourceFile, File JavaDoc targetFile, Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws Exception JavaDoc
91     {
92         Map JavaDoc<String JavaDoc, String JavaDoc> properties = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(5);
93         // set properties
94
properties.put(KEY_OPTIONS, (String JavaDoc) options.get(KEY_OPTIONS));
95         properties.put(VAR_SOURCE, sourceFile.getAbsolutePath());
96         properties.put(VAR_TARGET, targetFile.getAbsolutePath());
97         
98         // execute the statement
99
RuntimeExec.ExecutionResult result = executer.execute(properties);
100         if (result.getExitValue() != 0 && result.getStdErr() != null && result.getStdErr().length() > 0)
101         {
102             throw new ContentIOException("Failed to perform ImageMagick transformation: \n" + result);
103         }
104         // success
105
if (logger.isDebugEnabled())
106         {
107             logger.debug("ImageMagic executed successfully: \n" + executer);
108         }
109     }
110 }
111
Popular Tags