/*
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.ros.internal.system;
import java.lang.management.ManagementFactory;
/**
* Process-related utility methods.
*
* @author khughes@google.com (Keith M. Hughes)
*/
public class Process {
private Process() {
// Utility class.
}
/**
* @return PID of node process if available, throws
* {@link UnsupportedOperationException} otherwise.
*/
public static int getPid() {
// NOTE(kwc): Java has no standard way of getting PID. MF.getName()
// returns '1234@localhost'.
try {
String mxName = ManagementFactory.getRuntimeMXBean().getName();
int idx = mxName.indexOf('@');
if (idx > 0) {
try {
return Integer.parseInt(mxName.substring(0, idx));
} catch (NumberFormatException e) {
return 0;
}
}
} catch (NoClassDefFoundError unused) {
// Android does not support ManagementFactory. Try to get the PID on
// Android.
try {
return (Integer) Class.forName("android.os.Process").getMethod("myPid").invoke(null);
} catch (Exception unused1) {
// Ignore this exception and fall through to the
// UnsupportedOperationException.
}
}
throw new UnsupportedOperationException();
}
}