Instead of converting eclipselink.jar to a package, you can download our ready-made OSGi packages from the following location:
EclipseLink 2.4.1 Bundles
For EclipseLink JAXB (MOXy) you will need the following packages:
- org.eclipse.persistence.moxy_2.4.1.v20121003-ad44345.jar
- org.eclipse.persistence.core_2.4.1.v20121003-ad44345.jar
- org.eclipse.persistence.asm_3.3.1.v201206041142.jar
If you use MOXy JSON binding (see http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html ), you will also need the following package:
- org.eclipse.persistence.antlr_3.2.0.v201206041011.jar
UPDATE # 1
In an OSGi environment, you need to make sure that you import the MOXy package or the org.eclipse.persistence.jaxb packages.
another question in my code above on line 2, I found that JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY is deprecated in eclipse that you mentioned in your answer, are there any replace it?
We introduced new classes to simplify property searches on JAXBContext , Marshaller and Unmarshaller . These classes are called JAXBContextProperties , MarshallerProperties and UnmarshallerProperties and can be found in the org.eclipse.persistence.jaxb package.
- Next:
JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY - Replaced by:
JAXBContextProperties.OXM_METADATA_SOURCE
UPDATE # 2
I have not used Karaf, but below is an example of OSGi that I can run in Eclipse Equinox:
Example /Activator.java
I believe you need to create a JAXBContext using the context path. This allows you to pass a ClassLoader . This classloader should be aware of the EclipseLink JAXB (MOXy) implementation. I also included an example specifying an external MOXy mapping document.
package example; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextProperties; import org.osgi.framework.*; public class Activator implements BundleActivator { private static BundleContext context; static BundleContext getContext() { return context; } public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; Map<String, Object> properties = new HashMap<String, Object>(1); properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "example/oxm.xml"); JAXBContext jc = JAXBContext.newInstance("example", Customer.class.getClassLoader(), properties); Customer customer = new Customer(); customer.setName("Jane Doe"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(customer, System.out); } public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } }
Example /jaxb.index
When creating a JAXBContext in a context path, you need to include a file named jaxb.index in the context path in which the carriage returns a separate list of short class names.
Customer
Example /jaxb.properties
To specify MOXy as the JAXB provider, you need to include a file named jaxb.properties in the same package as your domain model, with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Example /oxm.xml
The following is an example of a MOXy mapping document.
<?xml version="1.0" encoding="UTF-8"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="example"> <java-types> <java-type name="Customer"> <xml-root-element/> <java-attributes> <xml-element java-attribute="name" xml-path="personal-info/name/text()"/> </java-attributes> </java-type> </java-types> </xml-bindings>
META-INF / MANIFEST.MF
The following is an example manifest. In this example, I used package import:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Example Bundle-SymbolicName: Example Bundle-Version: 1.0.0.qualifier Bundle-Activator: example.Activator Import-Package: org.osgi.framework;version="1.3.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Require-Bundle: org.eclipse.persistence.antlr, org.eclipse.persistence.asm, org.eclipse.persistence.core, org.eclipse.persistence.moxy, javax.xml.bind
Output
The following is the result of launching Activator :
<?xml version="1.0" encoding="UTF-8"?> <customer> <personal-info> <name>Jane Doe</name> </personal-info> </customer>