Add support for @EnableBinding (s-c-stream)

This commit is contained in:
Dave Syer
2015-09-08 17:49:48 +01:00
parent 2688e37be4
commit 89fb7a4059
7 changed files with 184 additions and 22 deletions

View File

@@ -28,6 +28,12 @@
<version>${spring-boot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -39,6 +45,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<spring-cloud.version>${project.version}</spring-cloud.version>
<spring-cloud-stream.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-stream.version>
<generated.pom.dir>${project.build.directory}/generated-resources/org/springframework/cloud/cli/compiler</generated.pom.dir>
</properties>

View File

@@ -0,0 +1,40 @@
/*
* Copyright 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.groovy.cloud;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.cloud.cli.compiler.StreamRabbitCompilerAutoConfiguration;
/**
* Pseudo annotation used to trigger {@link StreamRabbitCompilerAutoConfiguration} and
* Redis.
*/
@Target(ElementType.TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@org.springframework.cloud.stream.annotation.EnableBinding
public @interface EnableBinding {
Class<?>[]value() default {};
String transport() default "redis";
}

View File

@@ -16,8 +16,6 @@
package org.springframework.cloud.cli.compiler;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.boot.cli.compiler.AstUtils;
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
import org.springframework.boot.cli.compiler.DependencyCustomizer;
@@ -37,16 +35,8 @@ public class OAuth2SsoCompilerAutoConfiguration extends CompilerAutoConfiguratio
public void applyDependencies(DependencyCustomizer dependencies) {
dependencies
.ifAnyMissingClasses(
"org.springframework.cloud.security.oauth2.sso.EnableOAuth2Sso")
.add("spring-cloud-starter-security")
.add("spring-security-oauth2");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addImports(
"org.springframework.cloud.security.oauth2.sso.EnableOAuth2Sso",
"org.springframework.cloud.security.oauth2.sso.OAuth2SsoConfigurerAdapter");
"org.springframework.cloud.security.oauth2.client.OAuth2LoadBalancerClientAutoConfiguration")
.add("spring-cloud-starter-oauth2");
}
}

View File

@@ -26,27 +26,27 @@ import org.springframework.boot.cli.compiler.DependencyCustomizer;
* @author Dave Syer
*
*/
public class OAuth2ResourceCompilerAutoConfiguration extends CompilerAutoConfiguration {
public class StreamKafkaCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableOAuth2Resource");
boolean annotated = AstUtils.hasAtLeastOneAnnotation(classNode, "EnableBinding");
return annotated && StreamRedisCompilerAutoConfiguration.isTransport(classNode, "kafka");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies) {
dependencies
.ifAnyMissingClasses(
"org.springframework.cloud.security.oauth2.resource.EnableOAuth2Resource")
.add("spring-cloud-starter-security")
.add("spring-security-oauth2");
"org.springframework.cloud.stream.binder.kafka.config.KafkaServiceAutoConfiguration")
.add("spring-cloud-starter-stream-kafka");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addImports(
"org.springframework.cloud.security.oauth2.resource.EnableOAuth2Resource",
"org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter");
imports.addImports("org.springframework.boot.groovy.cloud.EnableBinding");
imports.addStarImports("org.springframework.cloud.stream.annotation",
"org.springframework.cloud.stream.messaging");
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2013-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.cloud.cli.compiler;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.boot.cli.compiler.AstUtils;
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
import org.springframework.boot.cli.compiler.DependencyCustomizer;
/**
* @author Dave Syer
*
*/
public class StreamRabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
boolean annotated = AstUtils.hasAtLeastOneAnnotation(classNode, "EnableBinding");
return annotated && StreamRedisCompilerAutoConfiguration.isTransport(classNode, "rabbit");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies) {
dependencies
.ifAnyMissingClasses(
"org.springframework.cloud.stream.binder.rabbit.config.RabbitServiceAutoConfiguration")
.add("spring-cloud-starter-stream-rabbit");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addImports("org.springframework.boot.groovy.cloud.EnableBinding");
imports.addStarImports("org.springframework.cloud.stream.annotation",
"org.springframework.cloud.stream.messaging");
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2013-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.cloud.cli.compiler;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.boot.cli.compiler.AstUtils;
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
import org.springframework.boot.cli.compiler.DependencyCustomizer;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.SystemPropertyUtils;
/**
* @author Dave Syer
*
*/
public class StreamRedisCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
boolean annotated = AstUtils.hasAtLeastOneAnnotation(classNode, "EnableBinding");
return annotated && isTransport(classNode, "redis");
}
static boolean isTransport(ClassNode node, String type) {
for (AnnotationNode annotationNode : node.getAnnotations()) {
String annotation = "EnableBinding";
if (PatternMatchUtils.simpleMatch(annotation,
annotationNode.getClassNode().getName())) {
Expression expression = annotationNode.getMembers().get("transport");
String transport = expression == null ? "redis" : expression.getText();
if (transport != null) {
transport = SystemPropertyUtils.resolvePlaceholders(transport);
}
return transport.equals(type);
}
}
return false;
}
@Override
public void applyDependencies(DependencyCustomizer dependencies) {
dependencies
.ifAnyMissingClasses(
"org.springframework.cloud.stream.binder.redis.config.RedisServiceAutoConfiguration")
.add("spring-cloud-starter-stream-redis");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addImports("org.springframework.boot.groovy.cloud.EnableBinding");
imports.addStarImports("org.springframework.cloud.stream.annotation",
"org.springframework.cloud.stream.messaging");
}
}

View File

@@ -1,10 +1,11 @@
org.springframework.cloud.cli.compiler.SpringCloudCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.ConfigServerCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.OAuth2ResourceCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.OAuth2SsoCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.EurekaClientCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.EurekaServerCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.HystrixCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.HystrixDashboardCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.RibbonClientCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.StreamKafkaCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.StreamRabbitCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.StreamRedisCompilerAutoConfiguration
org.springframework.cloud.cli.compiler.ZuulCompilerAutoConfiguration