Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use in OSGi container #4

Closed
b1nnar opened this issue Jul 5, 2015 · 17 comments
Closed

Unable to use in OSGi container #4

b1nnar opened this issue Jul 5, 2015 · 17 comments
Assignees

Comments

@b1nnar
Copy link

b1nnar commented Jul 5, 2015

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:

javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "oasis.names.specification.ubl.schema.xsd.applicationresponse_21" doesnt contain ObjectFactory.class or jaxb.index

What may cause this is described here

Thanks!

@phax phax self-assigned this Jul 6, 2015
@phax
Copy link
Owner

phax commented Jul 7, 2015

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

@b1nnar
Copy link
Author

b1nnar commented Jul 7, 2015

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.

@phax
Copy link
Owner

phax commented Jul 8, 2015

Thanks a lot - I'm investigating...

@phax
Copy link
Owner

phax commented Jul 8, 2015

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

@b1nnar
Copy link
Author

b1nnar commented Jul 8, 2015

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:
b1nnar/ph-ubl-osgi-client@9378258

@phax
Copy link
Owner

phax commented Jul 8, 2015

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.

@b1nnar
Copy link
Author

b1nnar commented Jul 8, 2015

I will test the RC1 with my application as soon as you provide it.
Thanks a lot!

@b1nnar
Copy link
Author

b1nnar commented Jul 9, 2015

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());

@phax
Copy link
Owner

phax commented Jul 9, 2015

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...

@phax
Copy link
Owner

phax commented Jul 9, 2015

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

@phax
Copy link
Owner

phax commented Jul 10, 2015

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....

@phax
Copy link
Owner

phax commented Jul 10, 2015

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.

@b1nnar
Copy link
Author

b1nnar commented Jul 11, 2015

Ok, thanks.

@phax
Copy link
Owner

phax commented Jul 20, 2015

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>

@phax
Copy link
Owner

phax commented Jul 21, 2015

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

@phax
Copy link
Owner

phax commented Jul 29, 2015

ph-ubl 4.0.0 is out...

@phax phax closed this as completed Jul 29, 2015
@b1nnar
Copy link
Author

b1nnar commented Jul 29, 2015

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants