1
14
15 package com.liferay.portal.kernel.util;
16
17 import java.io.IOException;
18
19 import java.lang.management.ManagementFactory;
20 import java.lang.management.RuntimeMXBean;
21
22
28 public class ProcessUtil {
29
30 public static void close(Process process) {
31 try {
32 process.waitFor();
33 }
34 catch (InterruptedException ie) {
35 ie.printStackTrace();
36 }
37
38 try {
39 process.getInputStream().close();
40 }
41 catch (IOException ioe) {
42 ioe.printStackTrace();
43 }
44
45 try {
46 process.getOutputStream().close();
47 }
48 catch (IOException ioe) {
49 ioe.printStackTrace();
50 }
51
52 try {
53 process.getErrorStream().close();
54 }
55 catch (IOException ioe) {
56 ioe.printStackTrace();
57 }
58 }
59
60 public static int getProcessId() {
61 RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
62
63 String name = runtimeMXBean.getName();
64
65 int index = name.indexOf(CharPool.AT);
66
67 if (index == -1) {
68 throw new RuntimeException("Unable to parse process name " + name);
69 }
70
71 String id = name.substring(0, index);
72
73 try {
74 return Integer.parseInt(id);
75 }
76 catch (NumberFormatException nfe) {
77 throw new RuntimeException(
78 "Unable to parse process name " + name, nfe);
79 }
80 }
81
82 }