001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.nio.intraband.welder.fifo;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.io.File;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class FIFOUtil {
026    
027            public static void createFIFO(File fifoFile) throws Exception {
028                    ProcessBuilder processBuilder = new ProcessBuilder(
029                            "mkfifo", fifoFile.getAbsolutePath());
030    
031                    Process mkfifoProcess = null;
032    
033                    try {
034                            mkfifoProcess = processBuilder.start();
035    
036                            int result = mkfifoProcess.waitFor();
037    
038                            if (result != 0) {
039                                    throw new Exception(
040                                            "Unable to create FIFO with command \"mkfifo\", " +
041                                                    "external process returned " + result);
042                            }
043                    }
044                    finally {
045                            if (mkfifoProcess != null) {
046                                    mkfifoProcess.destroy();
047                            }
048                    }
049            }
050    
051            public static boolean isFIFOSupported() {
052                    return _fifoSupported;
053            }
054    
055            private static Log _log = LogFactoryUtil.getLog(FIFOUtil.class);
056    
057            private static boolean _fifoSupported;
058    
059            static {
060                    try {
061                            File tempFIFOFile = new File(
062                                    System.getProperty("java.io.tmpdir"),
063                                    "temp-fifo-" + System.currentTimeMillis());
064    
065                            try {
066                                    createFIFO(tempFIFOFile);
067                            }
068                            finally {
069                                    if (!tempFIFOFile.delete()) {
070                                            if (tempFIFOFile.exists()) {
071                                                    tempFIFOFile.deleteOnExit();
072                                            }
073                                    }
074                            }
075    
076                            _fifoSupported = true;
077                    }
078                    catch (Throwable t) {
079                            if (_log.isWarnEnabled()) {
080                                    _log.warn("Unable to detect FIFO support", t);
081                            }
082    
083                            _fifoSupported = false;
084                    }
085            }
086    
087    }