camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Dozer Type Conversion
Date Thu, 28 Jan 2010 04:57:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Dozer+Type+Conversion">Dozer
Type Conversion</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~sgargan">Stephen
Gargan</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h1><a name="DozerTypeConversion-DozerTypeConversion."></a>Dozer
Type Conversion.</h1>

<p><a href="http://dozer.sourceforge.net/documentation/about.html" rel="nofollow">Dozer</a>
is a fast and flexible framework for mapping back and forth between Java Beans. Coupled with
Camel's automatic type conversion, it's a formidable tool for dealing object to object mapping
headaches that crop up in enterprise integration projects. </p>

<p>To explain how Dozer can be uses within Camel we'll use the following example of
a simple Customer Support Service. The initial version of the Service defined a 'Customer'
object used with a very flat structure. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Legacy Customer Service Class</b></div><div
class="codeContent panelContent">
<pre class="code-java">

<span class="code-keyword">public</span> class Customer {
    <span class="code-keyword">private</span> <span class="code-object">String</span>
firstName;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
lastName;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
street;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
zip;

    <span class="code-keyword">public</span> Customer() {}

    <span class="code-keyword">public</span> Customer(<span class="code-object">String</span>
firstName, <span class="code-object">String</span> lastName, <span class="code-object">String</span>
zip, <span class="code-object">String</span> street) {
        <span class="code-keyword">this</span>.firstName = firstName;
        <span class="code-keyword">this</span>.lastName = lastName;
        <span class="code-keyword">this</span>.zip = zip;
        <span class="code-keyword">this</span>.street = street;
    }

    <span class="code-keyword">public</span> <span class="code-object">String</span>
getFirstName() {
        <span class="code-keyword">return</span> firstName;
    }

    <span class="code-keyword">public</span> void setFirstName(<span class="code-object">String</span>
firstName) {
        <span class="code-keyword">this</span>.firstName = firstName;
    }

    ... getters and setters <span class="code-keyword">for</span> each field
</pre>
</div></div>

<p>In the next version it was decided to structure the data better in the model by moving
the address data into its own type, with the resultin domain object ending up looking like</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Next Gen Customer object</b></div><div
class="codeContent panelContent">
<pre class="code-java">

<span class="code-keyword">public</span> class Customer {
    <span class="code-keyword">private</span> <span class="code-object">String</span>
firstName;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
lastName;
    <span class="code-keyword">private</span> Address address;

    <span class="code-keyword">public</span> Customer() {}

    <span class="code-keyword">public</span> Customer(<span class="code-object">String</span>
firstName, <span class="code-object">String</span> lastName, Address address)
{
        <span class="code-keyword">this</span>.firstName = firstName;
        <span class="code-keyword">this</span>.lastName = lastName;
        <span class="code-keyword">this</span>.address = address;
    }
    ....


<span class="code-keyword">public</span> class Address {
    <span class="code-keyword">private</span> <span class="code-object">String</span>
zipCode;
    <span class="code-keyword">private</span> <span class="code-object">String</span>
streetName;

    <span class="code-keyword">public</span> Address() {}

    <span class="code-keyword">public</span> Address(<span class="code-object">String</span>
zipCode, <span class="code-object">String</span> streetName) {
        <span class="code-keyword">this</span>.zipCode = zipCode;
        <span class="code-keyword">this</span>.streetName = streetName;
    }
</pre>
</div></div> 

<p>Much nicer! But as often occurs, the previous version of the service, with the old
flat 'Customer' object, was in production with a client and the project must support the legacy
interface. To support both versions, we must add a mechanism to convert between the old Customer
service type and the new Customer domain type and back again. It would be a simple matter
to write a custom converter class to map between them, but this may not be the only service/domain
inconsistency and these tedious and error prone custom mappings could quickly start to add
up, and bugs with them.</p>

<p>To a large extent the two object share identical structure, with only the address
representation being different. It would be very helpful if there were a practical way to
to automate this kind of mapping, such that the similar properties could get mapped automatically
and only the inconsistencies requiring custom mapping.</p>

<p>This is where Dozer comes in; It uses reflection to map data between two bean types
using a set of simple mapping rules. Where no rule is specified, dozer will attempt to map
between them by using matching properties of two beans. In this way focus can be given to
the inconsistencies between the beans i.e. the address properties, knowing that dozer will
automatically match and convert the others.</p>

<h2><a name="DozerTypeConversion-ConfiguringDozer"></a>Configuring Dozer</h2>

<p>Dozer's configuration is extremely flexible and many mapping scenarios are covered
<a href="http://dozer.sourceforge.net/documentation/mappings.html" rel="nofollow">here</a>.
For our simple example, the configuration looks like the following.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
&lt;mappings xmlns=<span class="code-quote">"http://dozer.sourceforge.net"</span>
<span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
  xsi:schemaLocation=<span class="code-quote">"http://dozer.sourceforge.net  http://dozer.sourceforge.net/schema/beanmapping.xsd"</span>&gt;
  <span class="code-tag">&lt;mapping&gt;</span>
    <span class="code-tag">&lt;class-a&gt;</span>org.apache.camel.converter.dozer.service.Customer<span
class="code-tag">&lt;/class-a&gt;</span>
    <span class="code-tag">&lt;class-b&gt;</span>org.apache.camel.converter.dozer.model.Customer<span
class="code-tag">&lt;/class-b&gt;</span>
    <span class="code-tag">&lt;field&gt;</span>
      <span class="code-tag">&lt;a&gt;</span>street<span class="code-tag">&lt;/a&gt;</span>
      <span class="code-tag">&lt;b&gt;</span>address.streetName<span
class="code-tag">&lt;/b&gt;</span>
    <span class="code-tag">&lt;/field&gt;</span>
    <span class="code-tag">&lt;field&gt;</span>
      <span class="code-tag">&lt;a&gt;</span>zip<span class="code-tag">&lt;/a&gt;</span>
      <span class="code-tag">&lt;b&gt;</span>address.zipCode<span class="code-tag">&lt;/b&gt;</span>
    <span class="code-tag">&lt;/field&gt;</span>
  <span class="code-tag">&lt;/mapping&gt;</span>
<span class="code-tag">&lt;/mappings&gt;</span>
</pre>
</div></div>

<h2><a name="DozerTypeConversion-SupportforDozerinCamel"></a>Support for
Dozer in Camel</h2>

<p>Camel provides a simple mechanism to integrate Dozer Mappers with it's own powerful
<a href="http://camel.apache.org/type-converter.html" rel="nofollow">Type Conversion</a>
framework. Its configured by creating an instance of 'DozerTypeConverterLoader' providing
it the camel context and an optional Dozer mapper. If no mapper is supplied,  Camel's registry
will be searched for suitable instances. The loader will query the Dozer Mapper for the the
types it converts and a register them with Camel's Conversionn framework to be handled by
the mapper.</p>

<p>In Java it can be configured as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
DozerBeanMapper mapper = <span class="code-keyword">new</span> DozerBeanMapper(Arrays.asList(<span
class="code-keyword">new</span> <span class="code-object">String</span>[]{<span
class="code-quote">"mapping.xml"</span>}));
<span class="code-keyword">new</span> DozerTypeConverterLoader(camelContext, mapper);
</pre>
</div></div>

<p>Or in Spring</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">

<span class="code-tag"><span class="code-comment">&lt;!-- the registry will
be scannd and 'mapper' below will be found and installed --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"dozerConverterLoader"</span>
 class=<span class="code-quote">"org.apache.camel.converter.dozer.DozerTypeConverterLoader"</span>
/&gt;</span>
  
<span class="code-tag">&lt;bean id=<span class="code-quote">"mapper"</span>
class=<span class="code-quote">"org.dozer.DozerBeanMapper"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"mappingFiles"</span>&gt;</span>
    <span class="code-tag">&lt;list&gt;</span>
      <span class="code-tag">&lt;value&gt;</span>mapping.xml<span class="code-tag">&lt;/value&gt;</span>
    <span class="code-tag">&lt;/list&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>

<p>Now, where necessary, Camel will use Dozer to do conversions; In our case between
the new domain and legacy Customer types e.g. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// given the following route
</span>from(<span class="code-quote">"direct:legacy-service-in"</span>).bean(<span
class="code-keyword">new</span> CustomerProcessor());

<span class="code-comment">// and a processor
</span>
<span class="code-keyword">public</span> class CustomerProcessor {

    <span class="code-keyword">public</span> Customer processCustomer(org.apache.camel.converter.dozer.model.Customer
customer) {
       ...
    }
}

<span class="code-comment">// service objects can be sent to the processor and automagically
converted by Camel &amp; Dozer
</span>template.sendBody(<span class="code-quote">"direct:legacy-service-in"</span>,<span
class="code-keyword">new</span> org.apache.camel.converter.dozer.service.Customer(<span
class="code-quote">"Bob"</span>, <span class="code-quote">"Roberts"</span>,
<span class="code-quote">"12345"</span>, <span class="code-quote">"1 Main
st."</span>));

</pre>
</div></div>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Dozer+Type+Conversion">View
Online</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Dozer+Type+Conversion?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message