rid hungarian notation and fixup formatting
This commit is contained in:
parent
2f1392e264
commit
084300bce2
|
@ -71,18 +71,17 @@ public class Service extends Win32Bindings {
|
||||||
SERVICE_INTERROGATE |
|
SERVICE_INTERROGATE |
|
||||||
SERVICE_USER_DEFINED_CONTROL);
|
SERVICE_USER_DEFINED_CONTROL);
|
||||||
|
|
||||||
///////////////////////////////////////////////////////
|
private long manager;
|
||||||
// Object Variables
|
private long service;
|
||||||
private long m_hMgr;
|
|
||||||
private long m_hService;
|
|
||||||
|
|
||||||
private Service() throws Win32Exception
|
private Service() throws Win32Exception
|
||||||
{
|
{
|
||||||
this.m_hMgr = OpenSCManager("", SC_MANAGER_ALL_ACCESS);
|
this.manager = OpenSCManager("", SC_MANAGER_ALL_ACCESS);
|
||||||
|
|
||||||
if(this.m_hMgr == 0)
|
if (this.manager == 0) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static native List getServiceNames() throws Win32Exception;
|
public static native List getServiceNames() throws Win32Exception;
|
||||||
|
|
||||||
|
@ -90,28 +89,29 @@ public class Service extends Win32Bindings {
|
||||||
{
|
{
|
||||||
this();
|
this();
|
||||||
|
|
||||||
this.m_hService = OpenService(this.m_hMgr, serviceName,
|
this.service = OpenService(this.manager, serviceName,
|
||||||
SERVICE_ALL_ACCESS);
|
SERVICE_ALL_ACCESS);
|
||||||
|
|
||||||
if(this.m_hService == 0)
|
if (this.service == 0) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void finalize()
|
protected void finalize()
|
||||||
{
|
{
|
||||||
this.close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void close()
|
public synchronized void close()
|
||||||
{
|
{
|
||||||
if(this.m_hService != 0) {
|
if (this.service != 0) {
|
||||||
CloseServiceHandle(this.m_hService);
|
CloseServiceHandle(this.service);
|
||||||
this.m_hService = 0;
|
this.service = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(this.m_hMgr != 0) {
|
if (this.manager != 0) {
|
||||||
CloseServiceHandle(this.m_hMgr);
|
CloseServiceHandle(this.manager);
|
||||||
this.m_hMgr = 0;
|
this.manager = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ public class Service extends Win32Bindings {
|
||||||
String path)
|
String path)
|
||||||
throws Win32Exception
|
throws Win32Exception
|
||||||
{
|
{
|
||||||
return Service.create(serviceName, displayName, description,
|
return create(serviceName, displayName, description,
|
||||||
ServiceConfig.TYPE_WIN32_OWN_PROCESS,
|
ServiceConfig.TYPE_WIN32_OWN_PROCESS,
|
||||||
ServiceConfig.START_AUTO,
|
ServiceConfig.START_AUTO,
|
||||||
ServiceConfig.ERROR_NORMAL,
|
ServiceConfig.ERROR_NORMAL,
|
||||||
|
@ -131,33 +131,40 @@ public class Service extends Win32Bindings {
|
||||||
public static Service create(String serviceName, String displayName,
|
public static Service create(String serviceName, String displayName,
|
||||||
String description, int serviceType,
|
String description, int serviceType,
|
||||||
int startType, int errorControl,
|
int startType, int errorControl,
|
||||||
String path, String[] dependicies,
|
String path, String[] dependencies,
|
||||||
String startName, String password)
|
String startName, String password)
|
||||||
throws Win32Exception
|
throws Win32Exception
|
||||||
{
|
{
|
||||||
if(serviceName == null)
|
if (serviceName == null) {
|
||||||
throw new IllegalArgumentException("The serviceName argument " +
|
throw new IllegalArgumentException("The serviceName argument " +
|
||||||
"cannot be null.");
|
"cannot be null.");
|
||||||
if(displayName == null)
|
}
|
||||||
|
|
||||||
|
if (displayName == null) {
|
||||||
throw new IllegalArgumentException("The displayName argument " +
|
throw new IllegalArgumentException("The displayName argument " +
|
||||||
"cannot be null.");
|
"cannot be null.");
|
||||||
if(path == null)
|
}
|
||||||
|
|
||||||
|
if (path == null) {
|
||||||
throw new IllegalArgumentException("The displayName argument " +
|
throw new IllegalArgumentException("The displayName argument " +
|
||||||
"cannot be null.");
|
"cannot be null.");
|
||||||
|
}
|
||||||
|
|
||||||
Service service = new Service();
|
Service service = new Service();
|
||||||
service.m_hService = Service.CreateService(service.m_hMgr,
|
service.service = CreateService(service.manager,
|
||||||
serviceName,
|
serviceName,
|
||||||
displayName,
|
displayName,
|
||||||
serviceType,
|
serviceType,
|
||||||
startType,
|
startType,
|
||||||
errorControl,
|
errorControl,
|
||||||
path,
|
path,
|
||||||
dependicies,
|
dependencies,
|
||||||
startName,
|
startName,
|
||||||
password);
|
password);
|
||||||
if(service.m_hService == 0)
|
|
||||||
|
if (service.service == 0) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
|
}
|
||||||
|
|
||||||
service.setDescription(description);
|
service.setDescription(description);
|
||||||
|
|
||||||
|
@ -166,124 +173,130 @@ public class Service extends Win32Bindings {
|
||||||
|
|
||||||
public void delete() throws Win32Exception
|
public void delete() throws Win32Exception
|
||||||
{
|
{
|
||||||
DeleteService(this.m_hService);
|
DeleteService(this.service);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void control(int control)
|
public void control(int control)
|
||||||
throws Win32Exception
|
throws Win32Exception
|
||||||
{
|
{
|
||||||
if (!ControlService(this.m_hService, control)) {
|
if (!ControlService(this.service, control)) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDescription(String description)
|
public void setDescription(String description)
|
||||||
{
|
{
|
||||||
Service.ChangeServiceDescription(this.m_hService, description);
|
ChangeServiceDescription(this.service, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws Win32Exception
|
public void start() throws Win32Exception
|
||||||
{
|
{
|
||||||
if(Service.StartService(this.m_hService) == false)
|
if (StartService(this.service) == false) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void startAndWait() throws Win32Exception
|
public void startAndWait() throws Win32Exception
|
||||||
{
|
{
|
||||||
// Wait indefinitely
|
// Wait indefinitely
|
||||||
this.startAndWait(0);
|
startAndWait(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startAndWait(long timeout) throws Win32Exception
|
public void startAndWait(long timeout) throws Win32Exception
|
||||||
{
|
{
|
||||||
this.start();
|
start();
|
||||||
this.waitForStart(timeout);
|
waitForStart(timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int status()
|
public int status()
|
||||||
{
|
{
|
||||||
return QueryServiceStatus(this.m_hService);
|
return QueryServiceStatus(this.service);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() throws Win32Exception
|
public void stop() throws Win32Exception
|
||||||
{
|
{
|
||||||
if(StopService(this.m_hService) == false)
|
if (StopService(this.service) == false) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void stopAndWait() throws Win32Exception
|
public void stopAndWait() throws Win32Exception
|
||||||
{
|
{
|
||||||
// Wait indefinitely
|
// Wait indefinitely
|
||||||
this.stopAndWait(0);
|
stopAndWait(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopAndWait(long timeout) throws Win32Exception
|
public void stopAndWait(long timeout) throws Win32Exception
|
||||||
{
|
{
|
||||||
long lStatus;
|
long status;
|
||||||
|
|
||||||
this.stop();
|
stop();
|
||||||
|
|
||||||
long lStart = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
while((lStatus = this.status()) != SERVICE_STOPPED) {
|
while ((status = status()) != SERVICE_STOPPED) {
|
||||||
if(lStatus == SERVICE_STOP_PENDING) {
|
if (status == SERVICE_STOP_PENDING) {
|
||||||
// The start hasn't completed yet. Keep trying up to
|
// The start hasn't completed yet. Keep trying up to
|
||||||
// the timeout.
|
// the timeout.
|
||||||
if((System.currentTimeMillis() - lStart) <
|
if (((System.currentTimeMillis() - start) < timeout) ||
|
||||||
timeout || timeout <= 0) {
|
(timeout <= 0))
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(lStatus != SERVICE_STOPPED)
|
if (status != SERVICE_STOPPED) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void waitForStart(long timeout) throws Win32Exception
|
public void waitForStart(long timeout) throws Win32Exception
|
||||||
{
|
{
|
||||||
long lStatus;
|
long status;
|
||||||
boolean bResult = true;
|
boolean result = true;
|
||||||
long lStart = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
while((lStatus = this.status()) != SERVICE_RUNNING)
|
while ((status = status()) != SERVICE_RUNNING) {
|
||||||
{
|
if (status == SERVICE_START_PENDING) {
|
||||||
if(lStatus == SERVICE_START_PENDING)
|
|
||||||
{
|
|
||||||
// The start hasn't completed yet. Keep trying up to
|
// The start hasn't completed yet. Keep trying up to
|
||||||
// the timeout.
|
// the timeout.
|
||||||
if((System.currentTimeMillis() - lStart) <
|
if (((System.currentTimeMillis() - start) < timeout) ||
|
||||||
timeout || timeout <= 0) {
|
(timeout <= 0))
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
bResult = false;
|
result = false;
|
||||||
} else if(lStatus == SERVICE_STOPPED) {
|
}
|
||||||
|
} else if (status == SERVICE_STOPPED) {
|
||||||
// Start failed
|
// Start failed
|
||||||
bResult = false;
|
result = false;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// Hrm.
|
// Hrm.
|
||||||
bResult = false;
|
result = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(bResult == false)
|
if (result == false) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public ServiceConfig getConfig() throws Win32Exception {
|
public ServiceConfig getConfig() throws Win32Exception {
|
||||||
ServiceConfig config = new ServiceConfig();
|
ServiceConfig config = new ServiceConfig();
|
||||||
if (!QueryServiceConfig(this.m_hService, config)) {
|
if (!QueryServiceConfig(this.service, config)) {
|
||||||
Service.throwLastErrorException();
|
Service.throwLastErrorException();
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
|
@ -292,38 +305,48 @@ public class Service extends Win32Bindings {
|
||||||
private static final void throwLastErrorException()
|
private static final void throwLastErrorException()
|
||||||
throws Win32Exception
|
throws Win32Exception
|
||||||
{
|
{
|
||||||
int iErr = Service.GetLastError();
|
int err = Service.GetLastError();
|
||||||
throw new Win32Exception(iErr, "Win32 Error Code: " +
|
throw new Win32Exception(err, "Win32 Error Code: " +
|
||||||
iErr + ": " + Service.GetErrorMessage(iErr));
|
err + ": " + GetErrorMessage(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final native boolean
|
private static native boolean ChangeServiceDescription(long handle,
|
||||||
ChangeServiceDescription(long handle,
|
|
||||||
String description);
|
String description);
|
||||||
private static final native boolean CloseServiceHandle(long handle);
|
|
||||||
private static final native long CreateService(long handle,
|
private static native boolean CloseServiceHandle(long handle);
|
||||||
|
|
||||||
|
private static native long CreateService(long handle,
|
||||||
String serviceName,
|
String serviceName,
|
||||||
String displayName,
|
String displayName,
|
||||||
int serviceType,
|
int serviceType,
|
||||||
int startType,
|
int startType,
|
||||||
int errorControl,
|
int errorControl,
|
||||||
String path,
|
String path,
|
||||||
String[] dependicies,
|
String[] dependencies,
|
||||||
String startName,
|
String startName,
|
||||||
String password);
|
String password);
|
||||||
private static final native boolean ControlService(long handle,
|
|
||||||
|
private static native boolean ControlService(long handle,
|
||||||
int control);
|
int control);
|
||||||
private static final native boolean DeleteService(long handle);
|
|
||||||
private static final native String GetErrorMessage(int error);
|
private static native boolean DeleteService(long handle);
|
||||||
private static final native int GetLastError();
|
|
||||||
private static final native long OpenSCManager(String machine,
|
private static native String GetErrorMessage(int error);
|
||||||
|
|
||||||
|
private static native int GetLastError();
|
||||||
|
|
||||||
|
private static native long OpenSCManager(String machine,
|
||||||
int access);
|
int access);
|
||||||
private static final native long OpenService(long handle,
|
|
||||||
|
private static native long OpenService(long handle,
|
||||||
String service,
|
String service,
|
||||||
int access);
|
int access);
|
||||||
private static final native int QueryServiceStatus(long handle);
|
|
||||||
private static final native boolean StartService(long handle);
|
private static native int QueryServiceStatus(long handle);
|
||||||
private static final native boolean StopService(long handle);
|
|
||||||
|
private static native boolean StartService(long handle);
|
||||||
|
|
||||||
|
private static native boolean StopService(long handle);
|
||||||
|
|
||||||
private static native boolean QueryServiceConfig(long handle,
|
private static native boolean QueryServiceConfig(long handle,
|
||||||
ServiceConfig config);
|
ServiceConfig config);
|
||||||
|
|
Loading…
Reference in New Issue