Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
c951b1ab
提交
c951b1ab
authored
9月 15, 2013
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue 467: OSGi Class Loader (undo as requested)
上级
c4c7ba71
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
1 行增加
和
127 行删除
+1
-127
DbDriverActivator.java
h2/src/main/org/h2/util/DbDriverActivator.java
+1
-127
没有找到文件。
h2/src/main/org/h2/util/DbDriverActivator.java
浏览文件 @
c951b1ab
...
...
@@ -5,24 +5,18 @@
*/
package
org
.
h2
.
util
;
import
java.util.Properties
;
import
org.h2.engine.Constants
;
import
org.osgi.framework.Bundle
;
import
org.osgi.framework.BundleActivator
;
import
org.osgi.framework.BundleContext
;
import
org.osgi.framework.ServiceReference
;
import
org.osgi.service.jdbc.DataSourceFactory
;
import
java.util.Properties
;
/**
* The driver activator loads the H2 driver when starting the bundle. The driver
* is unloaded when stopping the bundle.
*/
public
class
DbDriverActivator
implements
BundleActivator
{
private
OSGIClassFactory
osgiClassFactory
;
private
OSGIServiceClassFactory
osgiServiceClassFactory
;
/**
* Start the bundle. This will load the database driver and register the
* DataSourceFactory service.
...
...
@@ -32,10 +26,6 @@ public class DbDriverActivator implements BundleActivator {
@Override
public
void
start
(
BundleContext
bundleContext
)
{
org
.
h2
.
Driver
driver
=
org
.
h2
.
Driver
.
load
();
osgiClassFactory
=
new
OSGIClassFactory
(
bundleContext
);
osgiServiceClassFactory
=
new
OSGIServiceClassFactory
(
bundleContext
);
Utils
.
addClassFactory
(
osgiClassFactory
);
Utils
.
addClassFactory
(
osgiServiceClassFactory
);
Properties
properties
=
new
Properties
();
properties
.
put
(
DataSourceFactory
.
OSGI_JDBC_DRIVER_CLASS
,
org
.
h2
.
Driver
.
class
.
getName
());
properties
.
put
(
DataSourceFactory
.
OSGI_JDBC_DRIVER_NAME
,
"H2 JDBC Driver"
);
...
...
@@ -52,123 +42,7 @@ public class DbDriverActivator implements BundleActivator {
*/
@Override
public
void
stop
(
BundleContext
bundleContext
)
{
Utils
.
removeClassFactory
(
osgiClassFactory
);
Utils
.
removeClassFactory
(
osgiServiceClassFactory
);
org
.
h2
.
Driver
.
unload
();
osgiClassFactory
=
null
;
}
/**
* Extend the H2 class loading in order to find class defined in other bundles.
*
* The class format for bundle class is the following:
* BundleSymbolicName:BundleVersion:BinaryClassName
*/
private
static
class
OSGIClassFactory
implements
Utils
.
ClassFactory
{
/**
* Separator character to merge bundle name,version and class binary
* name
*/
public
static
final
String
SEPARATOR
=
":"
;
private
static
final
int
BUNDLE_SYMBOLIC_NAME_INDEX
=
0
;
private
static
final
int
BUNDLE_VERSION_INDEX
=
1
;
private
static
final
int
BINARY_CLASS_NAME_INDEX
=
2
;
private
BundleContext
bundleContext
;
/**
* Constructor
*
* @param bundleContext Valid bundleContext instance
*/
OSGIClassFactory
(
BundleContext
bundleContext
)
{
this
.
bundleContext
=
bundleContext
;
}
@Override
public
boolean
match
(
String
name
)
{
// OSGi binary class name must contain two SEPARATOR character
int
index
=
name
.
indexOf
(
SEPARATOR
);
return
!(
index
==
-
1
||
name
.
indexOf
(
SEPARATOR
,
index
+
1
)
==
-
1
);
}
@Override
public
Class
<?>
loadClass
(
String
name
)
throws
ClassNotFoundException
{
String
[]
parts
=
name
.
split
(
SEPARATOR
);
if
(
parts
.
length
!=
3
)
{
throw
new
ClassNotFoundException
(
"OSGi class binary name must contain only 2 '"
+
SEPARATOR
+
"' characters"
);
}
for
(
Bundle
bundle
:
bundleContext
.
getBundles
())
{
if
(
bundle
.
getSymbolicName
().
equals
(
parts
[
BUNDLE_SYMBOLIC_NAME_INDEX
])
&&
bundle
.
getVersion
().
toString
()
.
equals
(
parts
[
BUNDLE_VERSION_INDEX
]))
{
// Found the right bundle
return
bundle
.
loadClass
(
parts
[
BINARY_CLASS_NAME_INDEX
]);
}
}
throw
new
ClassNotFoundException
(
"OSGi Bundle not found "
+
parts
[
BUNDLE_SYMBOLIC_NAME_INDEX
]
+
" "
+
parts
[
BUNDLE_VERSION_INDEX
]);
}
}
/**
* Extend the H2 class loading in order to find class defined in other
* bundles.
*
* The class must be registered as a service.
*
* The main difference with {@link OSGIClassFactory} is that it does not
* rely to a specific bundle. OSGIServiceClassFactory is the preferred way
* to register function used in table constraints, these functions should
* not be removed from the database.
*
* The class format for bundle service class is the following:
* OSGI:BinaryClassNameService
*/
private
static
class
OSGIServiceClassFactory
implements
Utils
.
ClassFactory
{
/**
* Separator character to merge bundle name, version and class binary
* name
*/
public
static
final
String
SEPARATOR
=
"="
;
private
static
final
int
BINARY_CLASS_NAME_INDEX
=
1
;
private
BundleContext
bundleContext
;
/**
* Constructor
* @param bundleContext Valid bundleContext instance
*/
OSGIServiceClassFactory
(
BundleContext
bundleContext
)
{
this
.
bundleContext
=
bundleContext
;
}
@Override
public
boolean
match
(
String
name
)
{
return
name
.
startsWith
(
"OSGI"
+
SEPARATOR
);
}
@Override
public
Class
<?>
loadClass
(
String
name
)
throws
ClassNotFoundException
{
String
[]
parts
=
name
.
split
(
SEPARATOR
);
if
(
parts
.
length
!=
2
)
{
throw
new
ClassNotFoundException
(
"OSGi class binary name must contain only 1 '"
+
SEPARATOR
+
"' characters"
);
}
ServiceReference
serviceReference
=
bundleContext
.
getServiceReference
(
parts
[
BINARY_CLASS_NAME_INDEX
]);
if
(
serviceReference
!=
null
)
{
return
bundleContext
.
getService
(
serviceReference
).
getClass
();
}
throw
new
ClassNotFoundException
(
"OSGi Service not found "
+
parts
[
BINARY_CLASS_NAME_INDEX
]);
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论