Bug 565265 - Use ServiceCaller in XMLRootHandler
The XMLRootHandler currently gets a reference to the SAXParserFactory
using a ServiceReference, but we can replace that with a ServiceCaller
that performs the lookup and obtains a factory.
Change-Id: Ic95851cfb886c4b9268c3e047aeaec9ec0fcbd68
Signed-off-by: Alex Blewitt <alex.blewitt@gmail.com>
diff --git a/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/XMLRootHandler.java b/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/XMLRootHandler.java
index a7665e2..2dde902 100644
--- a/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/XMLRootHandler.java
+++ b/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/XMLRootHandler.java
@@ -16,7 +16,7 @@
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.*;
-import org.osgi.framework.*;
+import org.eclipse.core.runtime.ServiceCaller;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
@@ -148,35 +148,26 @@
return namespaceFound;
}
- private static final BundleContext CONTEXT;
-
- static {
- Bundle BUNDLE = FrameworkUtil.getBundle(XMLRootHandler.class);
- CONTEXT = BUNDLE == null ? null : BUNDLE.getBundleContext();
+ @SuppressWarnings("unchecked")
+ static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
+ throw (E) e;
}
public boolean parseContents(InputSource contents) throws IOException, ParserConfigurationException, SAXException {
// Parse the file into we have what we need (or an error occurs).
- if (CONTEXT == null)
- return false;
- ServiceReference<SAXParserFactory> serviceReference = CONTEXT.getServiceReference(SAXParserFactory.class);
- try {
-
- SAXParserFactory factory = CONTEXT.getService(serviceReference);
- if (factory == null)
- return false;
- factory.setNamespaceAware(true);
- final SAXParser parser = createParser(factory);
- // to support external entities specified as relative URIs (see bug 63298)
- contents.setSystemId("/"); //$NON-NLS-1$
- parser.parse(contents, this);
-
- } catch (StopParsingException e) {
- // Abort the parsing normally. Fall through...
- } finally {
- CONTEXT.ungetService(serviceReference);
- }
- return true;
+ return ServiceCaller.callOnce(getClass(), SAXParserFactory.class, (factory) -> {
+ try {
+ factory.setNamespaceAware(true);
+ final SAXParser parser = createParser(factory);
+ // to support external entities specified as relative URIs (see bug 63298)
+ contents.setSystemId("/"); //$NON-NLS-1$
+ parser.parse(contents, this);
+ } catch (StopParsingException e) {
+ // Abort the parsing normally. Fall through...
+ } catch (SAXException | IOException | ParserConfigurationException e) {
+ sneakyThrow(e);
+ }
+ });
}
/*