Man*_*rdt 7 java xml parsing osgi sax
我正在使用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作为服务的bundle.
但是,为了找到提供SAXParserFactory的服务,我无法确定要添加哪个包作为依赖项.我尝试使用检索服务引用
context.getServiceReferences(SAXParserFactory.class.getName(), "(&(parser.namespaceAware=true)(parser.validating=true))")
Run Code Online (Sandbox Code Playgroud)
鉴于XML解析是一件相当常见的事情,我认为有可用的实现,或其他从平台获取XML解析器服务的方法.
非常欢迎任何帮助!
一般来说,在 OSGi 中使用 JAXP 并不是一个好主意(主要是因为类加载机制),而让工厂像服务一样更好。
如果您使用的是 equinox,SaxParserFactory(使用您正在运行的 JRE/JDK)实际上是由系统捆绑包提供的,这意味着您不需要额外的捆绑包:
{javax.xml.parsers.SAXParserFactory}={service.id=6} 由捆绑包注册:系统捆绑包 [0]
如果您想编写处理 OSGi 平台生命周期层的代码,我建议跟踪引用,而不是直接查找它。有很多方法可以实现这一点;我在这里写了一篇我称之为 ServiceMediator 的文章。
例如,对于您的情况(代码位于 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4224 次 |
| 最近记录: |