« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
不得窥道门,不得悟佛门,不得入窄门,实乃破门。 |
Blog信息 |
blog名称:破门点滴 日志总数:161 评论数量:404 留言数量:-2 访问次数:1422030 建立时间:2004年11月13日 |

| |
[开发笔记]Triones Runtime: 修改java.net.URL对象支持多个StreamHandlerFactory的注册 原创空间, 软件技术 破门 发表于 2004/12/26 18:59:04 | 思路:
1、将URL对象中的factory对象扩展为一个factorys的Hashtable对象,将setURLStreamHandlerFactory的调用修改为将新的factory对象放入列表。
2、创建Handler时则遍历factorys列表,按照注册的先后顺序调用factory对象尝试创建Handler对象。
实际解决方案:
通过java自带的源代码包找出 java.net.URL对象,做如下修改:
1、增加 factorys 列表属性
static Hashtable factorys = new Hashtable();
…..
2、修改 setURLStreamHandlerFactory() 方法
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
synchronized (streamHandlerLock) {
// 为了支持多Factory
// if (factory != null) {
// throw new Error("factory already defined");
// }
…..
factory = fac;
// 将factory 放入列表
factorys.put(Integer.toString(factorys.size()), fac);
}
}
1、 增加 createURLStreamHandler 私有方法,通过Factory列表创建Handler
private static URLStreamHandler createURLStreamHandler(String protocol) {
for (int i=0; i < factorys.size(); i++) {
factory = (URLStreamHandlerFactory) factorys.get(Integer.toString(i));
URLStreamHandler handler = factory.createURLStreamHandler(protocol);
if (handler != null) {
return handler;
}
}
return null;
}
2、 修改getURLStreamHandler中获取Handler的方法
…..
// Use the factory (if any)
// if (factory != null) {
// handler = factory.createURLStreamHandler(protocol);
// checkedWithFactory = true;
// }
// 使用factory列表
if (factorys != null) {
handler = createURLStreamHandler(protocol);
checkedWithFactory = true;
}
……
// Check with factory if another thread set a
// factory since our last check
// if (!checkedWithFactory && factory != null) {
// handler2 = factory.createURLStreamHandler(protocol);
// }
if (!checkedWithFactory && factorys != null) {
handler2 = createURLStreamHandler(protocol);
}
3、 编译修改后的URL类,将URL.class 替换 rt.jar 包中的原始类文件。
替换rt.jar的具体方法:
a) 用zip工具(或jar工具)将rt.jar文件解开到一个目录(假设为rt)
b) 替换 rt/java/net/URL.class 文件为新类文件
c) 在 rt 目录下运行命令 jar cvf0M rt.jar . 生成 rt.jar 文件
d) 将新生成的 rt.jar 文件覆盖原始的JRE中的 rt.jar 文件
至此,以上 ava.lang.Error: factory already defined 问题解决。
| |
|