Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/From sessionsxml"

m (Replacing page with ''''Warning This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink M...')
 
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink MOXy]'' for current information.'''
|info=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/jaxb/dynamic/DynamicJAXBContextFactory.html DynamicJAXBContextFactory]
+
}}
+
 
+
= Bootstrapping from EclipseLink sessions.xml =
+
 
+
Dynamic MOXy also supports bootstrapping from an EclipseLink '''Project''' specified in '''sessions.xml'''.
+
 
+
 
+
{{tip||The key thing to remember when bootstrapping from an EclipseLink project is that your project will only specify Java class ''names'', and not actual Java classes. Keep in mind that you are mapping "imaginary" classes to XML, and these classes will be dynamically generated in memory when the '''DynamicJAXBContext''' is built.<br><br>
+
 
+
<sub>'''CORRECT:'''</sub>
+
<source lang="java">
+
customerDescriptor.setJavaClassName("example.Customer");
+
...
+
addressMapping.setReferenceClassName("example.Address");
+
</source>
+
 
+
<sub>'''INCORRECT:'''</sub>
+
<source lang="java">
+
customerDescriptor.setJavaClass(example.Customer.class);
+
...
+
addressMapping.setReferenceClass(example.Address.class);
+
</source>}}
+
 
+
 
+
Once you have your EclipseLink '''Project''', you will need to include it in your EclipseLink '''sessions.xml''' file (for more information on '''sessions.xml''' please see the EclipseLink documentation). (*** TODO: Add Link Here ***)  You can then pass the '''Session's''' name to '''DynamicJAXBContextFactory''' to create your '''DynamicJAXBContext''' using the following API:
+
 
+
<source lang="java">
+
/**
+
* Create a '''DynamicJAXBContext''', using an EclipseLink '''sessions.xml''' as the metadata source.
+
* The '''sessionNames''' parameter is a colon-delimited list of session names within the
+
* '''sessions.xml''' file.  '''Descriptors''' in this session's '''Project''' must <i>not</i>
+
* have '''javaClass''' set, but <i>must</i> have '''javaClassName''' set.
+
*
+
* @param sessionNames
+
*      A colon-delimited '''String''' specifying the session names from the '''sessions.xml''' file.
+
* @param classLoader
+
*      The application's current class loader, which will be used to first lookup
+
*      classes to see if they exist before new '''DynamicTypes''' are generated.  Can be
+
*      '''null''', in which case '''Thread.currentThread().getContextClassLoader()''' will be used.
+
* @param properties
+
*      Map of properties to use when creating a new '''DynamicJAXBContext'''.  Can be null.
+
*
+
* @return
+
*      A new instance of '''DynamicJAXBContext'''.
+
*
+
* @throws JAXBException
+
*      if an error was encountered while creating the '''DynamicJAXBContext'''.
+
*/
+
public static DynamicJAXBContext createContext(String sessionNames, ClassLoader classLoader,
+
  Map<String, ?> properties) throws JAXBException
+
</source>
+
 
+
=== Example ===
+
 
+
Here is an example of an EclipseLink MOXy '''Project''' in code.  Notice that we have customizations that are made:
+
 
+
* We have specified that the package name of the generated classes should be '''com.mypackage'''.  If importing from XSD, we would have used the default namespace to build the package name instead, which would result in a package name of '''example'''.
+
* We have specified that the '''first-name''' element in XML should be mapped to a field called '''fName''' in Java.  If importing from XSD we would have generated a field called '''firstName'''.
+
* We have specified that a null '''address''' will be represented by '''xsi:nil="true"''' in XML.
+
 
+
<source lang="java">
+
package example;
+
 
+
import org.eclipse.persistence.oxm.NamespaceResolver;
+
import org.eclipse.persistence.oxm.XMLConstants;
+
import org.eclipse.persistence.oxm.XMLDescriptor;
+
 
+
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
+
import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping;
+
 
+
public class CustomerProject extends org.eclipse.persistence.sessions.Project {
+
 
+
  public CustomerProject() {
+
      super();
+
 
+
      NamespaceResolver nsResolver = new NamespaceResolver();
+
      nsResolver.put("ns0", "example");
+
      nsResolver.put("xsi", XMLConstants.SCHEMA_INSTANCE_URL);
+
 
+
      XMLDescriptor customerDescriptor = new XMLDescriptor();
+
      customerDescriptor.setJavaClassName("com.mypackage.Customer");
+
      customerDescriptor.setDefaultRootElement("customer");
+
      customerDescriptor.setNamespaceResolver(nsResolver);
+
 
+
      XMLDirectMapping firstNameMapping = new XMLDirectMapping();
+
      firstNameMapping.setAttributeName("fName");
+
      firstNameMapping.setXPath("first-name/text()");
+
      customerDescriptor.addMapping(firstNameMapping);
+
      // ...
+
 
+
      XMLCompositeObjectMapping addressMapping = new XMLCompositeObjectMapping();
+
      addressMapping.setAttributeName("address");
+
      addressMapping.setXPath("address");
+
      addressMapping.setReferenceClassName("example.Address");
+
      addressMapping.getNullPolicy().setNullRepresentedByXsiNil(true);
+
      addressMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
+
      customerDescriptor.addMapping(addressMapping);
+
 
+
      XMLDescriptor addressDescriptor = new XMLDescriptor();
+
      addressDescriptor.setJavaClassName("com.mypackage.Address");
+
      // ...
+
 
+
      this.addDescriptor(customerDescriptor);
+
      this.addDescriptor(addressDescriptor);
+
  }
+
 
+
}
+
</source>
+
 
+
Next, here is an example '''sessions.xml''' that includes our '''Project''':
+
 
+
<source lang="xml">
+
<?xml version="1.0" encoding="US-ASCII"?>
+
<sessions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...>
+
  <session xsi:type="database-session">
+
      <name>MyCustomerProject</name>
+
      <primary-project xsi:type="class">example.CustomerProject</primary-project>
+
  </session>
+
</sessions>
+
</source>
+
 
+
Finally, we can now instantiate a '''DynamicJAXBContext''' using the session name, and begin working with our '''DynamicEntities''':
+
 
+
<source lang="java">
+
DynamicJAXBContext dContext = DynamicJAXBContextFactory.createContext("MyCustomerProject", null, null);
+
 
+
DynamicEntity newCustomer = dContext.newDynamicEntity("com.mypackage.Customer");
+
newCustomer.set("fName", "Bob");
+
...
+
dContext.createMarshaller().marshal(newCustomer, System.out);
+
</source>
+
 
+
{{tip||You may specify multiple '''Session''' names when bootstrapping from an EclipseLink '''Project'''. For example,
+
 
+
<source lang="java">
+
DynamicJAXBContextFactory.createContext("ProjectA:ProjectB", null, null);
+
</source>
+
 
+
would create a single '''DynamicJAXBContext''' that is aware of '''Mappings''' and '''Descriptors''' from both '''Projects'''.}}
+

Latest revision as of 13:17, 30 January 2013

Warning This page is obsolete. Please see Developing JAXB Applications Using EclipseLink MOXy for current information.