Author: mturk
Date: Tue Jul 19 18:17:35 2011
New Revision: 1148470

URL: http://svn.apache.org/viewvc?rev=1148470&view=rev
Log:
Implement more of the win32 Service API

Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceErrorControl.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceStartType.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
    commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c

Modified: 
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=1148470&r1=1148469&r2=1148470&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
 Tue Jul 19 18:17:35 2011
@@ -34,6 +34,7 @@ public final class Service implements Cl
     private String  serviceName;
     private String  displayName;
     private int[]   serviceStatusProcess;
+    private long    currentQueryWindow = 0L;
 
     // Named indexes inside serviceStatusProcess array.
     // They are here just for easier entry access.
@@ -125,6 +126,15 @@ public final class Service implements Cl
     public static final int         SERVICE_USER_DEFINED_CONTROL    = 
0x00000100;
 
     /**
+     * The service has returned a service-specific error code.
+     */
+    public static final int         ERROR_SERVICE_SPECIFIC_ERROR    = 1066;
+
+    /**
+     * The service runs in a system process that must always be running.
+     */
+    public static final int         SERVICE_RUNS_IN_SYSTEM_PROCESS  = 
0x00000001;
+    /**
      * Creates a new service object
      */
     public Service(String name)
@@ -152,10 +162,21 @@ public final class Service implements Cl
     public synchronized void close()
         throws IOException
     {
-        if (handle != 0L) {
-            Win32.CloseServiceHandle(handle);
-            handle = 0L;    
-        }    
+        Win32.CloseServiceHandle(handle);
+        handle = 0L;
+    }
+
+    private synchronized void queryStatus()
+    {
+        if (handle == 0L)
+            return;
+        long now = System.nanoTime();
+        // Use one millisecond query window
+        //
+        if (now > (currentQueryWindow + 1000000)) {
+            Win32.QueryServiceStatusEx(handle, serviceStatusProcess);
+            currentQueryWindow = now;
+        }
     }
 
     /**
@@ -170,11 +191,7 @@ public final class Service implements Cl
     protected final void finalize()
         throws Throwable
     {
-        try {
-            close();
-        } catch (Exception x) {
-            // Ignore exceptions on finalize
-        }
+        close();
     }
 
     public String getName()
@@ -186,6 +203,102 @@ public final class Service implements Cl
     {
         return displayName;
     }
-    
-    
+
+    /**
+     * Get the current state of the service.
+     *
+     * @see ServiceState
+     */
+    public ServiceState getCurrentState()
+    {
+        queryStatus();
+        return ServiceState.valueOf(serviceStatusProcess[currentState]);
+    }
+
+    public int getProcessId()
+    {
+        queryStatus();
+        return serviceStatusProcess[processId];
+    }
+
+    public EnumSet<ServiceControlsAccepted>  getControlsAccepted()
+    {
+        queryStatus();
+        return 
ServiceControlsAccepted.valueOf(serviceStatusProcess[controlsAccepted]);
+    }
+
+    /**
+     * Gets the type of this service.
+     *
+     * @see ServiceType
+     */
+    public EnumSet<ServiceType> getType()
+    {
+        queryStatus();
+        return ServiceType.valueOf(serviceStatusProcess[serviceType]);
+    }
+
+    /**
+     * Gets the startup type of this service.
+     *
+     * @see ServiceStartType
+     */
+    public ServiceStartType getStartType()
+    {
+        queryStatus();
+        return ServiceStartType.valueOf(serviceStatusProcess[startType]);
+    }
+
+    /**
+     * The error code that the service uses to report an error that occurs
+     * when it is starting or stopping.
+     * To return an error code specific to the service, the service must
+     * set this value to {@code ERROR_SERVICE_SPECIFIC_ERROR} to indicate that
+     * the {@code getServiceExitCode} member contains the error code.
+     * The service should set this value to {@code zero} when it is running
+     * and when it terminates normally.
+     *
+     * @return the exit code.
+     */
+    public int getExitCode()
+    {
+        queryStatus();
+        return serviceStatusProcess[win32ExitCode];
+    }
+
+    /**
+     * The service-specific error code that the service returns when an
+     * error occurs while the service is starting or stopping.
+     * This value is ignored unless the getExitCod member is set to
+     * {@code ERROR_SERVICE_SPECIFIC_ERROR}.
+     *
+     * @return the service specific exit code.
+     */
+    public int getServiceExitCode()
+    {
+        queryStatus();
+        return serviceStatusProcess[serviceSpecificExitCode];
+    }
+
+    /**
+     * Gets the services' running flags.
+     * <p>
+     * If this value is {@code zero} the service is running in a
+     * process that is not a system process, or it is not running.
+     * If the service is running in a process that is not a system process,
+     * {@code getProcessId} is nonzero. If the service is not running,
+     * {@code getProcessId} is zero.
+     * <br/>
+     * If this value is {@code SERVICE_RUNS_IN_SYSTEM_PROCESS} then the
+     * service runs in a system process that must always be running.
+     * </p>
+     *
+     * @return service flags.
+     */
+    public int getFlags()
+    {
+        queryStatus();
+        return serviceStatusProcess[serviceFlags];
+
+    }
 }

Modified: 
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=1148470&r1=1148469&r2=1148470&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceControlManager.java
 Tue Jul 19 18:17:35 2011
@@ -140,10 +140,8 @@ public class ServiceControlManager imple
     public synchronized void close()
         throws IOException
     {
-        if (handle != 0L) {
-            Win32.CloseServiceHandle(handle);
-            handle = 0L;
-        }
+        Win32.CloseServiceHandle(handle);
+        handle = 0L;
     }
 
     /**
@@ -158,11 +156,7 @@ public class ServiceControlManager imple
     protected final void finalize()
         throws Throwable
     {
-        try {
-            close();
-        } catch (Exception x) {
-            // Ignore exceptions on finalize
-        }
+        close();
     }
 
     public Service open(String name, int desiredAccess)
@@ -190,7 +184,8 @@ public class ServiceControlManager imple
             throw new InvalidHandleException();
         // binaryPath is optional
         String path = binaryPath == null ? null : binaryPath.getPath();
-        long svc = Win32.CreateService(handle, name, displayName, 0,
+        long svc = Win32.CreateService(handle, name, displayName,
+                                       Service.SERVICE_ALL_ACCESS,
                                        serviceType.valueOf(),
                                        startType.valueOf(),
                                        errorControl.valueOf(),

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceErrorControl.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceErrorControl.java?rev=1148470&r1=1148469&r2=1148470&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceErrorControl.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceErrorControl.java
 Tue Jul 19 18:17:35 2011
@@ -25,6 +25,10 @@ public enum ServiceErrorControl
 {
 
     /**
+     * Unknown error contorl type.
+     */
+    UNKNOWN(   -1),
+    /**
      * The startup (boot) program logs the error but continues the
      * startup operation.
      */
@@ -66,6 +70,6 @@ public enum ServiceErrorControl
             if (e.value == value)
                 return e;
         }
-        return IGNORE;
+        return UNKNOWN;
     }
 }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceStartType.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceStartType.java?rev=1148470&r1=1148469&r2=1148470&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceStartType.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/ServiceStartType.java
 Tue Jul 19 18:17:35 2011
@@ -24,6 +24,10 @@ public enum ServiceStartType
 {
 
     /**
+     * Unknown start type.
+     */
+    UNKNOWN(   -1),
+    /**
      * A device driver started by the system loader.
      * This value is valid only for driver services.
      */
@@ -66,6 +70,6 @@ public enum ServiceStartType
             if (e.value == value)
                 return e;
         }
-        return BOOT;
+        return UNKNOWN;
     }
 }

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=1148470&r1=1148469&r2=1148470&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 18:17:35 2011
@@ -44,16 +44,19 @@ final class Win32
     public static final int         WAIT_TIMEOUT            = 0x00000102;
 
     /* Memory allocation flags  */
-    public static final int         MEM_COMMIT              = 0x00001000;
-    public static final int         MEM_RESERVE             = 0x00002000;
-    public static final int         MEM_RESET               = 0x00008000;
+    public static final int         MEM_COMMIT                      = 
0x00001000;
+    public static final int         MEM_RESERVE                     = 
0x00002000;
+    public static final int         MEM_RESET                       = 
0x00008000;
     /* Memory free flags  */
-    public static final int         MEM_DECOMMIT            = 0x00004000;
-    public static final int         MEM_RELEASE             = 0x00008000;
+    public static final int         MEM_DECOMMIT                    = 
0x00004000;
+    public static final int         MEM_RELEASE                     = 
0x00008000;
 
-    public static final int         INVALID_HANDLE_VALUE    = 0xFFFFFFFF;
+    public static final int         INVALID_HANDLE_VALUE            = 
0xFFFFFFFF;
+    public static final int         HANDLE_FLAG_INHERIT             = 
0x00000001;
+    public static final int         HANDLE_FLAG_PROTECT_FROM_CLOSE  = 
0x00000002;
 
     public static native int        CloseHandle(int handle);
+    public static native int        SetHandleInformation(int handle, int mask, 
int flags);
     public static native int        LocalFree(long ptr);
     public static native int        WaitForSingleObject(int handle, int 
timeout);
     public static native int        CreateMutex(long sa, boolean initialOwner, 
String name);

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java?rev=1148470&r1=1148469&r2=1148470&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
 Tue Jul 19 18:17:35 2011
@@ -72,6 +72,7 @@ final class WindowsShm extends Shm
             else
                 throw new SystemException(Status.describe(rc));
         }
+        Win32.SetHandleInformation(handle, Win32.HANDLE_FLAG_INHERIT, 0);
         base = Win32.MapViewOfFile(handle,
                                    AccessRights.FILE_MAP_ALL_ACCESS,
                                    0L, this.size + 16);
@@ -101,6 +102,7 @@ final class WindowsShm extends Shm
             else
                 throw new SystemException(Status.describe(rc));
         }
+        Win32.SetHandleInformation(handle, Win32.HANDLE_FLAG_INHERIT, 0);
         long header = Win32.MapViewOfFile(handle, AccessRights.FILE_MAP_READ, 
0L, 64);
         if (header == 0L) {
             Win32.CloseHandle(handle);

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=1148470&r1=1148469&r2=1148470&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 
18:17:35 2011
@@ -32,12 +32,23 @@ volatile LONG acr_signal_waiters = 0;
 
 ACR_WIN_EXPORT(jint, Win32, CloseHandle)(JNI_STDARGS, jint handle)
 {
+    if (handle == 0 || handle < 0)
+        return ACR_EINVAL;
     if (CloseHandle(I2H(handle)))
         return 0;
     else
         return ACR_GET_OS_ERROR();
 }
 
+ACR_WIN_EXPORT(jint, Win32, SetHandleInformation)(JNI_STDARGS, jint handle,
+                                                  jint mask, jint flags)
+{
+    if (SetHandleInformation(I2H(handle), mask, flags))
+        return 0;
+    else
+        return ACR_GET_OS_ERROR();
+}
+
 ACR_WIN_EXPORT(void, Win32, LocalFree)(JNI_STDARGS, jlong ptr)
 {
     if (ptr != 0LL)
@@ -269,7 +280,7 @@ ACR_WIN_EXPORT(jint, Win32, StartService
 
     if (IS_INVALID_HANDLE(svc))
         return ACR_EINVAL;
-    if (args != 0) {
+    if (args != 0 && nargs > 0) {
         wargs = AcrGetJavaStringArrayW(env, args);
         if (wargs == 0)
             return ACR_GET_OS_ERROR();


Reply via email to