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.
Difference between revisions of "User:Rick.barkhouse.oracle.com/Test2"
Line 74: | Line 74: | ||
public class MyMetadataSource extends MetadataSourceAdapter { | public class MyMetadataSource extends MetadataSourceAdapter { | ||
+ | |||
+ | private JAXBContext xmlBindingsContext; | ||
+ | private final String xmlBindingsPackage = "org.eclipse.persistence.jaxb.xmlmodel"; | ||
+ | private final URL bindingsUrl; | ||
+ | |||
+ | public MyMetadataSource() { | ||
+ | try { | ||
+ | xmlBindingsContext = JAXBContext.newInstance(xmlBindingsPackage); | ||
+ | bindingsUrl = new URL("http://www.example.com/metadata/oxm.xml"); | ||
+ | } catch (Exception e) { throw new RuntimeException(e); } | ||
+ | } | ||
@Override | @Override | ||
public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) { | public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) { | ||
try { | try { | ||
− | |||
− | |||
− | |||
XmlBindings bindings = (XmlBindings) ctx.createUnmarshaller().unmarshal(bindingsUrl); | XmlBindings bindings = (XmlBindings) ctx.createUnmarshaller().unmarshal(bindingsUrl); | ||
return bindings; | return bindings; | ||
− | } catch (Exception e) { | + | } catch (Exception e) { throw new RuntimeException(e); } |
− | + | ||
− | + | ||
} | } | ||
Revision as of 13:24, 7 July 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Contents
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:
/** * 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);
XmlBindings
Internally, EclipseLink metadata is stored in an XmlBindings object tree, which itself is mapped with JAXB. This means that you can actually use a JAXBUnmarshaller to read external metadata and create an XmlBindings from it:
... JAXBContext xmlBindingsContext = JAXBContext.newInstance("org.eclipse.persistence.jaxb.xmlmodel"); FileReader bindingsFile = new FileReader("eclipselink-oxm.xml"); XmlBindings bindings = (XmlBindings) xmlBindingsContext.createUnmarshaller().unmarshal(bindingsFile); ...
Example
The following example retrieves metadata XML from a URL, and then creates an XmlBindings object by unmarshalling from the URL using the "" package.
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 xmlBindingsContext; private final String xmlBindingsPackage = "org.eclipse.persistence.jaxb.xmlmodel"; private final URL bindingsUrl; public MyMetadataSource() { try { xmlBindingsContext = JAXBContext.newInstance(xmlBindingsPackage); bindingsUrl = new URL("http://www.example.com/metadata/oxm.xml"); } catch (Exception e) { throw new RuntimeException(e); } } @Override public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) { try { XmlBindings bindings = (XmlBindings) ctx.createUnmarshaller().unmarshal(bindingsUrl); return bindings; } catch (Exception e) { throw new RuntimeException(e); } } }
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:
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);