Instead of converting eclipselink.jar to a bundle, you can download our pre-built OSGi bundles from the following location:
EclipseLink 2.4.1 Bundles
For EclipseLink JAXB (MOXy) you will need the following bundles:
- 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 are using MOXy's JSON binding (see: http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html) you will also need the following bundle:
- org.eclipse.persistence.antlr_3.2.0.v201206041011.jar
UPDATE #1
In an OSGi environment you will need to ensure that you import either the MOXy bundle, or the org.eclipse.persistence.jaxb
packages.
another question in my code above at line 2, I found that
JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY is deprecated in the
eclipse bundles that you mentioned in your answer, is there any
replacement for it?
We have introduced new classes to make it easier to find the properties on JAXBContext
, Marshaller
, and Unmarshaller
. These classes are called JAXBContextProperties
, MarshallerProperties
, and UnmarshallerProperties
and can be found in the org.eclipse.persistence.jaxb
package.
- The following:
JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY
- Is replaced by:
JAXBContextProperties.OXM_METADATA_SOURCE
UPDATE #2
I haven't used Karaf, but below is an OSGi example I'm able to run in Eclipse Equinox:
example/Activator.java
I find you need to create the JAXBContext
using a context path. This allows you to pass in a ClassLoader
. This class loader needs to be aware of the EclipseLink JAXB (MOXy) implementation. I've also included an example of specifying MOXy's external 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
from a context path you need to include a file called jaxb.index
in the context path with a carriage return separated list of short class names.
Customer
example/jaxb.properties
To specify MOXy as your JAXB provider you need to include a file called 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
Below is an example 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
Below is an example of the manifest. In this example I have used bundle imports:
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
Below is the output from running the Activator
:
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<personal-info>
<name>Jane Doe</name>
</personal-info>
</customer>