Skip to content

Commit 4d8ecf1

Browse files
committed
feat(integrations/spring): add spring serialize method
Signed-off-by: ZhangJian He <[email protected]>
1 parent d1f8f0a commit 4d8ecf1

File tree

10 files changed

+226
-0
lines changed

10 files changed

+226
-0
lines changed

integrations/spring/opendal-spring-boot-starter-reactive/src/main/java/org/apache/opendal/spring/config/OpenDALReactiveAutoConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ public OpenDALReactiveAutoConfiguration(OpenDALProperties openDALProperties) {
4141
public ReactiveOpenDALTemplate reactiveOpendalTemplate(OpenDALProperties openDALProperties) {
4242
return new ReactiveOpenDALTemplate(openDALProperties);
4343
}
44+
45+
@Bean
46+
@ConditionalOnMissingBean(name = "openDALSerializerFactory")
47+
public OpenDALSerializerFactory openDALSerializerFactory() {
48+
return new DefaultOpenDALSerializerFactory();
49+
}
4450
}

integrations/spring/opendal-spring-boot-starter-reactive/src/test/java/org/apache/opendal/spring/config/OpenDALReactiveAutoConfigurationTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.opendal.spring.TestReactiveApplication;
2323
import org.apache.opendal.spring.core.OpenDALProperties;
24+
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
2425
import org.apache.opendal.spring.core.ReactiveOpenDALOperations;
2526
import org.junit.jupiter.api.Assertions;
2627
import org.junit.jupiter.api.Test;
@@ -38,6 +39,9 @@ public class OpenDALReactiveAutoConfigurationTest {
3839
@Autowired
3940
private ReactiveOpenDALOperations openDALReactive;
4041

42+
@Autowired
43+
private OpenDALSerializerFactory openDALSerializerFactory;
44+
4145
@Test
4246
public void propertiesBeanShouldBeDeclared() {
4347
Assertions.assertNotNull(openDALProperties);
@@ -53,4 +57,9 @@ public void reactiveBeanShouldBeDeclared() {
5357
Assertions.assertInstanceOf(ReactiveOpenDALOperations.class, openDALReactive);
5458
}
5559

60+
@Test
61+
public void serializerFactoryBeanShouldBeDeclared() {
62+
Assertions.assertNotNull(openDALSerializerFactory);
63+
}
64+
5665
}

integrations/spring/opendal-spring-boot-starter/src/main/java/org/apache/opendal/spring/config/OpenDALAutoConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
package org.apache.opendal.spring.config;
2121

22+
import org.apache.opendal.spring.core.DefaultOpenDALSerializerFactory;
2223
import org.apache.opendal.spring.core.OpenDALOperations;
24+
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
2325
import org.apache.opendal.spring.core.OpenDALTemplate;
2426
import org.apache.opendal.spring.core.OpenDALProperties;
2527
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -41,4 +43,10 @@ public OpenDALAutoConfiguration(OpenDALProperties openDALProperties) {
4143
public OpenDALTemplate opendalTemplate(OpenDALProperties openDALProperties) {
4244
return new OpenDALTemplate(openDALProperties);
4345
}
46+
47+
@Bean
48+
@ConditionalOnMissingBean(name = "openDALSerializerFactory")
49+
public OpenDALSerializerFactory openDALSerializerFactory() {
50+
return new DefaultOpenDALSerializerFactory();
51+
}
4452
}

integrations/spring/opendal-spring-boot-starter/src/test/java/org/apache/opendal/spring/config/OpenDALAutoConfigurationTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.opendal.spring.TestApplication;
2323
import org.apache.opendal.spring.core.OpenDALOperations;
2424
import org.apache.opendal.spring.core.OpenDALProperties;
25+
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
2526
import org.apache.opendal.spring.core.OpenDALTemplate;
2627
import org.junit.jupiter.api.Assertions;
2728
import org.junit.jupiter.api.Test;
@@ -38,6 +39,9 @@ public class OpenDALAutoConfigurationTest {
3839
@Autowired
3940
private OpenDALOperations openDAL;
4041

42+
@Autowired
43+
private OpenDALSerializerFactory openDALSerializerFactory;
44+
4145
@Test
4246
public void propertiesBeanShouldBeDeclared() {
4347
Assertions.assertNotNull(openDALProperties);
@@ -52,4 +56,9 @@ public void beanShouldBeDeclared() {
5256
Assertions.assertNotNull(openDAL);
5357
Assertions.assertInstanceOf(OpenDALTemplate.class, openDAL);
5458
}
59+
60+
@Test
61+
public void serializerFactoryBeanShouldBeDeclared() {
62+
Assertions.assertNotNull(openDALSerializerFactory);
63+
}
5564
}

integrations/spring/opendal-spring/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@
4141
<groupId>org.springframework.boot</groupId>
4242
<artifactId>spring-boot</artifactId>
4343
</dependency>
44+
<dependency>
45+
<groupId>com.fasterxml.jackson.core</groupId>
46+
<artifactId>jackson-databind</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.junit.jupiter</groupId>
50+
<artifactId>junit-jupiter</artifactId>
51+
<scope>test</scope>
52+
</dependency>
4453
</dependencies>
4554

4655
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.opendal.spring.core;
21+
22+
import com.fasterxml.jackson.databind.DeserializationFeature;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.apache.opendal.OpenDALException;
25+
26+
public class DefaultOpenDALSerializer<T> implements OpenDALSerializer<T> {
27+
private static final ObjectMapper MAPPER = new ObjectMapper();
28+
29+
static {
30+
MAPPER.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
31+
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
32+
}
33+
34+
private final Class<T> clazz;
35+
36+
public DefaultOpenDALSerializer(Class<T> clazz) {
37+
this.clazz = clazz;
38+
}
39+
40+
public static <T> DefaultOpenDALSerializer<T> of(Class<T> clazz) {
41+
return new DefaultOpenDALSerializer<>(clazz);
42+
}
43+
44+
@Override
45+
public byte[] serialize(T t) throws OpenDALException {
46+
try {
47+
return MAPPER.writeValueAsBytes(t);
48+
} catch (Exception e) {
49+
throw new OpenDALException(OpenDALException.Code.Unexpected, e.toString());
50+
}
51+
}
52+
53+
@Override
54+
public T deserialize(byte[] bytes) throws OpenDALException {
55+
try {
56+
return MAPPER.readValue(bytes, clazz);
57+
} catch (Exception e) {
58+
throw new OpenDALException(OpenDALException.Code.Unexpected, e.toString());
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.opendal.spring.core;
21+
22+
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
24+
25+
public class DefaultOpenDALSerializerFactory implements OpenDALSerializerFactory {
26+
private final Map<Class<?>, OpenDALSerializer<?>> serializerMap = new ConcurrentHashMap<>();
27+
28+
@SuppressWarnings("unchecked")
29+
@Override
30+
public <T> OpenDALSerializer<T> getSerializer(Class<T> clazz) {
31+
return (OpenDALSerializer<T>) serializerMap.computeIfAbsent(clazz, DefaultOpenDALSerializer::of);
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.opendal.spring.core;
21+
22+
import org.apache.opendal.OpenDALException;
23+
24+
public interface OpenDALSerializer<T> {
25+
byte[] serialize(T t) throws OpenDALException;
26+
27+
T deserialize(byte[] bytes) throws OpenDALException;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.opendal.spring.core;
21+
22+
public interface OpenDALSerializerFactory {
23+
<T> OpenDALSerializer<T> getSerializer(Class<T> clazz);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.opendal.spring.core;
21+
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
class DefaultOpenDALSerializerFactoryTest {
27+
private OpenDALSerializerFactory factory;
28+
29+
@BeforeEach
30+
void setUp() {
31+
factory = new DefaultOpenDALSerializerFactory();
32+
}
33+
34+
@Test
35+
void getSerializerSuccess() {
36+
OpenDALSerializer<DefaultOpenDALSerializerFactoryTest> serializer = factory.getSerializer(DefaultOpenDALSerializerFactoryTest.class);
37+
Assertions.assertNotNull(serializer);
38+
}
39+
}

0 commit comments

Comments
 (0)