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 "EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single Project/From sessions.xml using DynamicEntities"

m
m
Line 3: Line 3:
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=MOXy
 
|eclipselinktype=MOXy
}}
+
}}  
= Dynamic Bootstrapping (Single Project) from sessions.xml (EclipseLink project) =
+
With EclipseLink dynamic MOXy, you can bootstrapping from an EclipseLink project specified in '''sessions.xml''' file. In this scenario, your specifies only Java ''class names'' -- not the actual Java classes, as shown here:
+
  
'''CORRECT:'''
+
= Dynamic Bootstrapping (Single Project) from sessions.xml (EclipseLink project)  =
<source lang="java">
+
 
 +
With EclipseLink dynamic MOXy, you can bootstrapping from an EclipseLink project specified in '''sessions.xml''' file. In this scenario, your specifies only Java ''class names'' -- not the actual Java classes, as shown here:
 +
 
 +
'''CORRECT:''' <source lang="java">
 
customerDescriptor.setJavaClassName("mynamespace.Customer");
 
customerDescriptor.setJavaClassName("mynamespace.Customer");
 
...
 
...
 
addressMapping.setReferenceClassName("mynamespace.Address");
 
addressMapping.setReferenceClassName("mynamespace.Address");
</source>
+
</source>  
  
'''INCORRECT:'''
+
'''INCORRECT:''' &lt;sorce lang="java"&gt; customerDescriptor.setJavaClass(mynamespace.Customer.class); ... addressMapping.setReferenceClass(mynamespace.Address.class); &lt;/source&gt;
<sorce lang="java">
+
customerDescriptor.setJavaClass(mynamespace.Customer.class);
+
...
+
addressMapping.setReferenceClass(mynamespace.Address.class);
+
</source>
+
  
You map these "imaginary" classes to XML. EclipseLink dynamically generates the classes in memory, when buildingn the '''DynamicJAXBContext'''.
+
You map these "imaginary" classes to XML. EclipseLink dynamically generates the classes in memory, when buildingn the '''DynamicJAXBContext'''.  
  
Add your EclipseLink project to the EclipseLink '''sessions.xml''' file. Use the following API to pass the session's name to the '''DynamicJAXBContextFactory''' and create your '''DynamicJAXBContext''':
+
Add your EclipseLink project to the EclipseLink '''sessions.xml''' file. Use the following API to pass the session's name to the '''DynamicJAXBContextFactory''' and create your '''DynamicJAXBContext''':  
  
 
<source lang="Java">
 
<source lang="Java">
Line 50: Line 46:
 
public static DynamicJAXBContext createContext(String sessionNames, ClassLoader classLoader,  
 
public static DynamicJAXBContext createContext(String sessionNames, ClassLoader classLoader,  
 
   Map<String, ?> properties) throws JAXBException
 
   Map<String, ?> properties) throws JAXBException
</source>
+
</source>  
 +
 
 +
<br>
 +
 
 +
== Example ==
  
 +
This example shows an EclipseLink MOXy project in code. Notice that:
  
==Example==
+
*The package name of the generated classes will be '''com.mypackage'''.  
This example shows an EclipseLink MOXy project in code. Notice that:
+
**If you were importing from an XSD, you would have used the default namespace to build the package, resulting in '''mynamespace''' instead.  
*The package name of the generated classes will be '''com.mypackage'''.
+
*The '''first-name''' element in XML will be mapped to the '''fName''' field in Java  
** If you were importing from an XSD, you would have used the default namespace to build the package, resulting in '''mynamespace''' instead.
+
**If you were importing from an XSD, the generated field name would have been '''firstName'''.  
*The '''first-name''' element in XML will be mapped to the '''fName''' field in Java
+
*A null address will be represented by '''xsi:nil="true"''' in XML.
** If you were importing from an XSD, the generated field name would have been '''firstName'''.
+
*A null address will be represented by '''xsi:nil="true"''' in XML.  
+
  
 
<source lang="Java">
 
<source lang="Java">
Line 109: Line 108:
 
}
 
}
  
</source>
+
</source>  
  
Here is the '''sessions.xml'' that includes the project:
+
Here is the '''sessions.xml'' that includes the project:  
  
 
<source lang="xml">
 
<source lang="xml">
Line 121: Line 120:
 
   </session>
 
   </session>
 
</sessions>
 
</sessions>
</source>
+
</source>  
  
Now, you instantiate a '''DynamicJAXBContext''' using the session name and begin working with the '''DynamicEntities''':
+
Now, you instantiate a '''DynamicJAXBContext''' using the session name and begin working with the '''DynamicEntities''':  
  
 
<source lang="java">
 
<source lang="java">
Line 132: Line 131:
 
...
 
...
 
dContext.createMarshaller().marshal(newCustomer, System.out);
 
dContext.createMarshaller().marshal(newCustomer, System.out);
</source>
+
</source>  
  
 
+
<br> {{tip||You can specify multiple '''Session''' names when bootstrapping from an EclipseLink project, by using:
{{tip||You can specify multiple '''Session''' names when bootstrapping from an EclipseLink project, by using:
+
  
 
<source lang="java">
 
<source lang="java">
Line 141: Line 139:
 
</source>
 
</source>
  
This creates a single <tt>DynamicJAXBContext</tt> that is aware of <tt>Mappings</tt> and <tt>Descriptors</tt> from both <tt>Projects</tt>.}}
+
This creates a single <tt>DynamicJAXBContext</tt> that is aware of <tt>Mappings</tt> and <tt>Descriptors</tt> from both <tt>Projects</tt>.}}  
 
+
  
{{EclipseLink_MOXy
+
<br> {{EclipseLink_MOXy
 
|next    = [[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Multiple_Project|From multiple projects]]
 
|next    = [[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Multiple_Project|From multiple projects]]
 
|previous =[[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project/From_OXM_using_DynamicEntities|From OXM]]
 
|previous =[[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project/From_OXM_using_DynamicEntities|From OXM]]
 
|up      =[[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project_Dynamic|From a single project]]
 
|up      =[[EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Single_Project_Dynamic|From a single project]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Revision as of 11:14, 5 April 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


Dynamic Bootstrapping (Single Project) from sessions.xml (EclipseLink project)

With EclipseLink dynamic MOXy, you can bootstrapping from an EclipseLink project specified in sessions.xml file. In this scenario, your specifies only Java class names -- not the actual Java classes, as shown here:

CORRECT:
customerDescriptor.setJavaClassName("mynamespace.Customer");
...
addressMapping.setReferenceClassName("mynamespace.Address");

INCORRECT: <sorce lang="java"> customerDescriptor.setJavaClass(mynamespace.Customer.class); ... addressMapping.setReferenceClass(mynamespace.Address.class); </source>

You map these "imaginary" classes to XML. EclipseLink dynamically generates the classes in memory, when buildingn the DynamicJAXBContext.

Add your EclipseLink project to the EclipseLink sessions.xml file. Use the following API to pass the session's name to the DynamicJAXBContextFactory and create your DynamicJAXBContext:

/**
 * Create a <tt>DynamicJAXBContext</tt>, using an EclipseLink <tt>sessions.xml</tt> as the metadata source.
 * The <tt>sessionNames</tt> parameter is a colon-delimited list of session names within the
 * <tt>sessions.xml</tt> file.  <tt>Descriptors</tt> in this session's <tt>Project</tt> must <i>not</i>
 * have <tt>javaClass</tt> set, but <i>must</i> have <tt>javaClassName</tt> set.
 *
 * @param sessionNames
 *      A colon-delimited <tt>String</tt> specifying the session names from the <tt>sessions.xml</tt> file.
 * @param classLoader
 *      The application's current class loader, which will be used to first lookup
 *      classes to see if they exist before new <tt>DynamicTypes</tt> are generated.  Can be
 *      <tt>null</tt>, in which case <tt>Thread.currentThread().getContextClassLoader()</tt> will be used.
 * @param properties
 *      Map of properties to use when creating a new <tt>DynamicJAXBContext</tt>.  Can be null.
 *
 * @return
 *      A new instance of <tt>DynamicJAXBContext</tt>.
 *
 * @throws JAXBException
 *      if an error was encountered while creating the <tt>DynamicJAXBContext</tt>.
 */
public static DynamicJAXBContext createContext(String sessionNames, ClassLoader classLoader, 
   Map<String, ?> properties) throws JAXBException


Example

This example shows an EclipseLink MOXy project in code. Notice that:

  • The package name of the generated classes will be com.mypackage.
    • If you were importing from an XSD, you would have used the default namespace to build the package, resulting in mynamespace instead.
  • The first-name element in XML will be mapped to the fName field in Java
    • If you were importing from an XSD, the generated field name would have been firstName.
  • A null address will be represented by xsi:nil="true" in XML.
package mynamespace;
 
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", "mynamespace");
      nsResolver.put("xsi", XMLConstants.SCHEMA_INSTANCE_URL);
 
      XMLDescriptor customerDescriptor = new XMLDescriptor();
      customerDescriptor.setJavaClassName("mynamespace.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("mynamespace.Address");
      addressMapping.getNullPolicy().setNullRepresentedByXsiNil(true);
      addressMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
      customerDescriptor.addMapping(addressMapping);
 
      XMLDescriptor addressDescriptor = new XMLDescriptor();
      addressDescriptor.setJavaClassName("mynamespace.Address");
      // ...
 
      this.addDescriptor(customerDescriptor);
      this.addDescriptor(addressDescriptor);
   }
 
}

Here is the 'sessions.xml that includes the project:

<?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">mynamespace.CustomerProject</primary-project>
   </session>
</sessions>

Now, you instantiate a DynamicJAXBContext using the session name and begin working with the DynamicEntities:

DynamicJAXBContext dContext = DynamicJAXBContextFactory.createContext("MyCustomerProject", null, null);
 
DynamicEntity newCustomer = dContext.newDynamicEntity("com.mypackage.Customer");
newCustomer.set("fName", "Bob");
...
dContext.createMarshaller().marshal(newCustomer, System.out);

Idea.png
You can specify multiple Session names when bootstrapping from an EclipseLink project, by using:
DynamicJAXBContextFactory.createContext("ProjectA:ProjectB", null, null);
This creates a single DynamicJAXBContext that is aware of Mappings and Descriptors from both Projects.



Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top