<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Brendan Graetz</title>
	<atom:link href="http://brendangraetz.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://brendangraetz.wordpress.com</link>
	<description></description>
	<lastBuildDate>Thu, 14 Jul 2011 12:59:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='brendangraetz.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Brendan Graetz</title>
		<link>http://brendangraetz.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://brendangraetz.wordpress.com/osd.xml" title="Brendan Graetz" />
	<atom:link rel='hub' href='http://brendangraetz.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Inception &#8211; it really is just recursion</title>
		<link>http://brendangraetz.wordpress.com/2010/08/08/inception-it-really-just-recursion/</link>
		<comments>http://brendangraetz.wordpress.com/2010/08/08/inception-it-really-just-recursion/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 11:40:13 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[comic]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[xkcd]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=204</guid>
		<description><![CDATA[Inception recursion, XKCD style<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=204&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Inception is one helluva concentration-intensive edge-of-your-seat type movie. However, this blog is not your run of the mill list of of things I did each day types &#8211; so why blog about a movie, you ask?</p>
<p>Well, at the end of the movie I had a little discussion, and my take was that it really was not all that confusing, if you thought of it in terms of recursion. Add a couple of weeks and a little inspiration from another discussion thread, plus several years of reading <a title="XKCD" href="http://xkcd.com" target="_blank">XKCD</a>; and voila I drew a comic. Enjoy.</p>
<p><a href="http://brendangraetz.files.wordpress.com/2010/08/img_4867-copy.jpeg"><img class="alignnone size-full wp-image-205" title="Inception Recursion" src="http://brendangraetz.files.wordpress.com/2010/08/img_4867-copy.jpeg?w=640&#038;h=480" alt="Yes, the woman's height is indeed in need of an invariant" width="640" height="480" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/204/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/204/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/204/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=204&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/08/08/inception-it-really-just-recursion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>

		<media:content url="http://brendangraetz.files.wordpress.com/2010/08/img_4867-copy.jpeg" medium="image">
			<media:title type="html">Inception Recursion</media:title>
		</media:content>
	</item>
		<item>
		<title>Using servlet filters for user authentication</title>
		<link>http://brendangraetz.wordpress.com/2010/06/17/use-servlet-filters-for-user-authentication/</link>
		<comments>http://brendangraetz.wordpress.com/2010/06/17/use-servlet-filters-for-user-authentication/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 13:22:44 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[learn]]></category>
		<category><![CDATA[main]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java EE]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[servlet]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=198</guid>
		<description><![CDATA[The proper way to go about implementing restricted access to certain pages is to use filters from the Servlet API, and then configuring the web application to map select pages to these filters.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=198&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When creating a Java web application that requires access restrictions on some pages, one way to do it is to include user authentication logic within each JSP, or perhaps in a JSP fragment that is included in all of these JSPs. In that code perform a redirect or forward to an access denied page.</p>
<p><pre class="brush: java;">
req.getRequestDispatcher(&quot;/accessDenied.html&quot;).forward(request, response);
</pre></p>
<p>However, a Java web container is multi-threaded, and this merely spawns a new thread, and the server processes both the JSP and the page that has been forwarded to simultaneously. If one looks at the logging output from the server, or intercepts the HTTP traffic, using, for example, a packet sniffer (which are commonplace enough to exist as <a href="http://livehttpheaders.mozdev.org/" target="_blank">Firefox extensions</a>), one can see this happening live. This method therefore, clearly raises some nasty security concerns.</p>
<p>Furthermore, from a software engineering or architectural design point of view, using a JSP as a filter violates MVC design principles, as doing so would make the JSP act as a controller as well as a view at the same time.</p>
<p>The proper way to go about implementing restricted access to certain pages is to use filters from the Servlet API, and then configuring the web application to map select pages to these filters. This method utilises additional capability provided by web container that runs on the server. The web container constructs filter chains based on the filter mappings defined. Most crucially, these filters allow the authentication logic and consequent branching to be executed <em>prior to</em> the JSP being called;</p>
<p>Create an abstract class `LoginFilter`.</p>
<p><pre class="brush: java;">
    public abstract class LoginFilter implements javax.servlet.Filter {
        protected ServletContext servletContext
    
        public void init(FilterConfig filterConfig) {
            servletContext = filterConfig.getServletContext();
        }
    
        public void doFilter(
            ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest)request;
            HttpServletResponse resp = (HttpServletResponse)response;
        
            if (!isAuth()) {
                resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                return; //break filter chain, requested JSP/servlet will not be executed
            }
        
            //propagate to next element in the filter chain, ultimately JSP/ servlet gets executed
            chain.doFilter(request, response);        
        }
    
        /**
         * logic to accept or reject access to the page, check log in status
         * @return true when authentication is deemed valid
         */
        protected abstract boolean isAuth();
    
    }
</pre></p>
<p>Subclass the `LoginFilter` as a concrete class, `MemberLoginFilter`, by implementing the `isAuth` method. In this case add logic that determines whether a user is currently logged in. This post does not cover how this is done; in a nutshell, this would typically involve retrieving objects from the current HTTP session, and determining whether a user has logged in during the current session by comparing the values or states of these objects.</p>
<p>Now edit the `web.xml` configuration for the web application archive (WAR), to make the web app aware of the filter and what pages it should map to.</p>
<p><pre class="brush: xml;">
&lt;filter&gt;
    &lt;description&gt;Requires user to log in as a member&lt;/description&gt;
    &lt;filter-name&gt;MemberLoginFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;some.package.MemberLoginFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;MemberLoginFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/someRestrictedPage.jsp&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre></p>
<p>Add as many filter mappings as necessary for each page that needs to have access to it restricted to logged-in members only; use file name globs to match multiple files.</p>
<p>Also, still editing `web.xml` configuration, tell the web app how to handle access denied error messages in a more user friendly way, by adding the following node to the settings:</p>
<p><pre class="brush: xml;">
&lt;error-page&gt;
    &lt;error-code&gt;401&lt;/error-code&gt;
    &lt;location&gt;/accessDenied.html&lt;/location&gt;
&lt;/error-page&gt;
</pre></p>
<p>The HTTP 401 error code referenced here corresponds to `HttpServletResponse.SC_UNAUTHORIZED` in `LoginFilter.doFilter(..)`.<br />
This completes the loop: when the filter chain is broken because `LoginFilter.isAuth()` returns false, instead of getting a garish HTTP 401, the user will see an access denied page instead, which you can edit to inlcude a user friendly message.</p>
<p>If `LoginFilter.isAuth()` returns true, the filter chain continues execution, and assuming that there are no more filters in the filter chain, or all of the downstream filters in the filter chain pass, the requested JSP or servlet gets displayed.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/198/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=198&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/06/17/use-servlet-filters-for-user-authentication/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>A couple of command line commands for developers</title>
		<link>http://brendangraetz.wordpress.com/2010/06/14/command-line-commands-for-developers/</link>
		<comments>http://brendangraetz.wordpress.com/2010/06/14/command-line-commands-for-developers/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 06:32:08 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=189</guid>
		<description><![CDATA[Picked up a couple of handy command line tricks at work this week, and frankly I wish I had done so much earlier. I thought, what better way to commit them to permanent memory than to post it here, so here they are. Subversion diff ignoring whitespace Compares the local copy with a particular revision; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=189&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Picked up a couple of handy command line tricks at work this week, and frankly I wish I had done so much earlier. I thought, what better way to commit them to permanent memory than to post it here, so here they are.</p>
<h3>Subversion diff ignoring whitespace</h3>
<p><pre class="brush: bash;">
svn diff --diff-cmd /usr/bin/diff -x -bBw -r9999 &gt; r9999.diff
</pre></p>
<p>Compares the local copy with a particular revision; and uses the local diff command instead of the one built into the Subversion client.</p>
<p>The `-x` flags that the following option (in this case `-bBw`) is for the local diff command, instead of the Subversion command.</p>
<h3>Find files containing a particular string recursively in a directory</h3>
<p><pre class="brush: bash;">
find . -name \*.java -exec grep -n &quot;searchstringregex&quot; {} /dev/null \;
</pre></p>
<p>Lists all files within the current directory (recursively) which match a file glob pattern, and contain at least one line that matches a regular expression. Special characters in the file glob need to be escaped (e.g. `\*.java`), and the regex should be quoted.</p>
<p>The purpose of including `/dev/null` is to give the `grep` command more than one input file (per iteration), to force grep to output not just the matching string, but also the file name. This would be redundant if we are lookign at just one file, however it is quite useful, and necessary, here since we are grep&#8217;ping many files at once.</p>
<p><pre class="brush: bash;">
find . -name \*.java -exec grep -c &quot;searchstringregex&quot; {} /dev/null \; | grep -v :0$
</pre></p>
<p>A very slight variation on the previous command, this time with the number of occurrences of the regex in each file (`-c`) instead of the contents of each matching line. The output is filtered by piping it through another `grep` to remove items with a count of zero.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/189/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=189&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/06/14/command-line-commands-for-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>Oracle database query optimisation resources</title>
		<link>http://brendangraetz.wordpress.com/2010/06/02/oracle-database-query-optimisation-resources/</link>
		<comments>http://brendangraetz.wordpress.com/2010/06/02/oracle-database-query-optimisation-resources/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 05:12:05 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[learn]]></category>
		<category><![CDATA[main]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[optimisation]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=178</guid>
		<description><![CDATA[Here are the resources I find most useful for Oracle SQL query optimisartion, ordered from basic to advanced. Guide to understanding the basics of indexing and the various merges in line with their effects on execution plans How-to manual from Oracle themselves on how to use autotrace in SQLPlus The most complete reference on Oracle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=178&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here are the resources I find most useful for Oracle SQL query optimisartion, ordered from basic to advanced.</p>
<p>Guide to understanding the <a href="http://www.akadia.com/services/ora_interpreting_explain_plan.html">basics of indexing and the various merges</a> in line with their effects on execution plans</p>
<p>How-to manual from Oracle themselves on <a href="http://download.oracle.com/docs/cd/B10500_01/server.920/a96533/autotrac.htm">how to use autotrace in SQLPlus</a></p>
<p>The most complete reference on <a href="http://psoug.org/reference/hints.html">Oracle hints</a> I&#8217;ve come across</p>
<p>A collection of specific, and advanced, <a href="http://www.orafaq.com/tuningguide/">query tuning techniques</a></p>
<p>Also, here is the 1979 seminal paper from P. Selinger of IBM, which forms the roots of access plan optimisation in most current day databases; and addresses the basic concepts that need to be taken into account when optimising queries.<br />
<a href="http://doi.acm.org/10.1145/582095.582099﻿">Access path selection in a relational database management system</a></p>
<hr />
<b>EDIT</b></p>
<p>This one here does not have anything to do directly with query optimisation, but I found that attempting to optimise queries without first understanding the algorithms behind them is a fool&#8217;s errand. So here is a great post on <a href="http://www.necessaryandsufficient.net/2010/02/join-algorithms-illustrated/">the basic algorithms behind nested loop join, merge join and hash join</a> (code in C#).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=178&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/06/02/oracle-database-query-optimisation-resources/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>Python iView on Ubuntu</title>
		<link>http://brendangraetz.wordpress.com/2010/05/21/python-iview-on-ubuntu/</link>
		<comments>http://brendangraetz.wordpress.com/2010/05/21/python-iview-on-ubuntu/#comments</comments>
		<pubDate>Fri, 21 May 2010 09:42:57 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[iview]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rtmpdump]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=167</guid>
		<description><![CDATA[How to get python iview working on Ubuntu, including building rtmpdump<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=167&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the Ubuntu counterpart to my <a href="/index.php/2010/04/01/deploying-python-iview-on-windows/">earlier post about deploying python iView on Windows</a> (<a href="http://jeremy.visser.name/2009/08/30/python-iview/">python iView homepage</a>).</p>
<p>Fortunately, it is much easier than Windows, everything is achieved with a single shell script.</p>
<p><pre class="brush: bash;">
#!/bin/bash
# Installs python-iview including all dependencies
# Script assumes /usr/bin in on the path (is with default installs)
# Brendan Graetz 20100521
#
# python-iview by Jeremy Visser: http://jeremy.visser.name/2009/08/30/python-iview/

#install bazaar and python library
sudo apt-get install bzr
sudo apt-get install python-beautifulsoup

#install dev dependencies in order to be able to compile
sudo apt-get install libssl-dev
sudo apt-get install zlib1g-dev

#download rtmpdump source
wget http://rtmpdump.mplayerhq.hu/download/rtmpdump-2.2d.tgz ~/Downloads/rtmpdump-2.2d.tgz

#compile rtmpdump &amp; create softlink on path
cd /usr/local/
tar -xzf ~/Downloads/rtmpdump-2.2d.tgz
cd rtmpdump-2.2d
make posix
sudo ln -s `pwd`/rtmpdump /usr/bin/rtmpdump

#check out python iview from repository
cd /usr/local/
bzr branch http://jeremy.visser.name/bzr/python-iview/ ./python-iview

#create softlinks on path
sudo ln -s `pwd`/iview-gtk /usr/bin/iview-gtk
sudo ln -s `pwd`/iview-cli /usr/bin/iview-cli
sudo ln -s /usr/bin/iview-gtk /usr/bin/iview
</pre></p>
<p>The only somewhat tricky bit here is that you need to compile <a href="http://rtmpdump.mplayerhq.hu/" target="_blank">rtmpdump</a>, because it does not distribute binaries (not <em>.deb</em>&#8216;s anyway). For that you will need to install a couple of dependencies which are not immediately obvious: <em>libssl-dev</em> and <em>zlib1g-dev</em>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/167/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/167/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=167&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/05/21/python-iview-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>Try this (sed)</title>
		<link>http://brendangraetz.wordpress.com/2010/05/01/try-this-sed/</link>
		<comments>http://brendangraetz.wordpress.com/2010/05/01/try-this-sed/#comments</comments>
		<pubDate>Sat, 01 May 2010 07:04:18 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=163</guid>
		<description><![CDATA[Save Steve Jobs' thoughts on flash  to a plain text file.
There is no simpler way to expose hypocrisy than simple text substitution. Enjoy.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=163&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Save Steve Jobs&#8217; <a href="http://www.apple.com/hotnews/thoughts-on-flash/" target="_blank">thoughts on flash</a> to a plain text file.</p>
<p><pre class="brush: bash;">
sed -e 's/Adobe/Apple/g' -e 's/Flash/closed/g' -e 's/third party/proprietary/g' apple.txt
cat apple.txt
</pre></p>
<p>(substitute file names accordingly)</p>
<p>There is no simpler way to expose hypocrisy than simple text substitution. Enjoy.</p>
<p>(slightly modified version of the suggestion in <a href="http://hooptyrides.blogspot.com/2010/04/searching-and-replacing-jobs-flash.html" target="_blank">this post</a>)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/163/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=163&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/05/01/try-this-sed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>From &#8220;The Reinvigorated Programmer&#8221;: Are you one of the 10% of programmers who can write a binary search?</title>
		<link>http://brendangraetz.wordpress.com/2010/04/25/from-the-reinvigorated-programmer-are-you-one-of-the-10-of-programmers-who-can-write-a-binary-search/</link>
		<comments>http://brendangraetz.wordpress.com/2010/04/25/from-the-reinvigorated-programmer-are-you-one-of-the-10-of-programmers-who-can-write-a-binary-search/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 06:09:10 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[learn]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=155</guid>
		<description><![CDATA[The Reinvigorated Programmer (Mike Taylor) quotes from Jon Bentley’s 1986 classic Programming Pearls:

    Only 10% of programmers can write a binary search

Shocking indeed<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=155&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a title="The Reinvigorated Programmer" href="http://reprog.wordpress.com/about/" target="_blank">The Reinvigorated Programmer</a> (Mike Taylor) quotes from Jon Bentley’s 1986 classic Programming Pearls:</p>
<blockquote><p>Only 10% of programmers can write a binary search</p></blockquote>
<p>Shocking indeed</p>
<p>Better yet, he has a coding challenge for you to take part in (post answers as comments):</p>
<blockquote><p>Here are the rules:</p>
<ol>
<li>Use whatever programming language you like.</li>
<li>No cutting, pasting or otherwise copying code.  Don’t even look at other binary search code until you’re done.</li>
<li>I need hardly say, no calling <span style="color:#800000;">bsearch()</span>, or otherwise cheating <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Take as long as you like — you might finish, and feel confident in your code, after five minutes; or you’re welcome to take eight hours if you want (if you have the time to spare).</li>
<li>You’re allowed to use your compiler to shake out mechanical bugs such as syntax errors or failure to initialise variables, but …</li>
<li><strong>NO TESTING</strong> until <em>after</em> you’ve decided your program is correct.</li>
<li>Finally, the most important one: if you decide to begin this exercise, then you <em>must</em> report — either to say that you succeeded, failed or abandoned the attempt.  Otherwise the figures will be skewed towards success.</li>
</ol>
</blockquote>
<p><a href="http://reprog.wordpress.com/2010/04/19/are-you-one-of-the-10-percent/" target="_blank">If you haven&#8217;t yet, try it</a>. Read just the article, but do not look at the others&#8217; comments just yet.</p>
<p>After you have attempted this, there is his <a href="http://reprog.wordpress.com/2010/04/21/binary-search-redux-part-1/" target="_blank">redux</a>, on why coding exercises like this matter; and his <a href="http://reprog.wordpress.com/2010/04/23/testing-is-not-a-substitute-for-thinking-binary-search-part-3/" target="_blank">redux of the redux</a>, on why you should not test while attempting exercises like this. They are all worth a good read.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=155&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/04/25/from-the-reinvigorated-programmer-are-you-one-of-the-10-of-programmers-who-can-write-a-binary-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>Python antigravity easter egg</title>
		<link>http://brendangraetz.wordpress.com/2010/04/25/python-antigravity-easter-egg/</link>
		<comments>http://brendangraetz.wordpress.com/2010/04/25/python-antigravity-easter-egg/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 05:54:52 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[main]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[easterEgg]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[xkcd]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=151</guid>
		<description><![CDATA[python: import antigravity<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=151&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Was reading some old XKCD, and I came across this one:<br />
<a href="http://xkcd.com/353/" target="_blank"><img title="I wrote 20 short programs in Python yesterday.  It was wonderful.  Perl, I'm leaving you." src="http://imgs.xkcd.com/comics/python.png" alt="Python" /></a></p>
<p>So, on a whim, I decide plug it in. I would encourage you to try it too:</p>
<p><pre class="brush: python;">
import antigravity
</pre></p>
<p>Spoiler: <a href="http://svn.python.org/view/python/trunk/Lib/antigravity.py?view=markup&amp;pathrev=66902" target="_blank">python antigravity easter egg</a> as evidenced in python&#8217;s source code (circa 2008)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/151/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=151&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/04/25/python-antigravity-easter-egg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>

		<media:content url="http://imgs.xkcd.com/comics/python.png" medium="image">
			<media:title type="html">I wrote 20 short programs in Python yesterday.  It was wonderful.  Perl, I'm leaving you.</media:title>
		</media:content>
	</item>
		<item>
		<title>Client-side deployment with applets and JNLP</title>
		<link>http://brendangraetz.wordpress.com/2010/04/12/client-side-deployment/</link>
		<comments>http://brendangraetz.wordpress.com/2010/04/12/client-side-deployment/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 03:44:03 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[learn]]></category>
		<category><![CDATA[applets]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coursework]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=144</guid>
		<description><![CDATA[In the previous posts, on socket programming and JDBC, the focus has been on programming for the server. This post will focus on client-side programming: applets and web start &#8211; with particular focus on the latter. Java Web Start The Java Network Launch Protocol (JNLP) specifies how to deliver a desktop application over the internet. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=144&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the previous posts, on <a title="Java socket programming intro" href="/index.php/2010/04/12/java-socket-programming-intro/" target="_blank">socket programming</a> and <a title="Relational databases and JDBC" href="/index.php/2010/04/12/relational-databases-and-jdbc/" target="_blank">JDBC</a>, the focus has been on programming for the server. This post will focus on client-side programming: applets and web start &#8211; with particular focus on the latter.<br />
<span id="more-144"></span></p>
<h3>Java Web Start</h3>
<p>The Java Network Launch Protocol (JNLP) specifies how to deliver a desktop application over the internet. A webpage may link to a *.jnlp file, and when you download it, the JVM is invoked and will parse the file and download or stream the Java classes of the application, and run the application as if it were on your desktop (not within the browser).</p>
<p>Sun has a full-blown <a title="Sun tutorial: deploying java web start applications" href="http://java.sun.com/docs/books/tutorial/deployment/webstart/index.html" target="_blank">tutorial on how to develop, deploy and run Java Web Start applications</a> &#8211; it is a significantly trickier task to accomplish than with standard JAR file distributions; and rightly so, because applications deployed over networks need tighter security at several different levels.</p>
<p>By default, Java applications deployed or running over networks, including Java web start applications, are sandboxed by the security manager of the JVM. A sandbox may be thought of as a restricted environment. This means that the instruction set they are allowed to use is severely restricted such that only completely harmless operations are permitted. For these applications to gain more access rights on the local machine (the client machine on which it is deployed or running), the JAR files need to be digitally signed. Again, the Sun tutorial has a <a title="Sun tutorial: Digitally signing JAR files" href="http://java.sun.com/docs/books/tutorial/deployment/webstart/security.html" target="_blank">detailed explanation</a> about what this involves. Some of the restrictions include:</p>
<ul>
<li> Cannot I/O to local files or execute them</li>
<li> Can only phone home to the host from which it was downloaded &#8211; not to other clients or servers</li>
<li> Warning messages displayed to user</li>
</ul>
<h3>Applets</h3>
<p>Applets are another means to run remote code on a client machine. They differ significantly from the Java Webs Start because they do not run as a desktop application, but rather via a browser plug in.</p>
<p>JApplet is a Swing component; however it inherits from neither JComponent nor JFrame. Instead, it inherits from Applet, which is an AWT component.</p>
<h4>Deploying an applet on a web page</h4>
<p><a title="W3.org's object tag definition" href="http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.4" target="_blank">W3.org&#8217;s ﻿&lt;object&gt;  ﻿﻿tag definition</a> | <a title="Sun tutorial on Applets" href="http://java.sun.com/docs/books/tutorial/deployment/applet/" target="_blank"> Sun tutorial on Applets</a></p>
<p>The Sun tutorial and the W3.org specification disagree at the time of writing &#8211; the Sun tutorial still is uses the &lt;applet&gt; tag, whereas the W3.org specification says that the &lt;applet&gt; tag has been deprecated in favour of the object tag. The &lt;applet&gt; still tag works on all Java enabled browsers, and it is not too much effort to switch from one to the other &#8211; the commonly used attributes are almost identical. In the examples below, the only difference is that the &#8220;code&#8221; attribute is replaced by the &#8220;classid&#8221; attribute&#8221;.</p>
<p>Using the applet tag:</p>
<p><pre class="brush: xml;">
&lt;applet code=&quot;some.package.MyApplet&quot;
   archive=&quot;dist/myApp.jar&quot;
   width=&quot;400px&quot; height=&quot;300px&quot;&gt;
      &lt;param name=&quot;myString1&quot; value=&quot;myValue1&quot; /&gt;
      &lt;param name=&quot;myDouble2&quot; value=&quot;2.0&quot; /&gt;
&lt;/applet&gt;
</pre></p>
<p>Using the object tag:</p>
<p><pre class="brush: xml;">
&lt;object classid=&quot;some.package.MyApplet&quot;
   archive=&quot;dist/myApp.jar&quot;
   width=&quot;400px&quot; height=&quot;300px&quot;&gt;
      &lt;param name=&quot;myString1&quot; value=&quot;myValue1&quot; /&gt;
      &lt;param name=&quot;myDouble2&quot; value=&quot;2.0&quot; /&gt;
&lt;/object&gt;
</pre></p>
<h4>Java applet code</h4>
<p>Before jumping into coding the applet, it is a good idea to think about JApplet&#8217;s inheritance hierarchy. Because it inherits from neither JComponent nor JFrame, it will be missing some API (such as setTitle(String), and setSize(int, int)), and behave differently. A word of caution also that JApplet has some interesting painting and layout quirks, thus as a rule of thumb, avoid doing any GUI design work directly inside of an applet &#8211; instead do all the GUI design work in a JPanel, and then put the JPanel inside the JApplet.<br />
Another important distinction to make is the entry point into a standard Java application is different from the entry point into a Java applet. A standard Java application always needs (at least) one class that contains the special method with the signature:</p>
<p><pre class="brush: java;">public static void main (String[] args)</pre></p>
<p>This is its entry point, and any command line arguments get passed in via the string array. However, in the applet, there is no &#8220;main&#8221; method, and certainly no array of strings for command line arguments. However, the same effect is achieved using the init() method of the applet, and the parameters passed in using the &lt;param&gt; tags within the &lt;applet&gt; or &lt;object&gt; tags.</p>
<p><pre class="brush: java;">
public class MyApplet extends JApplet {
   private String myString1;
   private double myDouble2;
   public void init() {
      myString1 = getParameter(&quot;myString1&quot;);
      myDouble2 = Double.parseDouble(getParameter(&quot;myDouble2&quot;));
   }
}
</pre></p>
<p>Once this is done, you can do anything with an applet that you can with any other java application. Note that the same security restrictions still apply to code that is deployed over a network &#8211; the applets can be sandboxed.</p>
<h4>Applets&#8217; external communications</h4>
<p>Applets can communicate with other applets, the browser, and any other machine through sockets. However, if the applet is sandboxed, its socket communications will be restricted to only the host machine they originate from.<br />
Applets communicate with other machines through sockets, in the same way that a standard Java application does. See my previous <a title="Java socket programming intro" href="/index.php/2010/04/12/java-socket-programming-intro/" target="_blank">post on socket programming in java</a> for more details.</p>
<p>Applets can communicate with other applets running in the same environment (which in most cases are the other applets in the same web page). They do this through the AppletContext interface. In order to do this, each applet must have a name. This is not a class name, but a unique ID for each applet within the web page. To do this, just add a &#8220;name&#8221; attribute to each of the &lt;applet&gt; tags or &lt;object&gt; tags.</p>
<p><pre class="brush: java;">
       Applet receiver = getAppletContext().getApplet(&quot;myApplet2&quot;);
       if ((receiver != null) &amp;&amp; (receiver instanceof MyApplet)) {
           ((MyApplet)receiver).sendMessage(message);
       } else {
           System.err.println(&quot;The other applet was not found&quot;);
       }
</pre></p>
<p>The code above shows how one applet obtains the AppletContext, and uses this to obtain a reference to another applet on the same web page, whose name attribute is &#8220;myApplet2&#8243;. If such an applet is found, it type casts it to a particular class of Applet (in this case MyApplet), so that it may invoke a method particular to his type of applet &#8211; the sendMessage(String) method, which it uses to transmit the contents of the &#8220;message&#8221; variable. Thus, intra-web page applet communication is achieved.</p>
<p>Applets can also communicate with the browser. This is also achieved via the AppletContext interface. Invoke the AppletContext#showDocument(URL) method to tell the browser to navigate to a new location. Invoke the AppletContext#showStatus(String) method to change the text in the status bar of the browser.</p>
<p><pre class="brush: java;">
       Applet receiver = getAppletContext().getApplet(&quot;myApplet2&quot;);
       if ((receiver != null) &amp;&amp; (receiver instanceof MyApplet)) {
       ((MyApplet)receiver).sendMessage(message);
       } else {
           System.err.println(&quot;The other applet was not found&quot;);
       }
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=144&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/04/12/client-side-deployment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
		<item>
		<title>Relational databases and JDBC intro</title>
		<link>http://brendangraetz.wordpress.com/2010/04/12/relational-databases-and-jdbc/</link>
		<comments>http://brendangraetz.wordpress.com/2010/04/12/relational-databases-and-jdbc/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 03:26:43 +0000</pubDate>
		<dc:creator>bguiz</dc:creator>
				<category><![CDATA[learn]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coursework]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://brendangraetz.wordpress.com/?p=134</guid>
		<description><![CDATA[How to connect to a relational database from a Java program using Java Database Connectivity (JDBC); the JDBC API; some basic SQL; Object-Relational Mapping -all put together with a code example Why JDBC? There are a myriad of database vendors, each with their own software and hardware implementations/ requirements, and their won API: PostgreSQL, mySQL, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=134&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>How to connect to a relational database from a Java program using Java Database Connectivity (JDBC); the JDBC API; some basic SQL; Object-Relational Mapping -all put together with a code example<br />
<span id="more-134"></span></p>
<h3>Why JDBC?</h3>
<p>There are a myriad of database vendors, each with their own software and hardware implementations/ requirements, and their won API: PostgreSQL, mySQL, Oracle, to name a few. In java, there are two main ways to connect to these databases, with Persistence, or with JDBC, and we will be only looking at the latter.</p>
<p>JDBC allows a standard Java application (J2SE) application to communicate with a relational database management system (RDBMS). Like anything else in Java, the programmer uses it by adding a library containing the relevant classes to the class path, and then invoking its API. JDBC acts as a bridge between your java application running from within a Java Virtual Machine (JVM) and a RDBMS running on the native operating system, and provides a pure Java API for database access. The cross platform nature of both Java and JDBC make the Java programmer quite powerful, as he can write the application just once and expect it to work, with minimal need to change the code, for databases from any vendor* and on any platform.</p>
<p>Of course, in order to make access to any database possible through a standard API, the database vendors themselves must conform to a set of standards themselves, and JDBC provides a Service Provider Interface (SPI) that the vendor must implement or conform to. Unless you are a developer for a a database vendor, you will not ever have to worry about JDBC&#8217;s SPI. In essence, the Java program talks to the JDBC driver manager, which then translates and relays the message on to the database&#8217;s drivers.</p>
<p>Using JDBC, Java programs are able to achieve the classic <a href="http://www.linuxjournal.com/article/3508" title="Linux Journal on Client-Server architecture and three-tier architecture" target="_blank">client-server architecture</a> or <a href="http://en.kioskea.net/contents/cs/cs3tier.php3" title="Kioskea on Client-Server architecture and three-tier architecture" target="_blank">three-tier architecture</a>. Also, see my previous <a href="/index.php/2010/04/12/java-socket-programming-intro/" title="Java socket programming intro" target="_blank">post on socket programming in java</a>.</p>
<h3>Object Relational Mapping</h3>
<p>Objects are the lingua franca of Java programming (and any other OO language). Databases traditionally are about relations &#8211; they talk in tables and records. Therefore, unless the database you are talking to is an object database, you will need to do object relational mapping (ORM). That is after reading a record from a table, to transform them into an Object; or prior to writing a record to a table, to construct a record from the Object. Fortunately parallels exist between the object world and the relational world that make these mappings, for the most part**, intuitive.</p>
<ul>
<li> Relational &lt;&#8211;&gt; Object</li>
<li> Table &lt;&#8211;&gt; Class</li>
<li> Column &lt;&#8211;&gt; Attribute</li>
<li> Row &lt;&#8211;&gt; Instance (of an Object)</li>
<li> Foreign Key &lt;&#8211;&gt; Object reference member variable</li>
</ul>
<h3>Coding with JDBC API</h3>
<ul>
<li> Plugging your Java program into a JDBC driver &#8211; seven quick steps</li>
<li> Start the database server.</li>
<li> Add the JDBC&#8217;s driver library (usually one or more JAR files) to the class path</li>
<li> Load the class of the JDBC driver&#8217;s entry point using ClassLoader</li>
<li> Create a statement or a prepared statement</li>
<li> Execute a query or update via the statement or prepared statement</li>
<li> Catch and handle SQL Exception</li>
<li> Finally, close the statement and close the connection</li>
</ul>
<p>For this demo, we will use the apache derby JDBC driver and Java DB database, since that is developed by Sun and ships with Netbeans, and therefore requires minimal set up and configuration effort.</p>
<h4>Setup</h4>
<p>To start the database server in Netbeans, go to the services tab, select</p>
<pre>Databases --&gt; Java DB --&gt; (right click) --&gt; Start Server.</pre>
<p>Easy as pie &#8211; this will be more complex if you want to use another vendor&#8217;s database. You will of course, need a database instance to connect to, and if you do not already have one, right click on &#8220;Java DB&#8221; again and select &#8220;Create Database&#8221;. Give it a name, and create a user name and password to access it with. Note these down, as you will need them later. You will now see an entry that looks similar to:</p>
<pre>jdbc:derby://localhost:1527/yourDBname</pre>
<p>Note this down as well.</p>
<p>To add the JDBC driver&#8217;s library in Netbeans, go to the projects tab, right click on your project and select &#8220;Properties&#8221;. In the project properties window, select &#8220;Libraries&#8221;, then &#8220;Add Library&#8221;. Select the apache derby library, if it already exists, or create a new one (the JAR files are found in the Sun SDK installation directory ./javadb/lib).</p>
<p>Now create a new class for in your project that intends to handle the JDBC access functionality. In its constructor, load the JDBC driver using ClassLoader by doing<br />
<pre class="brush: java;">
Class.forName(&quot;org.apache.derby.jdbc.ClientDriver&quot;);
</pre></p>
<p>Note that we discard the return value of the forName method, because we do not need the actual class. We just want the ClientDriver class to be loaded into the JVM. In addition, we should handle the exception &#8211; this exception should never occur unless the JDBC driver&#8217;s library has not been added properly.<br />
<pre class="brush: java;">
try {
Class.forName(&quot;org.apache.derby.jdbc.ClientDriver&quot;);
} catch (ClassNotFoundException cnfe) {
//exception handling code
}
</pre></p>
<p>Before we delve into creating SQL queries, we should enable this class to connect to the database.<br />
Define the following static constants for this class, replacing &#8220;myDatabase&#8221;, &#8220;myUname&#8221; and &#8220;myPword&#8221; with the actual values noted down previously.<br />
<pre class="brush: java;">
private static final String DB_URL = &quot;jdbc:derby://localhost:1527/myDatabase&quot;;
private static final String DB_USERNAME = &quot;myUname&quot;;
private static final String DB_PASSWORD = &quot;myPword&quot;;
</pre></p>
<p>Now create a method that connect to the database and then returns the connection. This method is intended to be commonly used by several other methods that perform the database access.<br />
<pre class="brush: java;">
    /**
     * Utility method returns a database connection using the specified settings
     * @return Connection to the database
     */
    private Connection getDbConnection() {
        try {
            Connection dbCon = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
            return dbCon;
        } catch (SQLException ex) {
            System.err.println(&quot;Failed to create connection&quot;);
            return null;
        }
    }
</pre></p>
<h4>CRUD</h4>
<p>The actual legwork of this class is to perform create, read, update and delete operations, also known as CRUD. We will implement one of each of the CRUD operations that act on a single object/ single record, as well as just a read operation that returns the entire set of objects/ records.<br />
<pre class="brush: java;">
public void addProperty(Property property) throws SQLException
{
//code goes here
}

public void removeProperty(String id) throws SQLException
{
//code goes here
}

public void editProperty(Property property) throws SQLException
{
//code goes here
}

public Property searchProperty(String id) throws SQLException
{
//code goes here
}

public List
searchAllProperties() throws SQLException
{
//code goes here
}
</pre></p>
<h4>Create, Update and Delete</h4>
<p>Now for each of the operations, we should prepare a statement, execute the query or update, and then close the resources and handle the exceptions.<br />
<pre class="brush: java;">
        Connection dbCon = null;
        PreparedStatement ps = null;
        try {
            dbCon = getDbConnection();
            ps = dbCon.prepareStatement(&quot;INSERT INTO property VALUES ( ? , ? , ?)&quot;);
            ps.setString(1, property.getPropertyId());
            ps.setString(2, property.getAddress());
            ps.setDouble(3, property.getPrice());

            int affectedRows = ps.executeUpdate();
            if (affectedRows != 1) {
                System.err.println(&quot;Failed to update: Number of affected rows = &quot; + affectedRows);
            }
        } finally {
            if (ps != null) {
                ps.close();
            }
            if (dbCon != null) {
                dbCon.close();
            }
        }
</pre><br />
After obtaining a Connection, we can prepare a PreparedStatement. The Connection#prepareStatment(String) method take one argument, which is an SQL query, with the actual values omitted and their places marked with questions marks (&#8220;?&#8221;). After this, we prepare this statement by substituting the values in place of the question marks, using the PreparedStatement#setX(int, X) method. There are a couple of gotchas to be aware of when doing this:</p>
<ul>
<li>In Java, and in most programming, almost everything is zero-indexed. However, in prepared statements, the first question mark has an index of 1, and we need to keep this in mind.</li>
<li>In SQL, strings need to be surrounded by quotes. As such, it might be natural to surround the first two question marks above with quote. However, this is not necessary &#8211; when using the PreparedStatement#setString(int, String) method, you are already telling the database to expect a string, and thus quotes are made redundant (and cause errors if used).</li>
</ul>
<p>Next, we execute the update, in this case an Create operation, by invoking PreparedStatement#executeUpdate(). We use the return value, which indicates the number of affected rows, to determine whether the update has been successful.</p>
<p>In this case, we choose to have the exceptions handled by the invoking method or class &#8211; that is why each of the CRUD operation methods advertises that they throw SQLExceptions. However, these methods should still close the resources used (the database connection and prepared statement). </p>
<p>Therefore all the statements in the method are put in a try block without a catch block and with a finally block. We close these resources in the finally block, first checking that they are not null as they may never have been initialised, depending on where the exception occurred.</p>
<h4>Read</h4>
<p>The above template may be trivially modified to work for the Update and Delete operations as well. However, Read operations are fundamentally different; because the flow of information is in the opposite direction than it was in Create, Update and Delete statements. As a result, ORM occurs in the opposite direction, and also, instead of PreparedStatement#executeUpdate(), we will use PreparedStatement#executeQuery().<br />
<pre class="brush: java;">
    public List&lt;Property&gt; searchAllProperties() throws Exception {
        Connection dbCon = null;
        PreparedStatement ps = null;
        try {
            dbCon = getDbConnection();
            ps = dbCon.prepareStatement(&quot;SELECT * FROM property&quot;);
            List&lt;Property&gt; pptys = new ArrayList&lt;Property&gt;();

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String id = rs.getString(&quot;PROPERTY_ID&quot;);
                String address = rs.getString(&quot;ADDRESS&quot;);
                double price = rs.getDouble(&quot;PRICE&quot;);
                Property ppty = new Property(id, address, price);
                pptys.add(ppty);
            }
            return pptys;
        } finally {
            if (ps != null) {
                ps.close();
            }
            if (dbCon != null) {
                dbCon.close();
            }
        }
    }
</pre><br />
Unlike PreparedStatement#executeUpdate(), which returns an integer, PreparedStatement#executeQuery() returns a ResultSet object. The ResultSet object can be used/ manipulated in a manner similar to an iterator (although it technically is not an iterator). Use ResultSet#next() to go to the next row, and ResultSet#getX(String) to get the value of an attribute in the named column for the current row.</p>
<p>We can observe the opposite direction of the ORM by comparing this from the Create method:<br />
<pre class="brush: java;">
ps.setString(1, property.getPropertyId());
</pre><br />
to this from the Read method:<br />
<pre class="brush: java;">
String id = rs.getString(&quot;PROPERTY_ID&quot;);
</pre></p>
<p>Notes:<br />
* Provided the vendor is JDBC compliant, and most of the major database vendors are. At time of writing, they number 221. <a href="http://developers.sun.com/product/jdbc/drivers" title="JDBC Drivers" target="_blank">JDBC Drivers</a> provides a full, and searchable, list.</p>
<p>** There are indeed some grey areas, both conceptually and in physical implementation. For example, what happens to static members of classes? What happens to foreign keys that are a combination of multiple primary keys? How are SQL dates mapped to Java date and time objects? There are answers to these, but they are not exactly intuitive.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/brendangraetz.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/brendangraetz.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/brendangraetz.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/brendangraetz.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/brendangraetz.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/brendangraetz.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/brendangraetz.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/brendangraetz.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=brendangraetz.wordpress.com&amp;blog=12710586&amp;post=134&amp;subd=brendangraetz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://brendangraetz.wordpress.com/2010/04/12/relational-databases-and-jdbc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6297ea22cfec0d007c11412283a934bf?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bguiz</media:title>
		</media:content>
	</item>
	</channel>
</rss>
