-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#34 Adding annotation processor for exposing referenced stream names;
The file META-INF/decodable/stream-names.properties will be generated based on the InputStreams and OutputStreams annotations.
- Loading branch information
1 parent
a3297d7
commit 317e281
Showing
10 changed files
with
250 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
sdk/src/main/java/co/decodable/sdk/pipeline/internal/metadata/MetadataProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright Decodable, Inc. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package co.decodable.sdk.pipeline.internal.metadata; | ||
|
||
import co.decodable.sdk.pipeline.metadata.SinkStreams; | ||
import co.decodable.sdk.pipeline.metadata.SourceStreams; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.tools.Diagnostic.Kind; | ||
import javax.tools.FileObject; | ||
import javax.tools.StandardLocation; | ||
|
||
/** | ||
* An annotation processor for generating the file {@code | ||
* "META-INF/decodable/stream-names.properties"}, allowing the Decodable platform to display the | ||
* streams connected to a custom pipeline. | ||
*/ | ||
@SupportedAnnotationTypes({ | ||
"co.decodable.sdk.pipeline.metadata.SourceStreams", | ||
"co.decodable.sdk.pipeline.metadata.SinkStreams" | ||
}) | ||
public class MetadataProcessor extends AbstractProcessor { | ||
|
||
private static final String STREAM_NAMES_FILE = "META-INF/decodable/stream-names.properties"; | ||
|
||
private final Set<String> allSourceStreams; | ||
private final Set<String> allSinkStreams; | ||
|
||
public MetadataProcessor() { | ||
allSourceStreams = new HashSet<>(); | ||
allSinkStreams = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
for (TypeElement annotation : annotations) { | ||
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation); | ||
for (Element annotated : annotatedElements) { | ||
SourceStreams sourceStreams = annotated.getAnnotation(SourceStreams.class); | ||
allSourceStreams.addAll(Arrays.asList(sourceStreams.value())); | ||
|
||
SinkStreams sinkStreams = annotated.getAnnotation(SinkStreams.class); | ||
allSinkStreams.addAll(Arrays.asList(sinkStreams.value())); | ||
} | ||
} | ||
|
||
if (roundEnv.processingOver()) { | ||
try { | ||
FileObject streamNamesFile = | ||
processingEnv | ||
.getFiler() | ||
.createResource(StandardLocation.CLASS_OUTPUT, "", STREAM_NAMES_FILE); | ||
|
||
try (PrintWriter out = new PrintWriter(streamNamesFile.openWriter())) { | ||
out.println( | ||
"source-streams=" + allSourceStreams.stream().collect(Collectors.joining(","))); | ||
out.println("sink-streams=" + allSinkStreams.stream().collect(Collectors.joining(","))); | ||
} | ||
} catch (IOException e) { | ||
processingEnv | ||
.getMessager() | ||
.printMessage( | ||
Kind.ERROR, "Couldn't generate stream-names.properties file: " + e.getMessage()); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public SourceVersion getSupportedSourceVersion() { | ||
return SourceVersion.latest(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
sdk/src/main/java/co/decodable/sdk/pipeline/metadata/SinkStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright Decodable, Inc. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package co.decodable.sdk.pipeline.metadata; | ||
|
||
import co.decodable.sdk.pipeline.util.Incubating; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Denotes the sink streams accessed by a custom pipeline. Must be specified on the job class in | ||
* order for the Decodable platform to display the connected streams. | ||
*/ | ||
@Retention(RetentionPolicy.CLASS) | ||
@Target(ElementType.TYPE) | ||
@Incubating | ||
public @interface SinkStreams { | ||
|
||
/** One or more sink stream name. */ | ||
String[] value(); | ||
} |
27 changes: 27 additions & 0 deletions
27
sdk/src/main/java/co/decodable/sdk/pipeline/metadata/SourceStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright Decodable, Inc. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package co.decodable.sdk.pipeline.metadata; | ||
|
||
import co.decodable.sdk.pipeline.util.Incubating; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Denotes the source streams accessed by a custom pipeline. Must be specified on the job class in | ||
* order for the Decodable platform to display the connected streams. | ||
*/ | ||
@Retention(RetentionPolicy.CLASS) | ||
@Target(ElementType.TYPE) | ||
@Incubating | ||
public @interface SourceStreams { | ||
|
||
/** One or more source stream name. */ | ||
String[] value(); | ||
} |
10 changes: 10 additions & 0 deletions
10
sdk/src/main/java/co/decodable/sdk/pipeline/metadata/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright Decodable, Inc. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
/** Annotations for linking custom pipelines to managed Decodable streams. */ | ||
package co.decodable.sdk.pipeline.metadata; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
sdk/src/main/resources/META-INF/services/javax.annotation.processing.Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
co.decodable.sdk.pipeline.internal.metadata.MetadataProcessor |
47 changes: 47 additions & 0 deletions
47
...c/test/java/co/decodable/sdk/pipeline/internal/config/metadata/MetadataProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright Decodable, Inc. | ||
* | ||
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package co.decodable.sdk.pipeline.internal.config.metadata; | ||
|
||
import static com.google.testing.compile.CompilationSubject.assertThat; | ||
|
||
import co.decodable.sdk.pipeline.internal.metadata.MetadataProcessor; | ||
import com.google.common.io.CharSource; | ||
import com.google.testing.compile.Compilation; | ||
import com.google.testing.compile.Compiler; | ||
import com.google.testing.compile.JavaFileObjects; | ||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import javax.tools.StandardLocation; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MetadataProcessorTest { | ||
|
||
@Test | ||
public void shouldGenerateStreamNamesFile() throws MalformedURLException { | ||
URL jobFile = | ||
new File( | ||
"./src/test/java/co/decodable/sdk/pipeline/snippets/PurchaseOrderProcessingJob.java") | ||
.toURI() | ||
.toURL(); | ||
|
||
Compilation compilation = | ||
Compiler.javac() | ||
.withProcessors(new MetadataProcessor()) | ||
.compile(JavaFileObjects.forResource(jobFile)); | ||
|
||
assertThat(compilation).succeeded(); | ||
assertThat(compilation) | ||
.generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/decodable/stream-names.properties") | ||
.hasContents( | ||
CharSource.wrap( | ||
"source-streams=purchase-orders\nsink-streams=purchase-orders-processed\n") | ||
.asByteSource(Charset.forName("UTF-8"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters