Skip to content

Commit 5e134aa

Browse files
committed
[Fix #1266] Marshaling improvements
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 52af947 commit 5e134aa

36 files changed

Lines changed: 510 additions & 64 deletions

experimental/model/pom.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@
88
<artifactId>serverlessworkflow-experimental-model</artifactId>
99
<name>Serverless Workflow :: Experimental :: Model</name>
1010
<dependencies>
11-
<dependency>
12-
<groupId>io.serverlessworkflow</groupId>
13-
<artifactId>serverlessworkflow-impl-core</artifactId>
14-
</dependency>
11+
<dependency>
12+
<groupId>io.serverlessworkflow</groupId>
13+
<artifactId>serverlessworkflow-impl-core</artifactId>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.junit.jupiter</groupId>
17+
<artifactId>junit-jupiter-engine</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.assertj</groupId>
21+
<artifactId>assertj-core</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>ch.qos.logback</groupId>
25+
<artifactId>logback-classic</artifactId>
26+
</dependency>
1527
</dependencies>
1628
</project>

experimental/model/src/main/java/io/serverlessworkflow/impl/model/func/JavaModel.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collections;
2323
import java.util.Map;
2424
import java.util.Map.Entry;
25+
import java.util.Objects;
2526
import java.util.Optional;
2627
import java.util.stream.Collectors;
2728

@@ -104,4 +105,18 @@ protected <T> Optional<T> convert(Class<T> clazz) {
104105
? Optional.of(clazz.cast(object))
105106
: Optional.empty();
106107
}
108+
109+
@Override
110+
public int hashCode() {
111+
return Objects.hash(object);
112+
}
113+
114+
@Override
115+
public boolean equals(Object obj) {
116+
if (this == obj) return true;
117+
if (obj == null) return false;
118+
if (getClass() != obj.getClass()) return false;
119+
JavaModel other = (JavaModel) obj;
120+
return Objects.equals(object, other.object);
121+
}
107122
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.model.func;
17+
18+
import io.serverlessworkflow.impl.marshaller.CustomObjectMarshaller;
19+
import io.serverlessworkflow.impl.marshaller.WorkflowInputBuffer;
20+
import io.serverlessworkflow.impl.marshaller.WorkflowOutputBuffer;
21+
22+
public class JavaModelMarshaller implements CustomObjectMarshaller<JavaModel> {
23+
24+
@Override
25+
public void write(WorkflowOutputBuffer buffer, JavaModel object) {
26+
buffer.writeObject(object.asJavaObject());
27+
}
28+
29+
@Override
30+
public JavaModel read(WorkflowInputBuffer buffer, Class<? extends JavaModel> clazz) {
31+
return new JavaModel(buffer.readObject());
32+
}
33+
34+
@Override
35+
public Class<JavaModel> getObjectClass() {
36+
return JavaModel.class;
37+
}
38+
39+
@Override
40+
public int priority() {
41+
return Integer.MAX_VALUE;
42+
}
43+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.model.func;
17+
18+
import io.serverlessworkflow.impl.marshaller.CustomObjectMarshaller;
19+
import io.serverlessworkflow.impl.marshaller.WorkflowInputBuffer;
20+
import io.serverlessworkflow.impl.marshaller.WorkflowOutputBuffer;
21+
import java.io.ByteArrayInputStream;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
26+
import java.io.Serializable;
27+
import java.io.UncheckedIOException;
28+
29+
public class SerializableObjectMarshaller implements CustomObjectMarshaller<Serializable> {
30+
31+
@Override
32+
public void write(WorkflowOutputBuffer buffer, Serializable object) {
33+
try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
34+
ObjectOutputStream out = new ObjectOutputStream(bytesOut)) {
35+
out.writeObject(object);
36+
buffer.writeBytes(bytesOut.toByteArray());
37+
} catch (IOException io) {
38+
throw new UncheckedIOException(io);
39+
}
40+
}
41+
42+
@Override
43+
public Serializable read(WorkflowInputBuffer buffer, Class<? extends Serializable> objectClass) {
44+
try (ByteArrayInputStream bytesIn = new ByteArrayInputStream(buffer.readBytes());
45+
ObjectInputStream in = new ObjectInputStream(bytesIn)) {
46+
return objectClass.cast(in.readObject());
47+
} catch (IOException io) {
48+
throw new UncheckedIOException(io);
49+
} catch (ClassNotFoundException ex) {
50+
throw new IllegalStateException(ex);
51+
}
52+
}
53+
54+
@Override
55+
public Class<Serializable> getObjectClass() {
56+
return Serializable.class;
57+
}
58+
59+
@Override
60+
public int priority() {
61+
return Integer.MAX_VALUE;
62+
}
63+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
io.serverlessworkflow.impl.model.func.JavaModelMarshaller
2+
io.serverlessworkflow.impl.model.func.SerializableObjectMarshaller
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.model.func;
17+
18+
import java.io.Serializable;
19+
20+
record Address(String street, int number) implements Serializable {}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.model.func;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import io.serverlessworkflow.impl.marshaller.DefaultBufferFactory;
21+
import io.serverlessworkflow.impl.marshaller.WorkflowBufferFactory;
22+
import io.serverlessworkflow.impl.marshaller.WorkflowInputBuffer;
23+
import io.serverlessworkflow.impl.marshaller.WorkflowOutputBuffer;
24+
import java.io.ByteArrayInputStream;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import org.junit.jupiter.api.Test;
28+
29+
class JacksonModelSerializationTest {
30+
31+
@Test
32+
void testSerializableJavaModel() throws IOException {
33+
testMarshallUnMarshall(
34+
new JavaModel(new Person("Pepe Gotera", 32, new Address("Rue del Percebe", 13))));
35+
}
36+
37+
private void testMarshallUnMarshall(Object object) {
38+
WorkflowBufferFactory factory = DefaultBufferFactory.factory();
39+
ByteArrayOutputStream output = new ByteArrayOutputStream();
40+
try (WorkflowOutputBuffer writer = factory.output(output)) {
41+
writer.writeObject(object);
42+
}
43+
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
44+
try (WorkflowInputBuffer reader = factory.input(input)) {
45+
assertThat(reader.readObject()).isEqualTo(object);
46+
}
47+
}
48+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.model.func;
17+
18+
import java.io.Serializable;
19+
20+
record Person(String name, int age, Address address) implements Serializable {}

impl/persistence/api/src/main/java/io/serverlessworkflow/impl/marshaller/AbstractInputBuffer.java renamed to impl/core/src/main/java/io/serverlessworkflow/impl/marshaller/AbstractInputBuffer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ protected Class<?> loadClass(String className) throws ClassNotFoundException {
131131

132132
protected Object readCustomObject() {
133133
Class<?> objectClass = readClass();
134-
return customMarshallers.stream()
135-
.filter(m -> m.getObjectClass().isAssignableFrom(objectClass))
136-
.findFirst()
137-
.map(m -> m.read(this))
138-
.orElseThrow(() -> new IllegalArgumentException("Unsupported type " + objectClass));
134+
return MarshallingUtils.getCustomMarshaller(customMarshallers, objectClass)
135+
.read(this, objectClass);
139136
}
140137
}

impl/persistence/api/src/main/java/io/serverlessworkflow/impl/marshaller/AbstractOutputBuffer.java renamed to impl/core/src/main/java/io/serverlessworkflow/impl/marshaller/AbstractOutputBuffer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,11 @@ protected void writeClass(Class<?> objectClass) {
109109
writeString(objectClass.getCanonicalName());
110110
}
111111

112+
@SuppressWarnings({"rawtypes", "unchecked"})
112113
protected void writeCustomObject(Object object) {
113114
CustomObjectMarshaller marshaller =
114-
customMarshallers.stream()
115-
.filter(m -> m.getObjectClass().isAssignableFrom(object.getClass()))
116-
.findFirst()
117-
.orElseThrow(
118-
() -> new IllegalArgumentException("Unsupported type " + object.getClass()));
119-
writeClass(marshaller.getObjectClass());
115+
MarshallingUtils.getCustomMarshaller(customMarshallers, object.getClass());
116+
writeClass(object.getClass());
120117
marshaller.write(this, marshaller.getObjectClass().cast(object));
121118
}
122119

0 commit comments

Comments
 (0)