pluggable java object serialization mechanism added

上级 4f5fcd83
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.api;
/**
* Custom serialization mechanism for java objects being stored in column of type OTHER.
*
* @author Sergi Vladykin
*/
public interface JavaObjectSerializer {
/**
* Serialize object to byte array.
*
* @param obj
* @return
*/
byte[] serialize(Object obj) throws Exception;
/**
* Deserialize object from byte array.
*
* @param bytes
* @return
*/
Object deserialize(byte[] bytes) throws Exception;
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.constant; package org.h2.constant;
import org.h2.api.JavaObjectSerializer;
import org.h2.message.TraceSystem; import org.h2.message.TraceSystem;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -397,6 +398,13 @@ public class SysProperties { ...@@ -397,6 +398,13 @@ public class SysProperties {
*/ */
public static boolean serializeJavaObject = Utils.getProperty("h2.serializeJavaObject", true); public static boolean serializeJavaObject = Utils.getProperty("h2.serializeJavaObject", true);
/**
* System property <code>h2.javaObjectSerializer</code> (default: null).<br />
* {@link JavaObjectSerializer} class name for java objects being stored in column of type OTHER.
* It must be the same on client and server to work correctly.
*/
public static final String JAVA_OBJECT_SERIALIZER = Utils.getProperty("h2.javaObjectSerializer", null);
private static final String H2_BASE_DIR = "h2.baseDir"; private static final String H2_BASE_DIR = "h2.baseDir";
private SysProperties() { private SysProperties() {
......
...@@ -21,6 +21,7 @@ import java.util.HashMap; ...@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import org.h2.api.JavaObjectSerializer;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
...@@ -55,6 +56,20 @@ public class Utils { ...@@ -55,6 +56,20 @@ public class Utils {
private static HashSet<String> allowedClassNames; private static HashSet<String> allowedClassNames;
private static String[] allowedClassNamePrefixes; private static String[] allowedClassNamePrefixes;
public static JavaObjectSerializer serializer;
static {
String cls = SysProperties.JAVA_OBJECT_SERIALIZER;
if (cls != null) {
try {
serializer = (JavaObjectSerializer) loadUserClass(cls).newInstance();
} catch (Exception e) {
throw DbException.convert(e);
}
}
}
private Utils() { private Utils() {
// utility class // utility class
} }
...@@ -252,6 +267,9 @@ public class Utils { ...@@ -252,6 +267,9 @@ public class Utils {
*/ */
public static byte[] serialize(Object obj) { public static byte[] serialize(Object obj) {
try { try {
if (serializer != null)
return serializer.serialize(obj);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out); ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj); os.writeObject(obj);
...@@ -270,6 +288,9 @@ public class Utils { ...@@ -270,6 +288,9 @@ public class Utils {
*/ */
public static Object deserialize(byte[] data) { public static Object deserialize(byte[] data) {
try { try {
if (serializer != null)
return serializer.deserialize(data);
ByteArrayInputStream in = new ByteArrayInputStream(data); ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is; ObjectInputStream is;
if (SysProperties.USE_THREAD_CONTEXT_CLASS_LOADER) { if (SysProperties.USE_THREAD_CONTEXT_CLASS_LOADER) {
......
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;
import org.h2.api.JavaObjectSerializer;
import org.h2.test.TestBase;
import org.h2.util.Utils;
/**
* Tests {@link JavaObjectSerializer}.
*
* @author Sergi Vladykin
*/
public class TestJavaObjectSerializer extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase test = createCaller().init();
test.config.traceTest = true;
test.config.memory = true;
test.config.networked = true;
test.config.beforeTest();
test.test();
test.config.afterTest();
}
@Override
public void test() throws Exception {
Utils.serializer = new JavaObjectSerializer() {
@Override
public byte[] serialize(Object obj) throws Exception {
assertEquals(100500, ((Integer)obj).intValue());
return new byte[]{1,2,3};
}
@Override
public Object deserialize(byte[] bytes) throws Exception {
assertEquals(new byte[]{1,2,3}, bytes);
return 100500;
}
};
try {
deleteDb("javaSerializer");
Connection conn = getConnection("javaSerializer");
Statement stat = conn.createStatement();
stat.execute("create table t(id identity, val other)");
PreparedStatement ins = conn.prepareStatement("insert into t(val) values(?)");
ins.setObject(1, 100500, Types.JAVA_OBJECT);
assertEquals(1, ins.executeUpdate());
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("select val from t");
assertTrue(rs.next());
assertEquals(100500, ((Integer) rs.getObject(1)).intValue());
assertEquals(new byte[]{1,2,3}, rs.getBytes(1));
deleteDb("javaSerializer");
} finally {
Utils.serializer = null;
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论