com.gridsystems.windowsutils
Class WindowsUtils

java.lang.Object
  extended by com.gridsystems.windowsutils.WindowsUtils

public final class WindowsUtils
extends java.lang.Object

MS Windows service management.

Version:
1.0
Author:
Xmas

Method Summary
static void addUserPrivilege(java.lang.String hostName, java.lang.String user, java.lang.String privilege)
          Adds a specific privilege to a user.
static boolean checkAdministrator()
          Tells if the user executing the program has administrator rights.
static boolean checkWindowsUser(java.lang.String user)
          Chequeo de usuarios en XP y NT.
static void createWindowsService(java.lang.String servicename, java.lang.String executable, java.lang.String user, java.lang.String pass)
          Installs a windows service allowing to set a specific user account.
static java.lang.String getServiceStartUser(java.lang.String serviceName)
          Gets the name of the user on whose account the service starts.
static void removeWindowsService(java.lang.String servicename)
          Elimina un Servicio Windows.
static boolean serviceExists(java.lang.String serviceName)
          Tells if a service with the specified name exists.
static boolean startService(java.lang.String serviceName, java.lang.String[] arguments, boolean errorIfRunning)
          Starts a service.
static boolean stopService(java.lang.String serviceName, boolean errorIfStopped)
          Stops a service.
static boolean userHasPrivilege(java.lang.String user, java.lang.String privilege)
          Checks if an user has a permission.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createWindowsService

public static void createWindowsService(java.lang.String servicename,
                                        java.lang.String executable,
                                        java.lang.String user,
                                        java.lang.String pass)
                                 throws java.lang.Exception
Installs a windows service allowing to set a specific user account.

Parameters:
servicename - a String with the name of the service.
executable - a String with the path to the executable file.
user - a String with the user account to run the service with. If null or empty, the service is set to run as a system service. If an user account is specified, it is checked that startst with ".\" and, if it does not, ".\" is added at the beginning of the user account.
pass - a String with the password of the account used to run the service, if it is an user account.
Throws:
java.lang.Exception - with an human-readable message if any of the parameters was wrong or if the native process to install the service failed.

removeWindowsService

public static void removeWindowsService(java.lang.String servicename)
                                 throws java.lang.Exception
Elimina un Servicio Windows.

Parameters:
servicename - the service name
Throws:
java.lang.Exception - if error

addUserPrivilege

public static void addUserPrivilege(java.lang.String hostName,
                                    java.lang.String user,
                                    java.lang.String privilege)
                             throws java.lang.Exception
Adds a specific privilege to a user.

Parameters:
hostName - Host name
user - User name
privilege - Privilege name
Throws:
java.lang.Exception - if error

userHasPrivilege

public static boolean userHasPrivilege(java.lang.String user,
                                       java.lang.String privilege)
                                throws java.lang.Exception
Checks if an user has a permission.

Parameters:
user - a String with the username that is checked.
privilege - a String with the permission being checked.
Returns:
a boolean that is true if and only if the user has granted the permission.
Throws:
java.lang.Exception - if error

checkWindowsUser

public static boolean checkWindowsUser(java.lang.String user)
                                throws java.lang.Exception
Chequeo de usuarios en XP y NT.

Parameters:
user - el usuario.
Returns:
boolean Devuelve un boolean que indica si un usuario existe en XP y NT
Throws:
java.lang.Exception - if error

startService

public static boolean startService(java.lang.String serviceName,
                                   java.lang.String[] arguments,
                                   boolean errorIfRunning)
Starts a service.

Parameters:
serviceName - a String with the name of the service to start.
arguments - a String[] with the list of argument to pass to the service.
errorIfRunning - a boolean that is true if the method should consider that the service is already running as an error.
Returns:
a boolean that is true if and only if the service was correctly started or, if errorIfRunning is false, if the service was already running.

stopService

public static boolean stopService(java.lang.String serviceName,
                                  boolean errorIfStopped)
Stops a service.

Parameters:
serviceName - a String with the name of the service to stop.
errorIfStopped - a boolean that is true if the method should consider that the service is already stopped or stopping as an error.
Returns:
a boolean that is true if and only if the service received correctly the signal to stop or, if errorIfStopped is false, if it was already stopped or stopping.

serviceExists

public static boolean serviceExists(java.lang.String serviceName)
                             throws java.lang.Exception
Tells if a service with the specified name exists.

Parameters:
serviceName - a String with the name of the service.
Returns:
a boolean that is true if and only if a service with the specified name exists.
Throws:
java.lang.Exception - if an error happened when getting the installed services.

checkAdministrator

public static boolean checkAdministrator()
                                  throws java.lang.Exception
Tells if the user executing the program has administrator rights.

Returns:
a boolean that is true if and only if the user running the program has administrator rights (belongs to the group of administrators).
Throws:
java.lang.Exception - if an error happened when getting the information.

getServiceStartUser

public static java.lang.String getServiceStartUser(java.lang.String serviceName)
                                            throws java.lang.Exception
Gets the name of the user on whose account the service starts.

Parameters:
serviceName - the name of the service being checked.
Returns:
a String with the name of the user as returned by the function QueryServiceConfig function. In case of error, it returns an empty String.
Throws:
java.lang.Exception - in case of error.


Copyright © 2007-2008 Grid Systems, S.A.. All Rights Reserved.