| ProcessUtil.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 *
12 *
13 */
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 /**
23 * <a href="ProcessUtil.java.html"><b><i>View Source</i></b></a>
24 *
25 * @author Brian Wing Shun Chan
26 * @author Shuyang Zhou
27 */
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 }