You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by trkurc <gi...@git.apache.org> on 2017/04/02 21:33:49 UTC

[GitHub] nifi pull request #1586: NIFI-3586: Fix for retrieving ProcessID for NiFi un...

Github user trkurc commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1586#discussion_r109320687
  
    --- Diff: nifi-bootstrap/src/main/java/org/apache/nifi/bootstrap/util/OSUtil.java ---
    @@ -0,0 +1,81 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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.apache.nifi.bootstrap.util;
    +
    +import java.lang.reflect.Field;
    +
    +import org.slf4j.Logger;
    +import com.sun.jna.Pointer;
    +import com.sun.jna.platform.win32.Kernel32;
    +import com.sun.jna.platform.win32.WinNT;
    +
    +/**
    + * OS specific utilities with generic method interfaces
    + */
    +public final class OSUtil {
    +
    +    private static Long getUnicesPid(final Process process, final Logger logger) {
    +        try {
    +            final Class<?> procClass = process.getClass();
    +            final Field pidField = procClass.getDeclaredField("pid");
    +            pidField.setAccessible(true);
    +            final Object pidObject = pidField.get(process);
    +
    +            logger.debug("PID Object = {}", pidObject);
    +
    +            if (pidObject instanceof Number) {
    +                return ((Number) pidObject).longValue();
    +            }
    +            return null;
    +        } catch (final IllegalAccessException | NoSuchFieldException nsfe) {
    +            logger.debug("Could not find PID for child process due to {}", nsfe);
    +            return null;
    +        }
    +    }
    +
    +    private static Long getWindowsProcessId(final Process process, final Logger logger) {
    +        /* determine the pid on windows plattforms */
    +        try {
    +            Field f = process.getClass().getDeclaredField("handle");
    +            f.setAccessible(true);
    +            long handl = f.getLong(process);
    +
    +            Kernel32 kernel = Kernel32.INSTANCE;
    +            WinNT.HANDLE handle = new WinNT.HANDLE();
    +            handle.setPointer(Pointer.createConstant(handl));
    +            int ret = kernel.GetProcessId(handle);
    +            logger.debug("Detected pid: {}", ret);
    +            return Long.valueOf(ret);
    +        } catch (final IllegalAccessException | NoSuchFieldException nsfe) {
    +            logger.debug("Could not find PID for child process due to {}", nsfe);
    +        }
    +        return null;
    +    }
    +
    +    public static Long getProcessId(final Process process, final Logger logger) {
    --- End diff --
    
    I know it wasn't there before when this was a private method before, but I'd LOVE to see a javadoc comment to describe the contract for this method. I think returning null is a bit counter-intuitive


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---