Skip to content

Commit

Permalink
Make LaunchMode injectable
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored and mmusgrov committed Dec 12, 2019
1 parent 74cd054 commit fc22ad7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/runtime/src/main/java/io/quarkus/runtime/LaunchMode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.runtime;

import io.quarkus.runtime.configuration.ProfileManager;

public enum LaunchMode {

/**
Expand Down Expand Up @@ -29,4 +31,12 @@ public boolean isDevOrTest() {
public String getDefaultProfile() {
return defaultProfile;
}

/**
*
* @return The current launch mode
*/
public static LaunchMode current() {
return ProfileManager.getLaunchMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public static void setLaunchMode(LaunchMode mode) {
launchMode = mode;
}

public static LaunchMode getLaunchMode() {
return launchMode;
}

public static void setRuntimeDefaultProfile(final String profile) {
runtimeDefaultProfile = profile;
}
Expand Down
8 changes: 8 additions & 0 deletions docs/src/main/asciidoc/lifecycle.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ When the application is stopped, the second log message is printed.

As usual, the application can be packaged using `./mvnw clean package` and executed using the `-runner.jar` file.
You can also generate the native executable using `./mvnw clean package -Pnative`.

== Launch Modes

Quarkus has 3 different launch modes, `NORMAL` (i.e. production), `DEVELOPMENT` and `TEST`. If you are running `quarkus:dev`
then the mode will be `DEVELOPMENT`, if you are running a JUnit test it will be `TEST`, otherwise it will be `NORMAL`.

Your application can get the launch mode by injecting the `io.quarkus.runtime.LaunchMode` enum into a CDI bean,
or by invoking the static method `io.quarkus.runtime.LaunchMode.current()`.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.quarkus.arc.runtime.AdditionalBean;
import io.quarkus.arc.runtime.ArcRecorder;
import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.arc.runtime.LaunchModeProducer;
import io.quarkus.arc.runtime.LifecycleEventRunner;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -328,6 +329,11 @@ void setupExecutor(ExecutorBuildItem executor, ArcRecorder recorder) {
recorder.initExecutor(executor.getExecutorProxy());
}

@BuildStep
AdditionalBeanBuildItem launchMode() {
return new AdditionalBeanBuildItem(LaunchModeProducer.class);
}

private abstract static class AbstractCompositeApplicationClassesPredicate<T> implements Predicate<T> {

private final IndexView applicationClassesIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.runtime.LaunchMode;
import io.quarkus.test.QuarkusUnitTest;

public class SimpleBeanTest {
Expand All @@ -25,6 +26,9 @@ public class SimpleBeanTest {
@Inject
SimpleBean simpleBean;

@Inject
LaunchMode launchMode;

@Test
public void testSimpleBean() {
assertNotNull(simpleBean.getStartupEvent());
Expand All @@ -34,4 +38,9 @@ public void testSimpleBean() {
assertEquals("1", simpleBean.getBazProvider().get());
}

@Test
public void testLaunchModeInjection() {
assertEquals(LaunchMode.TEST, launchMode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.arc.runtime;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;

import io.quarkus.runtime.LaunchMode;

@Dependent
public class LaunchModeProducer {

@Produces
LaunchMode mode() {
return LaunchMode.current();
}
}

0 comments on commit fc22ad7

Please sign in to comment.