cleanup start/stop methods w/ timeout arg to reuse code and hence behave the same
This commit is contained in:
parent
ff25200d99
commit
bf281b55b6
|
@ -209,34 +209,58 @@ public class Service extends Win32 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop(long timeout) throws Win32Exception
|
private static class Waiter {
|
||||||
{
|
|
||||||
long status;
|
|
||||||
|
|
||||||
stop();
|
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
Service service;
|
||||||
|
long timeout;
|
||||||
|
int wantedState;
|
||||||
|
int pendingState;
|
||||||
|
|
||||||
while ((status = getStatus()) != SERVICE_STOPPED) {
|
Waiter(Service service,
|
||||||
if (status == SERVICE_STOP_PENDING) {
|
long timeout,
|
||||||
// The start hasn't completed yet. Keep trying up to
|
int wantedState,
|
||||||
// the timeout.
|
int pendingState)
|
||||||
if (((System.currentTimeMillis() - start) < timeout) ||
|
{
|
||||||
(timeout <= 0))
|
this.service = service;
|
||||||
|
this.timeout = timeout;
|
||||||
|
this.wantedState = wantedState;
|
||||||
|
this.pendingState = pendingState;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean sleep() {
|
||||||
|
int status;
|
||||||
|
while ((status = service.getStatus()) != this.wantedState) {
|
||||||
|
if (status != this.pendingState) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((timeout <= 0) ||
|
||||||
|
((System.currentTimeMillis() - this.start) < this.timeout))
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status != SERVICE_STOPPED) {
|
return status == this.wantedState;
|
||||||
throw getLastErrorException();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop(long timeout) throws Win32Exception
|
||||||
|
{
|
||||||
|
long status;
|
||||||
|
|
||||||
|
stop();
|
||||||
|
|
||||||
|
Waiter waiter =
|
||||||
|
new Waiter(this, timeout, SERVICE_STOPPED, SERVICE_STOP_PENDING);
|
||||||
|
|
||||||
|
if (!waiter.sleep()) {
|
||||||
|
throw new Win32Exception("Failed to stop service");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,37 +274,14 @@ public class Service extends Win32 {
|
||||||
public void start(long timeout) throws Win32Exception
|
public void start(long timeout) throws Win32Exception
|
||||||
{
|
{
|
||||||
long status;
|
long status;
|
||||||
boolean result = true;
|
|
||||||
long start = System.currentTimeMillis();
|
|
||||||
|
|
||||||
while ((status = getStatus()) != SERVICE_RUNNING) {
|
start();
|
||||||
if (status == SERVICE_START_PENDING) {
|
|
||||||
// The start hasn't completed yet. Keep trying up to
|
|
||||||
// the timeout.
|
|
||||||
if (((System.currentTimeMillis() - start) < timeout) ||
|
|
||||||
(timeout <= 0))
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch(InterruptedException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
} else if (status == SERVICE_STOPPED) {
|
|
||||||
// Start failed
|
|
||||||
result = false;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
// Hrm.
|
|
||||||
result = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result == false) {
|
Waiter waiter =
|
||||||
throw getLastErrorException();
|
new Waiter(this, timeout, SERVICE_RUNNING, SERVICE_START_PENDING);
|
||||||
|
|
||||||
|
if (!waiter.sleep()) {
|
||||||
|
throw new Win32Exception("Failed to start service");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,6 +343,20 @@ public class Service extends Win32 {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
services = getServiceNames();
|
services = getServiceNames();
|
||||||
}
|
}
|
||||||
|
else if ((args.length == 2) && (args[0].equals("-toggle"))) {
|
||||||
|
long timeout = 5 * 1000; //5 seconds
|
||||||
|
Service service = new Service(args[1]);
|
||||||
|
if (service.getStatus() == SERVICE_RUNNING) {
|
||||||
|
System.out.println("Stopping service...");
|
||||||
|
service.stop(timeout);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Starting service...");
|
||||||
|
service.start(timeout);
|
||||||
|
}
|
||||||
|
System.out.println(service.getStatusString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
services = Arrays.asList(args);
|
services = Arrays.asList(args);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue