001
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
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 }