I used a non-portable approach to extract a UNIX identifier from a Process object, which is very simple.
STEP 1: Use some Reflection API calls to identify the Process implementation class on the target JRE server (remember that Process is an abstract class). If your UNIX implementation is similar to mine, you will see an implementation class that has a property called pid that contains the process PID. Here is the registration code I used.
//-------------------------------------------------------------------- // Jim Tough - 2014-11-04 // This temporary Reflection code is used to log the name of the // class that implements the abstract Process class on the target // JRE, all of its 'Fields' (properties and methods) and the value // of each field. // // I only care about how this behaves on our UNIX servers, so I'll // deploy a snapshot release of this code to a QA server, run it once, // then check the logs. // // TODO Remove this logging code before building final release! final Class<?> clazz = process.getClass(); logger.info("Concrete implementation of " + Process.class.getName() + " is: " + clazz.getName()); // Array of all fields in this class, regardless of access level final Field[] allFields = clazz.getDeclaredFields(); for (Field field : allFields) { field.setAccessible(true); // allows access to non-public fields Class<?> fieldClass = field.getType(); StringBuilder sb = new StringBuilder(field.getName()); sb.append(" | type: "); sb.append(fieldClass.getName()); sb.append(" | value: ["); Object fieldValue = null; try { fieldValue = field.get(process); sb.append(fieldValue); sb.append("]"); } catch (Exception e) { logger.error("Unable to get value for [" + field.getName() + "]", e); } logger.info(sb.toString()); } //--------------------------------------------------------------------
STEP 2: Based on the implementation class and the field name that you received in the Reflection log, write some code for the pickpocket class of the Process implementation and extract the PID from it using the Reflection API. The code below works for me to my taste of UNIX. You may need to configure the EXPECTED_IMPL_CLASS_NAME and EXPECTED_PID_FIELD_NAME constants to work for you.
Integer retrievePID(final Process process) { if (process == null) { return null; }
Jim Tough Nov 04 '14 at 16:10 2014-11-04 16:10
source share