-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integrations/spring): add spring serialize method (#5154)
Signed-off-by: ZhangJian He <[email protected]>
- Loading branch information
Showing
10 changed files
with
228 additions
and
0 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
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
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
61 changes: 61 additions & 0 deletions
61
...opendal-spring/src/main/java/org/apache/opendal/spring/core/DefaultOpenDALSerializer.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,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()); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...-spring/src/main/java/org/apache/opendal/spring/core/DefaultOpenDALSerializerFactory.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,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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...spring/opendal-spring/src/main/java/org/apache/opendal/spring/core/OpenDALSerializer.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,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; | ||
} |
24 changes: 24 additions & 0 deletions
24
...opendal-spring/src/main/java/org/apache/opendal/spring/core/OpenDALSerializerFactory.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,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); | ||
} |
39 changes: 39 additions & 0 deletions
39
...ing/src/test/java/org/apache/opendal/spring/core/DefaultOpenDALSerializerFactoryTest.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,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); | ||
} | ||
} |