Provide auto-configuration for remote calls

Provide application auto-configuration to provide remote endpoint
support along with a client-side application that can be used to
establish connections.

See gh-3086
This commit is contained in:
Phillip Webb
2015-06-01 13:23:29 -07:00
parent 505cad48c1
commit ada3e1eca2
13 changed files with 674 additions and 1 deletions

View File

@@ -0,0 +1,76 @@
/*
* 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.developertools;
import org.springframework.boot.Banner;
import org.springframework.boot.ResourceBanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.developertools.remote.client.RemoteClientConfiguration;
import org.springframework.boot.developertools.restart.RestartInitializer;
import org.springframework.boot.developertools.restart.Restarter;
import org.springframework.core.io.ClassPathResource;
/**
* Application that can be used to establish a link to remotely running Spring Boot code.
* Allows remote debugging and remote updates (if enabled). This class should be launched
* from within your IDE and should have the same classpath configuration as the locally
* developed application. The remote URL of the application should be provided as a
* non-option argument.
*
* @author Phillip Webb
* @since 1.3.0
* @see RemoteClientConfiguration
*/
public class RemoteSpringApplication {
private void run(String[] args) {
Restarter.initialize(args, RestartInitializer.NONE);
SpringApplication application = new SpringApplication(
RemoteClientConfiguration.class);
application.setWebEnvironment(false);
application.setBanner(getBanner());
application.addListeners(new RemoteUrlPropertyExtractor());
application.run(args);
waitIndefinitely();
}
private Banner getBanner() {
ClassPathResource banner = new ClassPathResource("remote-banner.txt",
RemoteSpringApplication.class);
return new ResourceBanner(banner);
}
private void waitIndefinitely() {
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
}
}
/**
* Run the {@link RemoteSpringApplication}.
* @param args the program arguments (including the remote URL as a non-option
* argument)
*/
public static void main(String[] args) {
new RemoteSpringApplication().run(args);
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.developertools;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Map;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@link ApplicationListener} to extract the remote URL for the
* {@link RemoteSpringApplication} to use.
*
* @author Phillip Webb
*/
class RemoteUrlPropertyExtractor implements
ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final String NON_OPTION_ARGS = CommandLinePropertySource.DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
String url = environment.getProperty(NON_OPTION_ARGS);
Assert.state(StringUtils.hasLength(url), "No remote URL specified");
Assert.state(url.indexOf(",") == -1, "Multiple URLs specified");
try {
new URI(url);
}
catch (URISyntaxException ex) {
throw new IllegalStateException("Malformed URL '" + url + "'");
}
Map<String, Object> source = Collections.singletonMap("remoteUrl", (Object) url);
PropertySource<?> propertySource = new MapPropertySource("remoteUrl", source);
environment.getPropertySources().addLast(propertySource);
}
}

View File

@@ -33,6 +33,8 @@ public class DeveloperToolsProperties {
private Livereload livereload = new Livereload();
private RemoteDeveloperToolsProperties remote = new RemoteDeveloperToolsProperties();
public Restart getRestart() {
return this.restart;
}
@@ -41,6 +43,10 @@ public class DeveloperToolsProperties {
return this.livereload;
}
public RemoteDeveloperToolsProperties getRemote() {
return this.remote;
}
/**
* Restart properties
*/

View File

@@ -0,0 +1,70 @@
/*
* 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.developertools.autoconfigure;
import java.util.Collection;
import javax.servlet.Filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.developertools.remote.server.AccessManager;
import org.springframework.boot.developertools.remote.server.Dispatcher;
import org.springframework.boot.developertools.remote.server.DispatcherFilter;
import org.springframework.boot.developertools.remote.server.Handler;
import org.springframework.boot.developertools.remote.server.HandlerMapper;
import org.springframework.boot.developertools.remote.server.HttpStatusHandler;
import org.springframework.boot.developertools.remote.server.UrlHandlerMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
/**
* {@link EnableAutoConfiguration Auto-configuration} for remote development support.
*
* @author Phillip Webb
* @author Rob Winch
* @since 1.3.0
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.developertools.remote", name = "enabled")
@ConditionalOnClass({ Filter.class, ServerHttpRequest.class })
@EnableConfigurationProperties(DeveloperToolsProperties.class)
public class RemoteDeveloperToolsAutoConfiguration {
@Autowired
private DeveloperToolsProperties properties;
@Bean
public HandlerMapper remoteDeveloperToolsHealthCheckHandlerMapper() {
Handler handler = new HttpStatusHandler();
return new UrlHandlerMapper(this.properties.getRemote().getContextPath(), handler);
}
@Bean
@ConditionalOnMissingBean
public DispatcherFilter remoteDeveloperToolsDispatcherFilter(
Collection<HandlerMapper> mappers) {
Dispatcher dispatcher = new Dispatcher(AccessManager.PERMIT_ALL, mappers);
return new DispatcherFilter(dispatcher);
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.developertools.autoconfigure;
/**
* Configuration properties for remote Spring Boot applications.
*
* @author Phillip Webb
* @author Rob Winch
* @since 1.3.0
* @see DeveloperToolsProperties
*/
public class RemoteDeveloperToolsProperties {
public static final String DEFAULT_CONTEXT_PATH = "/.~~spring-boot!~";
/**
* Context path used to handle the remote connection.
*/
private String contextPath = DEFAULT_CONTEXT_PATH;
public String getContextPath() {
return this.contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
}

View File

@@ -0,0 +1,21 @@
/*
* 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.
*/
/**
* Spring Boot developer tools.
*/
package org.springframework.boot.developertools;

View File

@@ -0,0 +1,56 @@
/*
* 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.developertools.remote.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.developertools.autoconfigure.DeveloperToolsProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
/**
* Configuration used to connect to remote Spring Boot applications.
*
* @author Phillip Webb
* @since 1.3.0
* @see org.springframework.boot.developertools.RemoteSpringApplication
*/
@Configuration
@EnableConfigurationProperties(DeveloperToolsProperties.class)
public class RemoteClientConfiguration {
@Autowired
private DeveloperToolsProperties properties;
@Value("${remoteUrl}")
private String remoteUrl;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
return new SimpleClientHttpRequestFactory();
}
}

View File

@@ -0,0 +1,21 @@
/*
* 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.
*/
/**
* Client support for a remotely running Spring Boot application.
*/
package org.springframework.boot.developertools.remote.client;

View File

@@ -8,4 +8,5 @@ org.springframework.boot.developertools.restart.RestartApplicationListener
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.developertools.autoconfigure.LocalDeveloperToolsAutoConfiguration
org.springframework.boot.developertools.autoconfigure.LocalDeveloperToolsAutoConfiguration,\
org.springframework.boot.developertools.autoconfigure.RemoteDeveloperToolsAutoConfiguration

View File

@@ -0,0 +1,7 @@
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \
\\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) )
' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / /
=========|_|==============|___/===================================/_/_/_/
:: Spring Boot Remote :: ${spring-boot.formatted-version}