Skip to content

feat(integrations/spring): add spring serialize method #5154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.opendal.spring.config;

import org.apache.opendal.spring.core.DefaultOpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.ReactiveOpenDALTemplate;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.ReactiveOpenDALOperations;
Expand All @@ -41,4 +43,10 @@ public OpenDALReactiveAutoConfiguration(OpenDALProperties openDALProperties) {
public ReactiveOpenDALTemplate reactiveOpendalTemplate(OpenDALProperties openDALProperties) {
return new ReactiveOpenDALTemplate(openDALProperties);
}

@Bean
@ConditionalOnMissingBean(name = "openDALSerializerFactory")
public OpenDALSerializerFactory openDALSerializerFactory() {
return new DefaultOpenDALSerializerFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.opendal.spring.TestReactiveApplication;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.ReactiveOpenDALOperations;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +39,9 @@ public class OpenDALReactiveAutoConfigurationTest {
@Autowired
private ReactiveOpenDALOperations openDALReactive;

@Autowired
private OpenDALSerializerFactory openDALSerializerFactory;

@Test
public void propertiesBeanShouldBeDeclared() {
Assertions.assertNotNull(openDALProperties);
Expand All @@ -53,4 +57,9 @@ public void reactiveBeanShouldBeDeclared() {
Assertions.assertInstanceOf(ReactiveOpenDALOperations.class, openDALReactive);
}

@Test
public void serializerFactoryBeanShouldBeDeclared() {
Assertions.assertNotNull(openDALSerializerFactory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package org.apache.opendal.spring.config;

import org.apache.opendal.spring.core.DefaultOpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALOperations;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALTemplate;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
Expand All @@ -41,4 +43,10 @@ public OpenDALAutoConfiguration(OpenDALProperties openDALProperties) {
public OpenDALTemplate opendalTemplate(OpenDALProperties openDALProperties) {
return new OpenDALTemplate(openDALProperties);
}

@Bean
@ConditionalOnMissingBean(name = "openDALSerializerFactory")
public OpenDALSerializerFactory openDALSerializerFactory() {
return new DefaultOpenDALSerializerFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.opendal.spring.TestApplication;
import org.apache.opendal.spring.core.OpenDALOperations;
import org.apache.opendal.spring.core.OpenDALProperties;
import org.apache.opendal.spring.core.OpenDALSerializerFactory;
import org.apache.opendal.spring.core.OpenDALTemplate;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +39,9 @@ public class OpenDALAutoConfigurationTest {
@Autowired
private OpenDALOperations openDAL;

@Autowired
private OpenDALSerializerFactory openDALSerializerFactory;

@Test
public void propertiesBeanShouldBeDeclared() {
Assertions.assertNotNull(openDALProperties);
Expand All @@ -52,4 +56,9 @@ public void beanShouldBeDeclared() {
Assertions.assertNotNull(openDAL);
Assertions.assertInstanceOf(OpenDALTemplate.class, openDAL);
}

@Test
public void serializerFactoryBeanShouldBeDeclared() {
Assertions.assertNotNull(openDALSerializerFactory);
}
}
9 changes: 9 additions & 0 deletions integrations/spring/opendal-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.opendal.spring.core;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.opendal.OpenDALException;

public class DefaultOpenDALSerializer<T> implements OpenDALSerializer<T> {
private static final ObjectMapper MAPPER = new ObjectMapper();

static {
MAPPER.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

private final Class<T> clazz;

public DefaultOpenDALSerializer(Class<T> clazz) {
this.clazz = clazz;
}

public static <T> DefaultOpenDALSerializer<T> of(Class<T> clazz) {
return new DefaultOpenDALSerializer<>(clazz);
}

@Override
public byte[] serialize(T t) throws OpenDALException {
try {
return MAPPER.writeValueAsBytes(t);
} catch (Exception e) {
throw new OpenDALException(OpenDALException.Code.Unexpected, e.toString());
}
}

@Override
public T deserialize(byte[] bytes) throws OpenDALException {
try {
return MAPPER.readValue(bytes, clazz);
} catch (Exception e) {
throw new OpenDALException(OpenDALException.Code.Unexpected, e.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.opendal.spring.core;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultOpenDALSerializerFactory implements OpenDALSerializerFactory {
private final Map<Class<?>, OpenDALSerializer<?>> serializerMap = new ConcurrentHashMap<>();

@SuppressWarnings("unchecked")
@Override
public <T> OpenDALSerializer<T> getSerializer(Class<T> clazz) {
return (OpenDALSerializer<T>) serializerMap.computeIfAbsent(clazz, DefaultOpenDALSerializer::of);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.opendal.spring.core;

import org.apache.opendal.OpenDALException;

public interface OpenDALSerializer<T> {
byte[] serialize(T t) throws OpenDALException;

T deserialize(byte[] bytes) throws OpenDALException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.opendal.spring.core;

public interface OpenDALSerializerFactory {
<T> OpenDALSerializer<T> getSerializer(Class<T> clazz);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.opendal.spring.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class DefaultOpenDALSerializerFactoryTest {
private OpenDALSerializerFactory factory;

@BeforeEach
void setUp() {
factory = new DefaultOpenDALSerializerFactory();
}

@Test
void getSerializerSuccess() {
OpenDALSerializer<DefaultOpenDALSerializerFactoryTest> serializer = factory.getSerializer(DefaultOpenDALSerializerFactoryTest.class);
Assertions.assertNotNull(serializer);
}
}
Loading