<?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>JPz&#039;log &#187; Howto</title>
	<atom:link href="http://jpz-log.info/tags/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://jpz-log.info</link>
	<description>Coin Coin and Plop da Plop</description>
	<lastBuildDate>Thu, 29 Jul 2010 06:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sending an email on iPhone OS</title>
		<link>http://jpz-log.info/archives/2010/04/07/sending-an-email-on-iphone-os-2/</link>
		<comments>http://jpz-log.info/archives/2010/04/07/sending-an-email-on-iphone-os-2/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 08:54:42 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Geeking]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Technologies]]></category>

		<guid isPermaLink="false">http://jpz-log.info/?p=1348</guid>
		<description><![CDATA[I have been coding on the iPhone OS recently (for a research prototype). The application is basically capturing some data from the GPS and the accelerometers. The data is stored in a sqlite database that is accessed through Core Data. From time to time, the user can send the data by email. Doing that is [...]]]></description>
			<content:encoded><![CDATA[<p>I have been coding on the iPhone OS recently (for a research prototype). The application is basically capturing some data from the GPS and the accelerometers.</p>
<p>The data is stored in a <em>sqlite</em> database that is accessed through <a href="http://developer.apple.com/macosx/coredata.html">Core Data</a>. From time to time, the user can send the data by email. Doing that is extremely easy with the iPhone OS.</p>
<p>I first added a method for creating a CSV representation of the data:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> csvData <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self fetchedResultsController<span style="color: #002200;">&#93;</span> performFetch<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unresolved error %@, %@&quot;</span>, error, <span style="color: #002200;">&#91;</span>error userInfo<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">abort</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #400080;">NSMutableString</span><span style="color: #002200;">*</span> str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableString</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;timestamp,altitude,latitude,longitude,xAcceleration,yAcceleration,zAcceleration<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span><span style="color: #002200;">*</span> entry <span style="color: #a61390;">in</span> self.fetchedResultsController.fetchedObjects<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>str appendFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@,%@,%@,%@,%@,%@,%@<span style="color: #2400d9;">\n</span>&quot;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;timestamp&quot;</span><span style="color: #002200;">&#93;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;altitude&quot;</span><span style="color: #002200;">&#93;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude&quot;</span><span style="color: #002200;">&#93;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;longitude&quot;</span><span style="color: #002200;">&#93;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;xAcceleration&quot;</span><span style="color: #002200;">&#93;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;yAcceleration&quot;</span><span style="color: #002200;">&#93;</span>,
         <span style="color: #002200;">&#91;</span>entry valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zAcceleration&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>str autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> str;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The data is represented as <strong>NSMutableString</strong>, from which can later extract a <strong>NSData</strong> representation from. The actual code is just a walk over the data rows that Core Data manages.</p>
<p>The controller method that sends the email is very simple as it uses the ready-to-go <strong><a href="http://developer.apple.com/iphone/library/documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html">MFMailComposeViewController</a></strong> controller.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sendMail <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span> <span style="color: #002200;">&#91;</span>MFMailComposeViewController canSendMail<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        UIAlertView <span style="color: #002200;">*</span>alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unsupported operation&quot;</span>
                                                            message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;This device is not configured to send emails.&quot;</span> 
                                                           delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span>
                                                  otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>alertView release<span style="color: #002200;">&#93;</span>;        
        <span style="color: #a61390;">return</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    MFMailComposeViewController<span style="color: #002200;">*</span> mailController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MFMailComposeViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>mailController setSubject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Entropia Data&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>mailController setMessageBody<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;This data was captured by Entropia.app&quot;</span> isHTML<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>mailController addAttachmentData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self csvData<span style="color: #002200;">&#93;</span> dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>
                             mimeType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;application/csv&quot;</span>
                             fileName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data.csv&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    mailController.mailComposeDelegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>self.navigationController presentModalViewController<span style="color: #002200;">:</span>mailController animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The mail composition controller gets some pre-defined data: the subject, a message body and a first attachment which is the CSV data we computed earlier.</p>
<p><a href="http://jpz-log.info/wp-content/uploads/2010/04/mailcontroller.png"><img src="http://jpz-log.info/wp-content/uploads/2010/04/mailcontroller.png" alt="" title="mailcontroller" width="320" height="480" class="alignnone size-full wp-image-1353" /></a></p>
<p>When the user either taps for sending the email or canceling it, the mail controller delegate is called. It can perform some checks and cleanups to see if the mail was sent or not, but <strong>at the very least you must discard the mail controller and release it</strong>. Otherwise, your application will stay blocked on that screen!</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>mailComposeController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MFMailComposeViewController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controller 
          didFinishWithResult<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MFMailComposeResult<span style="color: #002200;">&#41;</span>result 
                        error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error <span style="color: #002200;">&#123;</span>    
    <span style="color: #002200;">&#91;</span>self.navigationController dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> MFMailComposeResultFailed<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        UIAlertView <span style="color: #002200;">*</span>alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span>
                                                            message<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>error localizedFailureReason<span style="color: #002200;">&#93;</span>
                                                           delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span>
                                                  otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>alertView release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>controller release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>As you can see, the iPhone OS provides a very sound and easy framework <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2010/04/07/sending-an-email-on-iphone-os-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Revisiting Guice and AOP with AspectJ</title>
		<link>http://jpz-log.info/archives/2010/02/11/revisiting-guice-and-aop-with-aspectj/</link>
		<comments>http://jpz-log.info/archives/2010/02/11/revisiting-guice-and-aop-with-aspectj/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 15:12:14 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[Geeking]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jpz-log.info/?p=1333</guid>
		<description><![CDATA[I decided to revisit my article on Guice and AOP with AspectJ instead of the AOP Alliance API that Guice comes with. The full source code is available on GitHub. To refresh your memory, we had implemented a declarative approach for controlling the access to a class methods through annotations: @WithUserProfileVerification public class InMemoryContactManager implements [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to revisit <a href="http://jpz-log.info/archives/2009/11/04/guice-it-up-or-aop-can-be-made-simple-sometimes/">my article on Guice and AOP</a> with <a href="http://www.eclipse.org/aspectj/">AspectJ</a> instead of the AOP Alliance API that Guice comes with.</p>
<p><a href="http://github.com/jponge/guice-aspectj-sample">The full source code is available on GitHub</a>.</p>
<p>To refresh your memory, we had implemented a declarative approach for controlling the access to a class methods through annotations:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@WithUserProfileVerification
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> InMemoryContactManager <span style="color: #000000; font-weight: bold;">implements</span> ContactManager <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Set<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> contacts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @RequiresProfile<span style="color: #009900;">&#40;</span>ADMIN<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ContactManager add<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @RequiresProfile<span style="color: #009900;">&#40;</span>ADMIN<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ContactManager remove<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        contacts.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @RequiresProfile<span style="color: #009900;">&#40;</span>USER<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Person lookup<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Person person <span style="color: #339933;">:</span> contacts<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>person.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First off AspectJ is way more expressive than the AOP Alliance API. Using Guice with AspectJ is not very different. If your aspects do not need injection then you fall back to plain AspectJ development, as Guice and AspectJ live apart from each other. Things are a little more subtile if you need to connect Guice with your aspects.</p>
<p>In our case we had one aspect that needed injection, hence the trick is to ask Guice to perform injection on the aspect instance. By default, an AspectJ aspect is a singleton, so all we need is to grab an access to the instance, which is as simple as <a href="http://www.eclipse.org/aspectj//doc/next/adk15notebook/ataspectj-aspectof.html">calling the <strong>Aspects#aspectOf()</strong> static method</a>.</p>
<p>Now let's see some code that differs from the Guice / AOP Alliance sample I showed you.</p>
<p>Here is how you can define a simple dirty console logger that hooks itself onto any implementation of the <strong>ContactManager</strong> interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.aspects</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.JoinPoint</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Aspect</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Before</span><span style="color: #339933;">;</span>
&nbsp;
@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContactManagerLogger <span style="color: #009900;">&#123;</span>
&nbsp;
    @Before<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;call( * info.ponge.julien.hacks.guiceaspectj.contact.ContactManager.*(..) )&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> methodCalled<span style="color: #009900;">&#40;</span>JoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Calling: &quot;</span> <span style="color: #339933;">+</span> thisJoinPoint.<span style="color: #006633;">getSignature</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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: #009900;">&#125;</span></pre></div></div>

<p>The aspect that checks for the user permissions is implemented as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.aspects</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Inject</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.auth.RequiresProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.auth.UserProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.auth.UserProfileChecker</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.auth.WithUserProfileVerification</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.JoinPoint</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Aspect</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Before</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> info.<span style="color: #006633;">ponge</span>.<span style="color: #006633;">julien</span>.<span style="color: #006633;">hacks</span>.<span style="color: #006633;">guiceaspectj</span>.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #006633;">ADMIN</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> info.<span style="color: #006633;">ponge</span>.<span style="color: #006633;">julien</span>.<span style="color: #006633;">hacks</span>.<span style="color: #006633;">guiceaspectj</span>.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #006633;">USER</span><span style="color: #339933;">;</span>
&nbsp;
@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProfileVerification <span style="color: #009900;">&#123;</span>
&nbsp;
    @Inject
    UserProfileChecker userProfileChecker<span style="color: #339933;">;</span>
&nbsp;
    @Before<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution( * *(..) ) &amp;&amp; @annotation( required ) &amp;&amp; within( @WithUserProfileVerification * )&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> verify<span style="color: #009900;">&#40;</span>RequiresProfile required<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        UserProfile expected <span style="color: #339933;">=</span> required.<span style="color: #006633;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UserProfile current <span style="color: #339933;">=</span> userProfileChecker.<span style="color: #006633;">getCurrentUserProfile</span><span style="color: #009900;">&#40;</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>insufficientProfile<span style="color: #009900;">&#40;</span>expected, current<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The current user profile (&quot;</span> <span style="color: #339933;">+</span> current <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) is not sufficient: &quot;</span> <span style="color: #339933;">+</span> required<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;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> insufficientProfile<span style="color: #009900;">&#40;</span>UserProfile required, UserProfile current<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>required <span style="color: #339933;">==</span> ADMIN <span style="color: #339933;">&amp;&amp;</span> current <span style="color: #339933;">!=</span> ADMIN<span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>required <span style="color: #339933;">==</span> USER <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>current <span style="color: #339933;">!=</span> USER <span style="color: #339933;">&amp;&amp;</span> current <span style="color: #339933;">!=</span> ADMIN<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: #009900;">&#125;</span></pre></div></div>

<p>Finally our Guice module is configured to perform the injection on the singleton that corresponds to the previous aspect:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.aspects.ProfileVerification</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.auth.UserProfileChecker</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.auth.dumb.DumbUserProfileChecker</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.contact.ContactManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.contact.Person</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">info.ponge.julien.hacks.guiceaspectj.contact.simple.InMemoryContactManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.Aspects</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">aspectj</span>.<span style="color: #006633;">lang</span>.<span style="color: #006633;">Aspects</span>.<span style="color: #339933;">*;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</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;">private</span> Module guiceModule <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AbstractModule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            bind<span style="color: #009900;">&#40;</span>ContactManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span>InMemoryContactManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span>Singleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            bind<span style="color: #009900;">&#40;</span>UserProfileChecker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span>DumbUserProfileChecker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span>Singleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            requestInjection<span style="color: #009900;">&#40;</span>aspectOf<span style="color: #009900;">&#40;</span>ProfileVerification.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Injector injector <span style="color: #339933;">=</span> Guice.<span style="color: #006633;">createInjector</span><span style="color: #009900;">&#40;</span>guiceModule<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ContactManager contacts <span style="color: #339933;">=</span> injector.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>ContactManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UserProfileChecker profileChecker <span style="color: #339933;">=</span> injector.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>UserProfileChecker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        profileChecker.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien&quot;</span>, <span style="color: #0000ff;">&quot;secret&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien Ponge&quot;</span>, <span style="color: #0000ff;">&quot;julien.ponge@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jean-Jacques&quot;</span>, <span style="color: #0000ff;">&quot;jean.jacques@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        profileChecker.<span style="color: #006633;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        profileChecker.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jean-Jacques&quot;</span>, <span style="color: #0000ff;">&quot;1234&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>contacts.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien Ponge&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        profileChecker.<span style="color: #006633;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mr Bean&quot;</span>, <span style="color: #0000ff;">&quot;mrbean@gmail.com&quot;</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: #009900;">&#125;</span></pre></div></div>

<p>As one would expect, trying to add Mr Bean fails due to insufficient permissions:</p>
<pre>
Calling: add
Calling: add
Calling: lookup
Julien Ponge <julien.ponge@gmail.com>
Calling: add
Exception in thread "main" java.lang.RuntimeException: The current user profile (ANONYMOUS) is not sufficient: @info.ponge.julien.hacks.guiceaspectj.auth.RequiresProfile(value=ADMIN)
	at info.ponge.julien.hacks.guiceaspectj.aspects.ProfileVerification.verify(ProfileVerification.java:27)
	at info.ponge.julien.hacks.guiceaspectj.contact.simple.InMemoryContactManager.add(InMemoryContactManager.java:21)
	at info.ponge.julien.hacks.guiceaspectj.Main.execute(Main.java:54)
	at info.ponge.julien.hacks.guiceaspectj.Main.main(Main.java:17)
</pre>
<p><a href="http://github.com/jponge/guice-aspectj-sample">(full code on GitHub)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2010/02/11/revisiting-guice-and-aop-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Guice it up (or AOP can be made simple sometimes)</title>
		<link>http://jpz-log.info/archives/2009/11/04/guice-it-up-or-aop-can-be-made-simple-sometimes/</link>
		<comments>http://jpz-log.info/archives/2009/11/04/guice-it-up-or-aop-can-be-made-simple-sometimes/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 09:48:45 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Geeking]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technologies]]></category>

		<guid isPermaLink="false">http://jpz-log.info/?p=1078</guid>
		<description><![CDATA[I have been knowing about the Google Guice dependency injection container features for quite some time. Guice is a really pleasant DI framework that does its job with brilliant simplicity from a developer point of view (oh yes, and you don't have to describe the classes wiring in a dumb XML file like Spring does). [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/untitlism/20116328/"><img src="http://farm1.static.flickr.com/17/20116328_1b27002566_m_d.jpg" alt="Juice" /></a></p>
<p>I have been knowing about the <a href="http://code.google.com/p/google-guice/">Google Guice</a> dependency injection container features for quite some time. Guice is a really pleasant DI framework that does its job with brilliant simplicity from a developer point of view (<em>oh yes, and you don't have to describe the classes wiring in a dumb XML file like <a href="http://www.springsource.org/">Spring</a> does</em>).</p>
<p>Guice has more than DI capabilities though, as long as you spice it up with extensions libraries. Today I'll show you the <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a> capabilities that Guice offers.</p>
<p>I must admit that I have always been puzzled by this thing called <em>aspect-oriented programming</em>. While the idea of separating actual code from cross-cutting concerns (e.g., security, transactions) makes a lot of sense, one may easily end-up writing <a href="http://en.wikipedia.org/wiki/Spaghetti_code">spaghetti code</a>.</p>
<p>If you don't believe me, have a look at <a href="http://www.springsource.org/roo">Spring ROO</a>, I am really curious to know if one can come up with serious arguments for not calling that mess of Java, AspectJ and Spring XML oddities <em>"spaghetti code"</em>.</p>
<p>Anyway, AOP flourished rapidly a few years back, as advanced developers, methodologists and event academics all went crazy about it through books, frameworks and claims of the death of OOP. The good news is that for a change, the AOP hype faded away in a flashlight (I whish the same could have been true for those silly things called SOAP and BPEL). However, AOP is still very useful in non-dynamic languages like Java, and reasonable use can make code quite elegant.</p>
<p>Let's get back to Guice, as I will quickly go through some code snippets. I wanted to play with Guice through the trivial use-case of a <strong>declaratively access-restricted contacts manager</strong>. The code that you will see exhibits AOP and DI features in Guice. As far as the quality is concerned, it will show you the approach, but in an industrial case one would of course need something a bit more elaborated.</p>
<p>First of all, I created a wonderful <strong>Person</strong> model class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">app</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> email<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Person<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">String</span> email<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">email</span> <span style="color: #339933;">=</span> email<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<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> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEmail<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> email<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<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> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &lt;&quot;</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #009900;">&#41;</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>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<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><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> o<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> o.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        Person person <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>email.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>name.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> result <span style="color: #339933;">=</span> name.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        result <span style="color: #339933;">=</span> <span style="color: #cc66cc;">31</span> <span style="color: #339933;">*</span> result <span style="color: #339933;">+</span> email.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><em>Disgression: it would have been much more concise in <a href="http://www.scala-lang.org/">Scala</a> or <a href="http://groovy.codehaus.org">Groovy</a>.</em></p>
<p>I then designed a <strong>ContactManager</strong> interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">app</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ContactManager <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ContactManager add<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ContactManager remove<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Person lookup<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that I made it minimalistically <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent</a> so that <strong>add</strong> and <strong>remove</strong> calls could be chained.</p>
<p>I then wanted to design a basic access control mechanism, so that an implementation of <strong>ContactManager</strong> could <strong>declaratively</strong> restrict its access to a certain type of user profile, much like what we can do with EJB 3.x (incidentally, good implementations like <a href="http://glassfish.org/">Glassfish</a> use AOP and bytecode engineering under the hood).</p>
<p>Hence, I designed annotations to let classes and methods specify access-control policies:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">app.auth</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.ElementType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Retention</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.RetentionPolicy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Target</span><span style="color: #339933;">;</span>
&nbsp;
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">TYPE</span><span style="color: #009900;">&#41;</span>
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> WithUserProfileVerification <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">app.auth</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.ElementType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Retention</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.RetentionPolicy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Target</span><span style="color: #339933;">;</span>
&nbsp;
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#41;</span>
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> RequiresProfile <span style="color: #009900;">&#123;</span>
&nbsp;
    UserProfile value<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>along with a user profile enumeration (anonymous user, regular user and administrator):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">app.auth</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> UserProfile <span style="color: #009900;">&#123;</span>
    ANONYMOUS, USER, ADMIN
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we are able to define a basic <strong>ContactManager</strong> implementation with declarative access-control policies:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">demo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.ContactManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.Person</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.RequiresProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> app.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #006633;">ADMIN</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> app.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #006633;">USER</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.WithUserProfileVerification</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
&nbsp;
@WithUserProfileVerification
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContactManagerImpl <span style="color: #000000; font-weight: bold;">implements</span> ContactManager <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Set<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> contacts <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @RequiresProfile<span style="color: #009900;">&#40;</span>ADMIN<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ContactManager add<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @RequiresProfile<span style="color: #009900;">&#40;</span>ADMIN<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ContactManager remove<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        contacts.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @RequiresProfile<span style="color: #009900;">&#40;</span>USER<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Person lookup<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Person person <span style="color: #339933;">:</span> contacts<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>person.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>From this definition, anonymous users cannot do anything, regular users can perform lookups and administrators can add/remove contacts.</p>
<p>This can be summarized by this class diagram:</p>
<p><img src="http://jpz-log.info/wp-content/uploads/2009/10/guiceaop-1.png" alt="guiceaop-1" title="guiceaop-1" width="445" height="350" class="aligncenter size-full wp-image-1106" /></p>
<p><a href="http://www.flickr.com/photos/obliterated/3880336156/"><img src="http://farm4.static.flickr.com/3527/3880336156_9c383e83e1_m_d.jpg" alt="Cables" /></a></p>
<p><strong>The next question is of course: how do you make that actually work?</strong></p>
<p>First, let's design a profile checker interface along with a dumb implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">app.auth</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> UserProfileChecker <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserProfile getCurrentUserProfile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserProfile login<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> login, <span style="color: #003399;">String</span> password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserProfile logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">demo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.UserProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> app.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #339933;">*;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.UserProfileChecker</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DumbUserProfileChecker <span style="color: #000000; font-weight: bold;">implements</span> UserProfileChecker <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> UserProfile userProfile <span style="color: #339933;">=</span> ANONYMOUS<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserProfile getCurrentUserProfile<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> userProfile<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserProfile login<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> login, <span style="color: #003399;">String</span> password<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>login.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> password.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;secret&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            userProfile <span style="color: #339933;">=</span> ADMIN<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>login.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jean-Jacques&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> password.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1234&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            userProfile <span style="color: #339933;">=</span> USER<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            userProfile <span style="color: #339933;">=</span> ANONYMOUS<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> getCurrentUserProfile<span style="color: #009900;">&#40;</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;">public</span> UserProfile logout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        userProfile <span style="color: #339933;">=</span> ANONYMOUS<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> userProfile<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><img src="http://jpz-log.info/wp-content/uploads/2009/10/guiceaop-2.png" alt="guiceaop-2" title="guiceaop-2" width="516" height="271" class="aligncenter size-full wp-image-1107" /></p>
<p>Sounds great isn't it? <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Still, the link is missing between our contact manager implementation, and this user profile checker.</p>
<p>The idea is to <strong>design an aspect</strong> for that: each class that uses our declarative access-control policies will have method calls beeing intercepted by the aspect. Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">demo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.RequiresProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.UserProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> app.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #006633;">ADMIN</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> app.<span style="color: #006633;">auth</span>.<span style="color: #006633;">UserProfile</span>.<span style="color: #006633;">USER</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.UserProfileChecker</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Inject</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aopalliance.intercept.MethodInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aopalliance.intercept.MethodInvocation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserProfileInterceptor <span style="color: #000000; font-weight: bold;">implements</span> MethodInterceptor <span style="color: #009900;">&#123;</span>
&nbsp;
    @Inject
    <span style="color: #000000; font-weight: bold;">private</span> UserProfileChecker profileChecker<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span>MethodInvocation methodInvocation<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
        UserProfile required <span style="color: #339933;">=</span> methodInvocation.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAnnotation</span><span style="color: #009900;">&#40;</span>RequiresProfile.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">value</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UserProfile current <span style="color: #339933;">=</span> profileChecker.<span style="color: #006633;">getCurrentUserProfile</span><span style="color: #009900;">&#40;</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>insufficientProfile<span style="color: #009900;">&#40;</span>required, current<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The current user profile (&quot;</span> <span style="color: #339933;">+</span> current <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) is not sufficient: &quot;</span> <span style="color: #339933;">+</span> required<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> methodInvocation.<span style="color: #006633;">proceed</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;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> insufficientProfile<span style="color: #009900;">&#40;</span>UserProfile required, UserProfile current<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>required <span style="color: #339933;">==</span> ADMIN <span style="color: #339933;">&amp;&amp;</span> current <span style="color: #339933;">!=</span> ADMIN<span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>required <span style="color: #339933;">==</span> USER <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>current <span style="color: #339933;">!=</span> USER <span style="color: #339933;">&amp;&amp;</span> current <span style="color: #339933;">!=</span> ADMIN<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: #009900;">&#125;</span></pre></div></div>

<p>The user profile checker will be injected by Guice in the aspect (see the <strong>@Inject</strong> annotation). The interesting work is performed by the <strong>invoke</strong> method that looks at the required user profile (through the invoked method annotation) and checks it against the user profile checker. When the profile matches, the method actually gets invoked, otherwise a <strong>RuntimeException</strong> is raised.</p>
<p>From there what is missing is simply the Guice wiring definitions. Before we look at that, I also designed an aspect for logging method calls (hence, we will inject two aspects):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">demo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aopalliance.intercept.MethodInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aopalliance.intercept.MethodInvocation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Level</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingInterceptor <span style="color: #000000; font-weight: bold;">implements</span> MethodInterceptor <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>LoggingInterceptor.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</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;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span>MethodInvocation methodInvocation<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">logp</span><span style="color: #009900;">&#40;</span>
                Level.<span style="color: #006633;">INFO</span>,
                methodInvocation.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                methodInvocation.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                <span style="color: #0000ff;">&quot;invocation&quot;</span>,
                methodInvocation.<span style="color: #006633;">getArguments</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: #003399;">Object</span> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            result <span style="color: #339933;">=</span> methodInvocation.<span style="color: #006633;">proceed</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: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
            logger.<span style="color: #006633;">logp</span><span style="color: #009900;">&#40;</span>
                    Level.<span style="color: #006633;">INFO</span>,
                    methodInvocation.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                    methodInvocation.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                    <span style="color: #0000ff;">&quot;return&quot;</span>,
                    result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><img src="http://jpz-log.info/wp-content/uploads/2009/10/guiceaop-3.png" alt="guiceaop-3" title="guiceaop-3" width="587" height="336" class="aligncenter size-full wp-image-1108" /></p>
<p>Finally, here is the <strong>Main</strong> class that has the Guice wiring configuration and a simple use-case:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">demo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.ContactManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.Person</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.RequiresProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.UserProfileChecker</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">app.auth.WithUserProfileVerification</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">inject</span>.<span style="color: #006633;">matcher</span>.<span style="color: #006633;">Matchers</span>.<span style="color: #339933;">*;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</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;">private</span> Module guiceModule <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AbstractModule<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            bind<span style="color: #009900;">&#40;</span>ContactManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span>ContactManagerImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span>Singleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            bind<span style="color: #009900;">&#40;</span>UserProfileChecker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span>DumbUserProfileChecker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                    .<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span>Singleton.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            UserProfileInterceptor userProfileInterceptor <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserProfileInterceptor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            requestInjection<span style="color: #009900;">&#40;</span>userProfileInterceptor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            bindInterceptor<span style="color: #009900;">&#40;</span>
                    annotatedWith<span style="color: #009900;">&#40;</span>WithUserProfileVerification.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>,
                    annotatedWith<span style="color: #009900;">&#40;</span>RequiresProfile.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>,
                    userProfileInterceptor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            bindInterceptor<span style="color: #009900;">&#40;</span>
                    subclassesOf<span style="color: #009900;">&#40;</span>ContactManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>,
                    any<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
                    <span style="color: #000000; font-weight: bold;">new</span> LoggingInterceptor<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: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Injector injector <span style="color: #339933;">=</span> Guice.<span style="color: #006633;">createInjector</span><span style="color: #009900;">&#40;</span>guiceModule<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ContactManager contacts <span style="color: #339933;">=</span> injector.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>ContactManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        UserProfileChecker profileChecker <span style="color: #339933;">=</span> injector.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>UserProfileChecker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        profileChecker.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien&quot;</span>, <span style="color: #0000ff;">&quot;secret&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien Ponge&quot;</span>, <span style="color: #0000ff;">&quot;julien.ponge@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        contacts.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jean-Jacques&quot;</span>, <span style="color: #0000ff;">&quot;jean.jacques@gmail.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        profileChecker.<span style="color: #006633;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        profileChecker.<span style="color: #006633;">login</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Jean-Jacques&quot;</span>, <span style="color: #0000ff;">&quot;1234&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>contacts.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Julien Ponge&quot;</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: #009900;">&#125;</span></pre></div></div>

<p><img src="http://jpz-log.info/wp-content/uploads/2009/10/guiceaop-4.png" alt="guiceaop-4" title="guiceaop-4" width="373" height="271" class="aligncenter size-full wp-image-1111" /></p>
<p>You can easily modify the <strong>execute</strong> method call to check that access-control is enforced (e.g., have an anonymous user attempt to add an entry and see that an exception is raised).</p>
<p>The wiring defined in the module configuration is straightforward. One should only pay attention to the <strong>requestInjection(userProfileInterceptor)</strong> call. Indeed, aspects are not managed by the DI container. As we request an injection in the corresponding class, this call will make it on-demand.</p>
<p>As we saw in this small showcase, <a href="http://code.google.com/p/google-guice/">Google Guice</a> is a very compelling DI framework with simple and efficient AOP capabilities.</p>
<p>Don't you think so? <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2009/11/04/guice-it-up-or-aop-can-be-made-simple-sometimes/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>OSGi development made easier</title>
		<link>http://jpz-log.info/archives/2009/10/27/osgi-development-made-easier/</link>
		<comments>http://jpz-log.info/archives/2009/10/27/osgi-development-made-easier/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 10:57:56 +0000</pubDate>
		<dc:creator>jponge</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://jpz-log.info/?p=1091</guid>
		<description><![CDATA[I must confess that I happen to write OSGi bundles, and somehow happen to actually enjoy that, despite all the care you need to have with respect to: dynamicity-handling boilerplate code having to maintain a MANIFEST forced mutable class design concurrency issues (you often have to spin a thread when your bundle starts). Creating an [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/dunechaser/3563994711/"><img class="alignright" src="http://farm3.static.flickr.com/2484/3563994711_3a37e12653_m_d.jpg" alt="Lego" /></a></p>
<p>I must confess that I happen to write OSGi bundles, and somehow happen to actually enjoy that, despite all the care you need to have with respect to:</p>
<ul>
<li>dynamicity-handling boilerplate code</li>
<li>having to maintain a <strong>MANIFEST</strong></li>
<li>forced mutable class design</li>
<li>concurrency issues (you often have to spin a thread when your bundle starts).</li>
</ul>
<p>Creating an OSGi bundle is not very complicated per-se, but tooling has historically been quite limited. Fortunately, we now have a good <a href="http://maven.apache.org/">Maven</a>-based toolchain.</p>
<p><strong>Indeed, wouldn't it be nice to focus on just writing your own bundles and forget about the rest?</strong></p>
<p>First of all I suggest that you use the <a href="http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html">Apache Felix Maven Bundle</a>. With minimal definitions, it takes the pain of maintaining the <strong>MANIFEST</strong> entries away from you with minimal informations:</p>
<ul>
<li>it makes a bundle by using most of the informations available from your POM</li>
<li>by default, it maintains the imported / exported packages much like you would expect</li>
<li>it leverages each of your POM depency nicely: if it's a bundle then fine, otherwise it will either wrap it as a bundle or simply embed the classes (dealing with non-bundles is a huge pain point in OSGi).</li>
</ul>
<p>The second Maven plugin that I wanted to tell you about is the <a href="http://www.ops4j.org/projects/pax/construct/maven-pax-plugin/">Pax Plugin</a> by the OPS4J crew. This plugin is another time-saver, as it is able to automatically provision an OSGi runtime (<a href="http://felix.apache.org/">Apache Felix</a> by default, but you can ask for <a href="http://www.eclipse.org/equinox/">Eclipse Equinox</a>, <a href="http://www.knopflerfish.org/">Knoplerfish</a>, etc).</p>
<p>Without this plugin, you either have to manually install your bundles into an OSGi runtime, or write configuration files to get the provisionned environment that you need for your tests. All of that is handled by the Pax plugin, and you get the extra benefit of beeing able to try your bundles on several OSGi runtimes.</p>
<p>Indeed, to try your bundles on Apache Felix, simply type:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mvn pax:provision</pre></div></div>

<p>and if you want to play with Eclipse Equinox:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mvn pax:provision <span style="color: #660033;">-Dframework</span>=equinox</pre></div></div>

<p>Those two complementary plugins should definitly make OSGi development simpler, at least from the toolchain point of view <img src='http://jpz-log.info/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jpz-log.info/archives/2009/10/27/osgi-development-made-easier/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
