commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1148309 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/platform/windows/ native/ native/include/acr/ native/os/win32/ native/shared/ test/org/apache/commons/runtime/
Date Tue, 19 Jul 2011 13:17:04 GMT
Author: mturk
Date: Tue Jul 19 13:17:02 2011
New Revision: 1148309

URL: http://svn.apache.org/viewvc?rev=1148309&view=rev
Log:
Implement win32 services enumeration. Full suport is following

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/win32/scm.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/win32/service.c   (with props)
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestService.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
    commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java?rev=1148309&r1=1148308&r2=1148309&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
Tue Jul 19 13:17:02 2011
@@ -95,15 +95,6 @@ public final class AccessRights
     public static final int         PAGE_NOCACHE                    = 0x00000200;
     public static final int         PAGE_WRITECOMBINE               = 0x00000400;
 
-    /* ServiceManager Access rights */
-    public static final int         SC_MANAGER_CONNECT              = 0x0001;
-    public static final int         SC_MANAGER_CREATE_SERVICE       = 0x0002;
-    public static final int         SC_MANAGER_ENUMERATE_SERVICE    = 0x0004;
-    public static final int         SC_MANAGER_LOCK                 = 0x0008;
-    public static final int         SC_MANAGER_QUERY_LOCK_STATUS    = 0x0010;
-    public static final int         SC_MANAGER_MODIFY_BOOT_CONFIG   = 0x0020;
-    public static final int         SC_MANAGER_ALL_ACCESS           = STANDARD_RIGHTS_REQUIRED
| 0x003F;
-
     /* Service Access rights */
     public static final int         SERVICE_ALL_ACCESS              = 0xF01FF;
     public static final int         SERVICE_CHANGE_CONFIG           = 0x0002;

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java?rev=1148309&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
Tue Jul 19 13:17:02 2011
@@ -0,0 +1,87 @@
+/* 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.platform.windows;
+
+import java.util.EnumSet;
+import java.util.List;
+import org.apache.commons.runtime.Errno;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.util.Utils;
+
+/**
+ * Service class.
+ */
+public final class Service
+{
+    private String  serviceName;
+    private String  displayName;
+    private int[]   serviceStatusProcess;
+
+    // Named indexes inside serviceStatusProcess array.
+    // They are here just for easier entry access.
+    private static final int     serviceType                = 0;
+    private static final int     currentState               = 1;
+    private static final int     controlsAccepted           = 2;
+    private static final int     win32ExitCode              = 3;
+    private static final int     serviceSpecificExitCode    = 4;
+    private static final int     checkPoint                 = 5;
+    private static final int     waitHint                   = 6;
+    private static final int     processId                  = 7;
+    private static final int     serviceFlags               = 8;
+    
+
+    private Service()
+    {
+        // No instance
+    }
+
+    /**
+     * Created from the native during services's enum.
+     */
+    private Service(String name, String disp, int[] stats)
+    {
+        serviceName = name;
+        displayName = disp;
+        serviceStatusProcess = stats;
+    }
+
+    /**
+     * Creates a new service object
+     */
+    public Service(String name)
+        throws IllegalArgumentException
+    {
+        if (name == null || name.length() < 1 || name.length() > 256)
+            throw new IllegalArgumentException("invalid service name length");
+        if (name.indexOf('/') != -1 || name.indexOf('\\') != -1)
+            throw new IllegalArgumentException("invalid service name characters");
+        serviceName = name;
+        serviceStatusProcess = new int[9];
+    }
+
+    public String getName()
+    {
+        return serviceName;
+    }
+
+    public String getDisplayName()
+    {
+        return displayName;
+    }
+    
+}

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

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java?rev=1148309&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
Tue Jul 19 13:17:02 2011
@@ -0,0 +1,132 @@
+/* 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.platform.windows;
+
+import java.util.EnumSet;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.runtime.Errno;
+import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.util.Utils;
+
+
+/**
+ * ServiceControlManager manages service control manager database.
+ *
+ */
+public class ServiceControlManager
+{
+
+    private long                handle = 0L;
+    private ArrayList<Service>  services;
+
+    private static native String[]  enum0(long instance, int stype, int sstate)
+                                          throws SystemException;
+    private static native int       enum1(long instance, int stype, int sstate,
+                                          ArrayList<Service> sset);
+
+    /* Public fields */
+    public static final String      SERVICES_ACTIVE_DATABASE        = "ServicesActive";
+    public static final String      SERVICES_FAILED_DATABASE        = "ServicesFailed";
+
+    /* ServiceManager Access rights */
+    public static final int         SC_MANAGER_CONNECT              = 0x0001;
+    public static final int         SC_MANAGER_CREATE_SERVICE       = 0x0002;
+    public static final int         SC_MANAGER_ENUMERATE_SERVICE    = 0x0004;
+    public static final int         SC_MANAGER_LOCK                 = 0x0008;
+    public static final int         SC_MANAGER_QUERY_LOCK_STATUS    = 0x0010;
+    public static final int         SC_MANAGER_MODIFY_BOOT_CONFIG   = 0x0020;
+    public static final int         SC_MANAGER_ALL_ACCESS           = AccessRights.STANDARD_RIGHTS_REQUIRED
| 0x003F;
+
+
+    /**
+     * Enumerates services of type SERVICE_KERNEL_DRIVER
+     * and SERVICE_FILE_SYSTEM_DRIVER.
+     */
+    public static final int SERVICE_DRIVER          = 0x0000000B;
+    /**
+     * Enumerates services of type SERVICE_WIN32_OWN_PROCESS
+     * and SERVICE_WIN32_SHARE_PROCESS.
+     */
+    public static final int SERVICE_WIN32           = 0x00000030;
+
+    /**
+     * Enumerates services that are in the following states:
+     * SERVICE_START_PENDING, SERVICE_STOP_PENDING,
+     * SERVICE_RUNNING, SERVICE_CONTINUE_PENDING,
+     * SERVICE_PAUSE_PENDING, and SERVICE_PAUSED.
+     */
+    public static final int SERVICE_ACTIVE          = 0x00000001;
+    /**
+     * Enumerates services that are in the SERVICE_STOPPED state.
+     */
+    public static final int SERVICE_INACTIVE        = 0x00000002;
+    /**
+     * Combines the SERVICE_ACTIVE and SERVICE_INACTIVE states.
+     */
+    public static final int SERVICE_STATE_ALL       = 0x00000003;
+
+
+
+    public ServiceControlManager()
+    {
+        services = new ArrayList<Service>();
+
+    }
+
+    public void open(int desiredAccess)
+        throws SystemException
+    {
+        handle = Win32.OpenSCManager(null, null, desiredAccess);
+        if (handle == 0L)
+            throw new SystemException(Errno.msg());
+    }
+
+    public final ArrayList<Service> getServices(int serviceType, int serviceState)
+        throws InvalidHandleException, SystemException
+    {
+        if (handle == 0L)
+            throw new InvalidHandleException();
+        services.clear();
+        int rc = enum1(handle, serviceType, serviceState, services);
+        if (rc != 0)
+            throw new SystemException(Status.describe(rc));
+        return services;
+    }
+
+    public final String[] getServiceNames(int serviceType, int serviceState)
+        throws InvalidHandleException, SystemException
+    {
+        if (handle == 0L)
+            throw new InvalidHandleException();
+        return enum0(handle, serviceType, serviceState);
+    }
+
+    /**
+     * Closes a connection to the service control manager on the
+     * local computer. The method will invalidate all opened
+     * Services connected to this manager.
+     */
+    public void close()
+    {
+        if (handle != 0L)
+            Win32.CloseServiceHandle(handle);
+        handle = 0L;
+    }
+
+}

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

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java?rev=1148309&r1=1148308&r2=1148309&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
Tue Jul 19 13:17:02 2011
@@ -52,8 +52,6 @@ final class Win32
     public static final int         MEM_RELEASE             = 0x00008000;
 
     public static final int         INVALID_HANDLE_VALUE    = 0xFFFFFFFF;
-    public static final String      SERVICES_ACTIVE_DATABASE        = "ServicesActive";
-    public static final String      SERVICES_FAILED_DATABASE        = "ServicesFailed";
 
     public static native int        CloseHandle(int handle);
     public static native int        LocalFree(long ptr);
@@ -70,8 +68,9 @@ final class Win32
     public static native int        VirtualFree(long addr, long size, int type);
 
     /* Service API */
-    public static native long       OpenSCManager(String machine, String database, int mode);
+    public static native long       OpenSCManager(String machine, String database, int desirecAccess);
     public static native int        CloseServiceHandle(long handle);
+    public static native int        DeleteService(long handle);
     public static native long       OpenService(long scm, String name, int access);
     public static native int        QueryServiceStatusEx(long service, int[] SERVICE_STATUS_PROCESS);
     public static native int        StartService(long service, int nargs, String[] args);

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1148309&r1=1148308&r2=1148309&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Tue Jul 19 13:17:02 2011
@@ -90,9 +90,11 @@ WIN32_SOURCES=\
 	$(TOPDIR)\os\win32\posix.c \
 	$(TOPDIR)\os\win32\procmutex.c \
 	$(TOPDIR)\os\win32\registry.c \
+	$(TOPDIR)\os\win32\scm.c \
 	$(TOPDIR)\os\win32\security.c \
 	$(TOPDIR)\os\win32\selectset.c \
 	$(TOPDIR)\os\win32\semaphore.c \
+	$(TOPDIR)\os\win32\service.c \
 	$(TOPDIR)\os\win32\shmem.c \
 	$(TOPDIR)\os\win32\sockopts.c \
 	$(TOPDIR)\os\win32\sockstream.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h?rev=1148309&r1=1148308&r2=1148309&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h Tue Jul 19 13:17:02 2011
@@ -27,6 +27,11 @@ ACR_CLASS_DTOR(ArrayList);
 ACR_CLASS_CTOR(HashSet);
 ACR_CLASS_DTOR(HashSet);
 
+#if defined(WINDOWS)
+ACR_CLASS_CTOR(Service);
+ACR_CLASS_DTOR(Service);
+#endif
+
 int     AcrArrayListAdd(JNI_STDARGS, jobject e);
 int     AcrArrayListEnsureCapacity(JNI_STDARGS, jint size);
 int     AcrHashSetAdd(JNI_STDARGS, jobject e);

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/scm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/scm.c?rev=1148309&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/scm.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/scm.c Tue Jul 19 13:17:02 2011
@@ -0,0 +1,209 @@
+/* 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.
+ */
+
+#include "acr/string.h"
+#include "acr/debug.h"
+#include "acr/memory.h"
+#include "acr/jniapi.h"
+#include "acr/port.h"
+#include "acr/clazz.h"
+#include "acr/misc.h"
+#include "arch_opts.h"
+
+/* Service Control Manager windows implementation
+ */
+extern jobject
+AcrNewServiceObject(JNI_STDENV, LPENUM_SERVICE_STATUS_PROCESSW ss);
+
+typedef struct svc_enum_t svc_enum_t;
+struct svc_enum_t {
+    svc_enum_t  *next;
+    DWORD        size;
+    BYTE         data[1];
+};
+
+static void free_list(svc_enum_t *head)
+{
+    svc_enum_t *next;
+
+    while (head != 0) {
+        next = head->next;
+        AcrFree(head);
+        head = next;
+    }
+}
+
+ACR_WIN_EXPORT(jobjectArray, ServiceControlManager, enum0)(JNI_STDARGS, jlong hscm,
+                                                           jint stype, jint sstate)
+{
+    jobjectArray es = 0;
+    SC_HANDLE scm = J2P(hscm, SC_HANDLE);
+    svc_enum_t *head = 0;
+    svc_enum_t *list = 0;
+    LPBYTE lpBuffer  = 0;
+    DWORD dwSize     = 0;
+    DWORD dwNumServices  = 0;
+    DWORD dwResumeHandle = 0;
+    BOOL rs;
+    int  rc = 0;
+    int cnt = 0;
+    LPENUM_SERVICE_STATUS_PROCESSW lpService;
+
+    if (IS_INVALID_HANDLE(scm)) {
+        ACR_THROW_SYS(ACR_EINVAL);
+        return 0;
+    }
+    do {
+        rs = EnumServicesStatusExW(scm,
+                                   SC_ENUM_PROCESS_INFO,
+                                   stype,
+                                   sstate,
+                                   lpBuffer,
+                                   dwSize,
+                                   &dwSize,
+                                   &dwNumServices,
+                                   &dwResumeHandle,
+                                   0 /* Ignore Group names */);
+        if (rs == FALSE) {
+            if ((rc = GetLastError()) != ERROR_MORE_DATA) {
+                /* Critical error */
+                goto finally;
+            }
+            rc = 0;
+            /* Allocate more storage */
+            list = AcrMalloc(env, dwSize + sizeof(svc_enum_t));
+            if (list == 0) {
+                /* Exception was already thrown */
+                goto finally;
+            }
+            list->next = head;
+            lpBuffer   = list->data;
+            /* TODO: Make list ordered as its populated */
+            head = list;
+        }
+        list->size = dwNumServices;
+        cnt += list->size;
+
+    } while (rs == FALSE);
+
+    /* Create output String[] array */
+    es = AcrNewCoreObjectArray(env, ACR_CC_STRING, cnt);
+    if (es == 0) {
+        goto finally;
+    }
+    list = head;
+    while (list != 0) {
+        DWORD i, idx = 0;
+        lpService = (LPENUM_SERVICE_STATUS_PROCESSW)list->data;
+        for (i = 0; i < list->size; i++) {
+            jstring s = AcrNewJavaStringW(env, lpService[i].lpServiceName);
+            if (s != 0)
+                (*env)->SetObjectArrayElement(env, es, idx++, s);
+            else
+                break;
+            if ((*env)->ExceptionCheck(env)) {
+                es = 0;
+                break;
+            }
+            (*env)->DeleteLocalRef(env, s);
+        }
+        list = list->next;
+    }
+finally:
+    free_list(head);
+    if (rc != 0) {
+        ACR_THROW_SYS(rc);
+        return 0;
+    }
+    return es;
+}
+
+ACR_WIN_EXPORT(jint, ServiceControlManager, enum1)(JNI_STDARGS, jlong hscm,
+                                                   jint stype, jint sstate,
+                                                   jobject sset)
+{
+    SC_HANDLE scm = J2P(hscm, SC_HANDLE);
+    svc_enum_t *head = 0;
+    svc_enum_t *list = 0;
+    LPBYTE lpBuffer  = 0;
+    DWORD dwSize     = 0;
+    DWORD dwNumServices  = 0;
+    DWORD dwResumeHandle = 0;
+    BOOL rs;
+    int  rc = 0;
+    int cnt = 0;
+    LPENUM_SERVICE_STATUS_PROCESSW lpService;
+
+    if (IS_INVALID_HANDLE(scm)) {
+        return ACR_EINVAL;
+    }
+    do {
+        rs = EnumServicesStatusExW(scm,
+                                   SC_ENUM_PROCESS_INFO,
+                                   stype,
+                                   sstate,
+                                   lpBuffer,
+                                   dwSize,
+                                   &dwSize,
+                                   &dwNumServices,
+                                   &dwResumeHandle,
+                                   0 /* Ignore Group names */);
+        if (rs == FALSE) {
+            if ((rc = GetLastError()) != ERROR_MORE_DATA) {
+                /* Critical error */
+                goto finally;
+            }
+            rc = 0;
+            /* Allocate more storage */
+            list = AcrMalloc(env, dwSize + sizeof(svc_enum_t));
+            if (list == 0) {
+                /* Exception was already thrown */
+                goto finally;
+            }
+            list->next = head;
+            lpBuffer   = list->data;
+            /* TODO: Make list ordered as its populated */
+            head = list;
+        }
+        list->size = dwNumServices;
+        cnt += list->size;
+
+    } while (rs == FALSE);
+
+    AcrArrayListEnsureCapacity(env, sset, cnt);
+    list = head;
+    while (list != 0) {
+        DWORD i, idx = 0;
+        lpService = (LPENUM_SERVICE_STATUS_PROCESSW)list->data;
+        for (i = 0; i < list->size; i++) {
+            jobject s = AcrNewServiceObject(env, lpService);
+            if (s != 0)
+                AcrArrayListAdd(env, sset, s);
+            else
+                break;
+            if ((*env)->ExceptionCheck(env)) {
+                rc = ACR_EGENERAL;
+                break;
+            }
+            (*env)->DeleteLocalRef(env, s);
+            ++lpService;
+        }
+        list = list->next;
+    }
+finally:
+    free_list(head);
+    return rc;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/scm.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/service.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/service.c?rev=1148309&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/service.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/service.c Tue Jul 19 13:17:02 2011
@@ -0,0 +1,96 @@
+/* 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.
+ */
+
+#include "acr/string.h"
+#include "acr/debug.h"
+#include "acr/memory.h"
+#include "acr/jniapi.h"
+#include "acr/port.h"
+#include "acr/clazz.h"
+#include "acr/misc.h"
+#include "arch_opts.h"
+
+/* Windows Service utility functions
+ */
+
+J_DECLARE_CLAZZ = {
+    INVALID_FIELD_BASE,
+    0,
+    0,
+    0,
+    ACR_WIN_CP "Service"
+};
+
+J_DECLARE_M_ID(0000) = {
+    0,
+    "<init>",
+    "(Ljava/lang/String;Ljava/lang/String;[I)V"
+};
+
+ACR_CLASS_CTOR(Service)
+{
+    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+        return JNI_FALSE;
+    J_LOAD_METHOD(0000);
+
+    _clazzn.u = 1;
+    return JNI_TRUE;
+}
+
+ACR_CLASS_DTOR(Service)
+{
+    AcrUnloadClass(env, &_clazzn);
+}
+
+jobject
+AcrNewServiceObject(JNI_STDENV, LPENUM_SERVICE_STATUS_PROCESSW ss)
+{
+    jobject si = 0;
+    jstring name;
+    jstring desc = 0;
+    jobjectArray stats;
+    jint ja[9];
+
+    if ((name = AcrNewJavaStringW(env, ss->lpServiceName)) == 0)
+        goto finally;
+    if ((stats = (*env)->NewIntArray(env, 9)) == 0)
+        goto finally;
+    if (ss->lpDisplayName != 0 && *(ss->lpDisplayName) != 0)
+        desc = AcrNewJavaStringW(env, ss->lpDisplayName);
+    ja[0] = ss->ServiceStatusProcess.dwServiceType;
+    ja[1] = ss->ServiceStatusProcess.dwCurrentState;
+    ja[2] = ss->ServiceStatusProcess.dwControlsAccepted;
+    ja[3] = ss->ServiceStatusProcess.dwWin32ExitCode;
+    ja[4] = ss->ServiceStatusProcess.dwServiceSpecificExitCode;
+    ja[5] = ss->ServiceStatusProcess.dwCheckPoint;
+    ja[6] = ss->ServiceStatusProcess.dwWaitHint;
+    ja[7] = ss->ServiceStatusProcess.dwProcessId;
+    ja[8] = ss->ServiceStatusProcess.dwServiceFlags;
+    (*env)->SetIntArrayRegion(env, stats, 0, 9, ja);
+    /* Create the object -> new Service(String,String,int[])
+     */
+    si = (*env)->NewObject(env, _clazzn.i, J4MID(0000), name, desc, stats);
+    /* Delete local references cause we are probably
+     * invoked inside the loop
+     */
+    (*env)->DeleteLocalRef(env, name);
+    (*env)->DeleteLocalRef(env, stats);
+    if (desc != 0)
+        (*env)->DeleteLocalRef(env, desc);
+finally:
+    return si;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/service.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c?rev=1148309&r1=1148308&r2=1148309&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c Tue Jul 19 13:17:02 2011
@@ -307,3 +307,16 @@ ACR_WIN_EXPORT(jint, Win32, ControlServi
     else
         return ACR_GET_OS_ERROR();
 }
+
+ACR_WIN_EXPORT(jint, Win32, DeleteService)(JNI_STDARGS, jlong hsvc)
+{
+    SC_HANDLE svc = J2P(hsvc, SC_HANDLE);
+
+    if (IS_INVALID_HANDLE(svc))
+        return ACR_EINVAL;
+    if (!DeleteService(svc))
+        return ACR_GET_OS_ERROR();
+    else
+        return 0;
+}
+

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=1148309&r1=1148308&r2=1148309&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Tue Jul 19 13:17:02 2011
@@ -227,7 +227,7 @@ AcrLoadRuntimeClasses(JNI_STDENV)
     ACR_CLASS_LOAD(FileDescriptor);
 
 #ifdef WIN32
-
+    ACR_CLASS_LOAD(Service);
 #endif
     return JNI_TRUE;
 }

Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestService.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestService.java?rev=1148309&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestService.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestService.java
Tue Jul 19 13:17:02 2011
@@ -0,0 +1,63 @@
+/* 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.platform.windows;
+
+import java.io.IOException;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import org.testng.annotations.*;
+import org.testng.Assert;
+
+public class TestService extends Assert
+{
+
+    @Test(groups = { "windows" })
+    public void enumServices()
+        throws Exception
+    {
+        ServiceControlManager scm = new ServiceControlManager();
+        scm.open(ServiceControlManager.SC_MANAGER_ENUMERATE_SERVICE);
+
+        ArrayList<Service> services = scm.getServices(ServiceControlManager.SERVICE_WIN32,
+                                                      ServiceControlManager.SERVICE_STATE_ALL);
+        assertFalse(services.isEmpty());
+        for (Service s : services) {
+            assertNotNull(s.getName());
+            // Uncomment for debugging purposes
+            // System.out.println(s.getName() + " - " + s.getDisplayName());
+        }
+    }
+
+    @Test(groups = { "windows" })
+    public void enumServiceNames()
+        throws Exception
+    {
+        ServiceControlManager scm = new ServiceControlManager();
+        scm.open(ServiceControlManager.SC_MANAGER_ENUMERATE_SERVICE);
+
+        String[] services = scm.getServiceNames(ServiceControlManager.SERVICE_WIN32,
+                                                ServiceControlManager.SERVICE_STATE_ALL);
+        assertNotNull(services);
+        for (int i = 0; i < services.length; i++) {
+            assertNotNull(services[i]);
+            // Uncomment for debugging purposes
+            // System.out.println(services[i]);
+        }
+    }
+
+}

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



Mime
View raw message