gpt4 book ai didi

java - 使用 XML 解析器实现作为 OSGi 服务

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:16 25 4
gpt4 key购买 nike

我正在使用 OSGi(Equinox 平台)开发应用程序,其中一个包需要解析 XML 文件。到目前为止,我使用 SAX (javax.xml.parsers.SAXParserFactory) 实现了它,我想从平台中检索 SAXParserFactory。

我看到 OSGi 标准提供了一个 XMLParserActivator 以允许 JAXP 实现自行注册 (http://www.osgi.org/javadoc/r4v41/org/osgi/util/xml/XMLParserActivator.html),所以我的猜测是应该有一些包将 SAXParserFactory 作为服务提供。

但是,我不知道要添加哪个包作为依赖项才能找到提供 SAXParserFactory 的服务。我尝试使用

检索服务引用
context.getServiceReferences(SAXParserFactory.class.getName(), "(&(parser.namespaceAware=true)(parser.validating=true))")

鉴于 XML 解析是一件相当常见的事情,我想有可用的实现或其他方法从平台获取 XML 解析器服务。

非常欢迎任何帮助!

最佳答案

一般来说,在 OSGi 中使用 JAXP 不是一个好主意(主要是因为类加载机制),而让工厂像服务一样好得多。

如果您使用的是 equinox,SaxParserFactory(使用您正在运行的 JRE/JDK)实际上是由 System Bundle 提供的,这意味着您不需要额外的包:

{javax.xml.parsers.SAXParserFactory}={service.id=6} 按捆绑注册:系统捆绑 [0]

如果您想编写处理 OSGi 平台生命周期层的代码,我建议您跟踪引用,而不是直接查找它。有很多方法可以做到这一点;我写过一个我称之为 ServiceMediator 的文章 here .

例如对于您的情况(代码在 Apache 2 许可下,Coalevo 项目):

        import org.osgi.framework.*;

import javax.xml.parsers.SAXParserFactory;

import net.wimpi.telnetd.util.Latch;

/**
* Implements a mediator pattern class for services from the OSGi container.
* <p/>
*
* @author Dieter Wimberger (wimpi)
* @version @version@ (@date@)
*/
class ServiceMediator {

private BundleContext m_BundleContext;

private SAXParserFactory m_SAXParserFactory;
private Latch m_SAXParserFactoryLatch;

public SAXParserFactory getSAXParserFactory(long wait) {
try {
if (wait < 0) {
m_SAXParserFactoryLatch.acquire();
} else if (wait > 0) {
m_SAXParserFactoryLatch.attempt(wait);
}
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}

return m_SAXParserFactory;
}//getSAXParserFactory

public boolean activate(BundleContext bc) {
//get the context
m_BundleContext = bc;

m_SAXParserFactoryLatch = createWaitLatch();

//prepareDefinitions listener
ServiceListener serviceListener = new ServiceListenerImpl();

//prepareDefinitions the filter
String filter = "(objectclass=" + SAXParserFactory.class.getName() + ")";

try {
//add the listener to the bundle context.
bc.addServiceListener(serviceListener, filter);

//ensure that already registered Service instances are registered with
//the manager
ServiceReference[] srl = bc.getServiceReferences(null, filter);
for (int i = 0; srl != null && i < srl.length; i++) {
serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, srl[i]));
}
} catch (InvalidSyntaxException ex) {
ex.printStackTrace(System.err);
return false;
}
return true;
}//activate

public void deactivate() {
m_SAXParserFactory = null;

m_SAXParserFactoryLatch = null;

m_BundleContext = null;
}//deactivate

private Latch createWaitLatch() {
return new Latch();
}//createWaitLatch

private class ServiceListenerImpl
implements ServiceListener {

public void serviceChanged(ServiceEvent ev) {
ServiceReference sr = ev.getServiceReference();
Object o = null;
switch (ev.getType()) {
case ServiceEvent.REGISTERED:
o = m_BundleContext.getService(sr);
if (o == null) {
return;
} else if (o instanceof SAXParserFactory) {
m_SAXParserFactory = (SAXParserFactory) o;
m_SAXParserFactory.setValidating(false);
m_SAXParserFactory.setNamespaceAware(true);
m_SAXParserFactoryLatch.release();
} else {
m_BundleContext.ungetService(sr);
}
break;
case ServiceEvent.UNREGISTERING:
o = m_BundleContext.getService(sr);
if (o == null) {
return;
} else if (o instanceof SAXParserFactory) {
m_SAXParserFactory = null;
m_SAXParserFactoryLatch = createWaitLatch();
} else {
m_BundleContext.ungetService(sr);
}
break;
}
}
}//inner class ServiceListenerImpl

public static long WAIT_UNLIMITED = -1;
public static long NO_WAIT = 0;

}//class ServiceMediator

关于java - 使用 XML 解析器实现作为 OSGi 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1158550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com