Skip to content

Commit b5374cb

Browse files
committed
apply suggestions
1 parent bc8e871 commit b5374cb

16 files changed

+551
-533
lines changed

pom.xml

+31-30
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<plugin>
2323
<groupId>org.apache.maven.plugins</groupId>
2424
<artifactId>maven-assembly-plugin</artifactId>
25+
<version>3.6.0</version>
2526
<configuration>
2627
<descriptorRefs>
2728
<descriptorRef>jar-with-dependencies</descriptorRef>
@@ -62,47 +63,47 @@
6263
</dependency>
6364

6465
<dependency>
65-
<groupId>com.google.code.gson</groupId>
66-
<artifactId>gson</artifactId>
67-
<version>2.8.8</version>
66+
<groupId>com.google.code.gson</groupId>
67+
<artifactId>gson</artifactId>
68+
<version>2.8.8</version>
6869
</dependency>
6970

7071
<dependency>
71-
<groupId>de.huxhorn.sulky</groupId>
72-
<artifactId>de.huxhorn.sulky.ulid</artifactId>
73-
<version>8.2.0</version>
72+
<groupId>de.huxhorn.sulky</groupId>
73+
<artifactId>de.huxhorn.sulky.ulid</artifactId>
74+
<version>8.2.0</version>
7475
</dependency>
7576

7677
<dependency>
77-
<groupId>org.eclipse.paho</groupId>
78-
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
79-
<version>1.2.5</version>
78+
<groupId>org.eclipse.paho</groupId>
79+
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
80+
<version>1.2.5</version>
8081
</dependency>
8182

8283
<dependency>
83-
<groupId>io.pravega</groupId>
84-
<artifactId>pravega-client</artifactId>
85-
<version>0.10.0</version>
84+
<groupId>io.pravega</groupId>
85+
<artifactId>pravega-client</artifactId>
86+
<version>0.10.0</version>
8687
</dependency>
8788

88-
<dependency>
89-
<groupId>org.apache.logging.log4j</groupId>
90-
<artifactId>log4j-api</artifactId>
91-
<version>2.21.0</version>
92-
</dependency>
93-
94-
<dependency>
95-
<groupId>org.apache.logging.log4j</groupId>
96-
<artifactId>log4j-core</artifactId>
97-
<version>2.21.0</version>
98-
<scope>test</scope>
99-
</dependency>
100-
101-
<dependency>
102-
<groupId>org.spdx</groupId>
103-
<artifactId>spdx-jackson-store</artifactId>
104-
<version>1.1.9.1</version>
105-
</dependency>
89+
<dependency>
90+
<groupId>org.apache.logging.log4j</groupId>
91+
<artifactId>log4j-api</artifactId>
92+
<version>2.21.0</version>
93+
</dependency>
94+
95+
<dependency>
96+
<groupId>org.apache.logging.log4j</groupId>
97+
<artifactId>log4j-core</artifactId>
98+
<version>2.21.0</version>
99+
<scope>test</scope>
100+
</dependency>
101+
102+
<dependency>
103+
<groupId>org.spdx</groupId>
104+
<artifactId>spdx-jackson-store</artifactId>
105+
<version>1.1.9.1</version>
106+
</dependency>
106107

107108
</dependencies>
108109
</project>

src/main/java/com/alvarium/DefaultSdk.java

+138-145
Original file line numberDiff line numberDiff line change
@@ -34,154 +34,147 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636

37-
public class DefaultSdk implements Sdk {
38-
private final Annotator[] annotators;
39-
private final SdkInfo config;
40-
private final StreamProvider stream;
41-
private final Logger logger;
42-
43-
public DefaultSdk(Annotator[] annotators, SdkInfo config, Logger logger) throws StreamException {
44-
this.annotators = annotators;
45-
this.config = config;
46-
this.logger = logger;
47-
48-
// init stream
49-
final StreamProviderFactory streamFactory = new StreamProviderFactory();
50-
this.stream = streamFactory.getProvider(this.config.getStream());
51-
this.stream.connect();
52-
this.logger.debug("stream provider connected successfully.");
53-
}
54-
55-
public void create(PropertyBag properties, byte[] data) throws AnnotatorException,
56-
StreamException {
57-
final List<Annotation> annotations = this.createAnnotations(properties, data);
58-
this.publishAnnotations(SdkAction.CREATE, annotations);
59-
this.logger.debug("data annotated and published successfully.");
60-
}
61-
62-
public void create(byte[] data) throws AnnotatorException, StreamException {
63-
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>());
64-
this.create(properties, data);
65-
}
66-
67-
public void mutate(PropertyBag properties, byte[] oldData, byte[] newData) throws
68-
AnnotatorException, StreamException {
69-
final List<Annotation> annotations = new ArrayList<Annotation>();
70-
71-
// source annotate the old data
72-
final AnnotatorFactory annotatorFactory = new AnnotatorFactory();
73-
final Annotator sourceAnnotator = annotatorFactory.getAnnotator(
74-
new AnnotatorConfig(AnnotationType.SOURCE),
75-
this.config,
76-
this.logger
77-
);
78-
79-
String key;
80-
try {
81-
var hasher = new HashProviderFactory().getProvider(config.getHash().getType());
82-
key = hasher.derive(oldData);
83-
} catch (HashTypeException e) {
84-
throw new RuntimeException(e);
85-
}
86-
87-
final Annotation sourceAnnotation = sourceAnnotator.execute(properties, oldData, key);
88-
annotations.add(sourceAnnotation);
89-
90-
// Add annotations for new data
91-
for (Annotation annotation : this.createAnnotations(properties, newData)) {
92-
// TLS is ignored in mutate to prevent needless penalization
93-
// See https://github.com/project-alvarium/alvarium-sdk-go/issues/19
94-
if (annotation.getKind() != AnnotationType.TLS) {
95-
annotations.add(annotation);
96-
}
97-
}
98-
99-
// publish to the stream provider
100-
this.publishAnnotations(SdkAction.MUTATE, annotations);
101-
this.logger.debug("data annotated and published successfully.");
102-
}
103-
104-
public void mutate(byte[] oldData, byte[] newData) throws AnnotatorException, StreamException {
105-
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>());
106-
this.mutate(properties, oldData, newData);
107-
}
10837

109-
public void transit(PropertyBag properties, byte[] data) throws AnnotatorException,
110-
StreamException {
111-
final List<Annotation> annotations = this.createAnnotations(properties, data);
112-
this.publishAnnotations(SdkAction.TRANSIT, annotations);
113-
this.logger.debug("data annotated and published successfully.");
114-
}
115-
116-
public void transit(byte[] data) throws AnnotatorException, StreamException {
117-
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>());
118-
this.transit(properties, data);
119-
}
120-
121-
public void publish(PropertyBag properties, byte[] data) throws AnnotatorException,
122-
StreamException {
123-
final List<Annotation> annotations = this.createAnnotations(properties, data);
124-
this.publishAnnotations(SdkAction.PUBLISH, annotations);
125-
this.logger.debug("data annotated and published successfully.");
126-
}
127-
128-
public void publish(byte[] data) throws AnnotatorException, StreamException {
129-
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>());
130-
this.publish(properties, data);
131-
}
132-
133-
public void close() throws StreamException {
134-
this.stream.close();
135-
this.logger.debug("stream provider connection terminated successfully.");
38+
public class DefaultSdk implements Sdk {
39+
private final Annotator[] annotators;
40+
private final SdkInfo config;
41+
private final StreamProvider stream;
42+
private final Logger logger;
43+
44+
public DefaultSdk(Annotator[] annotators, SdkInfo config, Logger logger) throws StreamException {
45+
this.annotators = annotators;
46+
this.config = config;
47+
this.logger = logger;
48+
49+
// init stream
50+
final StreamProviderFactory streamFactory = new StreamProviderFactory();
51+
this.stream = streamFactory.getProvider(this.config.getStream());
52+
this.stream.connect();
53+
this.logger.debug("stream provider connected successfully.");
54+
}
55+
56+
public void create(PropertyBag properties, byte[] data) throws AnnotatorException,
57+
StreamException, HashTypeException {
58+
final List<Annotation> annotations = this.createAnnotations(properties, data);
59+
this.publishAnnotations(SdkAction.CREATE, annotations);
60+
this.logger.debug("data annotated and published successfully.");
61+
}
62+
63+
public void create(byte[] data) throws AnnotatorException, StreamException, HashTypeException {
64+
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<>());
65+
this.create(properties, data);
66+
}
67+
68+
public void mutate(PropertyBag properties, byte[] oldData, byte[] newData) throws
69+
AnnotatorException, StreamException, HashTypeException {
70+
final List<Annotation> annotations = new ArrayList<Annotation>();
71+
72+
// source annotate the old data
73+
final AnnotatorFactory annotatorFactory = new AnnotatorFactory();
74+
final Annotator sourceAnnotator = annotatorFactory.getAnnotator(
75+
new AnnotatorConfig(AnnotationType.SOURCE),
76+
this.config,
77+
this.logger
78+
);
79+
80+
String key;
81+
var hasher = new HashProviderFactory().getProvider(config.getHash().getType());
82+
key = hasher.derive(oldData);
83+
84+
final Annotation sourceAnnotation = sourceAnnotator.execute(properties, oldData, key);
85+
annotations.add(sourceAnnotation);
86+
87+
// Add annotations for new data
88+
for (Annotation annotation : this.createAnnotations(properties, newData)) {
89+
// TLS is ignored in mutate to prevent needless penalization
90+
// See https://github.com/project-alvarium/alvarium-sdk-go/issues/19
91+
if (annotation.getKind() != AnnotationType.TLS) {
92+
annotations.add(annotation);
93+
}
13694
}
13795

138-
/**
139-
* Executes all the specified annotators and returns a list of all the created annotations
140-
*
141-
* @param properties
142-
* @param data
143-
* @return
144-
* @throws AnnotatorException
145-
*/
146-
private List<Annotation> createAnnotations(PropertyBag properties, byte[] data)
147-
throws AnnotatorException {
148-
final List<Annotation> annotations = new ArrayList<>();
149-
150-
String key;
151-
try {
152-
var hasher = new HashProviderFactory().getProvider(config.getHash().getType());
153-
key = hasher.derive(data);
154-
} catch (HashTypeException e) {
155-
throw new RuntimeException(e);
156-
}
157-
158-
// Annotate incoming data
159-
for (Annotator annotator : this.annotators) {
160-
final Annotation annotation = annotator.execute(properties, data, key);
161-
annotations.add(annotation);
162-
}
163-
164-
return annotations;
96+
// publish to the stream provider
97+
this.publishAnnotations(SdkAction.MUTATE, annotations);
98+
this.logger.debug("data annotated and published successfully.");
99+
}
100+
101+
public void mutate(byte[] oldData, byte[] newData) throws AnnotatorException, StreamException, HashTypeException {
102+
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<>());
103+
this.mutate(properties, oldData, newData);
104+
}
105+
106+
public void transit(PropertyBag properties, byte[] data) throws AnnotatorException,
107+
StreamException, HashTypeException {
108+
final List<Annotation> annotations = this.createAnnotations(properties, data);
109+
this.publishAnnotations(SdkAction.TRANSIT, annotations);
110+
this.logger.debug("data annotated and published successfully.");
111+
}
112+
113+
public void transit(byte[] data) throws AnnotatorException, StreamException, HashTypeException {
114+
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<String, Object>());
115+
this.transit(properties, data);
116+
}
117+
118+
public void publish(PropertyBag properties, byte[] data) throws AnnotatorException,
119+
StreamException, HashTypeException {
120+
final List<Annotation> annotations = this.createAnnotations(properties, data);
121+
this.publishAnnotations(SdkAction.PUBLISH, annotations);
122+
this.logger.debug("data annotated and published successfully.");
123+
}
124+
125+
public void publish(byte[] data) throws AnnotatorException, StreamException, HashTypeException {
126+
final PropertyBag properties = new ImmutablePropertyBag(new HashMap<>());
127+
this.publish(properties, data);
128+
}
129+
130+
public void close() throws StreamException {
131+
this.stream.close();
132+
this.logger.debug("stream provider connection terminated successfully.");
133+
}
134+
135+
/**
136+
* Executes all the specified annotators and returns a list of all the created annotations
137+
*
138+
* @param properties
139+
* @param data
140+
* @return
141+
* @throws AnnotatorException
142+
*/
143+
private List<Annotation> createAnnotations(PropertyBag properties, byte[] data)
144+
throws AnnotatorException, HashTypeException {
145+
final List<Annotation> annotations = new ArrayList<>();
146+
147+
String key;
148+
var hasher = new HashProviderFactory().getProvider(config.getHash().getType());
149+
key = hasher.derive(data);
150+
151+
// Annotate incoming data
152+
for (Annotator annotator : this.annotators) {
153+
final Annotation annotation = annotator.execute(properties, data, key);
154+
annotations.add(annotation);
165155
}
166156

167-
/**
168-
* Wraps the annotation list with a publish wrapper that specifies the SDK action and the
169-
* content type
170-
*
171-
* @param action
172-
* @param annotations
173-
* @throws StreamException
174-
*/
175-
private void publishAnnotations(SdkAction action, List<Annotation> annotations)
176-
throws StreamException {
177-
final AnnotationList annotationList = new AnnotationList(annotations);
178-
179-
// publish list of annotations to the StreamProvider
180-
final PublishWrapper wrapper = new PublishWrapper(
181-
action,
182-
annotationList.getClass().getName(),
183-
annotationList
184-
);
185-
this.stream.publish(wrapper);
186-
}
157+
return annotations;
158+
}
159+
160+
/**
161+
* Wraps the annotation list with a publish wrapper that specifies the SDK action and the
162+
* content type
163+
*
164+
* @param action
165+
* @param annotations
166+
* @throws StreamException
167+
*/
168+
private void publishAnnotations(SdkAction action, List<Annotation> annotations)
169+
throws StreamException {
170+
final AnnotationList annotationList = new AnnotationList(annotations);
171+
172+
// publish list of annotations to the StreamProvider
173+
final PublishWrapper wrapper = new PublishWrapper(
174+
action,
175+
annotationList.getClass().getName(),
176+
annotationList
177+
);
178+
this.stream.publish(wrapper);
179+
}
187180
}

src/main/java/com/alvarium/PublishWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*******************************************************************************/
1515
package com.alvarium;
1616

17-
import com.alvarium.serializers.AlvariumPersistence;
17+
import com.alvarium.serializers.PeristenceFunctions;
1818

1919
import java.io.Serializable;
2020

@@ -52,6 +52,6 @@ public Object getContent() {
5252
* @return String representation of the PublishWrapper JSON
5353
*/
5454
public String toJson() {
55-
return AlvariumPersistence.GSON.toJson(this);
55+
return PeristenceFunctions.serializeWrapper(this);
5656
}
5757
}

0 commit comments

Comments
 (0)