Closed b1nnar closed 9 years ago
This is kind of weird, as I'm using JAXBContext.newInstance (Class<?>)
internally and as well the appropriate ObjectFactory
class is present. Which version are you using?
Are you able to provide me with a short guideline how I can test this myself as I'm not an OSGI expert?? Thanks
I have created a simple OSGi bundle that uses your library and demonstrates the errors (both for marshal and unmarshal) - https://github.com/apiticas/ph-ubl-osgi-client. Instructions for install and use are in the README.
Thanks a lot - I'm investigating...
This guy had the same problem: http://blog.kusandriadi.com/problem-using-jaxb-marshallingunmarshalling-within-osgi-container/ - I will also investigate respective changes in ph-commons
Yes, it seems that either by passing the class instead of the package name, or the package name together with a bundle classloader to the JAXBContext newInstance method, the marshalling succeeds: https://github.com/apiticas/ph-ubl-osgi-client/commit/93782583bc03908215ca13ca4741697b695e7eed
Thanks a lot for testing this out. I already modified the ph-commons API but unfortunately this makes caching a bit more difficult. As soon as I release 6.0.1 I can provide a ph-ubl* 4.0.0-RC1 version for testing (hopefully tomorrow).
And when using ph-ubl with OSGI I noticed that it always requires ph-ubl20 and ph-ubl21. I will therefore modify the API so that there is a generic "ph-ubl-api" project and the main content is only in the ph-ubl20 or ph-ubl21 projects. The "ph-ubl" project will therefore be eliminated.
I will test the RC1 with my application as soon as you provide it. Thanks a lot!
Regarding the second problem:
Caused by: java.lang.IllegalStateException: Failed to open input stream for [cpPath=schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd; urlResolved=true; URL=null]
at com.helger.commons.xml.transform.ResourceStreamSource.getInputStream(ResourceStreamSource.java:66)
It is a classloader issue as well.
I have added 2 more commands to the client (getARSchemaWithClassloader
, getARSchemaWithoutClassloader
) on branch ResourceStreamProblem. They try to fetch the Schema resource InputStream using your API in commons. The one that specifies the classloader succeeds. So the problem may be in EUBL21DocumentType class where you call:
return new ClassPathResource (m_sXSDPath);
without specifying the classpath:
return new ClassPathResource (m_sXSDPath, getImplementationClass().getClassLoader());
I'm already doing this locally, but there is a follow-up error ClassPathResource.getInpuStream if a ClassLoader is specified - you see that may ClassLoader experience is growing heavily these days ;-) Stay tuned...
With my local modifications I'm a step further:
Marshalling UBL document of type APPLICATION_RESPONSE
[Gogo shell] WARN com.helger.commons.url.URLHelper - Failed to open input stream for 'bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonAggregateComponents
-2.1.xsd': java.io.IOException - Resource does not exist: bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonAggregateComponents-2.1.xsd
[Gogo shell] WARN com.helger.commons.xml.sax.LoggingSAXErrorHandler - [warn] bundle://18.0:1/schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd(21:82): [SAX]
schema_reference.4: Schemadokument "bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonAggregateComponents-2.1.xsd" konnte nicht gelesen werden, da 1) das
Dokument nicht gefunden werden konnte; 2) das Dokument nicht gelesen werden konnte; 3) das Root-Element des Dokuments nicht <xsd:schema> ist.
[Gogo shell] WARN com.helger.commons.url.URLHelper - Failed to open input stream for 'bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonBasicComponents-2.1
.xsd': java.io.IOException - Resource does not exist: bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonBasicComponents-2.1.xsd
[Gogo shell] WARN com.helger.commons.xml.sax.LoggingSAXErrorHandler - [warn] bundle://18.0:1/schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd(23:78): [SAX]
schema_reference.4: Schemadokument "bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonBasicComponents-2.1.xsd" konnte nicht gelesen werden, da 1) das Doku
ment nicht gefunden werden konnte; 2) das Dokument nicht gelesen werden konnte; 3) das Root-Element des Dokuments nicht <xsd:schema> ist.
[Gogo shell] WARN com.helger.commons.url.URLHelper - Failed to open input stream for 'bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonExtensionComponents
-2.1.xsd': java.io.IOException - Resource does not exist: bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonExtensionComponents-2.1.xsd
[Gogo shell] WARN com.helger.commons.xml.sax.LoggingSAXErrorHandler - [warn] bundle://18.0:1/schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd(25:82): [SAX]
schema_reference.4: Schemadokument "bundle://18.0:1/schemas/ubl21/maindoc/common/UBL-CommonExtensionComponents-2.1.xsd" konnte nicht gelesen werden, da 1) das
Dokument nicht gefunden werden konnte; 2) das Dokument nicht gelesen werden konnte; 3) das Root-Element des Dokuments nicht <xsd:schema> ist.
[Gogo shell] ERROR com.helger.commons.xml.sax.LoggingSAXErrorHandler - [error] bundle://18.0:1/schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd(46:77): [SA
X] src-resolve: Name "ext:UBLExtensions" kann nicht als "element declaration"-Komponente aufgelöst werden.
[Gogo shell] ERROR com.helger.commons.xml.sax.LoggingSAXErrorHandler - [error] bundle://18.0:1/schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd(51:75): [SA
X] src-resolve: Name "cbc:UBLVersionID" kann nicht als "element declaration"-Komponente aufgelöst werden.
[Gogo shell] ERROR com.helger.commons.xml.sax.LoggingSAXErrorHandler - [error] bundle://18.0:1/schemas/ubl21/maindoc/UBL-ApplicationResponse-2.1.xsd(247:79): [S
AX] src-resolve: Name "cac:Signature" kann nicht als "element declaration"-Komponente aufgelöst werden.
[Gogo shell] INFO com.helger.commons.jaxb.JAXBContextCache - Creating JAXB context for package oasis.names.specification.ubl.schema.xsd.applicationresponse_21
[Gogo shell] ERROR com.helger.ubl21.UBL21Marshaller - Failed to set the namespace prefix mapper: java.lang.IllegalArgumentException -- Failed to set JAXB proper
ty 'com.sun.xml.bind.namespacePrefixMapper' to com.helger.commons.jaxb.JAXBNamespacePrefixMapperOracleRT@492d0d
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns4:ApplicationResponse xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xml
ns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ns3="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateCompone
nts-2" xmlns:ns4="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"><ID>TEST_UBL_ID</ID></ns4:ApplicationResponse>
One difference was to use UBL21Marshaller.class.getClassLoader()
instead of PhUblClient.class.getClassLoader()
Update: this means that resource resolving in SAX still fails - work in progress
I'm still trying to figure out what the problem is and I'm stil stuck with the XML resource resolving. I finally managed to get all paths and ClassLoaders right, but Felix debug states e.g.
DEBUG: Bundle com.helger.ph-ubl21 [23] bundle://23.0:1/schemas/ubl21/common/UBL-SignatureBasicComponents-2.1.xsd not found by com.helger.ph-ubl21 [23]
So probably just joining URLs is not enough. E.g. http://stackoverflow.com/questions/6474634/how-do-i-access-a-file-inside-an-osgi-bundle suggests to use Bundle.getResource(name)
instead of classLoader.getResource(name)
. I need to understand the bundle URL scheme (the 23.0:1
) before I can continue: bundle://<revision-id>:<bundle-classpath-index>/<resource-path>
.
Maybe a special LSResourceResolver
for bundle URLs must be added....
Now I added a special LSResourceResolver but struggeling how I make it available in a nice way. I'm now on vacation for one week but will continue afterwards - please stay tuned.
Ok, thanks.
Finally the trunk versions (ph-commons 6.0.2-SNAPSHOT and ph-ubl21 4.0.0-SNAPSHOT) can handle the issue. It was basically a problem in the SimpleLSResourceResolver that was handling base URLs with a filename (like ".../ApplicationResponse.xsd") and a relative system URL incorrectly. This has now been fixed. There is now one little incident open so that the namespace mapper (mapping prefix to namespace URL) does not yet work:
[Gogo shell] ERROR com.helger.ubl21.UBL21Marshaller - Failed to set the namespace prefix mapper: java.lang.IllegalArgumentException -- Failed to set JAXB property 'com.sun.xml.bind
.namespacePrefixMapper' to com.helger.commons.jaxb.JAXBNamespacePrefixMapperOracleRT@7d5213
javax.xml.bind.PropertyException: name: com.sun.xml.bind.namespacePrefixMapper value: com.helger.commons.jaxb.JAXBNamespacePrefixMapperOracleRT@7d5213
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(Unknown Source)
at com.helger.commons.jaxb.JAXBMarshallerHelper._setProperty(JAXBMarshallerHelper.java:65)
at com.helger.commons.jaxb.JAXBMarshallerHelper.setSunNamespacePrefixMapper(JAXBMarshallerHelper.java:291)
at com.helger.commons.jaxb.JAXBMarshallerHelper.setSunNamespacePrefixMapper(JAXBMarshallerHelper.java:270)
at com.helger.ubl21.UBL21Marshaller._createFullMarshaller(UBL21Marshaller.java:226)
at com.helger.ubl21.UBL21Marshaller.writeUBLDocument(UBL21Marshaller.java:326)
at ro.alx.phublclient.impl.PhUblClient.marshal(PhUblClient.java:23)
at ro.alx.phublclient.commands.PhUblClientCommands.marshal(PhUblClientCommands.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)
at org.apache.felix.gogo.shell.Console.run(Console.java:62)
at org.apache.felix.gogo.shell.Shell.console(Shell.java:203)
at org.apache.felix.gogo.shell.Shell.gosh(Shell.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)
at org.apache.felix.gogo.shell.Activator.run(Activator.java:75)
at java.lang.Thread.run(Unknown Source)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns4:ApplicationResponse xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ns2="urn:oasis:na
mes:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ns3="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ns4="urn:oasis:names:specif
ication:ubl:schema:xsd:ApplicationResponse-2"><ID>TEST_UBL_ID</ID></ns4:ApplicationResponse>
Pleas try ph-commons 6.1.0, ph-jaxb 6.1.0 and the ph-ubl 4.0.0-RC2 artefacts. The problem with the namespacePrefixMapper stays because it is really hard to track and not so limiting. Btw. You may use GlobalDebug.setDebugModeDirect(false) to disable the stacktraces
ph-ubl 4.0.0 is out...
Thanks a lot!
Hello!
Can the library be used in OSGi containers? (like Apache Felix or Apache Karaf) I noticed the libraries are bundled so I tried using the read/write/marshal utils in a project that runs on Karaf. The following exception was thrown when the JAXB Context was created:
What may cause this is described here
Thanks!