<?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>North Concepts</title>
	<atom:link href="http://northconcepts.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://northconcepts.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Mon, 23 Apr 2012 02:19:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Data Pipeline 2.2.6 Now Available</title>
		<link>http://northconcepts.com/blog/2012/04/22/data-pipeline-2-2-6-now-available/</link>
		<comments>http://northconcepts.com/blog/2012/04/22/data-pipeline-2-2-6-now-available/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 02:19:33 +0000</pubDate>
		<dc:creator>Dele Taylor</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=399</guid>
		<description><![CDATA[performance improvements in CSV and fixed width handling untyped expression evaluation is now based on the value&#8217;s type, instead of the field&#8217;s declared type BUGFIX: now handles untyped expressions between primitive and object values float expressions are now upgraded to &#8230; <a href="http://northconcepts.com/blog/2012/04/22/data-pipeline-2-2-6-now-available/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<ul>
<li>performance improvements in CSV and fixed width handling</li>
<li>untyped expression evaluation is now based on the value&#8217;s type, instead of the field&#8217;s declared type</li>
<li>BUGFIX: now handles untyped expressions between primitive and object values</li>
<li>float expressions are now upgraded to doubles during evaluation</li>
<li>all non doubles and floats numbers are now upgraded to longs during evaluation</li>
<li>expressions can now reference Java beans, not just primitive values</li>
<li>method call expression now finds the most appropriate method based on the runtime argument types (<a href="http://en.wikipedia.org/wiki/Multiple_dispatch" target="_blank">http://en.wikipedia.org/wiki/Multiple_dispatch</a>)</li>
<li>improved handling for collections and arrays in DataException properties</li>
<li>Apache PoiProvider can now distinguish between date, time, and datetimes fields in Excel</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2012/04/22/data-pipeline-2-2-6-now-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to build a simple GWT event bus using Generators</title>
		<link>http://northconcepts.com/blog/2012/01/20/how-to-build-a-simple-gwt-event-bus-using-generators/</link>
		<comments>http://northconcepts.com/blog/2012/01/20/how-to-build-a-simple-gwt-event-bus-using-generators/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 11:33:50 +0000</pubDate>
		<dc:creator>Dele Taylor</dc:creator>
				<category><![CDATA[Code Generation]]></category>
		<category><![CDATA[Event Bus]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=90</guid>
		<description><![CDATA[In his Google I/O session Best Practices For Architecting Your GWT App, Ray Ryan discusses the benefits of using an event bus in GWT (Google Web Toolkit) applications. Inspired by this talk, I decided to try my hand at building &#8230; <a href="http://northconcepts.com/blog/2012/01/20/how-to-build-a-simple-gwt-event-bus-using-generators/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In his Google I/O session <a href="http://www.google.com/events/io/2009/sessions/GoogleWebToolkitBestPractices.html" target="_blank">Best Practices For Architecting Your GWT App</a>, Ray Ryan discusses the benefits of using an event bus in GWT (Google Web Toolkit) applications. Inspired by this talk, I decided to try my hand at building a simple GWT event bus modeled after our pure <a href="http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/" target="_blank">java event bus</a>.<br />
<span id="more-90"></span></p>
<blockquote><p>See the previous articles:<br />
<a href="http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/" target="_blank">Use dynamic proxies to create a simple, powerful event bus (Part 1)</a><br />
<a href="http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/" target="_blank">Use dynamic proxies to create a simple, powerful event bus (Part 2)</a></p></blockquote>
<h2>Using the event bus</h2>
<p>If you&#8217;ve followed along with the previous event bus blogs, you already know what I mean by simple – simple to use. We want an event bus that doesn&#8217;t require a fleet of event classes or fireXXX methods.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Listen for newSearch events</span>
eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>SearchListener.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> SearchListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> newSearch<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> query<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;Searching for &quot;</span> <span style="color: #339933;">+</span> query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Publish newSearch event</span>
SearchListener publisher <span style="color: #339933;">=</span> eventBus.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>SearchListener.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
publisher.<span style="color: #006633;">newSearch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GWT event bus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We want to call a method, like <code>newSearch("GWT event bus")</code>, and have it automatically invoked on all observers as if we&#8217;d called each one ourselves. We want method multicasting. As you can see in the code above, we use the same <code>SearchListener</code> interface to both observe newSearch events and publish them.</p>
<h2>GWT generators</h2>
<p>In the original event bus we used reflection, specifically, we used Java&#8217;s dynamic proxies to accomplish the publisher-listener magic. GWT doesn&#8217;t have anything like this, but it does provide hooks into the compiler for code generators. Basically, we can generate the publisher code at compile to perform the same function dynamic proxies did at runtime.</p>
<p><img style="display: inline; border: 0px;" title="gwt-compiler-steps" src="http://northconcepts.com/blog/wp-content/uploads/2011/07/gwtcompilersteps.png" alt="gwt-compiler-steps" width="769" height="126" border="0" /></p>
<p>Code generation happens (it seems) during GWT&#8217;s precompile phase. The actual compiler hook is triggered by a combination of a <code>GWT.create()</code> call in the Java source and a <code>generate-with</code> element in the GWT module XML file. The GWT.create method takes a class literal to instantiate – <code>GWT.create(PublisherFactoryRegistry.class)</code> in this case. The generate-with element tells the compiler that <code>EventPublisherGenerator</code> will create the Java source that extends (or implements if it were an interface) <code>PublisherFactoryRegistry</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;generate-with</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.northconcepts.eventbus.generator.EventPublisherGenerator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;when-type-assignable</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.northconcepts.eventbus.client.PublisherFactoryRegistry&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/generate-with<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The compiler treats the Java source emitted by EventPublisherGenerator like any other class and optimizes it, converts it to JavaScript, obfuscates it, etc. GWT.create then returns an instance of the generated class to its caller. Google calls this whole process <a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html#generators" target="_blank">Deferred Binding using Generators</a>.</p>
<p>For more details on the GWT compiler, watch Ray Cromwell&#8217;s <a href="http://www.youtube.com/watch?v=qT6ZsQBM7kY" target="_blank">Optimizing apps with the GWT Compiler</a> session at Google I/O.</p>
<h2>Event bus changes</h2>
<p>GWT is a special variant of Java and not everything from standard Java is available (primarily because GWT only supports what can be emulated in JavaScript). I mentioned before that reflection and dynamic proxies are not available, here are a few more differences you&#8217;ll want to note when comparing the <a href="#download">code</a> here with the pure <a href="http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/" target="_blank">Java event bus</a>:</p>
<ol>
<li>JavaScript is single-threaded so we don&#8217;t have to worry about threading or synchronization for the most part.</li>
<li>No threading also means no <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html" target="_blank">executor service</a>, but we&#8217;ll use <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/2.3/com/google/gwt/core/client/Scheduler.ScheduledCommand.html" target="_blank">deferred (or scheduled) commands</a> to simulate it.</li>
<li>Thread-locals are also out.</li>
<li>While JavaScript uses a garbage collector, we don&#8217;t have the same hooks into it as we do in Java. This means no <a href="http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/#garbage_collection">soft references</a> and no automatic releasing of observers.</li>
</ol>
<h3>EventBus.getPublisher() changes</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> T getPublisher<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> eventSource, 
Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> eventListenerClass, <span style="color: #003399;">Object</span> topic<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    T publisher <span style="color: #339933;">=</span> PUBLISHER_FACTORIES.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventListenerClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    EventPublisher<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> publisher2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EventPublisher<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> publisher<span style="color: #339933;">;</span>
    publisher2.<span style="color: #006633;">setEventBus</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    publisher2.<span style="color: #006633;">setEventSource</span><span style="color: #009900;">&#40;</span>eventSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    publisher2.<span style="color: #006633;">setEventListenerClass</span><span style="color: #009900;">&#40;</span>eventListenerClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    publisher2.<span style="color: #006633;">setTopic</span><span style="color: #009900;">&#40;</span>topic<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> publisher<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The old, pure Java getPublisher method would implement the listener interface using a dynamic proxy. The proxy would publish events to the bus whenever called. This new GWT event bus keeps a registry of factories to create publishers that implement each type of event listener interface. Publishers also extend the <code>EventPublisher</code> class which allows getPublisher to initialize them with the event source, topic, etc. Like the non-GWT version, publishers queue events to the bus when called.</p>
<h3>EventBus.publishEvent() changes</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> publishEvent<span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">final</span> Event<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Scheduler.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">scheduleDeferred</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ScheduledCommand<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;">public</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>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span> 
                deliverEvent<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">RuntimeException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
                <span style="color: #000000; font-weight: bold;">throw</span> e<span style="color: #339933;">;</span> 
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
                <span style="color: #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>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #009900;">&#125;</span> 
        <span style="color: #009900;">&#125;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The only change to publishEvent is in how it schedules event delivery. The new method uses GWT&#8217;s scheduler service (formerly called deferred commands) since executor services are not supported.</p>
<h3>EventBus.deliverEvent() changes</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEvent<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> event<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>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        currentEvent <span style="color: #339933;">=</span> event<span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Deliver to typed listeners</span>
        TypedListenerList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> getTypedListenerList<span style="color: #009900;">&#40;</span>
        event.<span style="color: #006633;">getEventListenerClass</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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>list <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EventListenerStub<span style="color: #339933;">&lt;?&gt;</span> stub <span style="color: #339933;">:</span> list.<span style="color: #006633;">getList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                stub.<span style="color: #006633;">deliverEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Deliver to untyped listeners</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span> stub <span style="color: #339933;">:</span> 
        untypedListeners<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            stub.<span style="color: #006633;">deliverEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
        currentEvent <span style="color: #339933;">=</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></td></tr></table></div>

<p>Changes to deliverEvent are also minimal. The <code>cleanupGarbage()</code> call is removed since we don&#8217;t have soft references to clean up and <code>currentEvent</code> is demoted from a thread-local to just an instance field.</p>
<h2>Generated code</h2>
<p>Let&#8217;s take a look at the code we need to generate. Here we produce two kinds of classes: a event publisher for each event listener and one <code>PublisherFactoryRegistry</code> implementation to hold the publisher factories.</p>
<h3>Event publishers</h3>
<p>Publishers implement the event listener interfaces, convert method calls into events, and publishes the events to the bus.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SearchListener <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">EventListener</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> newSearch<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> query<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">void</span> searchFinished<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> query, ArrayList<span style="color: #339933;">&lt;</span>Data<span style="color: #339933;">&gt;</span> results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066; font-weight: bold;">void</span> detailsViewed<span style="color: #009900;">&#40;</span>Data data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The code generator seeing the above <code>SearchListener</code> interface will create the following <code>SearchListener__publisher</code> class. Each implemented method creates an event object containing things you&#8217;d expect like event source, topic, and arguments. Since we don&#8217;t have reflection, the event object also contains an <code>EventDispatcher</code> to invoke the observer&#8217;s method when directed to do so by the bus.</p>
<p><a name="SearchListener__publisher"></a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchListener__publisher 
<span style="color: #000000; font-weight: bold;">extends</span> EventPublisher <span style="color: #000000; font-weight: bold;">implements</span> SearchListener <span style="color: #009900;">&#123;</span>
  ...
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> searchFinished<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> query, 
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">ArrayList</span> results<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    publishEvent<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #339933;">&lt;</span>SearchListener<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>
    getEventSource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 
    <span style="color: #000000; font-weight: bold;">this</span>, 
    SearchListener.<span style="color: #000000; font-weight: bold;">class</span>, 
    getTopic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 
    <span style="color: #0000ff;">&quot;searchFinished&quot;</span>, 
    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;query&quot;</span>, <span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #009900;">&#125;</span>, 
    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;java.lang.String&quot;</span>, <span style="color: #0000ff;">&quot;java.util.ArrayList&quot;</span><span style="color: #009900;">&#125;</span>, 
    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>query, results<span style="color: #009900;">&#125;</span>, 
    <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #339933;">&lt;</span>SearchListener<span style="color: #339933;">&gt;</span><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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dispatch<span style="color: #009900;">&#40;</span>SearchListener listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        listener.<span style="color: #006633;">searchFinished</span><span style="color: #009900;">&#40;</span>query, results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  ...
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3><a name="PublisherFactoryRegistry"></a>Publisher factory registry</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PublisherFactoryRegistryImpl 
<span style="color: #000000; font-weight: bold;">extends</span> PublisherFactoryRegistry <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #006633;">publishers</span>.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>SearchListener.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> EventPublisherFactory<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;">public</span> SearchListener create<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> SearchListener__publisher<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><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <code>PublisherFactoryRegistry</code> class is used by the bus to find factories that create publishers (like <code>SearchListener__publisher</code>). The concrete <code>PublisherFactoryRegistryImpl</code> we generate is only there to register a factory for each publisher we generate — by putting it into the publishers HashMap.</p>
<h3>Preserving the generated code</h3>
<p>You can tell the GWT compiler to keep the generated Java code around for you to review. Just add the <code>-gen folder</code> compiler flag, where <em>folder</em> is the path to generate temporary files.</p>
<h2>The Code Generator</h2>
<p>The contract for GWT code generators is to extend <code>com.google.gwt.core.ext.Generator</code> and implement its <code>generate(TreeLogger logger, GeneratorContext context, String typeName)</code> method. The implemented method should return the name of the generated subclass for the type passed into GWT.create. As you review the code, you&#8217;ll notice that we&#8217;ve added several helper classes (<code>ClassDeclaration</code>, <code>ClassBuilder</code>, <code>CSV</code>, etc.) to make the code less verbose.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventPublisherGenerator <span style="color: #000000; font-weight: bold;">extends</span> Generator <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> TreeLogger logger<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> GeneratorContext context<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> TypeOracle typeOracle<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> JClassType type<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> JClassType eventListenerType<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> JClassType eventType<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> JClassType eventDispatcherType<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> JClassType eventPublisherType<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> JClassType eventPublisherFactoryType<span style="color: #339933;">;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> generate<span style="color: #009900;">&#40;</span>TreeLogger logger, GeneratorContext context, <span style="color: #003399;">String</span> typeName<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> UnableToCompleteException <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      init<span style="color: #009900;">&#40;</span>logger, context, typeName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> generate<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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      logger.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>TreeLogger.<span style="color: #006633;">ERROR</span>, <span style="color: #0000ff;">&quot;generate failed: &quot;</span> <span style="color: #339933;">+</span> typeName, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> UnableToCompleteException<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;">void</span> init<span style="color: #009900;">&#40;</span>TreeLogger logger, GeneratorContext context, <span style="color: #003399;">String</span> typeName<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>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">logger</span> <span style="color: #339933;">=</span> logger<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">context</span> <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">typeOracle</span> <span style="color: #339933;">=</span> context.<span style="color: #006633;">getTypeOracle</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;">this</span>.<span style="color: #006633;">type</span> <span style="color: #339933;">=</span> typeOracle.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span>typeName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">eventListenerType</span> <span style="color: #339933;">=</span> typeOracle.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">EventListener</span>.<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>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">eventType</span> <span style="color: #339933;">=</span> typeOracle.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span>.<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>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">eventDispatcherType</span> <span style="color: #339933;">=</span> typeOracle.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span>EventDispatcher.<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>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">eventPublisherType</span> <span style="color: #339933;">=</span> typeOracle.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span>EventPublisher.<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>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">eventPublisherFactoryType</span> <span style="color: #339933;">=</span> typeOracle.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span>EventPublisherFactory.<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>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We&#8217;ve created an init method to act as a poor man&#8217;s constructor for the generator. It uses the compiler&#8217;s metadata registry (<code>TypeOracle</code>) to lookup several classes we&#8217;ll need later.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> generate<span style="color: #009900;">&#40;</span><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>
  <span style="color: #000000; font-weight: bold;">final</span> ClassDeclaration clazz <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassDeclaration<span style="color: #009900;">&#40;</span>
      type.<span style="color: #006633;">getPackage</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>, 
      type.<span style="color: #006633;">getSimpleSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;Impl&quot;</span>, 
      type.<span style="color: #006633;">getQualifiedSourceName</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;">final</span> ClassBuilder classBuilder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassBuilder<span style="color: #009900;">&#40;</span>logger, context, clazz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>classBuilder.<span style="color: #006633;">classAlreadyExists</span><span style="color: #009900;">&#40;</span><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> classBuilder.<span style="color: #006633;">commit</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;
  classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  classBuilder.<span style="color: #006633;">indent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  visitEventListeners<span style="color: #009900;">&#40;</span>logger.<span style="color: #006633;">branch</span><span style="color: #009900;">&#40;</span>TreeLogger.<span style="color: #006633;">TRACE</span>, <span style="color: #0000ff;">&quot;Event listeners&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> ClassVisitor<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> visitClass<span style="color: #009900;">&#40;</span>JClassType eventListenerType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> createPublisher<span style="color: #009900;">&#40;</span>eventListenerType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;publishers.put(%1$s.class, new %2$s() {public %1$s create() {return new %3$s();}});&quot;</span>, 
            eventListenerType.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 
            eventPublisherFactoryType.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
            name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  classBuilder.<span style="color: #006633;">outdentln</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">return</span> classBuilder.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The no-arg generate method is where the work starts. Here we create the <a href="#PublisherFactoryRegistry">PublisherFactoryRegistryImpl</a> class and give it an instance initializer where the factories are registered. We also call createPublisher() to generate the code for the <a href="#SearchListener__publisher">publisher implementation</a> for each <code>EventListener</code> sub-interface. The <a href="http://en.wikipedia.org/wiki/Visitor_pattern" target="_blank">visitor pattern</a> is used here to let us focus on code generation while delegating the EventListener finder logic to <code>visitEventListeners()</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> visitEventListeners<span style="color: #009900;">&#40;</span>TreeLogger logger, ClassVisitor visitor<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  FIND_CANDIDATES_LOOP<span style="color: #339933;">:</span> 
  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>JClassType type <span style="color: #339933;">:</span> typeOracle.<span style="color: #006633;">getTypes</span><span style="color: #009900;">&#40;</span><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;">if</span> <span style="color: #009900;">&#40;</span>type.<span style="color: #006633;">isInterface</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> type.<span style="color: #006633;">isAssignableTo</span><span style="color: #009900;">&#40;</span>eventListenerType<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>type.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>eventListenerType<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>type.<span style="color: #006633;">isParameterized</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        logger.<span style="color: #006633;">branch</span><span style="color: #009900;">&#40;</span>TreeLogger.<span style="color: #006633;">WARN</span>, type.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; - skipped - is parameterized type&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>
            TreeLogger.<span style="color: #006633;">WARN</span>, type.<span style="color: #006633;">getParameterizedQualifiedSourceName</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: #000000; font-weight: bold;">continue</span> FIND_CANDIDATES_LOOP<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>JMethod method <span style="color: #339933;">:</span> type.<span style="color: #006633;">getMethods</span><span style="color: #009900;">&#40;</span><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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>method.<span style="color: #006633;">getReturnType</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>JPrimitiveType.<span style="color: #006633;">VOID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          logger.<span style="color: #006633;">branch</span><span style="color: #009900;">&#40;</span>TreeLogger.<span style="color: #006633;">WARN</span>, type.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; - skipped - has method that returns non-void&quot;</span><span style="color: #009900;">&#41;</span>
              .<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>TreeLogger.<span style="color: #006633;">WARN</span>, method.<span style="color: #006633;">getReadableDeclaration</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: #000000; font-weight: bold;">continue</span> FIND_CANDIDATES_LOOP<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      logger.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>TreeLogger.<span style="color: #006633;">INFO</span>, type.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      visitor.<span style="color: #006633;">visitClass</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The visitEventListeners method looks for types the compiler knows about that match the following criteria:</p>
<ol>
<li>are interfaces</li>
<li>are not parameterized (like a <code>java.util.List</code>)</li>
<li>extend java.util.EventListener</li>
<li>have only <code>void</code> returning methods</li>
</ol>
<p>The visitor&#8217;s <code>visitClass</code> method is called for matches, while partial matches emit a compiler warning along with the reason for the mismatch.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> createPublisher<span style="color: #009900;">&#40;</span>JClassType eventListenerType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ClassDeclaration clazz <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassDeclaration<span style="color: #009900;">&#40;</span>
      eventListenerType.<span style="color: #006633;">getPackage</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>, 
      eventListenerType.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;__publisher&quot;</span>, 
      eventPublisherType.<span style="color: #006633;">getQualifiedSourceName</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;
  clazz.<span style="color: #006633;">addImplementedInterface</span><span style="color: #009900;">&#40;</span>eventListenerType.<span style="color: #006633;">getQualifiedSourceName</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;
  ClassBuilder classBuilder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassBuilder<span style="color: #009900;">&#40;</span>logger, context, clazz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>classBuilder.<span style="color: #006633;">classAlreadyExists</span><span style="color: #009900;">&#40;</span><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> classBuilder.<span style="color: #006633;">commit</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;">for</span> <span style="color: #009900;">&#40;</span>JMethod method <span style="color: #339933;">:</span> eventListenerType.<span style="color: #006633;">getMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    CSV paramerNames <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CSV<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    CSV paramerTypeNames <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CSV<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    CSV arguments <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CSV<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">final</span> JParameter<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> parameters <span style="color: #339933;">=</span> method.<span style="color: #006633;">getParameters</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;">final</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasParameters <span style="color: #339933;">=</span> parameters.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> parameters.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      JParameter parameter <span style="color: #339933;">=</span> parameters<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      paramerNames.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> parameter.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      paramerTypeNames.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> MethodDeclaration.<span style="color: #006633;">getParameterTypeName</span><span style="color: #009900;">&#40;</span>method, i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      arguments.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>parameter.<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;
    classBuilder.<span style="color: #006633;">startMethod</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MethodDeclaration<span style="color: #009900;">&#40;</span>method<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;publishEvent(new %1$s&lt;%2$s&gt;(&quot;</span>, 
        eventType.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 
        eventListenerType.<span style="color: #006633;">getQualifiedSourceName</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;
&nbsp;
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getEventSource(), &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;this, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%1$s.class, &quot;</span>, eventListenerType.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getTopic(), &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>%1$s<span style="color: #000099; font-weight: bold;">\&quot;</span>, &quot;</span>, method.<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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hasParameters<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;new String[]{%1$s}, &quot;</span>, paramerNames<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;new String[]{%1$s}, &quot;</span>, paramerTypeNames<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;new Object[]{%1$s}, &quot;</span>, arguments<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>
      classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;null, null, null, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    classBuilder.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;new %1$s&lt;%2$s&gt;(){&quot;</span>, 
        eventDispatcherType.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, 
        eventListenerType.<span style="color: #006633;">getQualifiedSourceName</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;
    classBuilder.<span style="color: #006633;">indentln</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;public void dispatch(%1$s listener) {&quot;</span>, eventListenerType.<span style="color: #006633;">getQualifiedSourceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">indentln</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listener.%1$s(%2$s);&quot;</span>, method.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #009900;">&#40;</span>hasParameters<span style="color: #339933;">?</span>arguments<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">outdentln</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    classBuilder.<span style="color: #006633;">outdentln</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;}));&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    classBuilder.<span style="color: #006633;">endMethod</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;">return</span> classBuilder.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This lengthy method creates the <a href="#SearchListener__publisher">publisher implementation</a>. It implements each method in the event listener interface to publish its description and payload as an event to the bus.</p>
<h2>Example</h2>
<p>The <a href="#download">downlad</a> for this blog includes a simple search example to demonstrate using the event bus. You can run the <code>EventBusExample</code> module (<code>/src/com/northconcepts/eventbusexample/EventBusExample.gwt.xml</code>) or try out the precompiled app by browsing to <code>WebContent/index.html</code>.</p>
<p><img style="display: inline; border: 0px;" title="search-example-mockup" src="http://northconcepts.com/blog/wp-content/uploads/2011/07/searchexamplemockup.png" alt="search-example-mockup" width="513" height="502" border="0" /></p>
<p>The app has four display areas that plug into the event bus:</p>
<ol>
<li><strong>SearchUI</strong>- accepts user input and starts the search</li>
<li><strong>ResultsUI</strong> &#8211; holds search results and handles <em>click</em>for full details</li>
<li><strong>DetailsUI</strong>- shows full details when result is clicked</li>
<li><strong>StatusUI</strong> &#8211; shows search progress and results summary.</li>
</ol>
<p>The actual search function is performed by a non-visual component called <code>SearchEngine</code>. Like the display areas, it also plugs into the bus to both observer and publish events.</p>
<p><img style="display: inline; border: 0px;" title="search-example-interaction-1" src="http://northconcepts.com/blog/wp-content/uploads/2011/07/searchexampleinteraction1.png" alt="search-example-interaction-1" width="564" height="122" border="0" /></p>
<p>When the user fills in the text field and presses the search button, the <code>SearchUI</code> publishes a <code>newSearch</code> event to the bus. The <code>SearchEngine</code> receives the notification and runs the search. The <code>StatusUI</code> also receives the notification and displays a search-in-progress message to the user.</p>
<p><img style="display: inline; border: 0px;" title="search-example-interaction-2" src="http://northconcepts.com/blog/wp-content/uploads/2011/07/searchexampleinteraction2.png" alt="searchexampleinteraction2" width="564" height="122" border="0" /></p>
<p>When the search is complete, <code>SearchEngine</code> publishes a <code>searchFinished</code> message to the bus. The <code>ResultsUI</code> receives the notification and displays the data, if any, in a table to the user. The <code>StatusUI</code> also receives the notification and displays to the user either a message with the number of results found or a message that none were found.</p>
<p><img style="display: inline; border: 0px;" title="search-example-interaction-3" src="http://northconcepts.com/blog/wp-content/uploads/2011/07/searchexampleinteraction3.png" alt="search-example-interaction-3" width="564" height="66" border="0" /></p>
<p>If results were found, the user can click one of the items in the <code>ResultsUI</code> which will publish a <code>detailsViewed</code> event. The <code>DetailsUI</code> will receive this event and display all the data for the selected record.</p>
<h2><a name="download"></a>Conclusion</h2>
<p>While GWT doesn&#8217;t support Java reflection, it does provide a powerful code generation facility. Code generation has the benefit of enabling dynamic, reflexive programming without the runtime cost. Once you get used to GWT&#8217;s approach, you&#8217;ll start seeing many opportunities to reduce your code size by leveraging GWT&#8217;s generators.</p>
<h2>Download</h2>
<p>The <a href="http://northconcepts.com/blog-downloads/event-bus-part3/NorthConcepts-EventBus-Part3.zip">event bus download</a> contains the entire source code (including Eclipse project). The source code is licensed under the terms of the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache License, Version 2.0</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2012/01/20/how-to-build-a-simple-gwt-event-bus-using-generators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Pipeline 2.2.5 Now Available</title>
		<link>http://northconcepts.com/blog/2012/01/08/data-pipeline-2-2-5-now-available/</link>
		<comments>http://northconcepts.com/blog/2012/01/08/data-pipeline-2-2-5-now-available/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 21:48:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=380</guid>
		<description><![CDATA[Added JavaBeanReader whice uses XPath expressions to identify field values and break records (see the Read from Java beans example) AbstractReader&#8217;s setStartingRow and setLastRow no return this Filter rule IsInstanceOfJavaType now returns false for null values Added number-to-date methods to &#8230; <a href="http://northconcepts.com/blog/2012/01/08/data-pipeline-2-2-5-now-available/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<ul>
<li>Added JavaBeanReader whice uses XPath expressions to identify field values and break records (see the <a href="http://northconcepts.com/data-pipeline/examples/read-from-java-beans/">Read from Java beans</a> example)</li>
<li>AbstractReader&#8217;s setStartingRow and setLastRow no return this</li>
<li>Filter rule IsInstanceOfJavaType now returns false for null values</li>
<li>Added number-to-date methods to BasicFieldTransformer (numberToDate(), minutesToDate(), hoursToDate(), and daysToDate())</li>
<li>BasicFieldTransformer.Operation and BasicFieldTransformer.StringOperation are now public classes</li>
<li>BasicFieldTransformer.add(Operation &#8230; operation) is now public</li>
<li>ConditionalTransformer is now private (use TransformingReader.filter instead)</li>
<li>TransformingReader now contains an optional Filter, allowing any transformer to be conditionally applied</li>
<li>Removed TransformingReader.add(Filter filter, Transformer &#8230; transformer) method</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2012/01/08/data-pipeline-2-2-5-now-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Manage Your Application Properties using WebDAV</title>
		<link>http://northconcepts.com/blog/2011/10/10/how-to-manage-your-application-properties-using-webdav/</link>
		<comments>http://northconcepts.com/blog/2011/10/10/how-to-manage-your-application-properties-using-webdav/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 13:58:08 +0000</pubDate>
		<dc:creator>Kuhan Paramsothy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[WebDAV]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=159</guid>
		<description><![CDATA[Managing the configuration of an application is a consistent pain-point for developers, administrators, and business analysts. Often in production environments, configuration is isolated as files on the local disk, limiting easy access by all but administrators.  Another common approach is &#8230; <a href="http://northconcepts.com/blog/2011/10/10/how-to-manage-your-application-properties-using-webdav/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Managing the configuration of an application is a consistent pain-point for developers, administrators, and business analysts.</p>
<p>Often in production environments, configuration is isolated as files on the local disk, limiting easy access by all but administrators.  Another common approach is to store configuration in a database or LDAP.  While this have benefits of a local disk file, it lacks the ability to manage properties as naturally as a file.</p>
<p>The solution proposed uses a database and WebDAV to help resolve many negatives to do with application configuration in both a simple and lightweight way.<br />
<span id="more-159"></span><br />
In the <a title="How To Write A Dead-Simple Online Network Drive using Java &amp; WebDAV" href="http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/">first blog</a> in the <a href="http://northconcepts.com/blog/category/webdav/">WebDAV series</a>, we discussed a simple example of how you can use WebDAV to create an online network drive, to illustrate how WebDAV can quickly enhance your application. I recommend reading it first before moving onto this blog.</p>
<p><center><a title="How To Write A Dead-Simple Online Network Drive using Java &amp; WebDAV" href="http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/">How To Write A Dead-Simple Online Network Drive using Java &amp; WebDAV</a></center></p>
<h2>Solution Features</h2>
<p>We can help resolve those pains and have added benefits just by using a database and WebDAV. This approach allows for accessibility (both read &amp; read/write) for a larger set of user types, and allows those users the ability to:</p>
<ul>
<li>view and edit individual properties from their favorite text editor</li>
<li>backup &amp; restore property files quickly, by just zipping them up for example</li>
<li>migrate property files from one environment to another by copying over a folder</li>
</ul>
<p>All this is packaged as a simple add-on that can be quickly integrated to your existing web application.</p>
<h2>Design</h2>
<h3>File &amp; Folder Structure</h3>
<p>This is an example of how the mapped drive would appear when mounted:</p>
<p><center><a href="http://northconcepts.com/blog/wp-content/uploads/2011/10/folders.png"><img title="folders" src="http://northconcepts.com/blog/wp-content/uploads/2011/10/folders.png" alt="" width="184" height="109" /></a></center><br />
Folders are nested and can be named accordingly to the type of properties that are held.  Property files appear as you would expect under folders (or at the root level as well).  Here we have put configuration specific to countries under the &#8220;country configs&#8221; folder with the country in the name of the file.</p>
<h3>Schema</h3>
<p>Below is the simple schema that is used:</p>
<p><center><a href="http://northconcepts.com/blog/wp-content/uploads/2011/10/webdav-example-2.png"><img class="aligncenter size-full wp-image-183" title="Schema" src="http://northconcepts.com/blog/wp-content/uploads/2011/10/webdav-example-2.png" alt="" width="572" height="129" /></a></center><br />
It allows having a recursive tree structure by the self-referencing folder table. Files contain a set of properties and existing within a folder.  Properties are stored in the property table as String key-value pairs. Basic DAO classes facilitate CRUD operations on the files and folders.</p>
<h3>Properties to File and File to Properties Transformation</h3>
<p>To keep things simple and consistent, we use the existing methods present in the <code>Properties</code> class to generate a file from properties and vice versa.  By using this method, properties appear in the file content as key-value pairs, one per line separated by &#8216;=&#8217; as you commonly see String properties represented.</p>
<h3>Usage</h3>
<p>Of course, having your application read these properties is vital.  To that effect, there is a class called WebDAVProperties where your application can both read and write properties:</p>
<p><code>Properties WebDAVProperties.getProperties(String path)</code> where path is the fully qualified path of the properties resource.</p>
<p><code>void WebDAVProperties.saveProperties(String path, Properties props)</code> where path is the fully qualified path of the properties resource.</p>
<p>Saving the properties using the above method calls the necessary DAOs to persist the properties in the schema.</p>
<h2>Implementation</h2>
<h3>Class Diagram</h3>
<p><center><a href="http://northconcepts.com/blog/wp-content/uploads/2011/10/big-class-diagram.png"><img class="aligncenter size-full wp-image-220" title="big class diagram" src="http://northconcepts.com/blog/wp-content/uploads/2011/10/big-class-diagram.png" alt="" width="758" height="557" /></a></center></p>
<h3>Model Objects</h3>
<p>There are 2 model objects in use in this approach (besides the <code>java.util.Properties</code> object). Both are under the package <code>com.northconcepts.webdav.model</code>.</p>
<p><center><a href="http://northconcepts.com/blog/wp-content/uploads/2011/10/model-class-diagram.png"><img class="aligncenter size-full wp-image-221" title="model class diagram" src="http://northconcepts.com/blog/wp-content/uploads/2011/10/model-class-diagram.png" alt="" width="385" height="79" /></a></center><br />
The <code>FolderInfo</code> self-references to allow it to keep track of it&#8217;s parent.  For each <code>FolderInfo</code>, there can be any number of <code>FileInfos</code>, directly mapping to a typical file systems model.  Each <code>FileInfo</code> has a Properties class to maintain the contents of the file.</p>
<h3>DAO</h3>
<p>Under the package com.northconcepts.webdav.dao you will see 3 DAO classes: <code>FileDAO</code>, <code>FolderDAO</code>, and <code>PropertyDAO</code>.</p>
<h4>FileDAO</h4>
<ul>
<li><code>List&lt;FileInfo&gt; getFilesInFolder(FolderInfo fi)</code> &#8211; gets the list of files that are in the specified folder</li>
</ul>
<h4>FolderDAO</h4>
<ul>
<li><code>FolderInfo getFolderById(final int id)</code> &#8211; gets the folder by it&#8217;s id</li>
<li><code>FolderInfo getParentFolder(final FolderInfo f)</code> &#8211; gets the parent folder by the provided folder&#8217;s id</li>
<li><code>List&lt;FolderInfo&gt; getSubFolders(final FolderInfo id)</code> &#8211; gets the subfolders by the provided folder&#8217;s id</li>
<li><code>FolderInfo createFolder(final String name, final FolderInfo parentFolder)</code> &#8211; persists a folder based on the specified values and returns that folder</li>
<li><code>void deleteFolder(final int id)</code> &#8211; deletes the folder by it&#8217;s id</li>
<li><code>void moveFolder(final int id, final int destFolderId, final String name)</code> &#8211; moves the folder of the specified id to below the specified destination folder id and rename the folder to the new name</li>
</ul>
<h4>PropertyDAO</h4>
<ul>
<li><code>Properties getProperties(final int fileId)</code> &#8211; gets all the property key-value pairs for the given file&#8217;s id</li>
<li><code>FileInfo createProperties(final String name, final Properties props, final FolderInfo folder)</code> &#8211; persists a properties based on the specified key-value pairs, name and folder location</li>
</ul>
<h3>WebDAV Implementation</h3>
<p title="Milton Java WebDAV Server Library">For a simple <a title="Milton Java WebDAV Server Library" href="http://milton.ettrema.com/index.html">Milton Java WebDAV Server Library</a> in-memory implementation example with just a single file and no folders, please review the previous <a title="How To Write A Dead-Simple Online Network Drive using Java &amp; WebDAV" href="http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/">blog post</a>. Similar to the aforementioned model objects, we have 2 classes implementing the various WebDAV Resource interfaces: DavFolder and DavFile.</p>
<p><a href="http://northconcepts.com/blog/wp-content/uploads/2011/10/webdav-class-diagram.png"><img class="aligncenter size-full wp-image-229" title="webdav class diagram" src="http://northconcepts.com/blog/wp-content/uploads/2011/10/webdav-class-diagram.png" alt="" width="752" height="207" /></a></p>
<h4>WebDAV Resource Interfaces</h4>
<p>Key to this part of the implementation is understand the behavior implications by implementing specific Resource interfaces.</p>
<ul>
<li><code>GetableResource</code> &#8211; allows the content of the resource can be retrieved</li>
<li><code>DeletableResource</code> &#8211; allows the resource to be deleted</li>
<li><code>CollectionResource</code> &#8211; the resource to have children implying the directory behavior</li>
<li><code>PutableResource</code> &#8211; allows file resources to be created as children below it</li>
<li><code>MakeCollectionableResource</code> &#8211; allows folder resources to be created as children below it</li>
<li><code>MoveableResource</code> &#8211; allows the resource to be moved to a child of another folder</li>
</ul>
<h4>Properties Processing</h4>
<p>Since we want to be able to read the contents of a file as well as delete the file, we need to implement the interfaces <code>GetableResource</code> and <code>DeletableResource</code>.<br />
Below is the <code>sendContent(...)</code> method of the <code>DavFile</code> class, required by the <code>GetableResource</code> interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendContent<span style="color: #009900;">&#40;</span><span style="color: #003399;">OutputStream</span> out, Range range, <span style="color: #003399;">Map</span> params, <span style="color: #003399;">String</span> contentType<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, NotAuthorizedException, BadRequestException <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">PrintStream</span> ps <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintStream</span><span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	p.<span style="color: #006633;">getProps</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span>ps<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ps.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method is using the existing list method of the <code>Properties</code> class to render the content of the file.<br />
The opposing act of persisting properties by file creation, that is driven by the <code>createNew(...)</code> method of the <code>DavFolder</code> class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> com.<span style="color: #006633;">bradmcevoy</span>.<span style="color: #006633;">http</span>.<span style="color: #006633;">Resource</span> createNew<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">InputStream</span> is, <span style="color: #003399;">Long</span> length, <span style="color: #003399;">String</span> contentType<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ConflictException, NotAuthorizedException, BadRequestException <span style="color: #009900;">&#123;</span>
	log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;attempting to create a new file in this folder name={} length={} contentType={}&quot;</span>,<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>name,length,contentType<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//CHECK IF INVALID NAME</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Util</span>.<span style="color: #006633;">isEmpty</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>
		log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bad request when creating a file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> BadRequestException<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//ASSERT NO CONFLICT WITH THE NAME</span>
	assertNoConflict<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//LOAD STREAM TO A PROPERTIES OBJECT</span>
	<span style="color: #003399;">Properties</span> p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	p.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//PERSIST THE PROPERTIES AND RETURN THE DAV-WRAPPED NCPROPERTIES</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DavFile<span style="color: #009900;">&#40;</span>PropertyDAO.<span style="color: #006633;">createProperties</span><span style="color: #009900;">&#40;</span>name,p,f<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once again we are using a provided method in the <code>Properties</code> class to parse and load the properties coming from the input stream.</p>
<h4>MyResourceFactory Implementation</h4>
<p>Finally, to be able to produce the file system structure as designed, the class implementing the <code>ResourceFactory</code> uses recursion to determine, based on the path, whether to offer back a appropriate folder, or the appropriate file.</p>
<p><center><a href="http://northconcepts.com/blog/wp-content/uploads/2011/10/resourcefactory-class-diagram.png"><img class="aligncenter size-full wp-image-232" title="resourcefactory class diagram" src="http://northconcepts.com/blog/wp-content/uploads/2011/10/resourcefactory-class-diagram.png" alt="" width="416" height="173" /></a></center><br />
The recursive search, shown below, essentially works by iterating through the path, where each part of the path is determined if it&#8217;s a folder or a file; if its a folder and there are more path parts then we recursively search within that folder with the remainder of the path.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyResourceFactory <span style="color: #000000; font-weight: bold;">implements</span> ResourceFactory <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger log <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>MyResourceFactory.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Resource getResource<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> host, <span style="color: #003399;">String</span> strPath<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Path path <span style="color: #339933;">=</span> Path.<span style="color: #006633;">path</span><span style="color: #009900;">&#40;</span>strPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getting resource - host={} path={}&quot;</span>,host,path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//STRIP PRECEEDING PATH</span>
		path <span style="color: #339933;">=</span> path.<span style="color: #006633;">getStripFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStripFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path trimmed - new webdav parts={}&quot;</span>,path.<span style="color: #006633;">getParts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">isRoot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path is just the root, returning the Root Folder&quot;</span><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;">new</span> DavFolder<span style="color: #009900;">&#40;</span>FolderDAO.<span style="color: #006633;">getRootFolder</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> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> recursePath<span style="color: #009900;">&#40;</span>path, FolderDAO.<span style="color: #006633;">getRootFolder</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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Resource recursePath<span style="color: #009900;">&#40;</span>Path path, FolderInfo parentFolder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;recursing path - path={} parentFolder={}&quot;</span>,path,parentFolder<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>FolderInfo f <span style="color: #339933;">:</span> FolderDAO.<span style="color: #006633;">getSubFolders</span><span style="color: #009900;">&#40;</span>parentFolder<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;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">getFirst</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>f.<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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</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> DavFolder<span style="color: #009900;">&#40;</span>f<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> recursePath<span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">getStripFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>FileInfo p <span style="color: #339933;">:</span> FileDAO.<span style="color: #006633;">getFilesInFolder</span><span style="color: #009900;">&#40;</span>parentFolder<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;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">getFirst</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>p.<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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</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> DavFile<span style="color: #009900;">&#40;</span>p<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> <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>
		<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>

<h2>Download &amp; Install</h2>
<h3>Step 1</h3>
<p>The <a href="http://northconcepts.com/blog-downloads/webdav-part2/NorthConcepts-WebDAV-Part2.zip">WebDAV download</a> contains the entire source code (including Eclipse project). The source code is licensed under the terms of the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache License, Version 2.0</a>. Download this source code and import the project to your Eclipse workspace.</p>
<h3>Step 2</h3>
<p>Run the MySQL <code>create_script.sql</code> script located in the project folder <code>src-database/scripts</code> in your MySQL database. If you are using another type of DB other than MySQL, please refer to the schema diagram under <code>src-database/model</code> to create the three necessary tables as illustrated.</p>
<h3>Step 3</h3>
<p>Add a <code>Resource</code> to define a data source for the application to use in your Tomcat context.xml file. Below is an example:<br />
<code>&lt;Resource name="jdbc/MySQLDB" auth="Container" type="javax.sql.DataSource" username="root" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/&gt;</code></p>
<h3>Step 4</h3>
<p>Create, if it doesn&#8217;t already exist, a Apache web server instance in Eclipse and publish the application to that server. Then start the application server.</p>
<h3>Step 5</h3>
<p>Configure your WebDAV client to point to the following URL <a href="http://localhost/webdav-example-2/config/">http://localhost/webdav-example-2/config/</a> to access the properties through WebDAV.</p>
<h2>Concluding Thoughts</h2>
<p>Certainly we have all felt the pain of managing application configuration from development environments through to production. This approach attempts to solve some of this pain. The example code provided can be plugged into your application to allow you to quickly move forward on improving the configuration efficiency and accessibility. More valuable, the design and implementation could inspire you to consider other objects your application uses or persists which can be expressed as a file system, giving you unprecedented accessibility.</p>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2011/10/10/how-to-manage-your-application-properties-using-webdav/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Use dynamic proxies to create a simple, powerful event bus (Part 2)</title>
		<link>http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/</link>
		<comments>http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 10:37:53 +0000</pubDate>
		<dc:creator>Dele Taylor</dc:creator>
				<category><![CDATA[Dynamic Proxies]]></category>
		<category><![CDATA[Event Bus]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=124</guid>
		<description><![CDATA[In part 1 of the event bus series we discussed implementing a simple and powerful event bus using just three classes. If you haven&#8217;t read it yet, I strongly recommend you read it first. Use dynamic proxies to create a &#8230; <a href="http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/" target="_blank">part 1</a> of the event bus series we discussed implementing a simple and powerful event bus using just three classes. If you haven&#8217;t read it yet, I strongly recommend you read it first.</p>
<blockquote><p><a href="http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/" target="_blank">Use dynamic proxies to create a simple, powerful event bus (Part 1)</a></p></blockquote>
<p>In this blog we&#8217;ll build on part 1 by adding several important features to the event bus to make it production ready.  <span id="more-124"></span>Since these features are fairly well contained, you can start with the section that most interests you, <a href="#download">download the code</a>, or <a href="https://github.com/NorthConcepts/event-bus" target="_blank">view the project on GitHub</a>.</p>
<ul>
<li><a href="#event_filtering">Event Filtering</a></li>
<li><a href="#event_topics">Event Topics</a></li>
<li><a href="#untyped_listeners">Untyped Listeners</a></li>
<li><a href="#exception_handling">Exception Handling</a></li>
<li><a href="#garbage_collection">Garbage Collection</a></li>
<li><a href="#thread_local_events">Thread-local Events</a></li>
</ul>
<h2><a name="event_filtering"></a>Event Filtering</h2>
<p>Event filters help reduce noise by allowing listeners to automatically ignore events they would normally receive. This is accomplished by including a new <code>EventFilter</code> object alongside every listener added to the bus.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventBus <span style="color: #009900;">&#123;</span>
&nbsp;
	...
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> addListener<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> eventListenerClass,
		EventFilter filter, T listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The <code>EventFilter</code> class has a single abstract <code>allow(event, listener)</code> method that returns true if the event should be delivered to the listener.</p>
<p><img class="alignnone size-full wp-image-121" title="Event Filters2" src="http://northconcepts.com/blog/wp-content/uploads/2011/08/Event-Filters2.png" alt="" width="757" height="196" /></p>
<p>Several filters are already are part of the bus, including <code>EventSourceFilter</code>. This filter only allow events that were produced by one of a set of sources.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventSourceFilter <span style="color: #000000; font-weight: bold;">implements</span> EventFilter <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;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> source<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> EventSourceFilter<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> ... <span style="color: #006633;">source</span><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;">source</span> <span style="color: #339933;">=</span> source<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> allow<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;?&gt;</span> event, <span style="color: #003399;">Object</span> listener<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>source <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> source.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</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: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> s <span style="color: #339933;">:</span> source<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>event.<span style="color: #006633;">getEventSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #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;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In addition to the new EventFilter interface and the addListener update, a few other changes are needed to handle filters:</p>
<ol>
<li>A new <code>EventListenerStub</code> class to hold a listener and filter together.</li>
<li>The <code>eventListeners</code> field on EventBus is converted from a <code>Set&lt;? extends EventListener&gt;</code> to a <code>List&lt;EventListenerStub&lt;? extends EventListener&gt;&gt;</code>. We swap <code>Set</code> for <code>List</code> because we can no longer depend on the listener&#8217;s object identity to prevent duplicate listeners in the set (now that it&#8217;s wrapped in an <code>EventListenerStub</code>).</li>
<li>The bus&#8217; <code>deliverEvent()</code> method calls <code>EventFilter.allow()</code>to check if the event should be delivered.</li>
</ol>
<p>Here&#8217;s a quick example of <code>EventSourceFilter</code> in action.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventSourceFilterExample <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: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		EventSourceFilterExample eventSource1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventSourceFilterExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		EventSourceFilterExample eventSource2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventSourceFilterExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Location store <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Location<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// listen for events from eventSource1</span>
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>,
				<span style="color: #000000; font-weight: bold;">new</span> EventSourceFilter<span style="color: #009900;">&#40;</span>eventSource1<span style="color: #009900;">&#41;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;walking from eventSource1...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// listen for events from eventSource1 or eventSource2</span>
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>,
				<span style="color: #000000; font-weight: bold;">new</span> EventSourceFilter<span style="color: #009900;">&#40;</span>eventSource1, eventSource2<span style="color: #009900;">&#41;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;walking from eventSource1 or eventSource2...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// eventSource2 is set as the source for this publisher</span>
		WalkListener publisher <span style="color: #339933;">=</span> eventBus.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventSource2, WalkListener.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;The End.&quot;</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></td></tr></table></div>

<h2><a name="event_topics"></a>Event Topics</h2>
<p>It&#8217;s not always ideal for us to filter by the event&#8217;s source. Sometimes it&#8217;s more convenient to filter by a value known to both publishers and listeners. That&#8217;s where topics come in. Topic values can bet passed as an optional parameter while obtaining a publisher from the bus. Every method call made on that publisher will include the topic value. Listeners can watch for specific topics by including a <code>TopicFilter</code> when registering with the bus. Topics can be any <code>java.lang.Object</code> or subclass we like. I recommend enums because they are IDE and refactoring friendly, but a string or any object will do.</p>
<h3>Publishers</h3>
<p>A few small changes on the publisher side will handle the registration and propagation of topics for us. First, both the <code>Event</code> and <code>EventSourceInvocationHandler</code> classes will track topics. <code>EventSourceInvocationHandler</code> will have it set during construction and pass it on to every event it creates.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventSourceInvocationHandler<span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">InvocationHandler</span> <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> EventBus eventBus<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> eventSource<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> eventListenerClass<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> topic<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> EventSourceInvocationHandler<span style="color: #009900;">&#40;</span>EventBus eventBus, <span style="color: #003399;">Object</span> eventSource,
		Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> eventListenerClass, <span style="color: #003399;">Object</span> topic<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ...
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> invoke<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> proxy, <span style="color: #003399;">Method</span> method, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arguments<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>
        eventBus.<span style="color: #006633;">publishEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>eventSource, proxy, eventListenerClass,
			topic, method, arguments<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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></td></tr></table></div>

<p>Second, the bus&#8217; <code>getPublisher()</code> method will be overloaded to take in a topic value and set it on the <code>EventSourceInvocationHandler</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventBus <span style="color: #009900;">&#123;</span>
&nbsp;
	...
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span>  T getPublisher<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> eventSource, <span style="color: #000000; font-weight: bold;">Class</span> eventListenerClass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> getPublisher<span style="color: #009900;">&#40;</span>eventSource, eventListenerClass, <span style="color: #000066; font-weight: bold;">null</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>  T getPublisher<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> eventSource, <span style="color: #000000; font-weight: bold;">Class</span> eventListenerClass, <span style="color: #003399;">Object</span> topic<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		EventSourceInvocationHandler handler <span style="color: #339933;">=</span>
			<span style="color: #000000; font-weight: bold;">new</span> EventSourceInvocationHandler<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, eventSource, eventListenerClass, topic<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #003399;">Proxy</span>.<span style="color: #006633;">newProxyInstance</span><span style="color: #009900;">&#40;</span>
				eventListenerClass.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>eventListenerClass<span style="color: #009900;">&#125;</span>,
				handler<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></td></tr></table></div>

<h3>Listeners</h3>
<p>The <code>TopicFilter</code> class allows listeners to receive events matching one or more topic values. The implementation uses exact matching, but if that&#8217;s too simplistic for your case, it should be easy enough to implement your own hierarchical or pattern matching filter.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TopicFilter <span style="color: #000000; font-weight: bold;">implements</span> EventFilter <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;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> topic<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> TopicFilter<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span>... <span style="color: #006633;">topic</span><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;">topic</span> <span style="color: #339933;">=</span> topic<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> allow<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;?&gt;</span> event, <span style="color: #003399;">Object</span> listener<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>topic <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> topic.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</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: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> t <span style="color: #339933;">=</span> event.<span style="color: #006633;">getTopic</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> t2 <span style="color: #339933;">:</span> topic<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>t <span style="color: #339933;">==</span> t2 <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>t <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> t.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>t2<span style="color: #009900;">&#41;</span><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> <span style="color: #000066; font-weight: bold;">true</span><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;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here&#8217;s an example that uses enum topics to publish and filter events.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TopicFilterExample <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: #000000; font-weight: bold;">enum</span> Topic <span style="color: #009900;">&#123;</span> STROLL, RUSH <span style="color: #009900;">&#125;</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: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		TopicFilterExample eventSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TopicFilterExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Location store <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Location<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// listen for events under the STROLL topic</span>
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>,
				<span style="color: #000000; font-weight: bold;">new</span> TopicFilter<span style="color: #009900;">&#40;</span>Topic.<span style="color: #006633;">STROLL</span><span style="color: #009900;">&#41;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;walking slowly...out for a stroll&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// listen for events under the RUSH topic</span>
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>,
				<span style="color: #000000; font-weight: bold;">new</span> TopicFilter<span style="color: #009900;">&#40;</span>Topic.<span style="color: #006633;">RUSH</span><span style="color: #009900;">&#41;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;walking quickly...in a rush&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// RUSH is set as the topic for this publisher</span>
		WalkListener publisher <span style="color: #339933;">=</span> eventBus.
			<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventSource, WalkListener.<span style="color: #000000; font-weight: bold;">class</span>, Topic.<span style="color: #006633;">RUSH</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;The End.&quot;</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></td></tr></table></div>

<h2><a name="untyped_listeners"></a>Untyped Listeners</h2>
<p>Some use-cases require that we listen to all events passing through the bus regardless of their type. Audit, monitoring, or even debug requirements might call for such a feature. In these special cases we can register an <code>UntypedEventListener</code> with the bus. These listeners are notified in the form of the <code>Event</code> object after all typed listeners have received their notifications.</p>
<h3>EventListenerStub Refactoring</h3>
<p>Now that we&#8217;re handling both typed and untyped listeners, <code>EventListenerStub</code> will undergo refactoring to reduce code duplication. First, <code>EventListenerStub</code> is divided into three classes. The original <code>EventListenerStub</code> and two inner subclasses: <code>Typed</code> and <code>Untyped</code>. The new subclasses are responsible for providing the delivery logic for their kind of listeners while allowing <code>EventListenerStub</code> to handle filtering and other common behaviour.</p>
<p><img class="alignnone size-full wp-image-123" title="EventListenerStub" src="http://northconcepts.com/blog/wp-content/uploads/2011/08/EventListenerStub.png" alt="" width="695" height="273" /></p>
<p>Just as before, the <code>Typed</code> subclass invokes the original called method on each listener.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Typed<span style="color: #339933;">&lt;</span>S <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> EventListenerStub<span style="color: #339933;">&lt;</span>S<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
        ...
        <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEventImpl<span style="color: #009900;">&#40;</span>EventBus eventBus, Event<span style="color: #339933;">&lt;?&gt;</span> event<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>
                S listener <span style="color: #339933;">=</span> getListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                event.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>listener, event.<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>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>While the <code>Untyped</code> subclass calls the onEvent method on untyped listeners.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Untyped <span style="color: #000000; font-weight: bold;">extends</span> EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
        ...
        <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEventImpl<span style="color: #009900;">&#40;</span>EventBus eventBus, Event<span style="color: #339933;">&lt;?&gt;</span> event<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>
                UntypedEventListener listener <span style="color: #339933;">=</span> getListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                listener.<span style="color: #006633;">onEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>EventBus Refactoring</h3>
<p>The event bus gets a few upgrades.</p>
<ol>
<li>The old <code>eventListeners</code> field is renamed to <code>typedListeners</code> to compliment the new <code>untypedListeners</code> field.</li>
<li>Both versions of addListener (typed and untyped) take care to add the correct subclass of EventListenerStub to their lists.</li>
<li>The bus&#8217; <code>deliverEvent</code> method now delegates to the <code>deliverEvent</code> method on each stub to ensure the correct delivery behaviour.</li>
</ol>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventBus <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> Map<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span>, TypedListenerList<span style="color: #339933;">&lt;?&gt;&gt;</span> typedListeners <span style="color: #339933;">=</span>
		<span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span>, TypedListenerList<span style="color: #339933;">&lt;?&gt;&gt;</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;">private</span> <span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;&gt;</span> untypedListeners <span style="color: #339933;">=</span>
		<span style="color: #000000; font-weight: bold;">new</span> CopyOnWriteArrayList<span style="color: #339933;">&lt;</span>EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;&gt;</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;">public</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> addListener<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> eventListenerClass, EventFilter filter, T listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		TypedListenerList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> list<span style="color: #339933;">;</span>
		...
		<span style="color: #006633;">list</span>.<span style="color: #006633;">getList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EventListenerStub.<span style="color: #006633;">Typed</span><span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>filter, listener<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addListener<span style="color: #009900;">&#40;</span>EventFilter filter, UntypedEventListener listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		untypedListeners.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EventListenerStub.<span style="color: #006633;">Untyped</span><span style="color: #009900;">&#40;</span>filter, listener<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEvent<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> event<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>
		<span style="color: #666666; font-style: italic;">// Deliver to typed listeners</span>
		TypedListenerList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> getTypedListenerList<span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getEventListenerClass</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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>list <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EventListenerStub<span style="color: #339933;">&lt;?&gt;</span> stub <span style="color: #339933;">:</span> list.<span style="color: #006633;">getList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				stub.<span style="color: #006633;">deliverEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<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: #666666; font-style: italic;">// Deliver to untyped listeners</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span> stub <span style="color: #339933;">:</span> untypedListeners<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			stub.<span style="color: #006633;">deliverEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here&#8217;s an example of an untyped listener watching all events on the bus.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UntypedEventListenerExample <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: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		UntypedEventListenerExample eventSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UntypedEventListenerExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Location store <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Location<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> UntypedEventListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onEvent<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;?&gt;</span> event<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;event: &quot;</span> <span style="color: #339933;">+</span> event.<span style="color: #006633;">getEventListenerClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
                    <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">+</span> event.<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
                  <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		WalkListener publisher <span style="color: #339933;">=</span> eventBus.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventSource, WalkListener.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;The End.&quot;</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></td></tr></table></div>

<h2><a name="exception_handling"></a>Exception Handling</h2>
<p>So far we haven&#8217;t said anything about exceptions. If you&#8217;ve looked at the source code from part 1, you&#8217;ll see that exceptions were caught, wrapped in RuntimeExceptions, and then rethrown. This saved us from the extra coding to declare checked exceptions, but did nothing to actually handle them. What we need now is an exception handling strategy that:</p>
<ol>
<li>Handles all exceptions regardless of which thread throws them</li>
<li>Handles exceptions for both typed and untyped listeners</li>
<li>Doesn&#8217;t require knowledge of the bus&#8217; internal workings</li>
<li>Is decoupled from listeners</li>
<li>Is simple to use</li>
</ol>
<h3>Exception Events</h3>
<p>We can accomplish all of these requirements by simply leveraging the bus itself. By letting the event bus publish exceptions like any other event, we gain all of its advantages (decoupling, threading, filters, etc.) for the price of a small change.</p>
<p><img class="alignnone size-full wp-image-126" title="Exceptions" src="http://northconcepts.com/blog/wp-content/uploads/2011/08/Exceptions.png" alt="" width="691" height="256" /></p>
<p>The new <code>ExceptionListener</code> interface is used for both publishing and observing exceptions. The bus holds a publisher reference which it exposes through its <code>handleException</code> method.<code> </code>Since the bus delegates the actual event delivery to <code>EventListenerStub</code>, that class is also responsible for reporting exceptions to the bus by calling <code>handleException</code>. The <code>handleException</code> method takes care not to introduce infinite loops by <em>not</em> publishing exceptions for events where it was the publisher.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventBus <span style="color: #009900;">&#123;</span>
&nbsp;
	...
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> ExceptionListener exceptionPublisher<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		exceptionPublisher <span style="color: #339933;">=</span> getPublisher<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, ExceptionListener.<span style="color: #000000; font-weight: bold;">class</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> <span style="color: #000066; font-weight: bold;">void</span> addExceptionListener<span style="color: #009900;">&#40;</span>EventFilter filter, ExceptionListener listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		addListener<span style="color: #009900;">&#40;</span>ExceptionListener.<span style="color: #000000; font-weight: bold;">class</span>, filter, listener<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;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> handleException<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;?&gt;</span> event, <span style="color: #003399;">Object</span> listener, <span style="color: #003399;">Throwable</span> e<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>e <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">InvocationTargetException</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e <span style="color: #339933;">=</span> e.<span style="color: #006633;">getCause</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;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getEventPublisher</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> exceptionPublisher<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			exceptionPublisher.<span style="color: #006633;">onException</span><span style="color: #009900;">&#40;</span>event, e, listener<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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This example prints each exception&#8217;s message along with the event class&#8217; name and method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExceptionListenerExample <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: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		ExceptionListenerExample eventSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ExceptionListenerExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Location store <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Location<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;always fails&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		eventBus.<span style="color: #006633;">addExceptionListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> ExceptionListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onException<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;?&gt;</span> event, <span style="color: #003399;">Throwable</span> exception, <span style="color: #003399;">Object</span> targetListener<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;exception in &quot;</span> <span style="color: #339933;">+</span> event.<span style="color: #006633;">getEventListenerClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.&quot;</span> <span style="color: #339933;">+</span> event.<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: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> exception.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		WalkListener publisher <span style="color: #339933;">=</span> eventBus.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventSource, WalkListener.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;The End.&quot;</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></td></tr></table></div>

<h2><a name="garbage_collection"></a>Garbage Collection</h2>
<p>Memory leaks are a real concern when using the observer pattern. Observers that are never unregistered may never get garbage collected and your application&#8217;s memory could continue to grow until it eventually fails with an <code>OutOfMemoryError</code>. Memory leaks are arguably even more of a concern when working with event buses because they are responsible for holding on to all observers for all subjects. If a bus lives for the entire run of your app while holding on to every single all observer, we could get that <code>OutOfMemoryError</code> even sooner.</p>
<h3>Soft References</h3>
<p>The <code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/ref/package-summary.html" target="_blank">java.lang.ref</a></code> package is Java&#8217;s answer to allowing objects to be garbage collected, even while there are still live references to them. The key is to wrap those objects with one of the classes in this package. In the event bus, we now wrap each listener in a <code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/ref/SoftReference.html" target="_blank">SoftReference</a></code>. This allows listeners to be automatically garbage collected when memory gets low, but now introduces a level of indirection to our code. We chose to use <code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/ref/SoftReference.html" target="_blank">SoftReference</a></code> over a <code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html" target="_blank">WeakReference</a></code> because they are garbage collected less aggressively by the JVM. Since most listeners tend to be anonymous inner classes, only referenced by the bus, I&#8217;d hate to see them disappear while the system is still flush with memory. (For more on Java references, read Ethan Nicholas&#8217; classic blog <a href="http://weblogs.java.net/blog/2006/05/04/understanding-weak-references" target="_blank">Understanding Weak References</a>. )</p>
<h3>EventListenerStub</h3>
<p>The code that manages listeners through soft references can e found in <code>EventListenerStub</code> and its two subclasses — <code>Typed</code> and <code>Untyped</code>. <code>EventListenerStub</code> wraps the listener and ensures the getter method is used for all access. The <code>Typed</code> and <code>Untyped</code> subclasses now perform null checks (in case listeners were garbage collected) before actually delivering events.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> EventListenerStub<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <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> Reference<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> listener<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> EventListenerStub<span style="color: #009900;">&#40;</span>EventFilter filter, T listener, ReferenceQueue<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">super</span> T<span style="color: #339933;">&gt;</span> referenceQueue<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;">listener</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoftReference<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>listener, referenceQueue<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;">protected</span> T getListener<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> listener.<span style="color: #006633;">get</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;
	...
&nbsp;
	<span style="color: #666666; font-style: italic;">//====================================================================</span>
	<span style="color: #666666; font-style: italic;">//  Typed Implementation</span>
	<span style="color: #666666; font-style: italic;">//====================================================================</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Typed<span style="color: #339933;">&lt;</span>S <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> EventListenerStub<span style="color: #339933;">&lt;</span>S<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
		...
		<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEventImpl<span style="color: #009900;">&#40;</span>EventBus eventBus, Event<span style="color: #339933;">&lt;?&gt;</span> event<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>
			S listener <span style="color: #339933;">=</span> getListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>listener <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// in case it was garbage collected</span>
				event.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>listener, event.<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>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//====================================================================</span>
	<span style="color: #666666; font-style: italic;">//  Untyped Implementation</span>
	<span style="color: #666666; font-style: italic;">//====================================================================</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Untyped <span style="color: #000000; font-weight: bold;">extends</span> EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
		...
		<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEventImpl<span style="color: #009900;">&#40;</span>EventBus eventBus, Event<span style="color: #339933;">&lt;?&gt;</span> event<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>
			UntypedEventListener listener <span style="color: #339933;">=</span> getListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>listener <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// in case it was garbage collected</span>
				listener.<span style="color: #006633;">onEvent</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Reference Clean-up</h3>
<p>You might have noticed the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/ref/ReferenceQueue.html" target="_blank"><code>ReferenceQueue</code></a> parameter in <code>EventListenerStub</code>&#8216;s constructor. Reference queues allow the garbage collector to inform us when the objects in the soft references are actually released. The exact details differ for each kind of reference, but generally the garbage collector will put released references into the queue for us. We simply have to poll the queue periodically to be notified. Using reference queues allow us to be very efficient at detecting garbage collection, it saves us from having to test every listener reference to do a proper clean-up.</p>
<p>The event bus now holds two reference queues for garbage. One queue for typed listeners and the other for untyped. The queues are passed into the <code>EventListenerStub</code> subclasses along with each listener during registration. Finally, each event delivery now finishes with a call to the bus&#8217; <code>cleanupGarbage()</code> method which polls the queues and removes anything found there. Although <code>cleanupGarbage()</code> returns very quickly when queues are empty, you could probably come up with an even better approach than checking every time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventBus <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> ReferenceQueue<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">super</span> EventListener<span style="color: #339933;">&gt;</span> garbageTypedListeners <span style="color: #339933;">=</span>
		<span style="color: #000000; font-weight: bold;">new</span> ReferenceQueue<span style="color: #339933;">&lt;</span>EventListener<span style="color: #339933;">&gt;</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;">private</span> <span style="color: #000000; font-weight: bold;">final</span> ReferenceQueue<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span> garbageUntypedListeners <span style="color: #339933;">=</span>
		<span style="color: #000000; font-weight: bold;">new</span> ReferenceQueue<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	...
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> addListener<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> eventListenerClass,
		EventFilter filter, T listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		...
		<span style="color: #006633;">list</span>.<span style="color: #006633;">getList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EventListenerStub.<span style="color: #006633;">Typed</span><span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>filter, listener, garbageTypedListeners<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> removeTypedListenerReference<span style="color: #009900;">&#40;</span>Reference<span style="color: #339933;">&lt;?&gt;</span> reference<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addListener<span style="color: #009900;">&#40;</span>EventFilter filter, UntypedEventListener listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		untypedListeners.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EventListenerStub.<span style="color: #006633;">Untyped</span><span style="color: #009900;">&#40;</span>filter, listener, garbageUntypedListeners<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> removeUntypedListenerReference<span style="color: #009900;">&#40;</span>Reference<span style="color: #339933;">&lt;?&gt;</span> reference<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEvent<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> event<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>
		...
		<span style="color: #006633;">cleanupGarbage</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;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> cleanupGarbage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Reference<span style="color: #339933;">&lt;?&gt;</span> reference<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>reference <span style="color: #339933;">=</span> garbageTypedListeners.<span style="color: #006633;">poll</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: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			removeTypedListenerReference<span style="color: #009900;">&#40;</span>reference<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;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>reference <span style="color: #339933;">=</span> garbageUntypedListeners.<span style="color: #006633;">poll</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: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			removeUntypedListenerReference<span style="color: #009900;">&#40;</span>reference<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here&#8217;s an example that shows soft references surviving a forced garbage collection (<code>System.gc()</code>), but not a true memory exhaustion (allocating 100 GB or RAM). This example was run with the following configuration:</p>
<ul>
<li>Sun/Oracle&#8217;s JDK 1.6.0.10 running in Eclipse 3.5</li>
<li>Windows XP Professional, Service Pack 3</li>
<li>3 GB or RAM</li>
<li>Swap file disabled</li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GarbageCollectionExample <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: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		GarbageCollectionExample eventSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GarbageCollectionExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Location store <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Location<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;walking...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		WalkListener publisher <span style="color: #339933;">=</span> eventBus.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventSource, WalkListener.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Force GC.  Should remove weak references.</span>
		<span style="color: #666666; font-style: italic;">// Soft references should not be affected</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">gc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Allocate memory until out-of-memory</span>
		<span style="color: #666666; font-style: italic;">// garbage collector remove soft references</span>
		List<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span> bytes <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>byte<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				bytes.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// allocate 10MB blocks</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</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;
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;The End.&quot;</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></td></tr></table></div>

<h2><a name="thread_local_events"></a>Thread-local Events</h2>
<p>In the <a href="#untyped_listeners">Untyped Listeners</a> section we saw how to implement a bus wide listener. This gave us access to the raw <code>Event</code> object containing things like the event source, listener interface, and invoked method. Unfortunately, this forced us into choice. Either we listen for typed events and used whatever parameters the listener interface defines or go untyped and have the full <code>Event</code> object. Going untyped means extra filtering coding to select the exact event we&#8217;re interested in. Fortunately, we don&#8217;t have to make that choice.  We can continue to work with typed listeners and access the full event using a thread-local when needed.</p>
<h3>Thread-locals</h3>
<p>Thread-locals allow us attach values to the current thread then access those values from any method running on that thread. This saves us having to modify methods to explicitly pass values around. For the event bus, this means linking the <code>Event</code> object to the delivery thread which we can then access anywhere, including inside a typed listener.</p>
<p>The event bus contains all of the changes:</p>
<ol>
<li>A new thread-local field for the event (<code>currentEvent</code>)</li>
<li>A getter method for use anywhere (as long as it&#8217;s in the delivery thread)</li>
<li>Setting and removing the current event during delivery</li>
</ol>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventBus <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> ThreadLocal<span style="color: #339933;">&lt;</span>Event<span style="color: #339933;">&lt;?&gt;&gt;</span> currentEvent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ThreadLocal<span style="color: #339933;">&lt;</span>Event<span style="color: #339933;">&lt;?&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	...
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> Event<span style="color: #339933;">&lt;?&gt;</span> getCurrentEvent<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> currentEvent.<span style="color: #006633;">get</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;">protected</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> EventListener<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> deliverEvent<span style="color: #009900;">&#40;</span>Event<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> event<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>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			currentEvent.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Deliver to typed listeners</span>
			TypedListenerList<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> getTypedListenerList<span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getEventListenerClass</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: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>list <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EventListenerStub<span style="color: #339933;">&lt;?&gt;</span> stub <span style="color: #339933;">:</span> list.<span style="color: #006633;">getList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					stub.<span style="color: #006633;">deliverEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<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: #666666; font-style: italic;">// Deliver to untyped listeners</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EventListenerStub<span style="color: #339933;">&lt;</span>UntypedEventListener<span style="color: #339933;">&gt;</span> stub <span style="color: #339933;">:</span> untypedListeners<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				stub.<span style="color: #006633;">deliverEvent</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			cleanupGarbage<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>
			currentEvent.<span style="color: #006633;">remove</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This example uses the thread-local&#8217;s event to display the name of the invoked method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThreadLocalExample <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: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
		ThreadLocalExample eventSource <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ThreadLocalExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Location store <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Location<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;">final</span> EventBus eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventBus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">addListener</span><span style="color: #009900;">&#40;</span>WalkListener.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">new</span> WalkListener<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> walkTo<span style="color: #009900;">&#40;</span>Location location<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;called &quot;</span> <span style="color: #339933;">+</span> eventBus.<span style="color: #006633;">getCurrentEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		WalkListener publisher <span style="color: #339933;">=</span> eventBus.<span style="color: #006633;">getPublisher</span><span style="color: #009900;">&#40;</span>eventSource, WalkListener.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		publisher.<span style="color: #006633;">walkTo</span><span style="color: #009900;">&#40;</span>store<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>2000L<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		eventBus.<span style="color: #006633;">shutdown</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;The End.&quot;</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></td></tr></table></div>

<h2>Next Time</h2>
<p>That&#8217;s a wrap. We&#8217;ve covered a lot of ground in this blog. Hopefully, I left you with a few ideas you can use in your own event bus or applications.  Do you have a question or suggestion to improve some of the ideas we&#8217;ve discussed?  Leave a comment, I&#8217;d like to hear about it.</p>
<p>In part 3 of the Event Bus Series, we&#8217;ll discuss implementing the event bus using GWT (Google Web Toolkit) Generators. Stay tuned.</p>
<h2><a name="download"></a>Download</h2>
<p>The <a href="http://northconcepts.com/blog-downloads/event-bus-part2/NorthConcepts-EventBus-Part2.zip">event bus download</a> contains the entire source code (including Eclipse project).  Alternatively, you can <a href="https://github.com/NorthConcepts/event-bus" target="_blank">view the project on GitHub</a>. The source code is licensed under the terms of the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache License, Version 2.0</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2011/08/12/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How To Write A Dead-Simple Online Network Drive using Java &amp; WebDAV</title>
		<link>http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/</link>
		<comments>http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 04:03:22 +0000</pubDate>
		<dc:creator>Kuhan Paramsothy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[WebDAV]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=53</guid>
		<description><![CDATA[There are a variety of ways you can enhance your application with WebDAV access which we will be going into in a latter part of the WebDAV series. For now we will show you how to create a basic online &#8230; <a href="http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are a variety of ways you can enhance your application with WebDAV access which we will be going into in a latter part of the WebDAV series. For now we will show you how to create a basic online network drive using WebDAV via Milton, a Java WebDAV Server library.<br />
<span id="more-53"></span></p>
<h2>Example</h2>
<p>We will write the logic to create a network drive containing just one file, scratchpad.txt, which can be modified at will from any location where the drive is mounted.<br />
You may want to load the project into your IDE so that you can follow along.</p>
<h3>Step 1: Create The Domain Object</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Scratchpad <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Scratchpad INSTANCE <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scratchpad<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;">private</span> <span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifiedDate<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Date</span> createdDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</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;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Scratchpad get<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> INSTANCE<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> getText<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;">this</span>.<span style="color: #006633;">text</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setText<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<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;">text</span> <span style="color: #339933;">=</span> text<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">modifiedDate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</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;">public</span> <span style="color: #003399;">Date</span> getModifiedDate<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> modifiedDate<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;">Date</span> getCreatedDate<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> createdDate<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a simple POJO which will allow us to hold some text and attributes which will nicely represent a virtual scratchpad.txt file. Your applications could have something similar which would benefit from being exposed though WebDAV.</p>
<h3>Step 2: Create The Equivalent Resource Class As A Composition</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScratchpadResource <span style="color: #000000; font-weight: bold;">implements</span> PropFindableResource, GetableResource <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FILENAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;scratchpad.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getFilename<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> FILENAME<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> getUniqueId<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> getFilename<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: #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> getFilename<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: #003399;">Object</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> user, <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;">return</span> <span style="color: #0000ff;">&quot;anonymous&quot;</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> authorise<span style="color: #009900;">&#40;</span><span style="color: #003399;">Request</span> request, <span style="color: #003399;">Method</span> method, Auth auth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getRealm<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: #000066; font-weight: bold;">null</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: #003399;">Date</span> getCreateDate<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> Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCreatedDate</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: #003399;">Date</span> getModifiedDate<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> Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getModifiedDate</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: #003399;">String</span> checkRedirect<span style="color: #009900;">&#40;</span><span style="color: #003399;">Request</span> request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getMaxAgeSeconds<span style="color: #009900;">&#40;</span>Auth auth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getContentType<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> accepts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;text/plain&quot;</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: #003399;">Long</span> getContentLength<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: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</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;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendContent<span style="color: #009900;">&#40;</span><span style="color: #003399;">OutputStream</span> out, Range range, <span style="color: #003399;">Map</span> params, <span style="color: #003399;">String</span> contentType<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, NotAuthorizedException, BadRequestException <span style="color: #009900;">&#123;</span>
		out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBytes</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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this step, we have created a class with a “has-a” relationship with the original domain object. The key part here is to note which interfaces are implemented. Implementing PropFindableResource allows the file or folder to be searchable. Implementing GetableResource allows you to define how WebDAV will get the content of the virtual file or folder. You should pick the interfaces based on the behavior of the resource. In this example, we want the file to be able to be read, and modified, but never deleted. Subsequently we have implemented only the required interfaces. For more details on these interfaces, please refer to the <a href="http://milton.ettrema.com/reference/index.html" target="_blank">Milton JavaDoc</a>.<br />
The methods implementations are done in this way so that the virtual scratchpad.txt file behaves like you would expect a text file to behave. Notably sendContent(&#8230;) will get the text stored in the POJO and supply it to the OutputStream.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RootResource <span style="color: #000000; font-weight: bold;">implements</span> PropFindableResource, CollectionResource, PutableResource <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FOLDERNAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getUniqueId<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> FOLDERNAME<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> 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> FOLDERNAME<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;">Object</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> user, <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;">return</span> <span style="color: #0000ff;">&quot;anonymous&quot;</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> authorise<span style="color: #009900;">&#40;</span><span style="color: #003399;">Request</span> request, <span style="color: #003399;">Method</span> method, Auth auth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getRealm<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: #000066; font-weight: bold;">null</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: #003399;">Date</span> getCreateDate<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> Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCreatedDate</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: #003399;">Date</span> getModifiedDate<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> Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCreatedDate</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: #003399;">String</span> checkRedirect<span style="color: #009900;">&#40;</span><span style="color: #003399;">Request</span> request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Resource child<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> childName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> getChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">List</span> resources <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		resources.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ScratchpadResource<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: #000000; font-weight: bold;">return</span> resources<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Resource createNew<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> newName, <span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">Long</span> length, <span style="color: #003399;">String</span> contentType<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ConflictException, NotAuthorizedException, BadRequestException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ScratchpadResource.<span style="color: #006633;">getFilename</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>newName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">StringWriter</span> writer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			IOUtils.<span style="color: #006633;">copy</span><span style="color: #009900;">&#40;</span>inputStream, writer, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			Scratchpad.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>writer.<span style="color: #006633;">toString</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: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ScratchpadResource<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;">else</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> BadRequestException<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</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: #009900;">&#125;</span></pre></div></div>

<p>To keep the file-structure analogy going, we should have a root folder to contain our one file. That’s the purpose of the RootResource class. Once again, key here is to determine which interfaces are implemented based on the behavior that we seek. Implementing PutableResource allows files to be created within this folder using the PUT request.<br />
The methods implementations are done in this way so that the scratchpad.txt file can have it&#8217;s contents modified. Any other file that is attempted to be created within the root folder will return an error.</p>
<h3>Step 3: Implement The ResourceFactory To Create Your Tree Structure</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyResourceFactory <span style="color: #000000; font-weight: bold;">implements</span> ResourceFactory <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> Resource getResource<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> host, <span style="color: #003399;">String</span> strPath<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Path path <span style="color: #339933;">=</span> Path.<span style="color: #006633;">path</span><span style="color: #009900;">&#40;</span>strPath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//STRIP PRECEEDING PATH</span>
		path <span style="color: #339933;">=</span> path.<span style="color: #006633;">getStripFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStripFirst</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>path.<span style="color: #006633;">isRoot</span><span style="color: #009900;">&#40;</span><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> <span style="color: #000000; font-weight: bold;">new</span> RootResource<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;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #006633;">getFirst</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>ScratchpadResource.<span style="color: #006633;">getFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><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> <span style="color: #000000; font-weight: bold;">new</span> ScratchpadResource<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;">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>Here is where you define how your tree structure will work. What you want to do is think about which resource is being referred to for a given URL. In our simple case, we have just two possible valid URLS: http:///scratchpad/ and http:///scratchpad/scratchpad.txt .<br />
This makes defining our tree structure pretty simple to code up as you can see below. If your tree structure is more complex with several child folders, you may want to consider a recursive strategy.</p>
<h3>Step 4: Implement The ResourceFactoryFactory To Allow Milton To Access Your Resource Classes</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyResourceFactoryFactory <span style="color: #000000; font-weight: bold;">implements</span> ResourceFactoryFactory <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> AuthenticationService authenticationService<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> MyResourceFactory resourceFactory<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> ResourceFactory createResourceFactory<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> resourceFactory<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> WebDavResponseHandler createResponseHandler<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> DefaultWebDavResponseHandler<span style="color: #009900;">&#40;</span>authenticationService<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;">void</span> init<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;">if</span> <span style="color: #009900;">&#40;</span>authenticationService <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			authenticationService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AuthenticationService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			resourceFactory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyResourceFactory<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: #009900;">&#125;</span></pre></div></div>

<p>This class exists most importantly to associate the MiltonServlet with the ResourceFactory implementation in the previous step. You can use this class to load some initialization parameters but for our simple example, it was not necessary.</p>
<h3>Step 5: Update Your web.xml To Allow Access To Your Milton Servlet And Classes</h3>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span style="color: #000066;">xmlns:web</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;WebApp_ID&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>webdav-example<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...  
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>WebDAVFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>WebDAVFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.northconcepts.webdav.WebDAVFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>WebDAVFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Finally, to direct requests appropriately, you will need to update the web.xml so that the MiltonServlet can serve up the content for the desired URLs.</p>
<p>That’s it! You can now point your web browser, WebDAV client, or network drive mount to the server URL that is running this project and you will see the root folder containing the scratchpad.txt file. You can open and edit it as you wish and the content is persisted and automatically shared across any computer that has shared that drive.</p>
<h2>Concluding Thoughts</h2>
<p>Our example made a single POJO accessible from a cloud. What’s remarkable about WebDAV shows itself when you start to think about the business objects in your application that can be represented in a file format. This would allow direct, real-time access to read and update this data in a way that is both natural and simple to code.</p>
<p>Future WebDAV posts will explore this thought as well as touch on some of the more advanced capabilities WebDAV offers that you find in full-fledged Content Management Systems.</p>
<h2>Download</h2>
<p>The <a href="http://northconcepts.com/blog-downloads/webdav-part1/NorthConcepts-WebDAV-Part1.zip">WebDAV download</a> contains the entire source code (including Eclipse project). The source code is licensed under the terms of the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache License, Version 2.0</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2011/07/29/how-to-write-a-dead-simple-online-network-drive-using-java-and-w/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Use dynamic proxies to create a simple, powerful event bus (Part 1)</title>
		<link>http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/</link>
		<comments>http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 02:06:07 +0000</pubDate>
		<dc:creator>Dele Taylor</dc:creator>
				<category><![CDATA[Dynamic Proxies]]></category>
		<category><![CDATA[Event Bus]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://northconcepts.com/blog/?p=31</guid>
		<description><![CDATA[In this blog I&#8217;m going to walk you through one of my favourite uses for Java&#8217;s dynamic proxies.  Why favourite?  Because it takes a powerful, sometimes misunderstood, feature of Java and creates a simple, useful tool that we can use &#8230; <a href="http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In this blog I&#8217;m going to walk you through one of my favourite uses for Java&#8217;s dynamic proxies.  Why favourite?  Because it takes a powerful, sometimes misunderstood, feature of Java and creates a simple, useful tool that we can use every day.<br />
<span id="more-31"></span></p>
<h2>Event Buses</h2>
<p>Event buses act as brokers or clearing houses for notifications. They provide a single point of contact where:</p>
<ol>
<li>Observers can register to be notified of events.</li>
<li>Subjects can publish events as they occur.</li>
</ol>
<p>Event buses are an evolution on the <a href="http://en.wikipedia.org/wiki/Observer_pattern" target="_blank">observer pattern</a>. They share many of  the same advantages (loose coupling and event broadcasting), while removing several shortcomings:</p>
<ol>
<li><strong>Code duplication</strong>. Subjects no longer need to explicitly track their observers. The list of observers, along with the registration and notification logic, can be moved from each subject into the event bus.</li>
<li><strong>Decentralized event management.</strong> Event registration and notification are no longer spread out over a potentially large number of classes. Centralized management also aids in debugging, eases maintenance, and reduces our overall software complexity.</li>
<li><strong>Simplified event wiring.</strong> Observers do not need to seek out subjects to register their interest and subjects do not need to notify observers directly when events occur.</li>
</ol>
<h2>Dynamic Proxies</h2>
<p>Dynamic proxies are a way to route  calls from all methods on one or more Java interfaces to a single function automagically (without programming). This happens while your program is running and doesn&#8217;t require recompilation or restart. All it needs is a set of interfaces to automatically implement and a concrete implementation of the <code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/InvocationHandler.html" target="_blank">java.lang.reflect.InvocationHandler</a></code> interface to receive the calls.</p>
<h2>One Call, Many Receivers</h2>
<p>What if we could call a method that instead of being invoked on one object was invoked on several objects automatically? For example, <code>person.walkTo(store)</code> invokes <code>walkTo</code> on a single <code>person</code> object. But what if we could invoke that call on many <em>person</em> objects at once without having to call a <code>fireWalkTo</code> method or create a <code>WalkEvent</code> class (a common way of implementing the observer pattern)? Well, that&#8217;s  exactly what this event bus does. It lets subjects call methods on the listener interface then invokes those calls on all registered observers.</p>
<p><img src="http://northconcepts.com/blog/wp-content/uploads/2011/07/One-Call-Many-Receivers.png" alt="One Call, Many Receivers" width="667" height="255" /></p>
<h2>Quick Example</h2>
<p>Before we dive into the details, let&#8217;s look at a quick example of how the final product can be used.</p>
<p>First we create a listener interface:</p>
<pre>public interface WalkListener extends EventListener {
	void walkTo(Location location);
}</pre>
<p>Then we use <code>WalkListener</code> to  both observer and publisher events:</p>
<pre>public class EventBusExample {

	public static void main(String[] args) {
		EventBusExample eventSource = new EventBusExample();
		Location store = new Location();

		EventBus eventBus = new EventBus();
		eventBus.<strong>addListener</strong>(WalkListener.class, new WalkListener() {
			public void <strong>walkTo</strong>(Location location) {
				System.out.println("walking slowly...");
			}
		});
		eventBus.<strong>addListener</strong>(WalkListener.class, new WalkListener() {
			public void <strong>walkTo</strong>(Location location) {
				System.out.println("walking quickly...");
			}
		});

		WalkListener publisher = eventBus.<strong>getPublisher</strong>(eventSource,
			WalkListener.class);

		publisher.<strong>walkTo</strong>(store);
		publisher.<strong>walkTo</strong>(store);
		publisher.<strong>walkTo</strong>(store);

		eventBus.shutdown();
		System.out.println("The End.");

	}

}</pre>
<h2>Implementation Details</h2>
<p>The Event Bus is made up of three collaborating classes: <code>Event</code>, <code>EventSourceInvocationHandler</code>, and <code>EventBus</code>. For most of our use-cases,  EventBus is all we&#8217;ll need to work with. In  some rare cases we may need direct access to the Event object — for example — to log all events flowing through the bus (regardless of type or method). We&#8217;ll discuss how to accomplish this in part 2 of this series.</p>
<h2>Event Class</h2>
<p>The Event class represents a single method invocation from an event source (publisher). It holds the actual invoked <code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html" target="_blank">java.lang.reflect.Method</a></code> along with any arguments that were passed into it.</p>
<pre>public class Event {

	private final Object eventSource;
	private final Class&lt;? extends EventListener&gt; eventListenerClass;
	private final Method method;
	private final Object[] arguments;

	...
}</pre>
<h2>EventSourceInvocationHandler Class</h2>
<p>EventSourceInvocationHandler is the concrete <span class="code">implementation of InvocationHandler</span> for use with dynamic proxies. Its <code>invoke()</code> method receives all event publisher method calls and posts them to the event bus for delivery.</p>
<pre>public class EventSourceInvocationHandler implements InvocationHandler {

	private final EventBus eventBus;
	private final Object eventSource;
	private final Class&lt;? extends EventListener&gt; eventListenerClass;

	public EventSourceInvocationHandler(EventBus eventBus, Object eventSource,
		Class&lt;? extends EventListener&gt; eventListenerClass) {
		this.eventBus = eventBus;
		this.eventSource = eventSource;
		this.eventListenerClass = eventListenerClass;
	}

	public Object invoke(Object proxy, Method method, Object[] arguments)
		throws Throwable {
		eventBus.publishEvent(new Event(eventSource, eventListenerClass,
			method, arguments));
		return null;
	}

}</pre>
<h2>EventBus Class</h2>
<p>EventBus is where all the magic happens.  It provides all of the registration, queuing, and delivery services to make the whole thing work. Let&#8217;s first look at the bus&#8217; surface area, followed by implementation details of the business methods: <code>getPublisher</code>, <code>addListener</code>, <code>deliverEvent</code>, and <code>publishEvent</code>.</p>
<p>First the basic structure of the bus:</p>
<pre>public class EventBus {

	private final Map&lt;Class&lt;? extends EventListener&gt;,
			Set&lt;? extends EventListener&gt;&gt; eventListeners =
		new HashMap&lt;Class&lt;? extends EventListener&gt;,
			Set&lt;? extends EventListener&gt;&gt;();

	private final ExecutorService executorService =
		Executors.newFixedThreadPool(1);

	public EventBus() {
	}

	public &lt;T extends EventListener&gt; void addListener(
		Class&lt;T&gt; eventListenerClass, T listener) {
	}

	public &lt;T extends EventListener&gt; void removeListener(
		Class&lt;T&gt; eventListenerClass, T listener) {
	}

	public void removeListener(EventListener listener) {
	}

	public &lt;T extends EventListener&gt; T getPublisher(Object eventSource,
		Class&lt;T&gt; eventListenerClass) {
		return null;
	}

	protected void publishEvent(Event event) {
	}

	protected void deliverEvent(Event event) {
	}

	public void shutdown() throws InterruptedException {
	}

}</pre>
<h2>EventBus.getPublisher()</h2>
<p>The getPublisher method takes an event source  and a listener class then returns an instance of that listener we can call to publish events. The event source can be any object for now. In the next blog we&#8217;ll see how we can use it to filter the events observers receive.</p>
<pre>public &lt;T extends EventListener&gt; T getPublisher(Object eventSource,
	Class&lt;T&gt; eventListenerClass) {

	EventSourceInvocationHandler handler =
		new EventSourceInvocationHandler(this, eventSource,
		eventListenerClass);

	return (T) Proxy.newProxyInstance(
			eventListenerClass.getClassLoader(),
			new Class[]{eventListenerClass},
			handler);

}</pre>
<h2>EventBus.addListener()</h2>
<p>Observers register for events by calling <code>addListener()</code> with the EventListener interface they are interested in and an object that implements it. <code>CopyOnWriteArraySet</code> is used because it is thread-safe without requiring much synchronization. This means we have the option to notify observers using separate threads without blocking registrations.</p>
<pre>public &lt;T extends EventListener&gt; void addListener(Class&lt;T&gt; eventListenerClass,
	T listener) {
	Set&lt;T&gt; listeners;
	synchronized (eventListeners) {
		listeners = (Set&lt;T&gt;) eventListeners.get(eventListenerClass);
		if (listeners == null) {
			listeners = new CopyOnWriteArraySet&lt;T&gt;();
			eventListeners.put(eventListenerClass, listeners);
		}
	}
	listeners.add(listener);
}</pre>
<h2>EventBus.deliverEvent()</h2>
<p>Event delivery is performed by invoking the original method called by the producer on every registered observer.</p>
<pre>protected void deliverEvent(Event event) throws Throwable {
	Set&lt;? extends EventListener&gt; listeners;
	synchronized (eventListeners) {
		listeners = eventListeners.get(event.getEventListenerClass());
	}
	if (listeners != null) {
		for (EventListener listener : listeners) {
			event.getMethod().invoke(listener, event.getArguments());
		}
	}
}</pre>
<h2>EventBus.publishEvent()</h2>
<p>Events are queued for delivery by adding a <code>Runnable</code> with the actual delivery task to the executor service. Executors are a great way to benefit from concurrency without the complexity (and subtle bugs) that come with  writing multi-threaded code.</p>
<pre>	protected void publishEvent(final Event event) {
		executorService.execute(new Runnable(){
			@Override
			public void run() {
				try {
					deliverEvent(event);
				} catch (Throwable e) {
					throw new RuntimeException(e.getMessage(), e);
				}
			}
		});
	}</pre>
<h2>Next Time</h2>
<p>That&#8217;s  all for now. I hope you&#8217;ve found this application of dynamic proxies useful. Please leave a comment if you have any suggestions, questions, or ideas on dynamic proxies or event busses.</p>
<p>In the next part of this series, we&#8217;ll focus on several topics (exception handling, garbage collection, etc.) to help round out the bus&#8217; feature set.</p>
<h2>Download</h2>
<p>The <a href="http://northconcepts.com/blog-downloads/event-bus-part1/NorthConcepts-EventBus-Part1.zip">event bus download</a> contains the entire source code (including Eclipse project). The source code is licensed under the terms of the <a href="http://www.apache.org/licenses/LICENSE-2.0.html" target="_blank">Apache License, Version 2.0</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2011/07/05/use-dynamic-proxies-to-create-a-simple-powerful-event-bus-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Data Pipeline 2.2.3 Now Available</title>
		<link>http://northconcepts.com/blog/2011/05/14/data-pipeline-2-2-3-now-available/</link>
		<comments>http://northconcepts.com/blog/2011/05/14/data-pipeline-2-2-3-now-available/#comments</comments>
		<pubDate>Sat, 14 May 2011 09:54:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://northconcepts.com/wordpress/?p=20</guid>
		<description><![CDATA[Data Pipeline 2.2.3 is now available with the following enhancements: added JdbcValueReaderto allow clients to override column reading strategy added the JdbcReader.valueReader property]]></description>
			<content:encoded><![CDATA[<p>Data Pipeline 2.2.3 is now available with the following enhancements:</p>
<ul>
<li>added <code>JdbcValueReader</code>to allow clients to override column reading strategy</li>
<li>added the <code>JdbcReader.valueReader</code> property</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2011/05/14/data-pipeline-2-2-3-now-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Pipeline 2.2.2 Now Available</title>
		<link>http://northconcepts.com/blog/2011/05/11/17/</link>
		<comments>http://northconcepts.com/blog/2011/05/11/17/#comments</comments>
		<pubDate>Wed, 11 May 2011 09:53:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://northconcepts.com/wordpress/?p=17</guid>
		<description><![CDATA[Data Pipeline 2.2.2 is now available with the following enhancements: added the XmlTemplate functionality XmlWriter now uses XmlTemplate to describe output patterns]]></description>
			<content:encoded><![CDATA[<p>Data Pipeline 2.2.2 is now available with the following enhancements:</p>
<ul>
<li>added the <code>XmlTemplate</code> functionality</li>
<li><code>XmlWriter</code> now uses <code>XmlTemplate</code> to describe output patterns</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2011/05/11/17/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Pipeline 2.2.1 Now Available</title>
		<link>http://northconcepts.com/blog/2010/12/09/data-pipeline-2-2-1-now-available/</link>
		<comments>http://northconcepts.com/blog/2010/12/09/data-pipeline-2-2-1-now-available/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 01:54:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://northconcepts.com/wordpress/?p=14</guid>
		<description><![CDATA[Data Pipeline 2.2.1 is now available with the following enhancements: added batch execution to JdbcWriter (see JdbcWriter.setBatchSize) added callback mechanism to track job progress (see JobTemplate.transfer(R reader, W writer, boolean async, JobCallback callback)) early access to DeMuxReader]]></description>
			<content:encoded><![CDATA[<p>Data Pipeline 2.2.1 is now available with the following enhancements:</p>
<ul>
<li>added batch execution to <code>JdbcWriter </code>(see <code>JdbcWriter.setBatchSize</code>)</li>
<li>added callback mechanism to track job progress (see <code>JobTemplate.transfer(R reader, W writer, boolean async, JobCallback callback)</code>)</li>
<li>early access to <code>DeMuxReader</code></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://northconcepts.com/blog/2010/12/09/data-pipeline-2-2-1-now-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

