Skip to content

Commit

Permalink
Win service - modified MidPointWarLauncher.java, added script and bin…
Browse files Browse the repository at this point in the history
…ares for service
  • Loading branch information
KristianSuchanovsky authored and semancik committed Mar 2, 2018
1 parent dc0135a commit 11e4710
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 3 deletions.
Binary file added dist/src/main/bin/midpoint.exe
Binary file not shown.
Binary file added dist/src/main/bin/midpointw.exe
Binary file not shown.
161 changes: 161 additions & 0 deletions dist/src/main/bin/service.bat
@@ -0,0 +1,161 @@
@echo off
set SERVICE_NAME=MidPoint
set "BIN_DIR=%~dp0%~2"

if not "%MIDPOINT_HOME%" == "" goto gotHome
cd "%BIN_DIR%.."
mkdir var
cd var
mkdir log
cd "%BIN_DIR%.."
set "MIDPOINT_HOME=%cd%\var"
:gotHome

REM if script for service exists
if exist "%BIN_DIR%\service.bat" goto okBoot
echo The service.bat file is not in \bin directory or is no accessible
goto end
:okBoot

if exist "%BIN_DIR%\midpoint.exe" goto okHome
echo The midpoint.exe was not found…
goto end
:okHome
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
if not "%JRE_HOME%" == "" goto gotJreHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo Service will try to guess them from the registry.
goto okJavaHome
:gotJreHome

if not exist "%JRE_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHome
goto okJavaHome
:gotJdkHome

if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
rem Java 9 has a different directory structure
if exist "%JAVA_HOME%\jre\bin\java.exe" goto preJava9Layout
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%"
goto okJavaHome
:preJava9Layout

if not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%\jre"
goto okJavaHome
:noJavaHome

echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
echo NB: JAVA_HOME should point to a JDK not a JRE
goto end
:okJavaHome

REM MIDPOINT_WAR if not defined
if exist "%cd%\lib\midpoint.war" goto gotWar
echo The midpoint.war is not in \lib directory
echo Can not start midPoint
goto end
:gotWar

if "%MIDPOINT_HOME%" == "%MIDPOINT_HOME:;=%" goto homeNoSemicolon
echo Using MIDPOINT_HOME: "%MIDPOINT_HOME%"
echo Unable to start as MIDPOINT_HOME contains a semicolon (;) character
goto end
:homeNoSemicolon

REM ----- Execute The Requested Command ---------------------------------------

set EXECUTABLE=%BIN_DIR%\midpoint.exe
set PR_INSTALL=%EXECUTABLE%
set MIDPOINT_START_CLASS=com.evolveum.midpoint.tools.layout.MidPointWarLauncher

REM Service log configuration
set PR_LOGPREFIX=%SERVICE_NAME%
set PR_LOGPATH=%MIDPOINT_HOME%\log
set PR_STDOUTPUT=auto
set PR_STDERROR=auto
set PR_LOGLEVEL=Error

REM Path to java installation
REM Try to use the server jvm
set "PR_JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%PR_JVM%" goto foundJvm
REM Try to use the client jvm
set "PR_JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%PR_JVM%" goto foundJvm
echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
set PR_JVM=auto
:foundJvm

set PR_CLASSPATH=%cd%\lib\midpoint.war

REM Statup configuration
set PR_STARTUP=auto
set PR_STARTMODE=jvm
set PR_STARTMETHOD=main
set PR_STARTPARAMS=start
set PR_STARTCLASS=%MIDPOINT_START_CLASS%

REM Shutdown configuration
set PR_STOPMODE=jvm
set PR_STOPMETHOD=%PR_STARTMETHOD%
set PR_STOPPARAMS=stop
set PR_STOPCLASS=%MIDPOINT_START_CLASS%

REM JVM configuration
set PR_JVMMS=1024
set PR_JVMMX=1024

if %1 == install goto doInstall
if %1 == remove goto doRemove
if %1 == uninstall goto doRemove
echo Unknown parameter "%1"

:doRemove
rem Remove the service
"%EXECUTABLE%" //DS//%SERVICE_NAME%
echo The service '%SERVICE_NAME%' has been removed
goto end

:doInstall
REM Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using MIDPOINT_HOME: "%MIDPOINT_HOME%"
echo Using JAVA_HOME: "%JAVA_HOME%"
echo Using JRE_HOME: "%JRE_HOME%"

REM Install service
"%PR_INSTALL%" //IS//%SERVICE_NAME% ^
--StartMode="%PR_STARTMODE%" ^
--StartClass="%PR_STARTCLASS%" ^
--StartMethod="%PR_STARTMETHOD%" ^
--StopMode="%PR_STOPMODE%" ^
--StopClass="%PR_STOPCLASS%" ^
--StopMethod="%PR_STOPMETHOD%" ^
--StartParams="%PR_STARTPARAMS%" ^
--StopParams="%PR_STOPPARAMS%" ^
--Jvm="%PR_JVM%" ^
--JvmMs="%PR_JVMMS%" ^
--JvmMx="%PR_JVMMX%" ^
--Startup="%PR_STARTUP%" ^
--LogPath="%PR_LOGPATH%" ^
--LogPrefix="%SERVICE_NAME%" ^
--LogLevel="%PR_LOGLEVEL%" ^
--StdOutput="%PR_STDOUTPUT%" ^
--StdError="%PR_STDERROR%" ^
--JvmOptions -Dmidpoint.home="%MIDPOINT_HOME%";-Dpython.cachedir="%MIDPOINT_HOME%\tmp";-Djavax.net.ssl.trustStore="%MIDPOINT_HOME%\keystore.jceks";-Djavax.net.ssl.trustStoreType=jceks ^
--Classpath="%PR_CLASSPATH%"

if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed

:end
cd %CURRENT_DIR%
Expand Up @@ -33,6 +33,8 @@
import org.apache.wicket.protocol.http.WicketFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
Expand All @@ -49,6 +51,7 @@
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.HttpStatus;
Expand All @@ -58,7 +61,7 @@

import javax.servlet.DispatcherType;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -106,11 +109,38 @@ public class MidPointSpringApplication extends SpringBootServletInitializer {

private static final String MIDPOINT_HOME_PROPERTY = "midpoint.home";
private static final String USER_HOME_PROPERTY_NAME = "user.home";
private static ConfigurableApplicationContext applicationContext = null;

public static void main(String[] args) {
System.setProperty("xml.catalog.className", CatalogImpl.class.getName());
String mode = args != null && args.length > 0 ? args[0] : null;

if(LOGGER.isDebugEnabled()){
LOGGER.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() +
" Application mode:" + mode + " context:" + applicationContext);
}

if (applicationContext != null && mode != null && "stop".equals(mode)) {
System.exit(SpringApplication.exit(applicationContext, new ExitCodeGenerator() {

@Override
public int getExitCode() {

return 0;
}
}));

} else {

applicationContext = configureApplication(new SpringApplicationBuilder()).run(args);

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() +
" Application started context:" + applicationContext);
}

}

configureApplication(new SpringApplicationBuilder()).run(args);
}

@Override
Expand Down
Expand Up @@ -19,6 +19,7 @@
import org.springframework.boot.loader.WarLauncher;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.archive.JarFileArchive;
import org.springframework.boot.loader.jar.JarFile;

import java.io.File;
import java.util.List;
Expand All @@ -27,6 +28,9 @@
* Created by Viliam Repan (lazyman).
*/
public class MidPointWarLauncher extends WarLauncher {

private static volatile MidPointWarLauncher warlauncher = null;
private static volatile ClassLoader classLoader = null;

public MidPointWarLauncher() {
}
Expand All @@ -36,7 +40,65 @@ public MidPointWarLauncher(Archive archive) {
}

public static void main(String[] args) throws Exception {
new MidPointWarLauncher().launch(args);
String mode = args != null && args.length > 0 ? args[0] : null;

if ("start".equals(mode)) {
MidPointWarLauncher.start(args);
} else if ("stop".equals(mode)) {
MidPointWarLauncher.stop(args);
} else {
new MidPointWarLauncher().launch(args);
}
}

public static synchronized void start(String[] args) throws Exception {
warlauncher = new MidPointWarLauncher();

try {
JarFile.registerUrlProtocolHandler();
classLoader = warlauncher.createClassLoader(warlauncher.getClassPathArchives());
warlauncher.launch(args, warlauncher.getMainClass(), classLoader, true);
} catch (Exception ex) {
StringBuilder sb = new StringBuilder();
sb.append("Could not start MidPoint application").append(";").append(ex.getLocalizedMessage());
throw new Exception(sb.toString(), ex);
}
}

public static synchronized void stop(String[] args) throws Exception {

try {
if (warlauncher != null) {
warlauncher.launch(args, warlauncher.getMainClass(), classLoader, true);
warlauncher = null;
classLoader = null;
}
} catch (Exception ex) {
StringBuilder sb = new StringBuilder();
sb.append("Could not stop MidPoint application").append(";").append(ex.getLocalizedMessage());
throw new Exception(sb.toString(), ex);

}
}

protected void launch(String[] args, String mainClass, ClassLoader classLoader, boolean wait) throws Exception {

Thread.currentThread().setContextClassLoader(classLoader);

Thread runnerThread = new Thread(() -> {
try {
createMainMethodRunner(mainClass, args, classLoader).run();
} catch (Exception ex) {

}
});
runnerThread.setContextClassLoader(classLoader);
runnerThread.setName(Thread.currentThread().getName());
runnerThread.start();
if (wait == true) {
runnerThread.join();
}

}

@Override
Expand Down

0 comments on commit 11e4710

Please sign in to comment.