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/07/20 09:14:51 UTC
svn commit: r1148632 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/platform/windows/Service.java
native/os/win32/registry.c
Author: mturk
Date: Wed Jul 20 07:14:50 2011
New Revision: 1148632
URL: http://svn.apache.org/viewvc?rev=1148632&view=rev
Log:
Implement win32 Service description set/get API
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Service.java
commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.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=1148632&r1=1148631&r2=1148632&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 Wed Jul 20 07:14:50 2011
@@ -23,6 +23,7 @@ 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.InvalidArgumentException;
import org.apache.commons.runtime.SystemException;
import org.apache.commons.runtime.TimeoutException;
import org.apache.commons.runtime.util.Utils;
@@ -148,12 +149,12 @@ public final class Service implements Cl
* Creates a new service object
*/
public Service(String name)
- throws IllegalArgumentException
+ throws InvalidArgumentException
{
if (name == null || name.length() < 1 || name.length() > 256)
- throw new IllegalArgumentException("invalid service name length");
+ throw new InvalidArgumentException("invalid service name length");
if (name.indexOf('/') != -1 || name.indexOf('\\') != -1)
- throw new IllegalArgumentException("invalid service name characters");
+ throw new InvalidArgumentException("invalid service name characters");
serviceName = name;
serviceStatusProcess = new int[9];
}
@@ -209,6 +210,9 @@ public final class Service implements Cl
return serviceName;
}
+ /**
+ * Gets the display name used by user interface programs to identify the service.
+ */
public String getDisplayName()
{
return displayName;
@@ -314,6 +318,39 @@ public final class Service implements Cl
}
/**
+ * Get this service's description.
+ *
+ * @return description string, or {@code null} if description was
+ * not set.
+ *
+ */
+ public String getDescription()
+ throws IOException
+ {
+ RegistryKey key = Registry.LocalMachine.open(SERVICES_KEY + "\\" + serviceName);
+ String desc = key.getValue("Description");
+ key.close();
+ return desc;
+ }
+
+ /**
+ * Set this service's description.
+ *
+ * @param desc description string to set, or {@code null} to remove the
+ * description.
+ */
+ public void setDescription(String desc)
+ throws IOException
+ {
+ RegistryKey key = Registry.LocalMachine.open(SERVICES_KEY + "\\" + serviceName, RegistryKeyAccessRights.WRITE);
+ if (desc == null)
+ key.deleteValue("Description");
+ else
+ key.setValue("Description", desc);
+ key.close();
+ }
+
+ /**
* Starts a service.
*
* @param args strings to be passed to the {@code ServiceMain} function for
@@ -382,7 +419,7 @@ public final class Service implements Cl
if (handle == 0L)
throw new InvalidHandleException();
if (c < 128 || c > 255)
- throw new IllegalArgumentException("control code must be in range from 128 to 255");
+ throw new InvalidArgumentException("control code must be in range from 128 to 255");
int rc = Win32.ControlService(handle, c, serviceStatusProcess);
if (rc != 0) {
if (Status.IS_EACCES(rc))
@@ -637,11 +674,30 @@ public final class Service implements Cl
}
}
+ /**
+ * Sets the display name to be used by user interface programs to
+ * identify the service.
+ * <p>
+ * This string has a maximum length of (@code 256} characters.
+ * The name is case-preserved in the service control manager.
+ * Display name comparisons are always case-insensitive.
+ * </p>
+ *
+ * @param name the description string to set.
+ *
+ * @throws InvalidArgumentException
+ * if the name is longer then {@code 256} characters.
+ */
public void setDisplayName(String name)
throws InvalidHandleException, SystemException
{
if (handle == 0L)
throw new InvalidHandleException();
+ if (name.length() > 256) {
+ // We could let the Windows API returns error.
+ // TODO: localize message.
+ throw new InvalidArgumentException("display name must have less then 256 characters");
+ }
int rc = Win32.ChangeServiceConfig(handle,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c?rev=1148632&r1=1148631&r2=1148632&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c Wed Jul 20 07:14:50 2011
@@ -94,7 +94,7 @@ ACR_WIN_EXPORT(jlong, RegistryKeyImpl, o
&skey);
} DONE_WITH_STR(name);
- if (rc) {
+ if (rc != 0) {
skey = NULL;
ACR_THROW_SYS_ERROR(rc);
}
@@ -121,7 +121,7 @@ ACR_WIN_EXPORT(jlong, RegistryKeyImpl, c
NULL);
} DONE_WITH_STR(name);
- if (rc) {
+ if (rc != 0) {
skey = 0;
ACR_THROW_SYS_ERROR(rc);
}