Search in sources :

Example 1 with SERVICE_FAILURE_ACTIONS

use of com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS in project jna by java-native-access.

the class W32ServiceTest method testSetAndGetFailureActions.

public void testSetAndGetFailureActions() {
    final String svcId = "w32time";
    final String rebootMsg = "Restarting " + svcId + " due to service failure";
    final String command = "echo " + svcId + " failure";
    final int resetPeriod = 5000;
    W32Service service = _serviceManager.openService(svcId, Winsvc.SC_MANAGER_ALL_ACCESS);
    SERVICE_FAILURE_ACTIONS prevActions = service.getFailureActions();
    List<SC_ACTION> actions = new LinkedList<SC_ACTION>();
    SC_ACTION action = new SC_ACTION();
    action.type = Winsvc.SC_ACTION_RESTART;
    action.delay = 1000;
    actions.add(action);
    action = new SC_ACTION();
    action.type = Winsvc.SC_ACTION_REBOOT;
    action.delay = 2000;
    actions.add(action);
    action = new SC_ACTION();
    action.type = Winsvc.SC_ACTION_RUN_COMMAND;
    action.delay = 3000;
    actions.add(action);
    action = new SC_ACTION();
    action.type = Winsvc.SC_ACTION_NONE;
    action.delay = 4000;
    actions.add(action);
    service.setFailureActions(actions, resetPeriod, rebootMsg, command);
    SERVICE_FAILURE_ACTIONS changedActions = service.getFailureActions();
    assertEquals(changedActions.lpRebootMsg, rebootMsg);
    assertEquals(changedActions.lpCommand, command);
    assertEquals(changedActions.dwResetPeriod, resetPeriod);
    assertEquals(changedActions.cActions, 4);
    SC_ACTION[] actionArray = (SC_ACTION[]) changedActions.lpsaActions.toArray(changedActions.cActions);
    assertEquals(actionArray[0].type, Winsvc.SC_ACTION_RESTART);
    assertEquals(actionArray[0].delay, 1000);
    assertEquals(actionArray[1].type, Winsvc.SC_ACTION_REBOOT);
    assertEquals(actionArray[1].delay, 2000);
    assertEquals(actionArray[2].type, Winsvc.SC_ACTION_RUN_COMMAND);
    assertEquals(actionArray[2].delay, 3000);
    assertEquals(actionArray[3].type, Winsvc.SC_ACTION_NONE);
    assertEquals(actionArray[3].delay, 4000);
    // restore old settings
    Advapi32.INSTANCE.ChangeServiceConfig2(service._handle, Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS, prevActions);
    service.close();
}
Also used : SC_ACTION(com.sun.jna.platform.win32.Winsvc.SC_ACTION) SERVICE_FAILURE_ACTIONS(com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS) LinkedList(java.util.LinkedList)

Example 2 with SERVICE_FAILURE_ACTIONS

use of com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS in project jna by java-native-access.

the class W32Service method getFailureActions.

/**
	 * Get the failure actions of the specified service. Corresponds to 
	 * <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988.aspx">QueryServiceConfig2</a>
	 * with parameter dwInfoLevel set to SERVICE_CONFIG_FAILURE_ACTIONS. 
	 */
public SERVICE_FAILURE_ACTIONS getFailureActions() {
    Pointer buffer = queryServiceConfig2(Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS);
    SERVICE_FAILURE_ACTIONS result = new SERVICE_FAILURE_ACTIONS(buffer);
    return result;
}
Also used : SERVICE_FAILURE_ACTIONS(com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS) Pointer(com.sun.jna.Pointer)

Aggregations

SERVICE_FAILURE_ACTIONS (com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS)2 Pointer (com.sun.jna.Pointer)1 SC_ACTION (com.sun.jna.platform.win32.Winsvc.SC_ACTION)1 LinkedList (java.util.LinkedList)1