Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 587727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:14:35+00:00 2026-05-13T15:14:35+00:00

I am developing an application using OSGi (Equinox platform), and one of the bundles

  • 0

I am developing an application using OSGi (Equinox platform), and one of the bundles needs to parse XML files. So far I implemented this with SAX (javax.xml.parsers.SAXParserFactory) and I would like to retrieve the SAXParserFactory from the platform.

I saw the OSGi standard provides for a XMLParserActivator to allow JAXP implementations to register themselves (http://www.osgi.org/javadoc/r4v41/org/osgi/util/xml/XMLParserActivator.html), so my guess is that there should be some bundles that offer the SAXParserFactory as a service.

However, I could not figure out which bundle to add as dependency in order to find a service that offers a SAXParserFactory. I try to retrieve a service reference using

context.getServiceReferences(SAXParserFactory.class.getName(), "(&(parser.namespaceAware=true)(parser.validating=true))")

Given that XML parsing is a rather common thing to do, I suppose there are implementations available, or other means for getting a XML parser service from the platform.

Any help would be very welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:14:35+00:00Added an answer on May 13, 2026 at 3:14 pm

    Generally it is not a good idea to use JAXP in OSGi (because of the classloading mechanism primarily) and a much better idea to get the factory like a service.

    If you are using equinox, the SaxParserFactory (using the JRE/JDK one you are running on) is actually provided by the System Bundle, which means you don’t need extra bundles:

    {javax.xml.parsers.SAXParserFactory}={service.id=6}
    Registered by bundle: System Bundle [0]

    If you want to write code that deals with the lifecycle layer of the OSGi platform, I would suggest to track the reference, rather than looking it up directly.
    There are many approaches for this; I have written about one I call ServiceMediator here.

    e.g. for your case (code is under Apache 2 License, Coalevo Project):

            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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m developing an application using Spring WebFlow 2, Facelets and JSF. One of my
I am developing an application using MVC Preview 5. I have used typed views.
I am developing an application using Windows Mobile 5.0, under embedded VC++ 4.0, and
I developing ASP.NET application using a Swedish version of Windows XP and Visual studio
I'm developing a web- application using NHibernate. Can you tell me how to write
I am developing a web application using Struts 2.1.2 and Hibernate 3.2.6.GA. I have
I'm developing a Java application using Eclipse. My project has two source directories that
I am developing a WinForms application using the MVP pattern. I would like to
I am currently developing a Rails application using a database that was designed before
Hey I am developing an desktop application using Spring and Hibernate, and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.