Search in sources :

Example 1 with Advapi32Ex

use of org.jumpmind.symmetric.wrapper.jna.Advapi32Ex in project symmetric-ds by JumpMind.

the class WindowsService method updateStatus.

protected void updateStatus(int status, int controlsAccepted) {
    if (serviceStatus != null) {
        Advapi32Ex advapi = Advapi32Ex.INSTANCE;
        serviceStatus.dwCurrentState = status;
        serviceStatus.dwControlsAccepted = controlsAccepted;
        if (!advapi.SetServiceStatus(serviceStatusHandle.getPointer(), serviceStatus)) {
            throwException("SetServiceStatus");
        }
    }
}
Also used : Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 2 with Advapi32Ex

use of org.jumpmind.symmetric.wrapper.jna.Advapi32Ex in project symmetric-ds by JumpMind.

the class WindowsService method waitForService.

protected Winsvc.SERVICE_STATUS_PROCESS waitForService(SC_HANDLE manager, SC_HANDLE service) {
    int seconds = 0;
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    IntByReference bytesNeeded = new IntByReference();
    advapi.QueryServiceStatusEx(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, bytesNeeded);
    Winsvc.SERVICE_STATUS_PROCESS status = new Winsvc.SERVICE_STATUS_PROCESS(bytesNeeded.getValue());
    while (seconds <= 5) {
        System.out.print(".");
        if (!advapi.QueryServiceStatusEx(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), bytesNeeded)) {
            throwException("QueryServiceStatusEx");
        }
        if (status.dwCurrentState == Winsvc.SERVICE_STOPPED) {
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        seconds++;
    }
    System.out.println("");
    return status;
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference) Winsvc(com.sun.jna.platform.win32.Winsvc) SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 3 with Advapi32Ex

use of org.jumpmind.symmetric.wrapper.jna.Advapi32Ex in project symmetric-ds by JumpMind.

the class WindowsService method closeServiceHandle.

protected void closeServiceHandle(SC_HANDLE handle) {
    if (handle != null) {
        Advapi32Ex advapi = Advapi32Ex.INSTANCE;
        advapi.CloseServiceHandle(handle);
    }
}
Also used : Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 4 with Advapi32Ex

use of org.jumpmind.symmetric.wrapper.jna.Advapi32Ex in project symmetric-ds by JumpMind.

the class WindowsService method install.

@Override
public void install() {
    if (isRunning()) {
        System.out.println("Server must be stopped before installing");
        System.exit(Constants.RC_NO_INSTALL_WHEN_RUNNING);
    }
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    SC_HANDLE manager = openServiceManager();
    SC_HANDLE service = advapi.OpenService(manager, config.getName(), Winsvc.SERVICE_ALL_ACCESS);
    try {
        if (service != null) {
            throw new WrapperException(Constants.RC_ALREADY_INSTALLED, 0, "Service " + config.getName() + " is already installed");
        } else {
            System.out.println("Installing " + config.getName() + " ...");
            String dependencies = null;
            if (config.getDependencies() != null && config.getDependencies().size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (String dependency : config.getDependencies()) {
                    sb.append(dependency).append("\0");
                }
                dependencies = sb.append("\0").toString();
            }
            service = advapi.CreateService(manager, config.getName(), config.getDisplayName(), Winsvc.SERVICE_ALL_ACCESS, WinsvcEx.SERVICE_WIN32_OWN_PROCESS, config.isAutoStart() || config.isDelayStart() ? WinsvcEx.SERVICE_AUTO_START : WinsvcEx.SERVICE_DEMAND_START, WinsvcEx.SERVICE_ERROR_NORMAL, commandToString(getWrapperCommand("init")), null, null, dependencies, null, null);
            if (service != null) {
                Advapi32Ex.SERVICE_DESCRIPTION desc = new Advapi32Ex.SERVICE_DESCRIPTION(config.getDescription());
                advapi.ChangeServiceConfig2(service, WinsvcEx.SERVICE_CONFIG_DESCRIPTION, desc);
                if (config.isDelayStart()) {
                    WinsvcEx.SERVICE_DELAYED_AUTO_START_INFO delayedInfo = new WinsvcEx.SERVICE_DELAYED_AUTO_START_INFO(true);
                    advapi.ChangeServiceConfig2(service, WinsvcEx.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, delayedInfo);
                }
            } else {
                throwException("CreateService");
            }
            System.out.println("Done");
        }
    } finally {
        closeServiceHandle(service);
        closeServiceHandle(manager);
    }
}
Also used : WinsvcEx(org.jumpmind.symmetric.wrapper.jna.WinsvcEx) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) WString(com.sun.jna.WString) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 5 with Advapi32Ex

use of org.jumpmind.symmetric.wrapper.jna.Advapi32Ex in project symmetric-ds by JumpMind.

the class WindowsService method uninstall.

@Override
public void uninstall() {
    if (isRunning()) {
        throw new WrapperException(Constants.RC_NO_INSTALL_WHEN_RUNNING, 0, "Server must be stopped before uninstalling");
    }
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    SC_HANDLE manager = openServiceManager();
    SC_HANDLE service = advapi.OpenService(manager, config.getName(), Winsvc.SERVICE_ALL_ACCESS);
    try {
        if (service != null) {
            System.out.println("Uninstalling " + config.getName() + " ...");
            if (!advapi.DeleteService(service)) {
                throwException("DeleteService");
            }
        } else {
            throw new WrapperException(Constants.RC_NOT_INSTALLED, 0, "Service " + config.getName() + " is not installed");
        }
    } finally {
        closeServiceHandle(service);
        closeServiceHandle(manager);
    }
    int seconds = 0;
    while (seconds <= 30) {
        if (!isInstalled()) {
            break;
        }
        System.out.print(".");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        seconds++;
    }
    if (seconds > 0) {
        System.out.println("");
    }
    if (isInstalled()) {
        System.out.println("Service manager did not complete");
    } else {
        System.out.println("Done");
    }
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Aggregations

Advapi32Ex (org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)5 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)2 WString (com.sun.jna.WString)1 Winsvc (com.sun.jna.platform.win32.Winsvc)1 SERVICE_STATUS_PROCESS (com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS)1 IntByReference (com.sun.jna.ptr.IntByReference)1 WinsvcEx (org.jumpmind.symmetric.wrapper.jna.WinsvcEx)1