diff options
author | Wesley Wolfe <weswolf@aol.com> | 2013-04-25 06:06:12 -0500 |
---|---|---|
committer | Wesley Wolfe <weswolf@aol.com> | 2013-08-06 18:19:15 -0500 |
commit | e5a14e2dcc4bd5eafc5de1124898e2393df2d945 (patch) | |
tree | 3a75d284a00950c99893138f540c4982fdb6e93a /src/main | |
parent | 5ebcf56b9bd8d3e773316003003294fdfd51393a (diff) | |
download | bukkit-e5a14e2dcc4bd5eafc5de1124898e2393df2d945.tar bukkit-e5a14e2dcc4bd5eafc5de1124898e2393df2d945.tar.gz bukkit-e5a14e2dcc4bd5eafc5de1124898e2393df2d945.tar.lz bukkit-e5a14e2dcc4bd5eafc5de1124898e2393df2d945.tar.xz bukkit-e5a14e2dcc4bd5eafc5de1124898e2393df2d945.zip |
Add ConfigurationSerializable-Serializable compatibility. Adds BUKKIT-4662
This commit adds a comaptibility layer for use between
ConfigurationSerializable and Java Serializable, such that when using the
Bukkit object streams, any ConfigurationSerializable acts as if it
implements Serializable for purposes of that wrapped stream.
Included are a set of unit tests for the stream with a check for backward
compatibility across versions.
Diffstat (limited to 'src/main')
4 files changed, 145 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/util/io/BukkitObjectInputStream.java b/src/main/java/org/bukkit/util/io/BukkitObjectInputStream.java new file mode 100644 index 00000000..ed5675fe --- /dev/null +++ b/src/main/java/org/bukkit/util/io/BukkitObjectInputStream.java @@ -0,0 +1,63 @@ +package org.bukkit.util.io; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; + +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.ConfigurationSerialization; + +/** + * This class is designed to be used in conjunction with the {@link + * ConfigurationSerializable} API. It translates objects back to their + * original implementation after being serialized by {@link + * BukkitObjectInputStream}. + * <p> + * Behavior of implementations extending this class is not guaranteed across + * future versions. + */ +public class BukkitObjectInputStream extends ObjectInputStream { + + /** + * Constructor provided to mirror super functionality. + * + * @throws IOException + * @throws SecurityException + * @see {@link ObjectInputStream#ObjectInputStream()} + */ + protected BukkitObjectInputStream() throws IOException, SecurityException { + super(); + super.enableResolveObject(true); + } + + /** + * Object input stream decoration constructor. + * + * @param in + * @throws IOException + * @see {@link ObjectInputStream#ObjectInputStream(InputStream)} + */ + public BukkitObjectInputStream(InputStream in) throws IOException { + super(in); + super.enableResolveObject(true); + } + + @Override + protected Object resolveObject(Object obj) throws IOException { + if (obj instanceof Wrapper) { + try { + (obj = ConfigurationSerialization.deserializeObject(((Wrapper<?>) obj).map)).getClass(); // NPE + } catch (Throwable ex) { + throw newIOException("Failed to deserialize object", ex); + } + } + + return super.resolveObject(obj); + } + + private static IOException newIOException(String string, Throwable cause) { + IOException exception = new IOException(string); + exception.initCause(cause); + return exception; + } +} diff --git a/src/main/java/org/bukkit/util/io/BukkitObjectOutputStream.java b/src/main/java/org/bukkit/util/io/BukkitObjectOutputStream.java new file mode 100644 index 00000000..afdd7e3f --- /dev/null +++ b/src/main/java/org/bukkit/util/io/BukkitObjectOutputStream.java @@ -0,0 +1,53 @@ +package org.bukkit.util.io; + +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; + +import org.bukkit.configuration.serialization.ConfigurationSerializable; + +/** + * This class is designed to be used in conjunction with the {@link + * ConfigurationSerializable} API. It translates objects to an internal + * implementation for later deserialization using {@link + * BukkitObjectInputStream}. + * <p> + * Behavior of implementations extending this class is not guaranteed across + * future versions. + */ +public class BukkitObjectOutputStream extends ObjectOutputStream { + + /** + * Constructor provided to mirror super functionality. + * + * @throws IOException + * @throws SecurityException + * @see {@link ObjectOutputStream#ObjectOutputStream()} + */ + protected BukkitObjectOutputStream() throws IOException, SecurityException { + super(); + super.enableReplaceObject(true); + } + + /** + * Object output stream decoration constructor. + * + * @param out + * @throws IOException + * @see {@link ObjectOutputStream#ObjectOutputStream(OutputStream)} + */ + public BukkitObjectOutputStream(OutputStream out) throws IOException { + super(out); + super.enableReplaceObject(true); + } + + @Override + protected Object replaceObject(Object obj) throws IOException { + if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) { + obj = Wrapper.newWrapper((ConfigurationSerializable) obj); + } + + return super.replaceObject(obj); + } +} diff --git a/src/main/java/org/bukkit/util/io/Wrapper.java b/src/main/java/org/bukkit/util/io/Wrapper.java new file mode 100644 index 00000000..e45605b3 --- /dev/null +++ b/src/main/java/org/bukkit/util/io/Wrapper.java @@ -0,0 +1,23 @@ +package org.bukkit.util.io; + +import java.io.Serializable; +import java.util.Map; + +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.ConfigurationSerialization; + +import com.google.common.collect.ImmutableMap; + +class Wrapper<T extends Map<String, ?> & Serializable> implements Serializable { + private static final long serialVersionUID = -986209235411767547L; + + final T map; + + static Wrapper<ImmutableMap<String, ?>> newWrapper(ConfigurationSerializable obj) { + return new Wrapper<ImmutableMap<String, ?>>(ImmutableMap.<String, Object>builder().put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(obj.getClass())).putAll(obj.serialize()).build()); + } + + private Wrapper(T map) { + this.map = map; + } +} diff --git a/src/main/javadoc/org/bukkit/util/io/package-info.java b/src/main/javadoc/org/bukkit/util/io/package-info.java new file mode 100644 index 00000000..8e46cca4 --- /dev/null +++ b/src/main/javadoc/org/bukkit/util/io/package-info.java @@ -0,0 +1,6 @@ +/** + * Classes used to facilitate stream processing for specific Bukkit concepts. + * <p> + */ +package org.bukkit.util.io; + |