Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/2.1/DynamicMOXy/296967/BootstrapFromOXM"

Line 21: Line 21:
 
The purpose of this feature is to allow the user to bootstrap EclipseLink Dynamic JAXB from an EclipseLink externalized metadata file (<tt>eclipselink-oxm.xml</tt>).  This is a continuation of the Dynamic JAXB features introduced in [http://wiki.eclipse.org/EclipseLink/Development/2.1/DynamicMOXy/296967/BootstrapFromXSD EclipseLink 2.1.0].
 
The purpose of this feature is to allow the user to bootstrap EclipseLink Dynamic JAXB from an EclipseLink externalized metadata file (<tt>eclipselink-oxm.xml</tt>).  This is a continuation of the Dynamic JAXB features introduced in [http://wiki.eclipse.org/EclipseLink/Development/2.1/DynamicMOXy/296967/BootstrapFromXSD EclipseLink 2.1.0].
  
Support for bootstrapping a <tt>DynamicJAXBContext</tt> from an EclipseLink Externalized Metadata file (OXM) will be implemented in multiple phases.  For each phase, OXM files will be created that would result in the following annotations being generated by Dynamic JAXB.  A <tt>DynamicJAXBContext</tt> will then be created from each OXM file, and an example object will be marshalled.  The resulting document will be inspected to ensure that it was marshalled properly, according to the annotations being tested.
+
Support for bootstrapping a <tt>DynamicJAXBContext</tt> from an EclipseLink Externalized Metadata file (OXM) will be implemented in multiple phases.  For each phase, OXM files will be created that will exercise the associated annotations.  A <tt>DynamicJAXBContext</tt> will be created from each OXM file, and an example object will be marshalled.  The resulting document will be inspected to ensure that it was marshalled properly, according to the annotations being tested.
  
 
See also: [http://wiki.eclipse.org/EclipseLink/Development/2.1/DynamicMOXy/296967/Documentation Dynamic JAXB Documentation]
 
See also: [http://wiki.eclipse.org/EclipseLink/Development/2.1/DynamicMOXy/296967/Documentation Dynamic JAXB Documentation]

Revision as of 12:51, 28 July 2010

Dynamic MOXy - Bootstrapping from EclipseLink Externalized Metadata (OXM)

ER 321152

Document History

Date Author Version Description & Notes
2010/07/26 Rick Barkhouse Initial contribution

Overview

The purpose of this feature is to allow the user to bootstrap EclipseLink Dynamic JAXB from an EclipseLink externalized metadata file (eclipselink-oxm.xml). This is a continuation of the Dynamic JAXB features introduced in EclipseLink 2.1.0.

Support for bootstrapping a DynamicJAXBContext from an EclipseLink Externalized Metadata file (OXM) will be implemented in multiple phases. For each phase, OXM files will be created that will exercise the associated annotations. A DynamicJAXBContext will be created from each OXM file, and an example object will be marshalled. The resulting document will be inspected to ensure that it was marshalled properly, according to the annotations being tested.

See also: Dynamic JAXB Documentation

Generating a DynamicJAXBContext with OXM

A DynamicJAXBContext is created from an XSD in the following way:

  1. The OXM file is parsed by JAXBContextFactory to generate an org.eclipse.persistence.jaxb.xmlmodel.XmlBindings object.
  2. The XmlBindings are inspected and an EclipseLink javamodel representing this information is created.
  3. This collection of EclipseLink javamodel classes is then passed to org.eclipse.persistence.jaxb.compiler.Generator, which generates a standard EclipseLink project.
  4. A Dynamic Project is then created from the standard project via DynamicTypeBuilder.loadDynamicProject().
  5. An org.eclipse.persistence.oxm.XMLContext is then created from the Dynamic Project, and a DynamicJAXBContext is returned backed by this XMLContext.


Development Phases

Phase 1

Annotation XML Metadata Tag Package Type Field Method
XmlNs xml-ns        
XmlSchema xml-schema X      
XmlSeeAlso xml-see-also   X    


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
    <java-types>
        <java-type name="mynamespace.Person">
            <xml-root-element name="person"/>
            <java-attributes>
                <xml-element type="java.lang.String" java-attribute="type" xml-path="bloodType/text()"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>
  • Text.
  • Text.

Limitations

  • Text.



Phase 2

Annotation XML Metadata Tag Package Type Field Method
XmlRootElement xml-root-element   X    
XmlType xml-type   X    


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 3

Annotation XML Metadata Tag Package Type Field Method
XmlAttribute xml-attribute     X X
XmlElement xml-element     X X


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 4

Annotation XML Metadata Tag Package Type Field Method
XmlList xml-list     X X
XmlValue xml-value X X
XmlAnyElement xml-any-element     X X
XmlAnyAttribute xml-any-attribute     X X
XmlMixed xml-mixed     X X


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 5

Annotation XML Metadata Tag Package Type Field Method
XmlID
xml-id     X X
XmlIDREF
xml-idref   X
X


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 6

Annotation XML Metadata Tag Package Type Field Method
XmlElements xml-elements     X X
XmlElementRef xml-element-ref     X X


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 7

Annotation XML Metadata Tag Package Type Field Method
XmlSchemaType xml-schema-type X   X X


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 8

Annotation XML Metadata Tag Package Type Field Method
XmlEnum xml-enum   X    
XmlEnumValue xml-enum-value     X
X


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Phase 9

Annotation XML Metadata Tag Package Type Field Method
XmlElementDecl xml-element-decl     X X
XmlRegistry xml-registry   X    


The following example OXM file demonstrates how these annotations can be specified:

Example OXM

<?xml version="1.0" encoding="UTF-8"?>
  • Text.
  • Text.

Limitations

  • Text.



Design

When constructing a DynamicJAXBContext from an OXM file, we will first parse the metadata in order to create our own EclipseLink JavaModel classes, then pass this code model into an EclipseLink Generator (org.eclipse.persistence.jaxb.compiler.Generator) to generate an EclipseLink project. After we have created this project, we can use the DynamicTypeBuilder to create a dynamic project, generating Java classes in memory along the way.

First, we use internal EclipseLink API to parse the OXM file and return an XmlBindings object:

Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, classLoader);
 
JavaClass[] elinkClasses = createClassModelFromOXM(bindings);
 
...
private JavaClass[] createClassModelFromOXM(Map<String, XmlBindings> bindings) throws JAXBException {
    List<OXMJavaClassImpl> oxmJavaClasses = new ArrayList<OXMJavaClassImpl>();
 
    Iterator<String> keys = bindings.keySet().iterator();
 
    while (keys.hasNext()) {
        XmlBindings b = bindings.get(keys.next());
 
        List<JavaType> javaTypes = b.getJavaTypes().getJavaType();
        for (Iterator<JavaType> iterator = javaTypes.iterator(); iterator.hasNext();) {
            JavaType type = iterator.next();
            oxmJavaClasses.add(new OXMJavaClassImpl(type));
        }
    }
    ...

At this point, we can instantiate a Generator and obtain a "dry" EclipseLink project, which we can then turn into a dynamic project with generated in-memory classes. Finally, we create and store an XMLContext and a DynamicHelper:

// Use the JavaModel to setup a Generator to generate an EclipseLink project
OXMJavaModelImpl javaModel = new OXMJavaModelImpl(classLoader, elinkClasses);
OXMJavaModelInputImpl javaModelInput = new OXMJavaModelInputImpl(elinkClasses, javaModel);
Generator g = new Generator(javaModelInput, bindings, dynamicClassLoader, null);
 
Project p = null;
Project dp = null;
try {
    p = g.generateProject();
    // Clear out InstantiationPolicy because it refers to ObjectFactory, which we won't be using
    Vector<ClassDescriptor> descriptors = (Vector<ClassDescriptor>) p.getOrderedDescriptors();
    for (ClassDescriptor classDescriptor : descriptors) {
        classDescriptor.setInstantiationPolicy(new InstantiationPolicy());
    }
    dp = DynamicTypeBuilder.loadDynamicProject(p, null, dynamicClassLoader);
} catch (Exception e) {
    throw new JAXBException(org.eclipse.persistence.exceptions.JAXBException.errorCreatingDynamicJAXBContext(e));
}
 
this.xmlContext = new XMLContext(dp);
 
List<Session> sessions = (List<Session>) this.xmlContext.getSessions();
for (Object session : sessions) {
    this.helpers.add(new DynamicHelper((DatabaseSession) session));
}

Back to the top