|
| 1 | +package grails.ui.command |
| 2 | + |
| 3 | +import grails.config.Settings |
| 4 | +import grails.dev.commands.ApplicationCommand |
| 5 | +import grails.dev.commands.ExecutionContext |
| 6 | +import grails.ui.support.DevelopmentGrailsApplication |
| 7 | +import groovy.transform.CompileStatic |
| 8 | +import org.grails.build.parsing.CommandLine |
| 9 | +import org.grails.build.parsing.CommandLineParser |
| 10 | +import org.springframework.beans.factory.config.AutowireCapableBeanFactory |
| 11 | +import org.springframework.context.ConfigurableApplicationContext |
| 12 | + |
| 13 | +/** |
| 14 | + * Created by Jim on 8/2/2016. |
| 15 | + */ |
| 16 | +@CompileStatic |
| 17 | +class GrailsRuntimeApplicationContextCommandRunner extends DevelopmentGrailsApplication { |
| 18 | + |
| 19 | + String commandName |
| 20 | + |
| 21 | + protected GrailsRuntimeApplicationContextCommandRunner(String commandName, Object... sources) { |
| 22 | + super(sources) |
| 23 | + this.commandName = commandName |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + ConfigurableApplicationContext run(String... args) { |
| 28 | + try { |
| 29 | + def commandClass = classLoader.loadClass(commandName) |
| 30 | + if (ApplicationCommand.isAssignableFrom(commandClass)) { |
| 31 | + def command = (ApplicationCommand)commandClass.newInstance() |
| 32 | + Object skipBootstrap = command.hasProperty("skipBootstrap")?.getProperty(command) |
| 33 | + if (skipBootstrap instanceof Boolean && !System.getProperty(Settings.SETTING_SKIP_BOOTSTRAP)) { |
| 34 | + System.setProperty(Settings.SETTING_SKIP_BOOTSTRAP, skipBootstrap.toString()) |
| 35 | + } |
| 36 | + |
| 37 | + ConfigurableApplicationContext ctx = null |
| 38 | + try { |
| 39 | + ctx = super.run(args) |
| 40 | + } catch (Throwable e) { |
| 41 | + System.err.println("Context failed to load: $e.message") |
| 42 | + System.exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + CommandLine commandLine = new CommandLineParser().parse(args) |
| 47 | + ctx.autowireCapableBeanFactory.autowireBeanProperties(command, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false) |
| 48 | + command.applicationContext = ctx |
| 49 | + def result = command.handle(new ExecutionContext(commandLine)) |
| 50 | + result ? System.exit(0) : System.exit(1) |
| 51 | + } catch (Throwable e) { |
| 52 | + System.err.println("Command execution error: $e.message") |
| 53 | + System.exit(1) |
| 54 | + } |
| 55 | + |
| 56 | + finally { |
| 57 | + try { |
| 58 | + ctx?.close() |
| 59 | + } catch (Throwable e) { |
| 60 | + // ignore |
| 61 | + } |
| 62 | + } |
| 63 | + } else { |
| 64 | + System.err.println("Command not found") |
| 65 | + System.exit(1) |
| 66 | + } |
| 67 | + } catch (ClassNotFoundException e) { |
| 68 | + System.err.println("Command not found") |
| 69 | + System.exit(1) |
| 70 | + } |
| 71 | + return null |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Main method to run an existing Application class |
| 76 | + * |
| 77 | + * @param args The first argument is the Command name, the last argument is the Application class name |
| 78 | + */ |
| 79 | + public static void main(String[] args) { |
| 80 | + if(args.size() > 1) { |
| 81 | + Class applicationClass |
| 82 | + try { |
| 83 | + applicationClass = Thread.currentThread().contextClassLoader.loadClass(args.last()) |
| 84 | + } catch (Throwable e) { |
| 85 | + System.err.println("Application class not found") |
| 86 | + System.exit(1) |
| 87 | + } |
| 88 | + |
| 89 | + def runner = new GrailsRuntimeApplicationContextCommandRunner(args[0], applicationClass) |
| 90 | + runner.run(args.init() as String[]) |
| 91 | + } |
| 92 | + else { |
| 93 | + System.err.println("Missing application class name and script name arguments") |
| 94 | + System.exit(1) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments