You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/19 10:57:12 UTC

svn commit: r1094941 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Vm.java native/os/unix/platform.c native/os/win32/platform.c test/org/apache/commons/runtime/TestMain.java

Author: mturk
Date: Tue Apr 19 08:57:11 2011
New Revision: 1094941

URL: http://svn.apache.org/viewvc?rev=1094941&view=rev
Log:
Add Vm class

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/platform.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java?rev=1094941&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java Tue Apr 19 08:57:11 2011
@@ -0,0 +1,41 @@
+/* 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.commons.runtime;
+
+/** Virtual Machine various info.
+ *
+ * @since Runtime 1.0
+ *
+ */
+public final class Vm
+{
+
+    private Vm()
+    {
+        // No class instance
+    }
+
+    /**
+     * Returns the process ID of the current VM.
+     */
+    public static native int getPid();
+
+    /**
+     * Returns the process ID of the parent of the current VM.
+     */
+    public static native int getParentPid();
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Vm.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c?rev=1094941&r1=1094940&r2=1094941&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/platform.c Tue Apr 19 08:57:11 2011
@@ -69,3 +69,13 @@ ACR_JNI_EXPORT(jboolean, Platform, suppo
      */
     return JNI_TRUE;
 }
+
+ACR_JNI_EXPORT(jint, Vm, getPid)(JNI_STDARGS)
+{
+    return (jint)getpid();
+}
+
+ACR_JNI_EXPORT(jint, Vm, getParentPid)(JNI_STDARGS)
+{
+    return (jint)getppid();
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/platform.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/platform.c?rev=1094941&r1=1094940&r2=1094941&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/platform.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/platform.c Tue Apr 19 08:57:11 2011
@@ -15,9 +15,12 @@
  */
 
 #include "acr/string.h"
+#include "acr/misc.h"
 #include "acr/port.h"
 #include "arch_opts.h"
 
+#include <tlhelp32.h>
+
 extern int acr_native_codepage;
 extern LPOSVERSIONINFOEXA acr_osver;
 
@@ -73,3 +76,49 @@ ACR_JNI_EXPORT(jboolean, Platform, suppo
     else
         return JNI_TRUE;
 }
+
+ACR_JNI_EXPORT(jint, Vm, getPid)(JNI_STDARGS)
+{
+    return GetCurrentProcessId();
+}
+
+ACR_JNI_EXPORT(jint, Vm, getParentPid)(JNI_STDARGS)
+{
+    static int      ppid = -1;
+    HANDLE          snap;
+    PROCESSENTRY32W e;
+
+    if (ppid >= 0) {
+        /* Use the cached value since the parent
+         * cannot change during process lifetime
+         */
+        return ppid;
+    }
+    AcrLibLockAcquire();
+    if (ppid >= 0)
+        goto finally;
+
+    snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+    if (IS_INVALID_HANDLE(snap))
+        goto finally;
+
+    e.dwSize = (DWORD)sizeof(PROCESSENTRY32W);
+    if (!Process32FirstW(snap, &e)) {
+        CloseHandle(snap);
+        goto finally;
+    }
+    do {
+        if (e.th32ProcessID == GetCurrentProcessId()) {
+            /* We found ourself :)
+             */
+            ppid = e.th32ParentProcessID;
+            break;
+        }
+
+    } while (Process32NextW(snap, &e));
+    CloseHandle(snap);
+
+finally:
+    AcrLibLockRelease();
+    return ppid;
+}

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java?rev=1094941&r1=1094940&r2=1094941&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java Tue Apr 19 08:57:11 2011
@@ -25,12 +25,14 @@ public class TestMain
     public void setUp()
     {
         Assert.assertTrue(Loader.load());
+        System.out.println("Test initialized (pid=" + Vm.getPid() + ")");
         System.out.flush();
     }
 
     @AfterSuite(groups = { "init" })
     public void shutDown()
     {
+        System.out.println("Test terminating (pid=" + Vm.getPid() + ")");
         Native.terminate();
         System.out.flush();
     }