Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
51de3761
提交
51de3761
authored
11月 24, 2012
作者:
Sergi.Vladykin@gmail.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pluggable java object serialization mechanism added
上级
4f5fcd83
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
143 行增加
和
0 行删除
+143
-0
JavaObjectSerializer.java
h2/src/main/org/h2/api/JavaObjectSerializer.java
+32
-0
SysProperties.java
h2/src/main/org/h2/constant/SysProperties.java
+8
-0
Utils.java
h2/src/main/org/h2/util/Utils.java
+21
-0
TestJavaObjectSerializer.java
h2/src/test/org/h2/test/jdbc/TestJavaObjectSerializer.java
+82
-0
没有找到文件。
h2/src/main/org/h2/api/JavaObjectSerializer.java
0 → 100644
浏览文件 @
51de3761
/*
* 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
;
}
h2/src/main/org/h2/constant/SysProperties.java
浏览文件 @
51de3761
...
@@ -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
()
{
...
...
h2/src/main/org/h2/util/Utils.java
浏览文件 @
51de3761
...
@@ -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
)
{
...
...
h2/src/test/org/h2/test/jdbc/TestJavaObjectSerializer.java
0 → 100644
浏览文件 @
51de3761
/*
* 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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论