Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "User:Rick.barkhouse.oracle.com/Test2"

(Removing all content from page)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
 
|eclipselink=y
 
|eclipselinktype=MOXy
 
|info=y
 
|api=y
 
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/metadata/MetadataSource.html MetadataSource]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/metadata/MetadataSourceAdapter.html MetadataSourceAdapter]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/metadata/XMLMetadataSource.html XMLMetadataSource]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/xmlmodel/XmlBindings.html XmlBindings]
 
|toc=y
 
}}
 
  
= XML MetadataSource =
 
 
EclipseLink '''2.3''' introduces the concept of a '''MetadataSource''', which is responsible for serving up EclipseLink metadata.  This allows you to store mapping information outside of your application and have it retrieved when the application's '''JAXBContext''' is being created or refreshed.
 
 
 
== Implementing a MetadataSource ==
 
 
To implement your own '''MetadataSource''', you can:
 
 
* Create a new class that implements the '''org.eclipse.persistence.jaxb.metadata.MetadataSource''' interface.
 
* Create a new class that extends the '''org.eclipse.persistence.jaxb.metadata.MetadataSourceAdapter''' class.  Using this method is preferred, as it will insulate you from future additions to the interface.
 
 
 
In either case, you will be responsible for implementing the following method:
 
 
<div style="width:750px">
 
<source lang="java">
 
/**
 
* Retrieve XmlBindings according to the JAXBContext bootstrapping information.
 
*
 
* @param properties - The properties passed in to create the JAXBContext
 
* @param classLoader - The ClassLoader passed in to create the JAXBContext
 
* @return the XmlBindings object representing the metadata
 
*/
 
XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader);
 
</source>
 
</div>
 
 
 
== XmlBindings ==
 
 
Internally, EclipseLink metadata is stored in an '''XmlBindings''' object, which itself is mapped with JAXB.  This means that you can actually use a JAXB unmarshaller to read external metadata and create an '''XmlBindings''' from it:
 
 
<div style="width:850px">
 
<source lang="java">
 
...
 
 
import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings;
 
 
...
 
 
JAXBContext xmlBindingsContext = JAXBContext.newInstance("org.eclipse.persistence.jaxb.xmlmodel");
 
FileReader bindingsFile = new FileReader("eclipselink-oxm.xml");
 
XmlBindings bindings = (XmlBindings) xmlBindingsContext.createUnmarshaller().unmarshal(bindingsFile);
 
</source>
 
</div>
 
 
 
== Example ==
 
 
The following example creates an '''XmlBindings''' object by unmarshalling from a URL:
 
 
<div style="width:850px">
 
<source lang="java">
 
package example;
 
 
import java.net.URL;
 
import java.util.Map;
 
 
import javax.xml.bind.JAXBContext;
 
 
import org.eclipse.persistence.jaxb.metadata.MetadataSourceAdapter;
 
import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings;
 
 
public class MyMetadataSource extends MetadataSourceAdapter {
 
 
  private JAXBContext bindingsContext;
 
  private URL bindingsUrl;
 
 
  private final String XML_BINDINGS_PACKAGE = "org.eclipse.persistence.jaxb.xmlmodel";
 
  private final String METADATA_URL = "http://www.zizucoffee.com/eclipselink-oxm3.xml";
 
 
  public MyMetadataSource() {
 
      try {
 
        bindingsContext = JAXBContext.newInstance(XML_BINDINGS_PACKAGE);
 
        bindingsUrl = new URL(METADATA_URL);
 
      } catch (Exception e) {
 
        throw new RuntimeException(e);
 
      }
 
  }
 
 
  @Override
 
  public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) {
 
      try {
 
        Unmarshaller u = bindingsContext.createUnmarshaller();
 
        XmlBindings bindings = (XmlBindings) u.unmarshal(bindingsUrl);
 
        return bindings;
 
      } catch (Exception e) {
 
        throw new RuntimeException(e);
 
      }
 
  }
 
 
}
 
</source>
 
</div>
 
 
 
== Specifying the MetadataSource ==
 
 
To use a '''MetadataSource''' in creating a '''JAXBContext''', add it to the '''properties''' map with the key '''JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY''':
 
 
<div style="width:750px">
 
<source lang="java">
 
MetadataSource metadataSource = new MyMetadataSourceImpl();
 
 
Map<String, Object> properties = new HashMap<String, Object>(1);
 
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataSource);
 
 
JAXBContext jc = JAXBContext.newInstance(new Class[] { Customer.class }, properties);
 
</source>
 
</div>
 

Latest revision as of 11:03, 7 November 2012

Back to the top