Skip to content

Commit 727d08d

Browse files
committed
Move main logic to Seed.launch()
1 parent b64db17 commit 727d08d

File tree

27 files changed

+97
-108
lines changed

27 files changed

+97
-108
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Version 3.11.1 (2021-01-31)
1+
# Version 3.12.0 (2021-01-31)
22

3+
* [chg] Move main entrypoint logic from `SeedMain.main()` to `Seed.launch()`, allowing custom main methods in addition to the built-in `SeedMain`.
34
* [fix] Detection of color output under recent versions of IntelliJ.
45

56
# Version 3.11.0 (2020-10-28)

cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.11.1-SNAPSHOT</version>
17+
<version>3.12.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-cli</artifactId>

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.11.1-SNAPSHOT</version>
17+
<version>3.12.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-core</artifactId>

core/src/main/java/org/seedstack/seed/core/Seed.java

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,10 @@
77
*/
88
package org.seedstack.seed.core;
99

10-
import static com.google.common.base.Preconditions.checkState;
11-
import static org.seedstack.shed.misc.PriorityUtils.sortByPriority;
12-
1310
import com.google.common.base.Strings;
1411
import com.google.common.collect.Lists;
1512
import io.nuun.kernel.api.Kernel;
1613
import io.nuun.kernel.api.config.KernelConfiguration;
17-
import java.io.IOException;
18-
import java.io.InputStream;
19-
import java.nio.charset.StandardCharsets;
20-
import java.util.HashMap;
21-
import java.util.List;
22-
import java.util.Map;
23-
import java.util.Optional;
24-
import java.util.Scanner;
25-
import java.util.ServiceLoader;
2614
import org.fusesource.jansi.Ansi;
2715
import org.fusesource.jansi.AnsiRenderer;
2816
import org.seedstack.coffig.Coffig;
@@ -33,12 +21,7 @@
3321
import org.seedstack.seed.core.internal.CoreErrorCode;
3422
import org.seedstack.seed.core.internal.ToolLauncher;
3523
import org.seedstack.seed.core.internal.diagnostic.DiagnosticManagerImpl;
36-
import org.seedstack.seed.core.internal.init.AutodetectLogManager;
37-
import org.seedstack.seed.core.internal.init.BaseConfigurationFactory;
38-
import org.seedstack.seed.core.internal.init.ConsoleManager;
39-
import org.seedstack.seed.core.internal.init.KernelManager;
40-
import org.seedstack.seed.core.internal.init.LogManager;
41-
import org.seedstack.seed.core.internal.init.ProxyManager;
24+
import org.seedstack.seed.core.internal.init.*;
4225
import org.seedstack.seed.diagnostic.DiagnosticManager;
4326
import org.seedstack.seed.spi.SeedExceptionTranslator;
4427
import org.seedstack.seed.spi.SeedInitializer;
@@ -49,11 +32,20 @@
4932
import org.seedstack.shed.reflect.Classes;
5033
import org.seedstack.shed.text.TextTemplate;
5134

35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.*;
39+
40+
import static com.google.common.base.Preconditions.checkState;
41+
import static org.seedstack.shed.misc.PriorityUtils.sortByPriority;
42+
5243
/**
5344
* This class is the SeedStack framework entry point, which is used create and dispose kernels.
5445
* It handles global initialization and cleanup.
5546
*/
5647
public class Seed {
48+
private static final int EXIT_FAILURE = 1;
5749
private static final String WELCOME_MESSAGE = "\n" +
5850
" ____ _ ____ _ _ \n" +
5951
"/ ___| ___ ___ __| / ___|| |_ __ _ ___| | __\n" +
@@ -118,7 +110,7 @@ private Seed() {
118110
}
119111
diagnosticManager.dumpDiagnosticReport(throwable);
120112
translated.printStackTrace(System.err);
121-
} catch(Throwable t) {
113+
} catch (Throwable t) {
122114
throwable.printStackTrace();
123115
}
124116
});
@@ -181,6 +173,50 @@ static void markLifecycleExceptionHandlerEnabled() {
181173
hasLifecycleExceptionHandler = true;
182174
}
183175

176+
/**
177+
* <p>
178+
* Main SeedStack Java application entry point. It searches classes implementing {@link SeedLauncher} through the
179+
* {@link ServiceLoader} mechanism. If no class or more than one class is found, it throws an exception. If exactly one
180+
* class is found, it delegates the SeedStack application startup to its {@link SeedLauncher#launch(String[])} method.
181+
* </p>
182+
* <p>
183+
* Exception handling and diagnostic during startup and shutdown is done directly in this class. This is materialized
184+
* by the fact that {@link Seed#hasLifecycleExceptionHandler()} returns true when {@link SeedMain} is used.
185+
* </p>
186+
* <p>
187+
* If an exception occurs during startup or shutdown, it is translated using {@link Seed#translateException(Exception)}
188+
* and its stack trace is printed on the standard error output. During startup only a diagnostic report is also dumped.
189+
* </p>
190+
*
191+
* @param args The launch arguments.
192+
*/
193+
public static void launch(String[] args) {
194+
try {
195+
final SeedLauncher seedLauncher;
196+
final String toolName = System.getProperty("seedstack.tool");
197+
if (!Strings.isNullOrEmpty(toolName)) {
198+
seedLauncher = Seed.getToolLauncher(toolName);
199+
} else {
200+
seedLauncher = Seed.getLauncher();
201+
}
202+
203+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
204+
try {
205+
seedLauncher.shutdown();
206+
Seed.close();
207+
} catch (Exception e) {
208+
handleException(e);
209+
}
210+
}, "shutdown"));
211+
Seed.markLifecycleExceptionHandlerEnabled();
212+
213+
seedLauncher.launch(args);
214+
} catch (Exception e) {
215+
handleException(e);
216+
System.exit(EXIT_FAILURE);
217+
}
218+
}
219+
184220
/**
185221
* Returns if a global exception handler for startup and shutdown is present or not.
186222
*
@@ -290,7 +326,7 @@ public static Kernel createKernel() {
290326
* @return the {@link Kernel} instance.
291327
*/
292328
public static Kernel createKernel(Object runtimeContext, KernelConfiguration kernelConfiguration,
293-
boolean autoStart) {
329+
boolean autoStart) {
294330
Seed instance = getInstance();
295331
return instance.kernelManager.createKernel(
296332
SeedRuntime.builder()
@@ -443,6 +479,12 @@ private void dispose() {
443479
Thread.setDefaultUncaughtExceptionHandler(null);
444480
}
445481

482+
private static void handleException(Exception e) {
483+
BaseException translated = Seed.translateException(e);
484+
Seed.diagnostic().dumpDiagnosticReport(translated);
485+
translated.printStackTrace(System.err);
486+
}
487+
446488
private static class Holder {
447489
private static final Seed INSTANCE = new Seed();
448490
}

core/src/main/java/org/seedstack/seed/core/SeedMain.java

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,16 @@
77
*/
88
package org.seedstack.seed.core;
99

10-
import com.google.common.base.Strings;
11-
import java.util.ServiceLoader;
12-
import org.seedstack.seed.spi.SeedLauncher;
13-
import org.seedstack.shed.exception.BaseException;
14-
1510
/**
16-
* <p>
17-
* Main SeedStack Java application entry point. It searches classes implementing {@link SeedLauncher} through the
18-
* {@link ServiceLoader} mechanism. If no class or more than one class is found, it throws an exception. If exactly one
19-
* class is found, it delegates the SeedStack application startup to its {@link SeedLauncher#launch(String[])} method.
20-
* </p>
21-
* <p>
22-
* Exception handling and diagnostic during startup and shutdown is done directly in this class. This is materialized
23-
* by the fact that {@link Seed#hasLifecycleExceptionHandler()} returns true when {@link SeedMain} is used.
24-
* </p>
25-
* <p>
26-
* If an exception occurs during startup or shutdown, it is translated using {@link Seed#translateException(Exception)}
27-
* and its stack trace is printed on the standard error output. During startup only a diagnostic report is also dumped.
28-
* </p>
11+
* Built-in main class delegating to {@link Seed#launch(String[])}.
2912
*/
3013
public class SeedMain {
31-
private static final int EXIT_FAILURE = 1;
32-
3314
/**
3415
* Entry point of SeedStack non-managed applications (launched from the command-line).
3516
*
3617
* @param args The command-line arguments.
3718
*/
3819
public static void main(String[] args) {
39-
try {
40-
final SeedLauncher seedLauncher;
41-
final String toolName = System.getProperty("seedstack.tool");
42-
if (!Strings.isNullOrEmpty(toolName)) {
43-
seedLauncher = Seed.getToolLauncher(toolName);
44-
} else {
45-
seedLauncher = Seed.getLauncher();
46-
}
47-
48-
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
49-
try {
50-
seedLauncher.shutdown();
51-
Seed.close();
52-
} catch (Exception e) {
53-
handleException(e);
54-
}
55-
}, "shutdown"));
56-
Seed.markLifecycleExceptionHandlerEnabled();
57-
58-
seedLauncher.launch(args);
59-
} catch (Exception e) {
60-
handleException(e);
61-
System.exit(EXIT_FAILURE);
62-
}
63-
}
64-
65-
private static void handleException(Exception e) {
66-
BaseException translated = Seed.translateException(e);
67-
Seed.diagnostic().dumpDiagnosticReport(translated);
68-
translated.printStackTrace(System.err);
20+
Seed.launch(args);
6921
}
7022
}

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<groupId>org.seedstack.seed</groupId>
2121
<artifactId>seed</artifactId>
22-
<version>3.11.1-SNAPSHOT</version>
22+
<version>3.12.0-SNAPSHOT</version>
2323
<packaging>pom</packaging>
2424

2525
<properties>
@@ -39,6 +39,7 @@
3939
<glassfish-javax.el.version>3.0.0</glassfish-javax.el.version>
4040
<arquillian.version>1.4.0.Final</arquillian.version>
4141
<tomcat.version>7.0.86</tomcat.version>
42+
<kubernetes-annotations.version>1.0.3</kubernetes-annotations.version>
4243

4344
<compatibility.version>3.1.0</compatibility.version>
4445

rest/core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.11.1-SNAPSHOT</version>
17+
<version>3.12.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-core</artifactId>

rest/jersey2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.11.1-SNAPSHOT</version>
17+
<version>3.12.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-jersey2</artifactId>

rest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<parent>
1414
<groupId>org.seedstack.seed</groupId>
1515
<artifactId>seed</artifactId>
16-
<version>3.11.1-SNAPSHOT</version>
16+
<version>3.12.0-SNAPSHOT</version>
1717
</parent>
1818

1919
<artifactId>seed-rest</artifactId>

rest/specs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<parent>
1414
<groupId>org.seedstack.seed</groupId>
1515
<artifactId>seed-rest</artifactId>
16-
<version>3.11.1-SNAPSHOT</version>
16+
<version>3.12.0-SNAPSHOT</version>
1717
</parent>
1818

1919
<artifactId>seed-rest-specs</artifactId>

0 commit comments

Comments
 (0)