-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
754 additions
and
78 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
21 changes: 0 additions & 21 deletions
21
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/Jackson.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/Protostuff.java
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/Serializer.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/SerializerDispatcher.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,26 @@ | ||
package cn.promptness.rpt.base.serialize; | ||
|
||
import cn.promptness.rpt.base.serialize.api.*; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
|
||
public class SerializerDispatcher { | ||
|
||
public byte[] serialize(SerializationType serializationType, Object obj) throws Exception { | ||
Serialization serialization = SerializeFactory.getSerialization(serializationType); | ||
try (ByteArrayOutputStream byteBufOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = serialization.serialize(byteBufOutputStream)) { | ||
objectOutputStream.writeObject(obj); | ||
objectOutputStream.flush(); | ||
return byteBufOutputStream.toByteArray(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> T deserialize(SerializationType serializationType, byte[] data) throws Exception { | ||
Serialization serialization = SerializeFactory.getSerialization(serializationType); | ||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data); ObjectInputStream objectInputStream = serialization.deserialize(byteArrayInputStream)) { | ||
return (T) objectInputStream.readObject(Object.class); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/api/ObjectInputStream.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,31 @@ | ||
package cn.promptness.rpt.base.serialize.api; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
|
||
public interface ObjectInputStream extends Closeable { | ||
|
||
int readInt() throws IOException; | ||
|
||
byte readByte() throws IOException; | ||
|
||
byte[] readBytes() throws IOException; | ||
|
||
String readUTF() throws IOException; | ||
|
||
<T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException; | ||
|
||
default <T> T readObject(Class<T> cls, Type genericType) throws IOException, ClassNotFoundException { | ||
return readObject(cls); | ||
} | ||
|
||
default Throwable readThrowable() throws IOException, ClassNotFoundException { | ||
return readObject(Throwable.class); | ||
} | ||
|
||
default Map readMap() throws IOException, ClassNotFoundException { | ||
return readObject(Map.class); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/api/ObjectOutputStream.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,32 @@ | ||
package cn.promptness.rpt.base.serialize.api; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public interface ObjectOutputStream extends Closeable { | ||
|
||
void writeInt(int v) throws IOException; | ||
|
||
void writeByte(byte v) throws IOException; | ||
|
||
void writeBytes(byte[] b) throws IOException; | ||
|
||
void writeUTF(String v) throws IOException; | ||
|
||
void writeObject(Object obj) throws IOException; | ||
|
||
default void writeMap(Map<String, String> map) throws IOException { | ||
writeObject(map); | ||
} | ||
|
||
default void writeThrowable(Object obj) throws IOException { | ||
writeObject(obj); | ||
} | ||
|
||
default void writeEvent(Object data) throws IOException { | ||
writeObject(data); | ||
} | ||
|
||
void flush() throws IOException; | ||
} |
15 changes: 15 additions & 0 deletions
15
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/api/Serialization.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,15 @@ | ||
package cn.promptness.rpt.base.serialize.api; | ||
|
||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public interface Serialization { | ||
|
||
SerializationType getType(); | ||
|
||
ObjectOutputStream serialize(OutputStream output) throws IOException; | ||
|
||
ObjectInputStream deserialize(InputStream input) throws IOException; | ||
} |
27 changes: 27 additions & 0 deletions
27
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/api/SerializationType.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,27 @@ | ||
package cn.promptness.rpt.base.serialize.api; | ||
|
||
public enum SerializationType { | ||
|
||
/** | ||
* | ||
*/ | ||
PROTOSTUFF(1, "protostuff"), | ||
JSON(2, "jackson"); | ||
|
||
SerializationType(int code, String desc) { | ||
this.code = code; | ||
this.desc = desc; | ||
} | ||
|
||
final int code; | ||
|
||
final String desc; | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public String getDesc() { | ||
return desc; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/api/SerializeFactory.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,23 @@ | ||
package cn.promptness.rpt.base.serialize.api; | ||
|
||
|
||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class SerializeFactory { | ||
|
||
private static final Map<SerializationType, Serialization> SERIALIZATION_MAP = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
ServiceLoader<Serialization> serializations = ServiceLoader.load(Serialization.class); | ||
for (Serialization serialization : serializations) { | ||
SERIALIZATION_MAP.put(serialization.getType(), serialization); | ||
} | ||
} | ||
|
||
public static Serialization getSerialization(SerializationType serializationType) { | ||
return SERIALIZATION_MAP.get(serializationType); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
rpt-base/src/main/java/cn/promptness/rpt/base/serialize/json/JacksonUtil.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,52 @@ | ||
package cn.promptness.rpt.base.serialize.json; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.cfg.MapperConfig; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; | ||
|
||
import static com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping.NON_FINAL; | ||
|
||
public class JacksonUtil { | ||
|
||
/** | ||
* used for take the bean actual type with json string; for de-serializing the json string into real type bean. | ||
*/ | ||
private static final String TYPE_AS_JSON_PROPERTY = "typeAsJsonProperty"; | ||
|
||
private static final JsonMapper MAPPER = new JsonMapper(); | ||
|
||
static { | ||
final boolean takeTypeAsProperty = Boolean.getBoolean(TYPE_AS_JSON_PROPERTY); | ||
if (takeTypeAsProperty) { | ||
MAPPER.activateDefaultTypingAsProperty(new PolymorphicTypeValidator() { | ||
@Override | ||
public Validity validateBaseType(final MapperConfig<?> config, | ||
final JavaType baseType) { | ||
return Validity.ALLOWED; | ||
} | ||
|
||
@Override | ||
public Validity validateSubClassName(final MapperConfig<?> config, | ||
final JavaType baseType, | ||
final String subClassName) throws JsonMappingException { | ||
return Validity.ALLOWED; | ||
} | ||
|
||
@Override | ||
public Validity validateSubType(final MapperConfig<?> config, | ||
final JavaType baseType, | ||
final JavaType subType) throws JsonMappingException { | ||
return Validity.ALLOWED; | ||
} | ||
}, NON_FINAL, null); | ||
} | ||
MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
} | ||
|
||
public static JsonMapper getJsonMapper() { | ||
return MAPPER; | ||
} | ||
} |
Oops, something went wrong.