Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,36 @@
*/
package org.springframework.shell.boot;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.ShellRunner;
import org.springframework.util.ClassUtils;

/**
* @author Janne Valkealahti
* @author Chris Bono
* @author Mahmoud Ben Hassine
* @author Piotr Olaszewski
*/
@AutoConfiguration
@EnableConfigurationProperties(SpringShellProperties.class)
public class ApplicationRunnerAutoConfiguration {

@Bean
public ApplicationRunner applicationRunner(ObjectProvider<ShellRunner> shellRunner) {
return new ShellApplicationRunner(shellRunner);
}

@Bean
@ConditionalOnProperty(prefix = "spring.shell.context", name = "close", havingValue = "true")
public ApplicationReadyEventListener applicationReadyEventListener() {
public ApplicationListener<ApplicationReadyEvent> applicationReadyEventListener() {
return new ApplicationReadyEventListener();
}

Expand All @@ -43,4 +59,22 @@ public void onApplicationEvent(ApplicationReadyEvent event) {

}

record ShellApplicationRunner(ObjectProvider<ShellRunner> shellRunner) implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
shellRunner.orderedStream().forEachOrdered(runner -> {
try {
boolean run = runner.run(args.getSourceArgs());
if (run) {
return;
}
}
catch (Exception e) {
throw new IllegalStateException(
"Unable to run '" + ClassUtils.getShortName(runner.getClass()) + "'", e);
}
});
}
}

}
Loading