parent
8ff2a88712
commit
b07a1998df
|
@ -1,164 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 groovy.lang.Binding;
|
||||
import groovy.lang.GroovyClassLoader;
|
||||
import groovy.lang.Script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import joptsimple.OptionSet;
|
||||
import joptsimple.OptionSpec;
|
||||
|
||||
import org.springframework.boot.cli.Command;
|
||||
import org.springframework.boot.cli.CommandFactory;
|
||||
import org.springframework.boot.cli.SpringCli;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompiler;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompilerConfigurationAdapter;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompilerScope;
|
||||
import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
|
||||
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
||||
import org.springframework.core.env.JOptCommandLinePropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Command to initialize the Spring CLI with commands from the classpath. If the current
|
||||
* context class loader is a GroovyClassLoader then it can be enhanced by passing in
|
||||
* compiler options (e.g. <code>--classpath=...</code>).
|
||||
* </p>
|
||||
* <p>
|
||||
* If the current context class loader is not already GroovyClassLoader then one will be
|
||||
* created and will replace the current context loader. In this case command arguments can
|
||||
* include files to compile that have <code>@Grab</code> annotations to process. By
|
||||
* default a script called "init.groovy" or "spring.groovy" is used if it exists in the
|
||||
* current directory or the root of the classpath.
|
||||
* </p>
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class InitCommand extends OptionParsingCommand {
|
||||
|
||||
public static final String NAME = "init";
|
||||
|
||||
public InitCommand(SpringCli cli) {
|
||||
super(NAME, "(Re)-initialize the Spring cli", new InitOptionHandler(cli));
|
||||
}
|
||||
|
||||
private static class InitOptionHandler extends CompilerOptionHandler {
|
||||
|
||||
private static final String DEFAULT_PATH = "file:init.groovy,file:spring.groovy";
|
||||
private SpringCli cli;
|
||||
private GroovyCompiler compiler;
|
||||
private OptionSpec<String> initOption;
|
||||
|
||||
public InitOptionHandler(SpringCli cli) {
|
||||
this.cli = cli;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOptions() {
|
||||
this.initOption = option("init",
|
||||
"Path to init file as comma-separated list (default file:init.groovy,file:spring.groovy)")
|
||||
.withOptionalArg().defaultsTo(
|
||||
System.getProperty("spring.cli.init", DEFAULT_PATH));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void run(OptionSet options) throws Exception {
|
||||
|
||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
boolean enhanced = false;
|
||||
|
||||
String[] paths = StringUtils.commaDelimitedListToStringArray(this.initOption
|
||||
.value(options));
|
||||
SourceOptions sourceOptions = new SourceOptions(options, loader, paths);
|
||||
String[] sources = sourceOptions.getSourcesArray();
|
||||
|
||||
if (!(loader instanceof GroovyClassLoader) && sources.length > 0) {
|
||||
|
||||
List<RepositoryConfiguration> repositoryConfiguration = RepositoryConfigurationFactory
|
||||
.createDefaultRepositoryConfiguration();
|
||||
|
||||
GroovyCompilerConfiguration configuration = new InitGroovyCompilerConfigurationAdapter(
|
||||
options, this, repositoryConfiguration);
|
||||
|
||||
this.compiler = new GroovyCompiler(configuration);
|
||||
loader = this.compiler.getLoader();
|
||||
Thread.currentThread().setContextClassLoader(loader);
|
||||
|
||||
}
|
||||
else {
|
||||
String classpath = getClasspathOption().value(options);
|
||||
if (classpath != null && classpath.length() > 0) {
|
||||
((GroovyClassLoader) loader).addClasspath(classpath);
|
||||
enhanced = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.compiler != null && sources.length > 0) {
|
||||
Class<?>[] classes = this.compiler.compile(sources);
|
||||
for (Class<?> type : classes) {
|
||||
if (Script.class.isAssignableFrom(type)) {
|
||||
Script script = (Script) type.newInstance();
|
||||
JOptCommandLinePropertySource properties = new JOptCommandLinePropertySource(
|
||||
options);
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
for (String key : properties.getPropertyNames()) {
|
||||
map.put(key, properties.getProperty(key));
|
||||
}
|
||||
script.setBinding(new Binding(map));
|
||||
script.run();
|
||||
}
|
||||
enhanced = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (enhanced) {
|
||||
|
||||
for (CommandFactory factory : ServiceLoader.load(CommandFactory.class,
|
||||
loader)) {
|
||||
for (Command command : factory.getCommands(this.cli)) {
|
||||
this.cli.register(command);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static class InitGroovyCompilerConfigurationAdapter extends
|
||||
GroovyCompilerConfigurationAdapter {
|
||||
private InitGroovyCompilerConfigurationAdapter(OptionSet optionSet,
|
||||
CompilerOptionHandler compilerOptionHandler,
|
||||
List<RepositoryConfiguration> repositoryConfiguration) {
|
||||
super(optionSet, compilerOptionHandler, repositoryConfiguration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroovyCompilerScope getScope() {
|
||||
return GroovyCompilerScope.EXTENSION;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -99,7 +99,6 @@ public class ShellCommand extends AbstractCommand {
|
|||
|
||||
PromptCommand prompt = new PromptCommand(this);
|
||||
cli.register(prompt);
|
||||
cli.register(new InitCommand(cli));
|
||||
}
|
||||
|
||||
private ConsoleReader createConsoleReader() throws IOException {
|
||||
|
|
|
@ -1,121 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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.Random;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.OutputCapture;
|
||||
import org.springframework.boot.cli.Command;
|
||||
import org.springframework.boot.cli.CommandFactory;
|
||||
import org.springframework.boot.cli.SpringCli;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class InitCommandPerformanceTests {
|
||||
|
||||
@Rule
|
||||
public OutputCapture output = new OutputCapture();
|
||||
private ClassLoader classLoader;
|
||||
private Random random = new Random();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.classLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
Thread.currentThread().setContextClassLoader(this.classLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initDefault() throws Exception {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
SpringCli cli = new SpringCli();
|
||||
InitCommand command = new InitCommand(cli);
|
||||
command.run();
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
// Fast...
|
||||
public void initNonExistent() throws Exception {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
SpringCli cli = new SpringCli();
|
||||
InitCommand command = new InitCommand(cli);
|
||||
command.run("--init=" + this.random.nextInt() + ".groovy");
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
// Fast...
|
||||
public void fakeCommand() throws Exception {
|
||||
final SpringCli cli = new SpringCli();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Command command = new AbstractCommand("fake", "") {
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
for (CommandFactory factory : ServiceLoader.load(
|
||||
CommandFactory.class, Thread.currentThread()
|
||||
.getContextClassLoader())) {
|
||||
for (Command command : factory.getCommands(cli)) {
|
||||
cli.register(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
command.run("--init=" + this.random.nextInt() + ".groovy");
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
// Fast...
|
||||
public void initNonExistentWithPrefix() throws Exception {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
SpringCli cli = new SpringCli();
|
||||
InitCommand command = new InitCommand(cli);
|
||||
command.run("--init=file:" + this.random.nextInt() + ".groovy");
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
// There is an init.groovy on the test classpath so this succeeds
|
||||
// Slow...
|
||||
public void initFromClasspath() throws Exception {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
SpringCli cli = new SpringCli();
|
||||
InitCommand command = new InitCommand(cli);
|
||||
command.run("--init=init.groovy");
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringCli.main("hint");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 groovy.lang.GroovyClassLoader;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.OutputCapture;
|
||||
import org.springframework.boot.cli.Command;
|
||||
import org.springframework.boot.cli.SpringCli;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class InitCommandTests {
|
||||
|
||||
@Rule
|
||||
public OutputCapture output = new OutputCapture();
|
||||
|
||||
private SpringCli cli = mock(SpringCli.class);
|
||||
private InitCommand command = new InitCommand(this.cli);
|
||||
private int defaultCount = new DefaultCommandFactory().getCommands(this.cli).size();
|
||||
private ClassLoader classLoader;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.classLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
Thread.currentThread().setContextClassLoader(this.classLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitClasspath() throws Exception {
|
||||
Thread.currentThread().setContextClassLoader(new GroovyClassLoader());
|
||||
this.command.run("--cp=src/test/plugins/custom/custom/0.0.1/custom-0.0.1.jar");
|
||||
verify(this.cli, times(this.defaultCount + 1)).register(any(Command.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initScript() throws Exception {
|
||||
this.command.run("src/test/resources/grab.groovy");
|
||||
verify(this.cli, times(this.defaultCount + 1)).register(any(Command.class));
|
||||
assertTrue(this.output.toString().contains("Hello Grab"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void initNonExistentScript() throws Exception {
|
||||
this.command.run("nonexistent.groovy");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initDefault() throws Exception {
|
||||
this.command.run();
|
||||
assertFalse(this.output.toString().contains("Hello Init"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initWithCommandline() throws Exception {
|
||||
this.command.run("--init=init.groovy");
|
||||
assertTrue(this.output.toString().contains("Hello Init"));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Random random = new Random();
|
||||
InitCommandTests test = new InitCommandTests();
|
||||
test.init();
|
||||
SpringCli cli = new SpringCli();
|
||||
while (true) {
|
||||
InitCommand command = new InitCommand(cli);
|
||||
command.run("--init=file:" + random.nextInt() + ".groovy");
|
||||
test.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue