Browse Source
Add ApplicationArguments interface which allows SpringApplication.run arguments to be injected into any bean. The interface provides access to both the raw String[] arguments and also provides some convenience methods to access the parsed 'option' and 'non-option' arguments. A new ApplicationRunner interface has also been added which is similar to the existing CommandLineRunner. Fixes gh-1990pull/3706/head
8 changed files with 468 additions and 43 deletions
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* Provides access to the arguments that were used run a {@link SpringApplication}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.3.0 |
||||
*/ |
||||
public interface ApplicationArguments { |
||||
|
||||
/** |
||||
* Return the raw unprocessed arguments that were passed to the application. |
||||
* @return the arguments |
||||
*/ |
||||
public String[] getSourceArgs(); |
||||
|
||||
/** |
||||
* Return then names of all option arguments. For example, if the arguments were |
||||
* "--foo=bar --debug" would return the values {@code ["foo", "bar"]}. |
||||
* @return the option names or an empty set |
||||
*/ |
||||
public Set<String> getOptionNames(); |
||||
|
||||
/** |
||||
* Return whether the set of option arguments parsed from the arguments contains an |
||||
* option with the given name. |
||||
* @param name the name to check |
||||
* @return {@code true} if the arguments contains an option with the given name |
||||
*/ |
||||
boolean containsOption(String name); |
||||
|
||||
/** |
||||
* Return the collection of values associated with the arguments option having the |
||||
* given name. |
||||
* <ul> |
||||
* <li>if the option is present and has no argument (e.g.: "--foo"), return an empty |
||||
* collection ({@code []})</li> |
||||
* <li>if the option is present and has a single value (e.g. "--foo=bar"), return a |
||||
* collection having one element ({@code ["bar"]})</li> |
||||
* <li>if the option is present and has multiple values (e.g. "--foo=bar --foo=baz"), |
||||
* return a collection having elements for each value ({@code ["bar", "baz"]})</li> |
||||
* <li>if the option is not present, return {@code null}</li> |
||||
* </ul> |
||||
* @param name the name of the option |
||||
* @return a list of option values for the given name |
||||
*/ |
||||
List<String> getOptionValues(String name); |
||||
|
||||
/** |
||||
* Return the collection of non-option arguments parsed. |
||||
* @return the non-option arguments or an empty list |
||||
*/ |
||||
List<String> getNonOptionArgs(); |
||||
|
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.core.annotation.Order; |
||||
|
||||
/** |
||||
* Interface used to indicate that a bean should <em>run</em> when it is contained within |
||||
* a {@link SpringApplication}. Multiple {@link ApplicationArguments} beans can be defined |
||||
* within the same application context and can be ordered using the {@link Ordered} |
||||
* interface or {@link Order @Order} annotation. |
||||
* |
||||
* @author Phillip Webb |
||||
* @see CommandLineRunner |
||||
* @since 1.3.0 |
||||
*/ |
||||
public interface ApplicationRunner { |
||||
|
||||
/** |
||||
* Callback used to run the bean. |
||||
* @param args incoming application arguments |
||||
* @throws Exception on error |
||||
*/ |
||||
void run(ApplicationArguments args) throws Exception; |
||||
|
||||
} |
||||
@ -0,0 +1,90 @@
@@ -0,0 +1,90 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.core.env.SimpleCommandLinePropertySource; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Default internal implementation of {@link ApplicationArguments}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
class DefaultApplicationArguments implements ApplicationArguments { |
||||
|
||||
private final Source source; |
||||
|
||||
private final String[] args; |
||||
|
||||
public DefaultApplicationArguments(String[] args) { |
||||
Assert.notNull(args, "Args must not be null"); |
||||
this.source = new Source(args); |
||||
this.args = args; |
||||
} |
||||
|
||||
@Override |
||||
public String[] getSourceArgs() { |
||||
return this.args; |
||||
} |
||||
|
||||
@Override |
||||
public Set<String> getOptionNames() { |
||||
String[] names = this.source.getPropertyNames(); |
||||
return Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(names))); |
||||
} |
||||
|
||||
@Override |
||||
public boolean containsOption(String name) { |
||||
return this.source.containsProperty(name); |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getOptionValues(String name) { |
||||
List<String> values = this.source.getOptionValues(name); |
||||
return (values == null ? null : Collections.unmodifiableList(values)); |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getNonOptionArgs() { |
||||
return this.source.getNonOptionArgs(); |
||||
} |
||||
|
||||
private static class Source extends SimpleCommandLinePropertySource { |
||||
|
||||
public Source(String[] args) { |
||||
super(args); |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getNonOptionArgs() { |
||||
return super.getNonOptionArgs(); |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getOptionValues(String name) { |
||||
return super.getOptionValues(name); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
|
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.junit.Assert.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link DefaultApplicationArguments}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class DefaultApplicationArgumentsTests { |
||||
|
||||
private static final String[] ARGS = new String[] { "--foo=bar", "--foo=baz", |
||||
"--debug", "spring", "boot" }; |
||||
|
||||
@Rule |
||||
public ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
@Test |
||||
public void argumentsMustNoBeNull() throws Exception { |
||||
this.thrown.expect(IllegalArgumentException.class); |
||||
this.thrown.expectMessage("Args must not be null"); |
||||
new DefaultApplicationArguments(null); |
||||
} |
||||
|
||||
@Test |
||||
public void getArgs() throws Exception { |
||||
ApplicationArguments arguments = new DefaultApplicationArguments(ARGS); |
||||
assertThat(arguments.getSourceArgs(), equalTo(ARGS)); |
||||
} |
||||
|
||||
@Test |
||||
public void optionNames() throws Exception { |
||||
ApplicationArguments arguments = new DefaultApplicationArguments(ARGS); |
||||
Set<String> expected = new HashSet<String>(Arrays.asList("foo", "debug")); |
||||
assertThat(arguments.getOptionNames(), equalTo(expected)); |
||||
} |
||||
|
||||
@Test |
||||
public void containsOption() throws Exception { |
||||
ApplicationArguments arguments = new DefaultApplicationArguments(ARGS); |
||||
assertThat(arguments.containsOption("foo"), equalTo(true)); |
||||
assertThat(arguments.containsOption("debug"), equalTo(true)); |
||||
assertThat(arguments.containsOption("spring"), equalTo(false)); |
||||
} |
||||
|
||||
@Test |
||||
public void getOptionValues() throws Exception { |
||||
ApplicationArguments arguments = new DefaultApplicationArguments(ARGS); |
||||
assertThat(arguments.getOptionValues("foo"), equalTo(Arrays.asList("bar", "baz"))); |
||||
assertThat(arguments.getOptionValues("debug"), |
||||
equalTo(Collections.<String> emptyList())); |
||||
assertThat(arguments.getOptionValues("spring"), equalTo(null)); |
||||
} |
||||
|
||||
@Test |
||||
public void getNonOptionArgs() throws Exception { |
||||
ApplicationArguments arguments = new DefaultApplicationArguments(ARGS); |
||||
assertThat(arguments.getNonOptionArgs(), equalTo(Arrays.asList("spring", "boot"))); |
||||
} |
||||
|
||||
@Test |
||||
public void getNoNonOptionArgs() throws Exception { |
||||
ApplicationArguments arguments = new DefaultApplicationArguments( |
||||
new String[] { "--debug" }); |
||||
assertThat(arguments.getNonOptionArgs(), |
||||
equalTo(Collections.<String> emptyList())); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue