Skip to content

Commit

Permalink
midpoint-boot-layout: update/modernization, and JAR support
Browse files Browse the repository at this point in the history
- Now only single Launcher class is added to WAR or JAR.
- The base package of the launcher is changed to c.e.m.launcher.
- This change should be compatible with Spring Boot 2.6.x as well.
  • Loading branch information
virgo47 committed Apr 19, 2022
1 parent fe6de37 commit dd9eab7
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 66 deletions.
2 changes: 1 addition & 1 deletion dist/src/main/bin/service.bat
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ REM ----- Execute The Requested Command ---------------------------------------

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

REM Service log configuration
set PR_LOGPREFIX=%SERVICE_NAME%
Expand Down
2 changes: 1 addition & 1 deletion gui/admin-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@
<phase>package</phase>
<configuration>
<classifier>executable</classifier>
<layoutFactory implementation="com.evolveum.midpoint.tools.layout.MidPointWarLayoutFactory"/>
<layoutFactory implementation="com.evolveum.midpoint.launcher.MidPointWarLayoutFactory"/>
</configuration>
</execution>
</executions>
Expand Down
51 changes: 12 additions & 39 deletions gui/midpoint-jar/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
<artifactId>midpoint-jar</artifactId>
<!--
TODO: EXPERIMENTAL AT THE MOMENT:
Support of midPoint as JAR is experimental at the moment and further testing is necessary:
- included libs need to be checked, compared with WAR content of lib + lib-provided
- thorough testing of ${midpoint.home}/lib loading is needed
(perhaps it's possible to support it without special command line using simple custom launcher)
Support of midPoint as JAR is experimental at the moment but should work, perhaps even faster than WAR!
To try midPoint as a JAR:
- enable "mpjar" profile (defined in gui/pom.xml) during Maven build (mvn ... -P mpjar ...)
Expand Down Expand Up @@ -59,7 +56,8 @@
Following drivers are excluded in repackaging, we don't want to distribute it (or can't).
TODO: in admin-gui they are provided, because it's handy for developer
but perhaps we can just delete it here and recheck whether they are packaged.
See repackaging "exclusions" and truezip-maven-plugin lower.
This may actually be useless here, because developer runs main class from the GUI module (at least now).
But we leave it here + the exclusion in repackage, so no harm done.
-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
Expand Down Expand Up @@ -97,10 +95,7 @@
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.evolveum.midpoint.web.boot.MidPointSpringApplication</mainClass>
<layout>ZIP</layout>
<!--
Exclude does NOT exclude transitive dependencies (as of Boot 2.3.3). How crazy is that?!
See truezip-maven-plugin plugin lower.
<!-- This excludes these deps, including their transitive dependencies. -->
<excludes>
<exclude>
<groupId>com.microsoft.sqlserver</groupId>
Expand All @@ -111,46 +106,24 @@
<artifactId>ojdbc11</artifactId>
</exclude>
</excludes>
-->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>remove-unwanted-libs</id>
<goals>
<goal>remove</goal>
</goals>
<phase>package</phase>
<configuration>
<fileset>
<!-- note how the archive is treated as a normal file directory -->
<directory>${project.build.directory}/midpoint.jar/BOOT-INF/lib</directory>
<includes>
<include>admin-gui-*.war</include>
<include>ojdbc11-*.jar</include>
<include>ucp-*.jar</include>
<include>oraclepki-*.jar</include>
<include>osdt_cert-*.jar</include>
<include>osdt_core-*.jar</include>
<include>simplefan-*.jar</include>
<include>ons-*.jar</include>
<include>mssql-jdbc-*.jar</include>
</includes>
</fileset>
<layoutFactory implementation="com.evolveum.midpoint.launcher.MidPointJarLayoutFactory"/>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.evolveum.midpoint.tools</groupId>
<artifactId>midpoint-boot-layout</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.launcher;

import org.springframework.boot.loader.tools.Layouts;

public class MidPointJarLayout extends Layouts.Jar implements MidPointLayoutCommon {

@Override
public String getLauncherClassName() {
return MidPointLauncher.class.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.launcher;

import java.io.File;

import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LayoutFactory;

/**
* Layout factory used in POM repackaging the application (JAR version).
*/
@SuppressWarnings("unused")
public class MidPointJarLayoutFactory implements LayoutFactory {

@Override
public Layout getLayout(File source) {
return new MidPointJarLayout();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.launcher;

import java.nio.file.Path;

import org.springframework.boot.loader.PropertiesLauncher;

/**
* This is a "pre-launcher" to {@link PropertiesLauncher} which is the actual launcher.
*
* `PropertiesLauncher` is used because we can specify additional classpath components with it.
* This launcher sets `loader.path` system property if not specified.
* If `loader.path` is specified the user/system administrator is fully responsible for it.
*
* This also sets `midpoint.home` system property if not set using `MIDPOINT_HOME` environment
* variable as the first default and then `${user.home}/midpoint`.
*
* Finally, this class also provides {@link #stop(String[])} method for Windows service.
*/
public class MidPointLauncher {

private MidPointLauncher() {
throw new AssertionError("Non-instantiable launcher class");
}

// Mostly duplicates with WAR launcher, but we want to keep these separate.
@SuppressWarnings("DuplicatedCode")
public static void main(String[] args) throws Exception {
String midpointHome = System.getProperty("midpoint.home");
if (midpointHome == null) {
midpointHome = System.getenv("MIDPOINT_HOME");
if (midpointHome == null) {
midpointHome = Path.of(System.getProperty("user.home"), "midpoint").toString();
}
}
midpointHome = Path.of(midpointHome).toAbsolutePath().toString();
System.setProperty("midpoint.home", midpointHome);
System.out.println("midPoint home: " + midpointHome);

// if loader.path is set we will respect it, although it's unlikely
if (System.getProperty("loader.path") == null) {
System.setProperty("loader.path", Path.of(midpointHome, "lib").toString());
}
System.out.println("Using loader path (for additional JARs): " + System.getProperty("loader.path"));

PropertiesLauncher.main(args);
}

/**
* Used as Windows service stop method.
*/
@SuppressWarnings("unused")
public static synchronized void stop(String[] args) {
System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.tools.layout;
package com.evolveum.midpoint.launcher;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.springframework.boot.loader.tools.CustomLoaderLayout;
import org.springframework.boot.loader.tools.JarWriter;
import org.springframework.boot.loader.tools.Layouts;
import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LoaderClassesWriter;

public class MidPointWarLayout extends Layouts.War implements CustomLoaderLayout {

@Override
public void writeLoadedClasses(LoaderClassesWriter writer) throws IOException {
writer.writeLoaderClasses(); // this writes

// This writes this JAR (layout) to the root of the Spring Boot archive.
// TODO: Is it possible to write *Launcher classes without layout and factory?
JarFile self = createSelf();
((JarWriter) writer).writeEntries(self);
}
/**
* Common custom midPoint JAR/WAR layout functionality.
*/
public interface MidPointLayoutCommon extends CustomLoaderLayout, Layout {

@Override
public String getLauncherClassName() {
return MidPointWarLauncher.class.getName();
default void writeLoadedClasses(LoaderClassesWriter writer) throws IOException {
String zipEntryName = getLauncherClassName().replace('.', '/') + ".class";
writer.writeLoaderClasses(); // this writes the default classes
try (JarFile self = customLayoutJar();
InputStream classInputStream = self.getInputStream(new ZipEntry(zipEntryName))) {
writer.writeEntry(zipEntryName, classInputStream);
}
}

private JarFile createSelf() throws IOException {
/**
* Creates JarFile instance for midpoint-boot-layout JAR for launcher classes extraction.
*/
private JarFile customLayoutJar() throws IOException {
try {
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
Expand All @@ -53,7 +55,7 @@ private JarFile createSelf() throws IOException {

return new JarFile(root);
} catch (Exception e) {
throw new IOException("Could not find self jar", e);
throw new IOException("Could not find self JAR", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.tools.layout;
package com.evolveum.midpoint.launcher;

import java.nio.file.Path;

Expand All @@ -29,6 +29,8 @@ private MidPointWarLauncher() {
throw new AssertionError("Non-instantiable launcher class");
}

// Mostly duplicates with JAR launcher, but we want to keep these separate.
@SuppressWarnings("DuplicatedCode")
public static void main(String[] args) throws Exception {
String midpointHome = System.getProperty("midpoint.home");
if (midpointHome == null) {
Expand All @@ -47,7 +49,7 @@ public static void main(String[] args) throws Exception {
"WEB-INF/classes,WEB-INF/lib,WEB-INF/lib-provided,"
+ Path.of(midpointHome, "lib").toString());
}
System.out.println("Using loader path: " + System.getProperty("loader.path"));
System.out.println("Using loader path (for additional JARs): " + System.getProperty("loader.path"));

PropertiesLauncher.main(args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.launcher;

import org.springframework.boot.loader.tools.Layouts;

public class MidPointWarLayout extends Layouts.War implements MidPointLayoutCommon {

@Override
public String getLauncherClassName() {
return MidPointWarLauncher.class.getName();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
* Copyright (C) 2010-2022 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.tools.layout;
package com.evolveum.midpoint.launcher;

import java.io.File;

import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LayoutFactory;

// Used in POM repackaging the application.
/**
* Layout factory used in POM repackaging the application (WAR version).
*/
@SuppressWarnings("unused")
public class MidPointWarLayoutFactory implements LayoutFactory {

Expand Down

0 comments on commit dd9eab7

Please sign in to comment.