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.util;
016    
017    import java.io.IOException;
018    
019    import java.lang.management.ManagementFactory;
020    import java.lang.management.RuntimeMXBean;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @author Shuyang Zhou
025     */
026    public class ProcessUtil {
027    
028            public static void close(Process process) {
029                    try {
030                            process.waitFor();
031                    }
032                    catch (InterruptedException ie) {
033                            ie.printStackTrace();
034                    }
035    
036                    try {
037                            process.getInputStream().close();
038                    }
039                    catch (IOException ioe) {
040                            ioe.printStackTrace();
041                    }
042    
043                    try {
044                            process.getOutputStream().close();
045                    }
046                    catch (IOException ioe) {
047                            ioe.printStackTrace();
048                    }
049    
050                    try {
051                            process.getErrorStream().close();
052                    }
053                    catch (IOException ioe) {
054                            ioe.printStackTrace();
055                    }
056            }
057    
058            public static int getProcessId() {
059                    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
060    
061                    String name = runtimeMXBean.getName();
062    
063                    int index = name.indexOf(CharPool.AT);
064    
065                    if (index == -1) {
066                            throw new RuntimeException("Unable to parse process name " + name);
067                    }
068    
069                    String id = name.substring(0, index);
070    
071                    try {
072                            return GetterUtil.getInteger(id);
073                    }
074                    catch (NumberFormatException nfe) {
075                            throw new RuntimeException(
076                                    "Unable to parse process name " + name, nfe);
077                    }
078            }
079    
080    }