title: Intermittent ClassCastException from ElementNSImpl to own type during unmarshalling
body: <p>We are experiencing an exceedingly hard to track down issue where we are seeing ClassCastExceptions <em>sometimes</em> when trying to iterate over a list of unmarshalled objects. The important bit is <em>sometimes</em>, after a reboot the particular code works fine. This seems to point in the direction of concurrency/timing/race condition. I can confirm that neither the JAXBContext, nor the marshallers and unmarshallers are being used concurrently. We've gone as far as serializing access to them through locking.</p>
  
  <p>However, since we run on an OSGi platform where individual bundles are getting initialized asynchronously through Spring DM it can be that 2 different bundles are creating their JAXBContext at the same time.</p>
  
  <p>In any case I would appreciate any pointers towards an explanation for what could cause these <em>intermittent</em> ClassCastExceptions. The intermittent is important since they indicate that the code itself is normally working fine but that some external factor seems to influence the behaviour.</p>
  
  <p>Here's a specific example of the exception (note I removed the company specific stuff):</p>
  
  <pre><code>Caused by: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to com.foobar.TunnelType
      at com.foobar.NetMonitorImpl.getVpnStatus(NetMonitorImpl.java:180)
  </code></pre>
  
  <p>That method at line 180 is a for() construct looping over a Collection of TunnelType objects inside of an unmarshalled object (said unmarshalling works fine BTW).</p>
  
  <p>Given that the actual object unmarshalling went fine, is it even physically possible for JAXB to leave ElementNSImpl objects inside of nested collections?</p>
  
  <p>Runtime environment:</p>
  
  <ul>
  <li>JAXB 2.1</li>
  <li>OSGi</li>
  <li>Spring DM</li>
  <li>The JAXBContext is initialised with the ClassLoader of the bundle containing the classes to be marshalled/unmarshalled</li>
  </ul>