001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.antivirus;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    
019    import java.io.File;
020    import java.io.IOException;
021    
022    /**
023     * @author Michael C. Han
024     */
025    public class ClamAntivirusScannerImpl extends BaseFileAntivirusScanner {
026    
027            public void scan(File file)
028                    throws AntivirusScannerException, SystemException {
029    
030                    ProcessBuilder processBuilder = new ProcessBuilder(
031                            "clamscan", "--stdout", "--no-summary", file.getAbsolutePath());
032    
033                    processBuilder.redirectErrorStream(true);
034    
035                    Process process = null;
036    
037                    try {
038                            process = processBuilder.start();
039    
040                            process.waitFor();
041    
042                            int exitValue = process.exitValue();
043    
044                            if (exitValue == 1) {
045                                    throw new AntivirusScannerException(
046                                            "Virus detected in " + file.getAbsolutePath());
047                            }
048                            else if (exitValue >= 2) {
049                                    throw new AntivirusScannerException(
050                                            "Unable to scan file due to inability to execute " +
051                                                    "antivirus process");
052                            }
053                    }
054                    catch (IOException ioe) {
055                            throw new SystemException("Unable to scan file", ioe);
056                    }
057                    catch (InterruptedException ie) {
058                            throw new SystemException("Unable to scan file", ie);
059                    }
060                    finally {
061                            if (process != null) {
062                                    process.destroy();
063                            }
064                    }
065            }
066    
067    }