001    /**
002     * Copyright (c) 2000-present 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 _FIFO_SUPPORTED;
053            }
054    
055            private static final boolean _FIFO_SUPPORTED;
056    
057            private static final Log _log = LogFactoryUtil.getLog(FIFOUtil.class);
058    
059            static {
060                    boolean fifoSupport = false;
061    
062                    try {
063                            File tempFIFOFile = new File(
064                                    System.getProperty("java.io.tmpdir"),
065                                    "temp-fifo-" + System.currentTimeMillis());
066    
067                            try {
068                                    createFIFO(tempFIFOFile);
069                            }
070                            finally {
071                                    if (!tempFIFOFile.delete()) {
072                                            if (tempFIFOFile.exists()) {
073                                                    tempFIFOFile.deleteOnExit();
074                                            }
075                                    }
076                            }
077    
078                            fifoSupport = true;
079                    }
080                    catch (Throwable t) {
081                            if (_log.isWarnEnabled()) {
082                                    _log.warn("Unable to detect FIFO support", t);
083                            }
084                    }
085    
086                    _FIFO_SUPPORTED = fifoSupport;
087            }
088    
089    }