com.jniwrapper.win32.service
Class ServiceManager

java.lang.Object
  extended by com.jniwrapper.Parameter
      extended by com.jniwrapper.Pointer.Void
          extended by com.jniwrapper.win32.Handle
              extended by com.jniwrapper.win32.service.ServiceManager
All Implemented Interfaces:
com.jniwrapper.IntegerParameter, com.jniwrapper.PointerParameter

public class ServiceManager
extends Handle

This class represents a service manager that contains operations for working with services.

Note: Service objects obtained from this class must be closed when done.


     Service service = serviceManager.open("myservice");
 

// last usage of service object service.start();

service.close();

Note: Available for Windows NT, Windows 2000, Windows XP, Windows Server 2003.


Nested Class Summary
static class ServiceManager.AccessRights
          This class represents access to the service manager.
 
Field Summary
 
Fields inherited from class com.jniwrapper.win32.Handle
INFINITE_TIMEOUT, INVALID_HANDLE_VALUE, STATUS_ABANDONED_WAIT_0, STATUS_TIMEOUT, STATUS_WAIT_0
 
Fields inherited from class com.jniwrapper.Parameter
LICENSE_TYPE, V
 
Constructor Summary
ServiceManager()
          Creates a service manager with full access.
 
Method Summary
protected  void checkOpened()
           
protected  void checkResult(java.lang.String msg, com.jniwrapper.Bool result)
          A utility method for checking an operation result.
 boolean close()
          Closes the handle to a service-related object.
 Service create(java.lang.String serviceName, java.lang.String displayName, java.io.File executable)
          Creates a service with given attributes in the service database.
 Service create(java.lang.String serviceName, java.lang.String displayName, java.io.File executable, Service.StartupType startupType)
          Creates a service with given attributes in the service database.
 Service create(java.lang.String serviceName, java.lang.String displayName, java.io.File executable, Service.StartupType startupType, java.lang.String[] dependencies)
          Creates a service with given attributes in the service database.
 Service create(java.lang.String serviceName, java.lang.String displayName, java.io.File executableFile, Service.StartupType startupType, java.lang.String[] dependencies, Service.AccessRights access, Service.Type serviceType, Service.ErrorControl errorControl, java.lang.String startAccount, java.lang.String password)
          Creates a service with the given attributes in the service database.
 ServiceManager.AccessRights getAccess()
          Returns access to the service manager.
 java.lang.String getComputerName()
          Returns the name of the computer to where create, enumerate or open services.
 java.lang.String[] getServiceNames()
          Returns names of all registered services.
 Service[] getServices()
          Returns available services.
 Service[] getServices(Service.AccessRights serviceAccessRight)
          Returns available services.
protected  boolean isLocked()
           
protected  boolean isOpened()
           
protected  void lock()
          Locks the handle for closing.
 Service open(java.lang.String serviceName)
          Opens an existing service.
 Service open(java.lang.String serviceName, Service.AccessRights access)
          Opens an existing service.
 void setAccess(ServiceManager.AccessRights access)
          Sets access to the service manager.
 void setComputerName(java.lang.String computerName)
          Sets the name of the computer to where create, enumerate or open services.
protected  void setOpened(boolean isOpened)
           
protected  void unlock()
          Unlocks the handle for closing.
 
Methods inherited from class com.jniwrapper.win32.Handle
clone, closeHandle, equals, waitFor, waitFor
 
Methods inherited from class com.jniwrapper.Pointer.Void
asFunction, asFunction, asTypedPointer, castTo, castTo, getDebugInfo, getLength, getValue, hashCode, isNull, read, setValue, toString, write
 
Methods inherited from class com.jniwrapper.Parameter
, a, a, acceptIOPerformer, asReturnValue, b, dataBufferAssigned, getAlignedLength, getAlignmentRequirement, getDataBuffer, getDataBufferOffset, indent, pop, push, read, read, setDataBuffer, toByteArray, write
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ServiceManager

public ServiceManager()
Creates a service manager with full access.

Method Detail

getComputerName

public java.lang.String getComputerName()
Returns the name of the computer to where create, enumerate or open services.


setComputerName

public void setComputerName(java.lang.String computerName)
Sets the name of the computer to where create, enumerate or open services.


getAccess

public ServiceManager.AccessRights getAccess()
Returns access to the service manager.

Returns:
access to the service manager.
See Also:
Platform SDK: DLLs, Processes, and Threads

setAccess

public void setAccess(ServiceManager.AccessRights access)
Sets access to the service manager.

To access services on remote computer use query status access right shown in the sample below:


     ServiceManager.AccessRights accessRights = new ServiceManager.AccessRights();
     accessRights.setEnumerateService(true);

     serviceManager.setAccess(accessRights);
 

Parameters:
access - access to the service manager.

create

public Service create(java.lang.String serviceName,
                      java.lang.String displayName,
                      java.io.File executable)
Creates a service with given attributes in the service database. You can specify a computer where the service database resides if the computerName property is set.

Parameters:
serviceName - service name.
displayName - display name.
executable - binary path of the executable file for the service.
Returns:
Service object.
Throws:
java.lang.RuntimeException - if fails to create a service.

create

public Service create(java.lang.String serviceName,
                      java.lang.String displayName,
                      java.io.File executable,
                      Service.StartupType startupType)
Creates a service with given attributes in the service database. You can specify a computer where the service database resides if the computerName property is set.

Parameters:
serviceName - service name.
displayName - display name.
executable - binary path of the executable file for the service.
startupType - startup type of service.
Returns:
handle to service object.
Throws:
java.lang.RuntimeException - if fails to create a service.

create

public Service create(java.lang.String serviceName,
                      java.lang.String displayName,
                      java.io.File executable,
                      Service.StartupType startupType,
                      java.lang.String[] dependencies)
Creates a service with given attributes in the service database. You can specify computer where the service database resides if the computerName property is set.

Parameters:
serviceName - service name.
displayName - display name.
executable - binary path of the executable file for the service.
startupType - startup type of the service.
dependencies - names of services that the system must start before this service.
Returns:
handle to service object.
Throws:
java.lang.RuntimeException - if fails to create a service.

create

public Service create(java.lang.String serviceName,
                      java.lang.String displayName,
                      java.io.File executableFile,
                      Service.StartupType startupType,
                      java.lang.String[] dependencies,
                      Service.AccessRights access,
                      Service.Type serviceType,
                      Service.ErrorControl errorControl,
                      java.lang.String startAccount,
                      java.lang.String password)
Creates a service with the given attributes in the service database. You can specify a computer where the service database resides if the computerName property is set.

Parameters:
serviceName - service name.
displayName - display name.
executableFile - executable file for the service.
startupType - startup type of the service.
dependencies - names of services that the system must start before this service.
access - access to the service.
serviceType - service type.
errorControl - error control for the service.
startAccount - Account under which the service starts: in the form domainName\\userName or null for LocalSystem account.
password - password for start account.
Returns:
handle to the service object.
Throws:
java.lang.RuntimeException - if fails to create service.

open

public Service open(java.lang.String serviceName)
Opens an existing service.

Parameters:
serviceName - service name.
Returns:
handle to an existing service if succeeded; otherwise null.
Throws:
java.lang.RuntimeException - if the service is not found.

open

public Service open(java.lang.String serviceName,
                    Service.AccessRights access)
Opens an existing service.

Parameters:
serviceName - service name.
access - access to the service.
Returns:
handle to an existing service.
Throws:
java.lang.RuntimeException - if the service is not found.

getServices

public Service[] getServices()
Returns available services.

Use getServices(Service.AccessRights) method for getting services on remote computer.

Note: Returned services are in open state. So you should call service.close() for each returned service.

Returns:
available services.

getServices

public Service[] getServices(Service.AccessRights serviceAccessRight)
Returns available services.

To access services on remote computer use query status access right shown in the sample below:


     Service.AccessRights serviceAccessRights = new Service.AccessRights();
     serviceAccessRights.setQueryStatus(true);
     Service[] remoteServices = serviceManager.getServices(serviceAccessRights);
 

Note: Returned services are in open state. So you should call service.close() for each returned service.

Parameters:
serviceAccessRight - access rights for opened services
Returns:
available services.

getServiceNames

public java.lang.String[] getServiceNames()
Returns names of all registered services.

Returns:
service names.

setOpened

protected void setOpened(boolean isOpened)

isOpened

protected boolean isOpened()

lock

protected void lock()
Locks the handle for closing.


unlock

protected void unlock()
Unlocks the handle for closing.


isLocked

protected boolean isLocked()
Returns:
true, if the handle is locked for closing.

checkOpened

protected void checkOpened()
                    throws java.lang.IllegalStateException
Throws:
java.lang.IllegalStateException - if the handle is closed.

close

public boolean close()
Closes the handle to a service-related object.

Returns:
true, if succeeds; otherwise false.

checkResult

protected void checkResult(java.lang.String msg,
                           com.jniwrapper.Bool result)
A utility method for checking an operation result.

Parameters:
msg - error message for the exception to be thrown.
result - boolean value to verify.
Throws:
ServiceException - if the result is false.