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 "EclipseLink/UserGuide/MOXy/Runtime/XML MetadataSource"
Line 126: | Line 126: | ||
== Building XmlBindings Programatically == | == Building XmlBindings Programatically == | ||
− | You also have the option of building your own '''XmlBindings''' object from scratch in code. | + | You also have the option of building your own '''XmlBindings''' object from scratch in code. The example below modifies the '''pCode''' field of the '''Address''' class to use a locale-specific name: |
<div style="width:850px"> | <div style="width:850px"> | ||
Line 132: | Line 132: | ||
package example; | package example; | ||
− | import java. | + | import java.util.Locale; |
import java.util.Map; | import java.util.Map; | ||
− | |||
− | |||
import org.eclipse.persistence.jaxb.metadata.MetadataSourceAdapter; | import org.eclipse.persistence.jaxb.metadata.MetadataSourceAdapter; | ||
+ | import org.eclipse.persistence.jaxb.xmlmodel.JavaType; | ||
+ | import org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes; | ||
+ | import org.eclipse.persistence.jaxb.xmlmodel.ObjectFactory; | ||
import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings; | import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings; | ||
+ | import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes; | ||
+ | import org.eclipse.persistence.jaxb.xmlmodel.XmlElement; | ||
+ | |||
+ | public class AddressMetadataSource extends MetadataSourceAdapter { | ||
+ | |||
+ | private ObjectFactory objectFactory; | ||
+ | private XmlBindings xmlBindings; | ||
+ | |||
+ | public AddressMetadataSource() { | ||
+ | objectFactory = new ObjectFactory(); | ||
+ | |||
+ | xmlBindings = new XmlBindings(); | ||
+ | xmlBindings.setPackageName("otherpackage"); | ||
+ | xmlBindings.setJavaTypes(new JavaTypes()); | ||
+ | } | ||
− | public | + | @Override |
+ | public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) { | ||
+ | JavaType javaType = new JavaType(); | ||
+ | javaType.setName("Address"); | ||
+ | javaType.setJavaAttributes(new JavaAttributes()); | ||
− | + | XmlElement pCodeElement = new XmlElement(); | |
− | + | pCodeElement.setJavaAttribute("pCode"); | |
+ | pCodeElement.setType("java.lang.String"); | ||
− | + | if (Locale.getDefault().getCountry() == Locale.US.getCountry()) { | |
− | + | pCodeElement.setName("zip-code"); | |
+ | } else if (Locale.getDefault().getCountry() == Locale.UK.getCountry()) { | ||
+ | pCodeElement.setName("post-code"); | ||
+ | } else if (Locale.getDefault().getCountry() == Locale.CANADA.getCountry()) { | ||
+ | pCodeElement.setName("postal-code"); | ||
+ | } | ||
− | + | javaType.getJavaAttributes().getJavaAttribute().add(objectFactory.createXmlElement(pCodeElement)); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | xmlBindings.getJavaTypes().getJavaType().add(javaType); | |
− | + | return xmlBindings; | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</source> | </source> | ||
</div> | </div> |
Revision as of 11:19, 8 July 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Contents
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, 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:
package example; import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings; ... JAXBContext xmlBindingsContext = JAXBContext.newInstance("org.eclipse.persistence.jaxb.xmlmodel"); FileReader bindingsFile = new FileReader("xml-bindings.xml"); XmlBindings bindings = (XmlBindings) xmlBindingsContext.createUnmarshaller().unmarshal(bindingsFile);
Example
The following example creates an XmlBindings object by unmarshalling from a URL:
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.example.com/private/metadata/xml-bindings.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); } } }
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 MyMetadataSource(); 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);
Building XmlBindings Programatically
You also have the option of building your own XmlBindings object from scratch in code. The example below modifies the pCode field of the Address class to use a locale-specific name:
package example; import java.util.Locale; import java.util.Map; import org.eclipse.persistence.jaxb.metadata.MetadataSourceAdapter; import org.eclipse.persistence.jaxb.xmlmodel.JavaType; import org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes; import org.eclipse.persistence.jaxb.xmlmodel.ObjectFactory; import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings; import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes; import org.eclipse.persistence.jaxb.xmlmodel.XmlElement; public class AddressMetadataSource extends MetadataSourceAdapter { private ObjectFactory objectFactory; private XmlBindings xmlBindings; public AddressMetadataSource() { objectFactory = new ObjectFactory(); xmlBindings = new XmlBindings(); xmlBindings.setPackageName("otherpackage"); xmlBindings.setJavaTypes(new JavaTypes()); } @Override public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) { JavaType javaType = new JavaType(); javaType.setName("Address"); javaType.setJavaAttributes(new JavaAttributes()); XmlElement pCodeElement = new XmlElement(); pCodeElement.setJavaAttribute("pCode"); pCodeElement.setType("java.lang.String"); if (Locale.getDefault().getCountry() == Locale.US.getCountry()) { pCodeElement.setName("zip-code"); } else if (Locale.getDefault().getCountry() == Locale.UK.getCountry()) { pCodeElement.setName("post-code"); } else if (Locale.getDefault().getCountry() == Locale.CANADA.getCountry()) { pCodeElement.setName("postal-code"); } javaType.getJavaAttributes().getJavaAttribute().add(objectFactory.createXmlElement(pCodeElement)); xmlBindings.getJavaTypes().getJavaType().add(javaType); return xmlBindings; } }