From 1e9a82d2a232dd4e81b41ef3c524ab0e68f016d0 Mon Sep 17 00:00:00 2001 From: JunqiangYang Date: Thu, 20 Jul 2017 17:51:18 +0800 Subject: [PATCH] add code --- src/META-INF/MANIFEST.MF | 13 + .../spring-boot-loader/pom.properties | 4 + .../spring-boot-loader/pom.xml | 91 + .../boot/cli/DefaultCommandFactory.java | 51 + .../springframework/boot/cli/SpringCli.java | 102 + .../cli/app/SpringApplicationLauncher.java | 87 + ...gApplicationWebApplicationInitializer.java | 87 + .../PackagedSpringApplicationLauncher.java | 87 + .../boot/cli/archive/package-info.java | 21 + .../boot/cli/command/AbstractCommand.java | 76 + .../boot/cli/command/Command.java | 81 + .../boot/cli/command/CommandException.java | 117 + .../boot/cli/command/CommandFactory.java | 37 + .../boot/cli/command/CommandRunner.java | 304 ++ .../boot/cli/command/HelpExample.java | 49 + .../cli/command/NoArgumentsException.java | 28 + .../NoHelpCommandArgumentsException.java | 32 + .../cli/command/NoSuchCommandException.java | 32 + .../cli/command/OptionParsingCommand.java | 61 + .../cli/command/archive/ArchiveCommand.java | 339 ++ .../boot/cli/command/archive/JarCommand.java | 51 + .../cli/command/archive/ResourceMatcher.java | 221 + .../boot/cli/command/archive/WarCommand.java | 75 + .../boot/cli/command/core/HelpCommand.java | 125 + .../boot/cli/command/core/HintCommand.java | 115 + .../boot/cli/command/core/VersionCommand.java | 41 + .../boot/cli/command/grab/GrabCommand.java | 69 + .../boot/cli/command/init/Dependency.java | 51 + .../boot/cli/command/init/InitCommand.java | 274 ++ .../cli/command/init/InitializrService.java | 264 ++ .../init/InitializrServiceMetadata.java | 254 ++ .../init/ProjectGenerationRequest.java | 436 ++ .../init/ProjectGenerationResponse.java | 72 + .../cli/command/init/ProjectGenerator.java | 165 + .../boot/cli/command/init/ProjectType.java | 72 + .../cli/command/init/ReportableException.java | 35 + .../ServiceCapabilitiesReportGenerator.java | 168 + .../command/install/DependencyResolver.java | 41 + .../install/GroovyGrabDependencyResolver.java | 93 + .../cli/command/install/InstallCommand.java | 69 + .../boot/cli/command/install/Installer.java | 163 + .../cli/command/install/UninstallCommand.java | 85 + .../options/CompilerOptionHandler.java | 73 + .../cli/command/options/OptionHandler.java | 188 + .../boot/cli/command/options/OptionHelp.java | 40 + .../OptionSetGroovyCompilerConfiguration.java | 100 + .../cli/command/options/SourceOptions.java | 139 + .../boot/cli/command/run/RunCommand.java | 161 + .../command/run/SpringApplicationRunner.java | 278 ++ .../SpringApplicationRunnerConfiguration.java | 44 + .../boot/cli/command/shell/AnsiString.java | 81 + .../boot/cli/command/shell/ClearCommand.java | 46 + .../cli/command/shell/CommandCompleter.java | 149 + ...scapeAwareWhiteSpaceArgumentDelimiter.java | 103 + .../boot/cli/command/shell/ExitCommand.java | 39 + .../cli/command/shell/ForkProcessCommand.java | 82 + .../boot/cli/command/shell/PromptCommand.java | 51 + .../cli/command/shell/RunProcessCommand.java | 65 + .../boot/cli/command/shell/Shell.java | 238 ++ .../boot/cli/command/shell/ShellCommand.java | 41 + .../cli/command/shell/ShellExitException.java | 34 + .../boot/cli/command/shell/ShellPrompts.java | 59 + .../boot/cli/command/status/ExitStatus.java | 104 + .../AnnotatedNodeASTTransformation.java | 135 + .../boot/cli/compiler/AstUtils.java | 195 + .../compiler/CompilerAutoConfiguration.java | 95 + ...ndencyAutoConfigurationTransformation.java | 82 + .../cli/compiler/DependencyCustomizer.java | 270 ++ ...DependencyManagementBomTransformation.java | 243 ++ .../compiler/ExtendedGroovyClassLoader.java | 241 ++ .../compiler/GenericBomAstTransformation.java | 135 + .../compiler/GroovyBeansTransformation.java | 120 + .../boot/cli/compiler/GroovyCompiler.java | 331 ++ .../compiler/GroovyCompilerConfiguration.java | 80 + .../cli/compiler/GroovyCompilerScope.java | 37 + .../RepositoryConfigurationFactory.java | 131 + ...veDependencyCoordinatesTransformation.java | 112 + .../cli/compiler/SmartImportCustomizer.java | 61 + .../compiler/SpringBootAstTransformation.java | 30 + .../CachingCompilerAutoConfiguration.java | 53 + ...ovyTemplatesCompilerAutoConfiguration.java | 54 + .../JdbcCompilerAutoConfiguration.java | 52 + .../JmsCompilerAutoConfiguration.java | 55 + .../RabbitCompilerAutoConfiguration.java | 59 + .../SpringBatchCompilerAutoConfiguration.java | 68 + .../SpringBootCompilerAutoConfiguration.java | 106 + ...gIntegrationCompilerAutoConfiguration.java | 65 + ...SpringMobileCompilerAutoConfiguration.java | 53 + .../SpringMvcCompilerAutoConfiguration.java | 59 + .../SpringRetryCompilerAutoConfiguration.java | 52 + ...ringSecurityCompilerAutoConfiguration.java | 59 + ...curityOAuth2CompilerAutoConfiguration.java | 59 + ...cialFacebookCompilerAutoConfiguration.java | 55 + ...cialLinkedInCompilerAutoConfiguration.java | 55 + ...ocialTwitterCompilerAutoConfiguration.java | 54 + .../SpringTestCompilerAutoConfiguration.java | 76 + ...ingWebsocketCompilerAutoConfiguration.java | 57 + ...onManagementCompilerAutoConfiguration.java | 56 + .../ArtifactCoordinatesResolver.java | 55 + .../CompositeDependencyManagement.java | 70 + .../cli/compiler/dependencies/Dependency.java | 199 + .../dependencies/DependencyManagement.java | 48 + ...ManagementArtifactCoordinatesResolver.java | 75 + .../MavenModelDependencyManagement.java | 80 + ...gBootDependenciesDependencyManagement.java | 55 + .../compiler/dependencies/effective-pom.xml | 3752 +++++++++++++++++ .../cli/compiler/grape/AetherGrapeEngine.java | 360 ++ .../grape/AetherGrapeEngineFactory.java | 93 + .../grape/CompositeProxySelector.java | 51 + ...ositorySystemSessionAutoConfiguration.java | 72 + .../grape/DependencyResolutionContext.java | 115 + .../DependencyResolutionFailedException.java | 36 + .../grape/DetailedProgressReporter.java | 70 + .../compiler/grape/GrapeEngineInstaller.java | 44 + ...ositorySystemSessionAutoConfiguration.java | 56 + .../cli/compiler/grape/ProgressReporter.java | 32 + .../grape/RepositoryConfiguration.java | 99 + ...ositorySystemSessionAutoConfiguration.java | 38 + ...ositorySystemSessionAutoConfiguration.java | 55 + .../grape/SummaryProgressReporter.java | 102 + .../cli/compiler/maven/MavenSettings.java | 326 ++ .../compiler/maven/MavenSettingsReader.java | 126 + .../springframework/boot/cli/util/Log.java | 42 + .../boot/cli/util/ResourceUtils.java | 180 + .../boot/groovy/DependencyManagementBom.java | 44 + .../boot/groovy/EnableDeviceResolver.java | 37 + .../boot/groovy/EnableGroovyTemplates.java | 38 + .../boot/groovy/GroovyTemplate.java | 75 + .../boot/groovy/package-info.java | 22 + 129 files changed, 16645 insertions(+) create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.properties create mode 100644 src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.xml create mode 100644 src/org/springframework/boot/cli/DefaultCommandFactory.java create mode 100644 src/org/springframework/boot/cli/SpringCli.java create mode 100644 src/org/springframework/boot/cli/app/SpringApplicationLauncher.java create mode 100644 src/org/springframework/boot/cli/app/SpringApplicationWebApplicationInitializer.java create mode 100644 src/org/springframework/boot/cli/archive/PackagedSpringApplicationLauncher.java create mode 100644 src/org/springframework/boot/cli/archive/package-info.java create mode 100644 src/org/springframework/boot/cli/command/AbstractCommand.java create mode 100644 src/org/springframework/boot/cli/command/Command.java create mode 100644 src/org/springframework/boot/cli/command/CommandException.java create mode 100644 src/org/springframework/boot/cli/command/CommandFactory.java create mode 100644 src/org/springframework/boot/cli/command/CommandRunner.java create mode 100644 src/org/springframework/boot/cli/command/HelpExample.java create mode 100644 src/org/springframework/boot/cli/command/NoArgumentsException.java create mode 100644 src/org/springframework/boot/cli/command/NoHelpCommandArgumentsException.java create mode 100644 src/org/springframework/boot/cli/command/NoSuchCommandException.java create mode 100644 src/org/springframework/boot/cli/command/OptionParsingCommand.java create mode 100644 src/org/springframework/boot/cli/command/archive/ArchiveCommand.java create mode 100644 src/org/springframework/boot/cli/command/archive/JarCommand.java create mode 100644 src/org/springframework/boot/cli/command/archive/ResourceMatcher.java create mode 100644 src/org/springframework/boot/cli/command/archive/WarCommand.java create mode 100644 src/org/springframework/boot/cli/command/core/HelpCommand.java create mode 100644 src/org/springframework/boot/cli/command/core/HintCommand.java create mode 100644 src/org/springframework/boot/cli/command/core/VersionCommand.java create mode 100644 src/org/springframework/boot/cli/command/grab/GrabCommand.java create mode 100644 src/org/springframework/boot/cli/command/init/Dependency.java create mode 100644 src/org/springframework/boot/cli/command/init/InitCommand.java create mode 100644 src/org/springframework/boot/cli/command/init/InitializrService.java create mode 100644 src/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java create mode 100644 src/org/springframework/boot/cli/command/init/ProjectGenerationRequest.java create mode 100644 src/org/springframework/boot/cli/command/init/ProjectGenerationResponse.java create mode 100644 src/org/springframework/boot/cli/command/init/ProjectGenerator.java create mode 100644 src/org/springframework/boot/cli/command/init/ProjectType.java create mode 100644 src/org/springframework/boot/cli/command/init/ReportableException.java create mode 100644 src/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java create mode 100644 src/org/springframework/boot/cli/command/install/DependencyResolver.java create mode 100644 src/org/springframework/boot/cli/command/install/GroovyGrabDependencyResolver.java create mode 100644 src/org/springframework/boot/cli/command/install/InstallCommand.java create mode 100644 src/org/springframework/boot/cli/command/install/Installer.java create mode 100644 src/org/springframework/boot/cli/command/install/UninstallCommand.java create mode 100644 src/org/springframework/boot/cli/command/options/CompilerOptionHandler.java create mode 100644 src/org/springframework/boot/cli/command/options/OptionHandler.java create mode 100644 src/org/springframework/boot/cli/command/options/OptionHelp.java create mode 100644 src/org/springframework/boot/cli/command/options/OptionSetGroovyCompilerConfiguration.java create mode 100644 src/org/springframework/boot/cli/command/options/SourceOptions.java create mode 100644 src/org/springframework/boot/cli/command/run/RunCommand.java create mode 100644 src/org/springframework/boot/cli/command/run/SpringApplicationRunner.java create mode 100644 src/org/springframework/boot/cli/command/run/SpringApplicationRunnerConfiguration.java create mode 100644 src/org/springframework/boot/cli/command/shell/AnsiString.java create mode 100644 src/org/springframework/boot/cli/command/shell/ClearCommand.java create mode 100644 src/org/springframework/boot/cli/command/shell/CommandCompleter.java create mode 100644 src/org/springframework/boot/cli/command/shell/EscapeAwareWhiteSpaceArgumentDelimiter.java create mode 100644 src/org/springframework/boot/cli/command/shell/ExitCommand.java create mode 100644 src/org/springframework/boot/cli/command/shell/ForkProcessCommand.java create mode 100644 src/org/springframework/boot/cli/command/shell/PromptCommand.java create mode 100644 src/org/springframework/boot/cli/command/shell/RunProcessCommand.java create mode 100644 src/org/springframework/boot/cli/command/shell/Shell.java create mode 100644 src/org/springframework/boot/cli/command/shell/ShellCommand.java create mode 100644 src/org/springframework/boot/cli/command/shell/ShellExitException.java create mode 100644 src/org/springframework/boot/cli/command/shell/ShellPrompts.java create mode 100644 src/org/springframework/boot/cli/command/status/ExitStatus.java create mode 100644 src/org/springframework/boot/cli/compiler/AnnotatedNodeASTTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/AstUtils.java create mode 100644 src/org/springframework/boot/cli/compiler/CompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/DependencyAutoConfigurationTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/DependencyCustomizer.java create mode 100644 src/org/springframework/boot/cli/compiler/DependencyManagementBomTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/ExtendedGroovyClassLoader.java create mode 100644 src/org/springframework/boot/cli/compiler/GenericBomAstTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/GroovyCompiler.java create mode 100644 src/org/springframework/boot/cli/compiler/GroovyCompilerConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/GroovyCompilerScope.java create mode 100644 src/org/springframework/boot/cli/compiler/RepositoryConfigurationFactory.java create mode 100644 src/org/springframework/boot/cli/compiler/ResolveDependencyCoordinatesTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/SmartImportCustomizer.java create mode 100644 src/org/springframework/boot/cli/compiler/SpringBootAstTransformation.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/CachingCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/GroovyTemplatesCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/JdbcCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/JmsCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/RabbitCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringBootCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringIntegrationCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringMobileCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringMvcCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringRetryCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringSecurityCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringSecurityOAuth2CompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialFacebookCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialLinkedInCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialTwitterCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringTestCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/SpringWebsocketCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/autoconfigure/TransactionManagementCompilerAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/ArtifactCoordinatesResolver.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/CompositeDependencyManagement.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/Dependency.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/DependencyManagement.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/DependencyManagementArtifactCoordinatesResolver.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/MavenModelDependencyManagement.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/SpringBootDependenciesDependencyManagement.java create mode 100644 src/org/springframework/boot/cli/compiler/dependencies/effective-pom.xml create mode 100644 src/org/springframework/boot/cli/compiler/grape/AetherGrapeEngine.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/AetherGrapeEngineFactory.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/CompositeProxySelector.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/DefaultRepositorySystemSessionAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/DependencyResolutionContext.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/DependencyResolutionFailedException.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/DetailedProgressReporter.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/GrapeEngineInstaller.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/GrapeRootRepositorySystemSessionAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/ProgressReporter.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/RepositoryConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/RepositorySystemSessionAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/SettingsXmlRepositorySystemSessionAutoConfiguration.java create mode 100644 src/org/springframework/boot/cli/compiler/grape/SummaryProgressReporter.java create mode 100644 src/org/springframework/boot/cli/compiler/maven/MavenSettings.java create mode 100644 src/org/springframework/boot/cli/compiler/maven/MavenSettingsReader.java create mode 100644 src/org/springframework/boot/cli/util/Log.java create mode 100644 src/org/springframework/boot/cli/util/ResourceUtils.java create mode 100644 src/org/springframework/boot/groovy/DependencyManagementBom.java create mode 100644 src/org/springframework/boot/groovy/EnableDeviceResolver.java create mode 100644 src/org/springframework/boot/groovy/EnableGroovyTemplates.java create mode 100644 src/org/springframework/boot/groovy/GroovyTemplate.java create mode 100644 src/org/springframework/boot/groovy/package-info.java diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 00000000..46c139e2 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,13 @@ +Manifest-Version: 1.0 +Implementation-Title: Spring Boot CLI +Implementation-Version: 2.0.0.BUILD-SNAPSHOT +Archiver-Version: Plexus Archiver +Built-By: bamboo +Start-Class: org.springframework.boot.cli.SpringCli +Implementation-Vendor-Id: org.springframework.boot +Class-Loader: groovy.lang.GroovyClassLoader +Created-By: Apache Maven 3.5.0 +Build-Jdk: 1.8.0_121 +Implementation-Vendor: Pivotal Software, Inc. +Main-Class: org.springframework.boot.loader.JarLauncher + diff --git a/src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.properties b/src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.properties new file mode 100644 index 00000000..2481fe49 --- /dev/null +++ b/src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.properties @@ -0,0 +1,4 @@ +#Created by Apache Maven 3.5.0 +version=2.0.0.BUILD-SNAPSHOT +groupId=org.springframework.boot +artifactId=spring-boot-loader diff --git a/src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.xml b/src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.xml new file mode 100644 index 00000000..1d72ad5a --- /dev/null +++ b/src/META-INF/maven/org.springframework.boot/spring-boot-loader/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-tools + 2.0.0.BUILD-SNAPSHOT + + spring-boot-loader + Spring Boot Loader + Spring Boot Loader + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + + ${basedir}/../.. + + + + + org.springframework + spring-core + true + + + + org.slf4j + jcl-over-slf4j + test + + + ch.qos.logback + logback-classic + test + + + org.springframework + spring-webmvc + test + + + + org.bouncycastle + bcprov-jdk16 + 1.46 + test + + + org.springframework.boot + spring-boot-test-support + test + + + + + integration + + true + + + + + org.apache.maven.plugins + maven-invoker-plugin + + ${project.build.directory}/it + src/it/settings.xml + ${project.build.directory}/local-repo + verify + true + ${skipTests} + true + + + + integration-test + + install + run + + + + + + + + + diff --git a/src/org/springframework/boot/cli/DefaultCommandFactory.java b/src/org/springframework/boot/cli/DefaultCommandFactory.java new file mode 100644 index 00000000..8c5813e9 --- /dev/null +++ b/src/org/springframework/boot/cli/DefaultCommandFactory.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.springframework.boot.cli.command.Command; +import org.springframework.boot.cli.command.CommandFactory; +import org.springframework.boot.cli.command.archive.JarCommand; +import org.springframework.boot.cli.command.archive.WarCommand; +import org.springframework.boot.cli.command.core.VersionCommand; +import org.springframework.boot.cli.command.grab.GrabCommand; +import org.springframework.boot.cli.command.init.InitCommand; +import org.springframework.boot.cli.command.install.InstallCommand; +import org.springframework.boot.cli.command.install.UninstallCommand; +import org.springframework.boot.cli.command.run.RunCommand; + +/** + * Default implementation of {@link CommandFactory}. + * + * @author Dave Syer + */ +public class DefaultCommandFactory implements CommandFactory { + + private static final List DEFAULT_COMMANDS = Arrays.asList( + new VersionCommand(), new RunCommand(), new GrabCommand(), new JarCommand(), + new WarCommand(), new InstallCommand(), new UninstallCommand(), + new InitCommand()); + + @Override + public Collection getCommands() { + return DEFAULT_COMMANDS; + } + +} diff --git a/src/org/springframework/boot/cli/SpringCli.java b/src/org/springframework/boot/cli/SpringCli.java new file mode 100644 index 00000000..00998f56 --- /dev/null +++ b/src/org/springframework/boot/cli/SpringCli.java @@ -0,0 +1,102 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; + +import org.springframework.boot.cli.command.CommandFactory; +import org.springframework.boot.cli.command.CommandRunner; +import org.springframework.boot.cli.command.core.HelpCommand; +import org.springframework.boot.cli.command.core.HintCommand; +import org.springframework.boot.cli.command.core.VersionCommand; +import org.springframework.boot.cli.command.shell.ShellCommand; +import org.springframework.boot.loader.tools.LogbackInitializer; +import org.springframework.util.ClassUtils; +import org.springframework.util.SystemPropertyUtils; + +/** + * Spring Command Line Interface. This is the main entry-point for the Spring command line + * application. + * + * @author Phillip Webb + * @see #main(String...) + * @see CommandRunner + */ +public final class SpringCli { + + private SpringCli() { + } + + public static void main(String... args) { + System.setProperty("java.awt.headless", Boolean.toString(true)); + LogbackInitializer.initialize(); + + CommandRunner runner = new CommandRunner("spring"); + ClassUtils.overrideThreadContextClassLoader(createExtendedClassLoader(runner)); + runner.addCommand(new HelpCommand(runner)); + addServiceLoaderCommands(runner); + runner.addCommand(new ShellCommand()); + runner.addCommand(new HintCommand(runner)); + runner.setOptionCommands(HelpCommand.class, VersionCommand.class); + runner.setHiddenCommands(HintCommand.class); + + int exitCode = runner.runAndHandleErrors(args); + if (exitCode != 0) { + // If successful, leave it to run in case it's a server app + System.exit(exitCode); + } + } + + private static void addServiceLoaderCommands(CommandRunner runner) { + ServiceLoader factories = ServiceLoader + .load(CommandFactory.class); + for (CommandFactory factory : factories) { + runner.addCommands(factory.getCommands()); + } + } + + private static URLClassLoader createExtendedClassLoader(CommandRunner runner) { + return new URLClassLoader(getExtensionURLs(), runner.getClass().getClassLoader()); + } + + private static URL[] getExtensionURLs() { + List urls = new ArrayList<>(); + String home = SystemPropertyUtils + .resolvePlaceholders("${spring.home:${SPRING_HOME:.}}"); + File extDirectory = new File(new File(home, "lib"), "ext"); + if (extDirectory.isDirectory()) { + for (File file : extDirectory.listFiles()) { + if (file.getName().endsWith(".jar")) { + try { + urls.add(file.toURI().toURL()); + } + catch (MalformedURLException ex) { + throw new IllegalStateException(ex); + } + } + } + } + return urls.toArray(new URL[urls.size()]); + } + +} diff --git a/src/org/springframework/boot/cli/app/SpringApplicationLauncher.java b/src/org/springframework/boot/cli/app/SpringApplicationLauncher.java new file mode 100644 index 00000000..16faeba2 --- /dev/null +++ b/src/org/springframework/boot/cli/app/SpringApplicationLauncher.java @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.app; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * A launcher for {@code SpringApplication} or a {@code SpringApplication} subclass. The + * class that is used can be configured using the System property + * {@code spring.application.class.name} or the {@code SPRING_APPLICATION_CLASS_NAME} + * environment variable. Uses reflection to allow the launching code to exist in a + * separate ClassLoader from the application code. + * + * @author Andy Wilkinson + * @since 1.2.0 + * @see System#getProperty(String) + * @see System#getenv(String) + */ +public class SpringApplicationLauncher { + + private static final String DEFAULT_SPRING_APPLICATION_CLASS = "org.springframework.boot.SpringApplication"; + + private final ClassLoader classLoader; + + /** + * Creates a new launcher that will use the given {@code classLoader} to load the + * configured {@code SpringApplication} class. + * @param classLoader the {@code ClassLoader} to use + */ + public SpringApplicationLauncher(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + /** + * Launches the application created using the given {@code sources}. The application + * is launched with the given {@code args}. + * @param sources The sources for the application + * @param args The args for the application + * @return The application's {@code ApplicationContext} + * @throws Exception if the launch fails + */ + public Object launch(Class[] sources, String[] args) throws Exception { + Map defaultProperties = new HashMap<>(); + defaultProperties.put("spring.groovy.template.check-template-location", "false"); + Class applicationClass = this.classLoader + .loadClass(getSpringApplicationClassName()); + Constructor constructor = applicationClass.getConstructor(Class[].class); + Object application = constructor.newInstance((Object) sources); + applicationClass.getMethod("setDefaultProperties", Map.class).invoke(application, + defaultProperties); + Method method = applicationClass.getMethod("run", String[].class); + return method.invoke(application, (Object) args); + } + + private String getSpringApplicationClassName() { + String className = System.getProperty("spring.application.class.name"); + if (className == null) { + className = getEnvironmentVariable("SPRING_APPLICATION_CLASS_NAME"); + } + if (className == null) { + className = DEFAULT_SPRING_APPLICATION_CLASS; + } + return className; + } + + protected String getEnvironmentVariable(String name) { + return System.getenv(name); + } + +} diff --git a/src/org/springframework/boot/cli/app/SpringApplicationWebApplicationInitializer.java b/src/org/springframework/boot/cli/app/SpringApplicationWebApplicationInitializer.java new file mode 100644 index 00000000..b87d8e26 --- /dev/null +++ b/src/org/springframework/boot/cli/app/SpringApplicationWebApplicationInitializer.java @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.app; + +import java.io.IOException; +import java.io.InputStream; +import java.util.jar.Manifest; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +/** + * {@link SpringBootServletInitializer} for CLI packaged WAR files. + * + * @author Phillip Webb + * @since 1.3.0 + */ +public class SpringApplicationWebApplicationInitializer + extends SpringBootServletInitializer { + + /** + * The entry containing the source class. + */ + public static final String SOURCE_ENTRY = "Spring-Application-Source-Classes"; + + private String[] sources; + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + try { + this.sources = getSources(servletContext); + } + catch (IOException ex) { + throw new IllegalStateException(ex); + } + super.onStartup(servletContext); + } + + private String[] getSources(ServletContext servletContext) throws IOException { + Manifest manifest = getManifest(servletContext); + if (manifest == null) { + throw new IllegalStateException("Unable to read manifest"); + } + String sources = manifest.getMainAttributes().getValue(SOURCE_ENTRY); + return sources.split(","); + } + + private Manifest getManifest(ServletContext servletContext) throws IOException { + InputStream stream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF"); + Manifest manifest = (stream == null ? null : new Manifest(stream)); + return manifest; + } + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { + try { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + Class[] sourceClasses = new Class[this.sources.length]; + for (int i = 0; i < this.sources.length; i++) { + sourceClasses[i] = classLoader.loadClass(this.sources[i]); + } + return builder.sources(sourceClasses) + .properties("spring.groovy.template.check-template-location=false"); + } + catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/src/org/springframework/boot/cli/archive/PackagedSpringApplicationLauncher.java b/src/org/springframework/boot/cli/archive/PackagedSpringApplicationLauncher.java new file mode 100644 index 00000000..2f03e58b --- /dev/null +++ b/src/org/springframework/boot/cli/archive/PackagedSpringApplicationLauncher.java @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.archive; + +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +import org.springframework.boot.cli.app.SpringApplicationLauncher; + +/** + * A launcher for a CLI application that has been compiled and packaged as a jar file. + * + * @author Andy Wilkinson + * @author Phillip Webb + */ +public final class PackagedSpringApplicationLauncher { + + /** + * The entry containing the source class. + */ + public static final String SOURCE_ENTRY = "Spring-Application-Source-Classes"; + + /** + * The entry containing the start class. + */ + public static final String START_CLASS_ENTRY = "Start-Class"; + + private PackagedSpringApplicationLauncher() { + } + + private void run(String[] args) throws Exception { + URLClassLoader classLoader = (URLClassLoader) Thread.currentThread() + .getContextClassLoader(); + new SpringApplicationLauncher(classLoader).launch(getSources(classLoader), args); + } + + private Class[] getSources(URLClassLoader classLoader) throws Exception { + Enumeration urls = classLoader.getResources("META-INF/MANIFEST.MF"); + while (urls.hasMoreElements()) { + URL url = urls.nextElement(); + Manifest manifest = new Manifest(url.openStream()); + if (isCliPackaged(manifest)) { + String sources = manifest.getMainAttributes().getValue(SOURCE_ENTRY); + return loadClasses(classLoader, sources.split(",")); + } + } + throw new IllegalStateException( + "Cannot locate " + SOURCE_ENTRY + " in MANIFEST.MF"); + } + + private boolean isCliPackaged(Manifest manifest) { + Attributes attributes = manifest.getMainAttributes(); + String startClass = attributes.getValue(START_CLASS_ENTRY); + return getClass().getName().equals(startClass); + } + + private Class[] loadClasses(ClassLoader classLoader, String[] names) + throws ClassNotFoundException { + Class[] classes = new Class[names.length]; + for (int i = 0; i < names.length; i++) { + classes[i] = classLoader.loadClass(names[i]); + } + return classes; + } + + public static void main(String[] args) throws Exception { + new PackagedSpringApplicationLauncher().run(args); + } + +} diff --git a/src/org/springframework/boot/cli/archive/package-info.java b/src/org/springframework/boot/cli/archive/package-info.java new file mode 100644 index 00000000..3824714e --- /dev/null +++ b/src/org/springframework/boot/cli/archive/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Class that are packaged as part of CLI generated JARs. + * @see org.springframework.boot.cli.command.archive.JarCommand + */ +package org.springframework.boot.cli.archive; diff --git a/src/org/springframework/boot/cli/command/AbstractCommand.java b/src/org/springframework/boot/cli/command/AbstractCommand.java new file mode 100644 index 00000000..0b56071e --- /dev/null +++ b/src/org/springframework/boot/cli/command/AbstractCommand.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.command; + +import java.util.Collection; +import java.util.Collections; + +import org.springframework.boot.cli.command.options.OptionHelp; + +/** + * Abstract {@link Command} implementation. + * + * @author Phillip Webb + * @author Dave Syer + */ +public abstract class AbstractCommand implements Command { + + private final String name; + + private final String description; + + /** + * Create a new {@link AbstractCommand} instance. + * @param name the name of the command + * @param description the command description + */ + protected AbstractCommand(String name, String description) { + this.name = name; + this.description = description; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public String getDescription() { + return this.description; + } + + @Override + public String getUsageHelp() { + return null; + } + + @Override + public String getHelp() { + return null; + } + + @Override + public Collection getOptionsHelp() { + return Collections.emptyList(); + } + + @Override + public Collection getExamples() { + return null; + } + +} diff --git a/src/org/springframework/boot/cli/command/Command.java b/src/org/springframework/boot/cli/command/Command.java new file mode 100644 index 00000000..adf0fe1a --- /dev/null +++ b/src/org/springframework/boot/cli/command/Command.java @@ -0,0 +1,81 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.command; + +import java.util.Collection; + +import org.springframework.boot.cli.command.options.OptionHelp; +import org.springframework.boot.cli.command.status.ExitStatus; + +/** + * A single command that can be run from the CLI. + * + * @author Phillip Webb + * @author Dave Syer + * @author Stephane Nicoll + * @see #run(String...) + */ +public interface Command { + + /** + * Returns the name of the command. + * @return the command's name + */ + String getName(); + + /** + * Returns a description of the command. + * @return the command's description + */ + String getDescription(); + + /** + * Returns usage help for the command. This should be a simple one-line string + * describing basic usage. e.g. '[options] <file>'. Do not include the name of + * the command in this string. + * @return the command's usage help + */ + String getUsageHelp(); + + /** + * Gets full help text for the command, e.g. a longer description and one line per + * option. + * @return the command's help text + */ + String getHelp(); + + /** + * Returns help for each supported option. + * @return help for each of the command's options + */ + Collection getOptionsHelp(); + + /** + * Return some examples for the command. + * @return the command's examples + */ + Collection getExamples(); + + /** + * Run the command. + * @param args command arguments (this will not include the command itself) + * @return the outcome of the command + * @throws Exception if the command fails + */ + ExitStatus run(String... args) throws Exception; + +} diff --git a/src/org/springframework/boot/cli/command/CommandException.java b/src/org/springframework/boot/cli/command/CommandException.java new file mode 100644 index 00000000..bf2b9f71 --- /dev/null +++ b/src/org/springframework/boot/cli/command/CommandException.java @@ -0,0 +1,117 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.command; + +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + +/** + * Runtime exception wrapper that defines additional {@link Option}s that are understood + * by the {@link CommandRunner}. + * + * @author Phillip Webb + */ +public class CommandException extends RuntimeException { + + private static final long serialVersionUID = 0L; + + private final EnumSet