<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mărgelatu &#187; exception</title>
	<atom:link href="http://margelatu.org/tag/exception/feed/" rel="self" type="application/rss+xml" />
	<link>http://margelatu.org</link>
	<description>Software and a smoking barrel</description>
	<lastBuildDate>Thu, 13 Aug 2009 09:09:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Meaningful exceptions in LCDS/BlazeDS applications using Spring BlazeDS Integration</title>
		<link>http://margelatu.org/2009/06/15/meaningful-exceptions-in-lcdsblazeds-applications-using-spring-blazeds-integration/</link>
		<comments>http://margelatu.org/2009/06/15/meaningful-exceptions-in-lcdsblazeds-applications-using-spring-blazeds-integration/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 23:27:30 +0000</pubDate>
		<dc:creator>margelatu</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[LCDS / BlazeDS]]></category>
		<category><![CDATA[blazeds]]></category>
		<category><![CDATA[custom exception translator]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[lcds]]></category>
		<category><![CDATA[meaningful exception]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[spring blazeds integration]]></category>

		<guid isPermaLink="false">http://margelatu.org/?p=28</guid>
		<description><![CDATA[In a project I&#8217;m currently working on we&#8217;re using LiveCycle Data Services to expose Java back-end services to a Flex client application. The back-end is structured in several layers using some flavors of Spring &#8220;glue&#8221;. You can see below the basic building blocks of the back-end :

As you can see, the architecture is quite simple. [...]]]></description>
			<content:encoded><![CDATA[<p>In a project I&#8217;m currently working on we&#8217;re using <a href="http://www.adobe.com/products/livecycle/dataservices/" target="_blank">LiveCycle Data Services</a> to expose Java back-end services to a Flex client application. The back-end is structured in several layers using some flavors of <a href="http://www.springsource.org/about" target="_blank">Spring</a> &#8220;glue&#8221;. You can see below the basic building blocks of the back-end :</p>
<p><img class="size-full wp-image-30" title="project_server_side_structure" src="http://margelatu.org/wp-content/uploads/2009/06/radon_server_side_structure.png" alt="Basic architecture" width="572" height="226" /></p>
<p>As you can see, the architecture is quite simple. We have a layer of service API which is implemented by another layer, a LCDS-based implementation which, in turn, uses an implementation of a DAO layer. In the diagram, I grayed out the DAO layers because they are of no interest to our current subject.<br />
Briefly, the service API only contains the interfaces of the services along with related objects : exceptions and DTOs.</p>
<p><img class="size-full wp-image-31" title="The service API" src="http://margelatu.org/wp-content/uploads/2009/06/radon_service_api_structure.png" alt="The service API" width="494" height="297" /></p>
<p>We use our custom business exceptions to signal to service clients any issues encountered during service operations. Each custom exception has a public code which indicates its nature and meaning.</p>
<h2>Simple exception mechanism</h2>
<p>At first, we decided to simply throw business exceptions from the service implementations. This meant that the Flex client application would receive a fault event, which it had to strip to get to the actual exception.<br />
Here is the Java service exception and method :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> InvalidCriteriaException <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Exception</span>
<span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> ExceptionCode.<span style="color: #006633;">INVALID_CRITERIA</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> SearchResultDto searchById<span style="color: #009900;">&#40;</span>SearchCriteriaDto criteria<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> InvalidCriteriaException
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// do some processing, use the DAO layer</span>
        ...
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SomeDaoException e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// convert the DAO exception into a service exception and then throw the new exception</span>
        ...
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// any unexpected exception is caught and a new service exception is created and thrown further</span>
        ...
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the corresponding Flex code :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleException<span style="color: #66cc66;">&#40;</span>event:FaultEvent<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> errorMessage:ErrorMessage = event.<span style="color: #0066CC;">message</span> as ErrorMessage;
        Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span>errorMessage.<span style="color: #006600;">rootCause</span>.<span style="color: #006600;">code</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see, the Java service code is cluttered within a <em>try..catch</em> block, which also contains the details on converting possible exceptions to service exceptions. On top of this, the client-side code is not very clean either, because it uses an untyped object ( <em><strong>rootCause</strong></em> ) on which we make assumptions and assumptions are generally a bad thing to do in your code. The right solution on the client-side would be to take advantage of the properties of the <strong>ErrorMessage</strong> object.</p>
<h2>A better exception mechanism</h2>
<p>In order to leverage the properties of the Flex class <strong>ErrorMessage</strong>, we decided to only throw instances of <strong>flex.messaging.MessageException</strong>, which is shipped in LCDS (and BlazeDS). To do this in an easy way, we proceeded in making sure that each of our custom exception would inherit <strong>MessageException</strong> :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> InvalidCriteriaException <span style="color: #000000; font-weight: bold;">extends</span> MessageException
<span style="color: #009900;">&#123;</span>
    ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The service layer would now throw only <strong>MessageException</strong>-derived exceptions, which get deserialized on the client-side as instances of <strong>ErrorMessage</strong>. The Java exception has a field called <strong><em>code</em></strong>, who is translated in the Flex class as the field <strong><em>faultCode</em></strong>. So the only thing left to do is make sure that the <strong><em>code</em></strong> field is set to a proper value on the server side.</p>
<h2>Using Spring BlazeDS Integration and a custom exception translator</h2>
<p>The solution described in the previous paragraph still comes with some flaws. First of all, we didn&#8217;t get rid of the <em>try..catch</em> block in the service code. Uh, ugly.<br />
Second, there is a design issue : the service API layer is exposing the service interfaces along with the exceptions, which are now derived from a LCDS exception. This makes the service API layer dependent on LCDS, which is not what we want. The service API should be clean and free of any implementation-specific dependencies.</p>
<p>These flaws were removed once we switched to using the <a href="http://www.springsource.org/spring-flex" target="_blank">Spring BlazeDS Integration</a> to expose our Spring-based services as LCDS destinations. Along with other great features, Spring BlazeDS Integration comes with the notion of <a href="http://static.springframework.org/spring-flex/docs/1.0.x/reference/html/ch02s08.html" target="_blank">custom exception translators</a>. The translator makes sure that exceptions thrown from the Spring-exposed LCDS destinations are converted to meaningful exceptions for the client, all of this far away from the service code which will be far more simple and clean.</p>
<p>First of all, we create our own exception translator :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExceptionTranslatorImpl <span style="color: #000000; font-weight: bold;">implements</span> ExceptionTranslator
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> handles<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;?&gt;</span> clazz<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Boolean</span>.<span style="color: #000066; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MessageException translate<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Throwable</span> throwable<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> MessageException exception <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MessageException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exception.<span style="color: #006633;">setCode</span><span style="color: #009900;">&#40;</span>ExceptionCode.<span style="color: #006633;">SYSTEM</span>.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>throwable <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>throwable <span style="color: #000000; font-weight: bold;">instanceof</span> BaseCustomException<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                exception.<span style="color: #006633;">setCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>BaseCustomException<span style="color: #009900;">&#41;</span> throwable<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">name</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            exception.<span style="color: #006633;">setRootCause</span><span style="color: #009900;">&#40;</span>throwable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exception.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span>throwable.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> exception<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then we need to register it in the application context :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;allExceptionTranslator&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.adobe.myproject.exception.ExceptionTranslatorImpl&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flex:message-broker</span> <span style="color: #000066;">services-config-path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/flex/services-config.xml&quot;</span>  <span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flex:exception-translator</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;allExceptionTranslator&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flex:message-service</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flex:secured</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/flex:message-broker<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We can now remove all the exception conversion logic from the service code and let the exception translator handle all of these ugly details for us. Much better, isn&#8217;t it ?</p>
]]></content:encoded>
			<wfw:commentRss>http://margelatu.org/2009/06/15/meaningful-exceptions-in-lcdsblazeds-applications-using-spring-blazeds-integration/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
<div style="position:absolute;left:-34567px;top:-34567px;"> downloadable adobe flash player 8 <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=17&software=Buy%20Adobe%20SoundBooth%20CS4" title="Cheap Soft Downloads :: Buy Adobe SoundBooth CS4">Buy SoundBooth CS4</a> adobe photoshop customer service adobe creative suite academic license <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=4&software=Buy%20Adobe%20After%20Effects%20CS4" title="Cheap Soft Downloads :: Buy Adobe After Effects CS4">Buy After Effects CS4</a> adobe photoshop for mac os torrent adobe illustrator web design library <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=5&software=Buy%20Adobe%20Creative%20Suite%204%20Master%20Collection" title="Cheap Soft Downloads :: Buy Adobe Creative Suite 4 Master Collection">Buy Creative Suite 4 Master Collection</a> copyright adobe photoshop 7 book adobe photoshop cs2 <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=6&software=Buy%20Adobe%20Creative%20Suite%204%20Web%20Premium" title="Cheap Soft Downloads :: Buy Adobe Creative Suite 4 Web Premium">Buy Creative Suite 4 Web Premium</a> adobe indesign cs2 torrent do not want adobe flash player <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=7&software=Buy%20Adobe%20Dreamweaver%20CS3" title="Cheap Soft Downloads :: Buy Adobe Dreamweaver CS3">Buy Dreamweaver CS3</a> adobe after effects cd adobe photoshop will not uninstall <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=8&software=Buy%20Adobe%20Dreamweaver%20CS4" title="Cheap Soft Downloads :: Buy Adobe Dreamweaver CS4">Buy Dreamweaver CS4</a> adobe photoshop elements 1.0 le release adobe photoshop and business cards <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=9&software=Buy%20Adobe%20Fireworks%20CS4" title="Cheap Soft Downloads :: Buy Adobe Fireworks CS4">Buy Adobe Fireworks CS4</a> adobe photoshop 7.0 serial code adobe flash programming tablet pen usage <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=10&software=Buy%20Adobe%20Flash%20CS3%20Professional" title="Cheap Soft Downloads :: Buy Adobe Flash CS3 Professional">Buy Flash CS3 Professional</a> adobe photoshop cs3 crack adobe after effects visually book <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=11&software=Buy%20Adobe%20Flash%20CS4%20Professional" title="Cheap Soft Downloads :: Buy Adobe Flash CS4 Professional">Buy Flash CS4 Professional</a> adobe premiere pro 1.5 crackz flash adobe occasion <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=12&software=Buy%20Adobe%20Illustrator%20CS4" title="Cheap Soft Downloads :: Buy Adobe Illustrator CS4">Buy Illustrator CS4</a> creating a city adobe after effects adobe premiere pack <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=13&software=Buy%20Adobe%20InDesign%20CS3" title="Cheap Soft Downloads :: Buy Adobe InDesign CS3">Buy InDesign CS3</a> adobe photoshop trial extension adobe premiere elements 2.0 export mov <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=14&software=Buy%20Adobe%20InDesign%20CS4" title="Cheap Soft Downloads :: Buy Adobe InDesign CS4">Buy InDesign CS4</a> adobe photoshop elements 2.0 driver update adobe photoshop style layer effects series <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=15&software=Buy%20Adobe%20Photoshop%20CS3%20Extended" title="Cheap Soft Downloads :: Buy Adobe Photoshop CS3 Extended">Buy Photoshop CS3 Extended</a> xp sp2 adobe flash problem download adobe after effects 6.5 pro <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=16&software=Buy%20Adobe%20Premiere%20Pro%20CS4" title="Cheap Soft Downloads :: Buy Adobe Premiere Pro CS4">Buy Premiere Pro CS4</a> adobe flash player8  </div>
