<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Steve Levine</title>
  <link href="http://blue64.net/atom.xml" rel="self"/>
  <link href="http://blue64.net/"/>
  <updated>2012-03-08T08:55:14-05:00</updated>
  <id>http://blue64.net/</id>
  <author>
    <name>Steve Levine</name>
    
  </author>

  
  <entry>
    <title>WebView(Javascript) -> Native Android API</title>
    <link href="http://blue64.net/2012/03/webview-javascript-native-android/"/>
    <updated>2012-03-03T15:35:00-05:00</updated>
    <id>http://blue64.net/2012/03/webview-javascript-native-android</id>
    <content type="html">&lt;p&gt;For a few weeks towards the end of last year I was developing an Android application.  During that time I discovered a few new (to me) things about the Android platform. One in particular that caught my attention was having Javascript code running locally on the device call a native Android function.&lt;/p&gt;

&lt;p&gt;Before getting into the technical details, let me first talk about a situation where this piece of functionality would be useful.  Lets say (for arguments sake), that you have to write an Android application that requires advanced and very polished charts.  What are you options?  You can try to find a native Android implementation that meets the requirement, but I must admit, I have been there and done that, and couldn&amp;#8217;t find any particularly good libraries.  Yes there are some out there, but they didn&amp;#8217;t have the polish I was looking for (please comment if you know of good ones).&lt;/p&gt;

&lt;p&gt;If there aren&amp;#8217;t any good Android libraries available what can you do?  From personal experience I know there are a lot of good Javascript charting libraries out there.  How can this help when developing an Android application not a web application?  You can host the charting library on a server somewhere and reference them from an Android WebView?  From my experience, this solution is not optimal because of slow performance.  Even thought it was too slow, it still looked much better than any of the native libraries available.&lt;/p&gt;

&lt;p&gt;Is there way to get the Javascript code to run faster perhaps by taking advantage of the beefy hardware most Android devices run on?  Turns out it is quite easy to run the Javascript libraries directly on the device.  After moving the Javascript code from the server to the device the performance was greatly improved.  The charts rendered fast and were very responsive to the touch.&lt;/p&gt;

&lt;p&gt;Running Javascript on the device instead of the Server is fast, but it creates a different sort of problem, namely, now you have a view (Javascript Chart) running inside another view (WebView), how does the Javascript library get its data?  The obvious answer is to have the Javascript code call some (REST) service via HTTP.  For arguments sake, lets say, this would not work due to the fact that the data is only available via a proprietary Java wrapped protocol.  Is there a way for the Javascript code to make a Java call? There is and that is what the rest of this post is going to be about.&lt;/p&gt;

&lt;p&gt;For simplicity, I am going to abstract away the charts and data and replace them with a simple requirement, namely, have a WebView render the underlyingAndroid SDK version.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: For the purposes of this post, and because I like it, I am going to use Scala as the programming language.  As always, you can find all the code on &lt;a href=&quot;https://github.com/slevine/android-javascript-demo&quot;&gt;Github&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The goal of this demo is to show how you can call Java from Javascript running in an Android WebView, thus we need to create a WebView, populate it with a simple html file and then enable Javascript.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;MainActivity.scala - setting up basic view&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;// Step 1: Create WebView &lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WebView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WebView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;// Step 2: Load page from assets&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loadUrl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;file:///android_asset/index.html&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;// Step 3: Enable Javascript&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getSettings&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;setJavaScriptEnabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;So far, so easy.  Next, we need to create a simple Scala function in our &lt;code&gt;MainActivity&lt;/code&gt; to expose for Javascript to call.  We said we wanted our view to expose the underlying Android sdk version, so lets create a function called &lt;code&gt;sdkVersion()&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;MainActivity.scala - function to expose SDK version&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;jsFun&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sdkVersion&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;SDK&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Next we need to make this function available to Javascript by adding it to the DOM.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;MainActivity.scala - Adding a function to the DOM&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;// Add the above function to the DOM as &amp;quot;Android&amp;quot; &lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;// The function can now be invoked from Javascript with the following: Android.sdkVersion()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addJavascriptInterface&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jsFun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Android&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;em&gt;Optional step:&lt;/em&gt; When developing Javascript applications it is sometimes helpfull to be able to debug something to the browser console.  Believe it or not, it is quite easy to implement the browser&amp;#8217;s console implementation with an Android one, and instead of logging the message to the browser console, it will send them to the Android Logcat system.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;MainActivity.scala - Implement Javascript console.log &lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;12&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;13&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;14&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;15&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;   &lt;span class=&quot;c1&quot;&gt;// provide the WebView with a console.log implementation&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;   &lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;setWebChromeClient&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WebChromeClient&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;      &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;onConsoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;consoleMessage&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ConsoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Boolean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;consoleMessage&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;          &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;messageLevel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;&amp;#39;\t&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;          &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;consoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;&amp;#39;\t&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;          &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;consoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sourceId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot; (&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;          &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;consoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;)\n&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;consoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;messageLevel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ConsoleMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MessageLevel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ERROR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;          &lt;span class=&quot;nc&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;JavascriptExmple&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;          &lt;span class=&quot;nc&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;JavascriptExmple&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;      &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Finally we need to create our html/javascript view.&lt;/p&gt;

&lt;p&gt;Lets create a file called &lt;code&gt;index.html&lt;/code&gt; and place it under &lt;code&gt;src/main/assets&lt;/code&gt; and add at least the following code to it:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;index.html - default page that calls our Android.sdkVersion() function via Javascript&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;html&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&lt;/span&gt;Click &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;#&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;here&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; to invoke an Android function to
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;   find out the Android SDK version used to build this App.
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;sdk&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;SDK:&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;This message should appear as a debug message in Logcat.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;.sdk&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Android&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sdkVersion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;As you can see here, we are invoking the Android (Scala) method called &lt;code&gt;sdkVersion()&lt;/code&gt; and appending the results of the call to a div using JQuery.&lt;/p&gt;

&lt;p&gt;Thats all there is to it, now you know how to invoke an Android function from Javascript running in an Android WebView.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Mission Accomplished, Migration From Wordpress to Octopress Complete!</title>
    <link href="http://blue64.net/2011/09/migration-from-wordpress-to-octopress-complete/"/>
    <updated>2011-09-28T14:26:00-04:00</updated>
    <id>http://blue64.net/2011/09/migration-from-wordpress-to-octopress-complete</id>
    <content type="html">&lt;p&gt;Someone told me about the &lt;a href=&quot;http://octopress.org/&quot;&gt;Octopress&lt;/a&gt; blog engine last week, which is a blog engine based on &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki&quot;&gt;Jekyll&lt;/a&gt;, the engine that powers sites such as &lt;a href=&quot;http://pages.github.com/&quot;&gt;GitHub Pages&lt;/a&gt;.  The main difference between Octopress and plain vanilla Jekyll is with Jekyll, you have to write your own Templates, Styles, and Javacscript code, but with Octopress, it has been abstracted away.  All you need to do is clone the Octopress repository and start writing posts (or migrating posts in my case).  With that being said, I have spend the past week migrating my blog from Wordpress to Octopress.&lt;/p&gt;

&lt;p&gt;Here are some of my initial thoughts, first on Wordpress:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Compared to when it first came out, Wordpress is now too heavy for a simple blog.  It has the functionality of a content management system, and that is why sites like TechCrunch use to manage their content.  They also have an army of authors creating content, vs one for my blog.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every time the admin site opens, it says that an Wordpress upgrade is available.  At first this was neat, a one click upgrade, but now, it is becoming a bit of a risk.  A lot of effort has gone in to the site, and if for some reason the upgrade process is not clean, it can break a lot of things.  Clearly, you should be backing up as you go along, but then it is no longer a one click upgrade.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The process of writing a post is very heavy.  First you need to log in to the Admin site, start writing the post in html markup.  If you want to include code in your post, there is not really a clean way to do this.  To preview your post you hit preview, but most of the time the code plugins do not behave correctly thus the only real way to preview is to publish the post.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An internet connection is required to do anything.  This is not 100% true, as you can always use a tool such as Mars Edit to write the post offline, but the preview never looks the same as it does when published on the real site.  It also doesn&amp;#8217;t understand how to preview or author code samples.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Now my thoughts on Octopress:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It is based on Ruby which is Cool!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No database required!  Again, since Octopress is nothing more than a Template Engine, the end product is static html that you can copy to any host and it will be live.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since Octopress is a template engine, not a blog engine, you have your entire blog including all your posts in a single project.  This works out great because you can open the project in RubyMine and see your posts as Markdown, your design as Sass and CSS, and your site layout as Liquid Templates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The writing process is very Agile, as you can preview the site as many times as you want without having to copy/push/deploy code anywhere.  All that you need to do is &lt;code&gt;cd&lt;/code&gt; in to your blog&amp;#8217;s project directory and type &lt;code&gt;rake preview&lt;/code&gt; and browse to &lt;code&gt;localhost:4000&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can write posts in Markdown, which happens to be my favorite way of generating web content (thanks to GitHub and Stackoverflow).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are a variety of great ways to embed code within a post.  The first great option is to use the neat GitHub notation by surrounding your code with three tick marks.  The second is to use the Liquid Template notation, which is equally nice.  Lets not forget that you can include a GitHub gist VERY easily, just &lt;code&gt;{ gist gistIdNumber }&lt;/code&gt;. Can&amp;#8217;t get much easier than that!  Last but defiantly, not least is you can include a source file from the filesystem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code samples look fantastic when they are rendered in the Solarized themed code viewer! Hers&amp;#8217;s a sample:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;Tail Recursive Fibonacci&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib_tr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib_tr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fib_tr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;It has been about a week and my blog is completely migrated over to Octopress now.  It has been a rather enlightening experience for me because Octopress and Wordpress are so different.  Bottom line is Octopress was written by and targeted at hackers, and that fact inspires me at a level that Wordpress never was able to do.  I am hoping to leverage that hacker inspiration to get my self writing posts on a more regular basis starting today.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>In case you haven&#8217;t heard - Apple is not showing Java any more love&#8230;</title>
    <link href="http://blue64.net/2010/10/in-case-you-havent-heard-apple-is-not-showing-java-any-more-love/"/>
    <updated>2010-10-21T16:29:24-04:00</updated>
    <id>http://blue64.net/2010/10/in-case-you-havent-heard-apple-is-not-showing-java-any-more-love</id>
    <content type="html">&lt;p&gt;&lt;a href=&quot;http://goo.gl/mDkf&quot;&gt;Official release notes&lt;/a&gt;.
If you think about it
it makes perfect sense for them. To them Java is no different than
Flash, just a GUI platform. How many killer Java applications are
there out there (besides Java IDE&amp;#8217;s) ? Add to the fact that
SunOracle is probably twisting their arm for more licensing money.&lt;/p&gt;

&lt;p&gt;Thus, it makes perfect sense for them, why would they want to waste
their resources implementing a JVM that is required by only a few
applications especially when they are trying to boot strap their
&amp;#8220;App Store&amp;#8221;? Supporting Java developers working on server software
is not part of their business model.&lt;/p&gt;

&lt;p&gt;But… This is &lt;strong&gt;not&lt;/strong&gt; bad for Java developers (although lots of Java developers are showing the
Apple hate right now), because the Apple JVM was never up to date,
and always behind the &amp;#8220;real&amp;#8221; JDK implementation. Remember the
Eclipse 64 bit fiasco?&lt;/p&gt;

&lt;p&gt;My hopes moving forward is that the
community comes together to come up with a completely open source
version of the JVM for BSD/Mac. There are already two really good
starting points, &lt;a href=&quot;http://goo.gl/JMNB&quot;&gt;SoyLatte&lt;/a&gt; and
&lt;a href=&quot;http://goo.gl/8wkg&quot;&gt;OpenJDK&lt;/a&gt;. It would be great if Apple were to
open source their JVM code base (although this is not likely due to
SunOracle Licensing), can still hope.&lt;/p&gt;

&lt;p&gt;Bottom line, if you are a
Java developer you do not have to start migrating away from OS X,
everything will be fine – just give it a bit of time.&lt;/p&gt;

&lt;p&gt;Just my thoughts.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>First Thoughts On My New Eee Pc (1005PE)</title>
    <link href="http://blue64.net/2010/03/first-thoughts-on-my-new-eee-pc-1005pe/"/>
    <updated>2010-03-23T18:30:30-04:00</updated>
    <id>http://blue64.net/2010/03/first-thoughts-on-my-new-eee-pc-1005pe</id>
    <content type="html">&lt;p&gt;So far, so good with my new Eee PC (1005PE) Netbook. The machine
came pre-installed with Win 7 Starter edition, so the first task
for me to do was to get rid of Windows 7 and install
&lt;a href=&quot;http://www.ubuntu.com/GetUbuntu/download-netbook&quot;&gt;Ubuntu Netbook&lt;/a&gt;
on it. So I downloaded the Ubuntu Netbook 10.04
&lt;a href=&quot;http://cdimage.ubuntu.com/ubuntu-netbook/daily-live/current/&quot;&gt;daily build&lt;/a&gt;
image (yes, I am daring), and was on my way.&lt;/p&gt;

&lt;p&gt;At first, this task seemed easier said than done because I was running in to the most
fundamental problem possible, namely, I could not get the Eee PC to
boot from the &amp;#8216;bootable&amp;#8217; usb stick I created on my Mac Pro desktop.
I checked every single bios settings, made sure that USB was chosen
as priority boot drive, Still nothing. Kept getting the Windows 7
startup sound which was starting to get a bit tedious.&lt;/p&gt;

&lt;p&gt;The next thing I thought was maybe the Cruser U3 software was causing the
trouble so I went out and found a U3 uninstaller, and ran it. Still
didn&amp;#8217;t boot from the USB Stick. I then found out that in order to
get the boot menu on the Eee PC, you need to hold down the Escape
key while its booting. I tried that, it had the USB as a target
bootable device, I selected it, but still went in to Windows 7.&lt;/p&gt;

&lt;p&gt;At this point I was quickly running out of ideas, the only other thing
I can think of was perhaps the USB stick was some how not bootable?
Maybe the USB stick was not created correctly even though I
followed the Ubuntu Mac instructions step by step. I downloaded the
Ubuntu 10.04
&lt;a href=&quot;http://cdimage.ubuntu.com/ubuntu-netbook/daily-live/current/&quot;&gt;daily build&lt;/a&gt;
on to my Eee PC while booted in to Windows 7, and then downloaded
this program called &lt;a href=&quot;http://unetbootin.sourceforge.net/&quot;&gt;UNetbootin&lt;/a&gt;. This time I used
that program to create my bootable USB stick, and then I tried to
reboot again.&lt;/p&gt;

&lt;p&gt;This time it booted in to the Live CD version of
Ubuntu, yes, I was saved! I couldn&amp;#8217;t believe that it was a bad
image on my USB Stick. Why can&amp;#8217;t a Mac create a bootable USB Stick?
The strange thing is that the Eee couldn&amp;#8217;t even read the files on
the USB Stick when connected in Windows 7, but when the Stick was
plugged in to my Mac Pro, I was able to see the files fine. And
vise versa, once I created the USB Stick in Windows, I coulsn&amp;#8217;t see
the files on Mac. What is the deal here? I thought ISO images were
platform independent?&lt;/p&gt;

&lt;p&gt;With that being said the USB Stick problem is well in the past for me.
Look for my next post where I will give my impressions of Ubuntu Netbook 10.04.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>RubyMine 2 Debugging Issue Resolved</title>
    <link href="http://blue64.net/2010/01/rubymine-2-debugging-issue-resolved/"/>
    <updated>2010-01-23T15:27:19-05:00</updated>
    <id>http://blue64.net/2010/01/rubymine-2-debugging-issue-resolved</id>
    <content type="html">&lt;p&gt;If you are trying to debug Ruby code in RubyMine 2 IDE, but are
having difficulties such as, the IDE freezes after you try to step
in, step over, or step next and are wondering if your configuration
is wrong? It is not, if you happen to have installed the
&lt;code&gt;ruby-debug-ide19&lt;/code&gt; gem from the command line (not from IDE), you
need to patch the actual gem code to get things working nicely.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open the following file with your favorite text editor (part of
&lt;code&gt;ruby ruby-debug-ide19&lt;/code&gt; gem)&lt;/li&gt;
&lt;/ul&gt;


&lt;figure class=&#8217;code&#8217;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8221;&gt;&lt;span class=&#8217;line&#8217;&gt;$GEM_HOME/ruby-debug-ide19-0.4.12/lib/ruby-debug/command.rb&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;ul&gt;
&lt;li&gt;Add the following code at line \~120 (look below for full code
location):&lt;/li&gt;
&lt;/ul&gt;


&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;ruby&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$FILENAME&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;ul&gt;
&lt;li&gt;After the modifications, the code should look like:&lt;/li&gt;
&lt;/ul&gt;


&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;ruby&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;debug_eval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_binding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_s&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;$FILENAME&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;max_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Thats it, you should be able to debug your Rails/Ruby code in
RubyMine without issues.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>With Grape, Groovy Is On Par with Native Scripting Languages</title>
    <link href="http://blue64.net/2009/12/with-grape-groovy-is-on-par-with-native-scripting-languages/"/>
    <updated>2009-12-24T14:31:36-05:00</updated>
    <id>http://blue64.net/2009/12/with-grape-groovy-is-on-par-with-native-scripting-languages</id>
    <content type="html">&lt;p&gt;If you haven&amp;#8217;t heard, the latest version of Groovy was
&lt;a href=&quot;http://docs.codehaus.org/display/GROOVY/Groovy+1.7+release+notes&quot;&gt;released&lt;/a&gt;
this week and included with it, among many other great features,
was &lt;a href=&quot;http://groovy.codehaus.org/Grape&quot;&gt;Grape&lt;/a&gt; (Groovy Advanced
Packaging Engine). Grape is an annotation based dependency
management system that provides functionality similar to that of
Maven and Ivy with one clear advantage, namely, no build file.&lt;/p&gt;

&lt;p&gt;If Grape doesn&amp;#8217;t use a build file, how does it know what dependencies
are necessary to run the code? Does it figure it out for you on the
fly? Unfortunately, it is not that smart (yet), perhaps the next
release. If it doesn&amp;#8217;t figure it out for you, then how do you
specify your dependencies? You configure your dependencies by using
the&lt;code&gt;@Grapes&lt;/code&gt; or &lt;code&gt;@Grab&lt;/code&gt; annotations.&lt;/p&gt;

&lt;p&gt;What is so good about being able to configure your dependencies via annotations?&lt;/p&gt;

&lt;p&gt;If you are working with Groovy scripts, it frees you up from having to worry
about dependency management and allows you to focus more on what
the script needs to do much like when working with other scripting
languages like Ruby or Perl. In order to clearly demonstrate the
advantages of Grape, lets walk through an example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Trying to keep up with my ever changing IP address after switching
ISP&amp;#8217;s earlier this year. There are several services running at my
home that I need access to on a daily basis. If my IP changes over
night, after a brown out, or for some other reason, I need to know
about it asap.&lt;/p&gt;

&lt;p&gt;In order to keep up with my IP address, I wrote a set of scripts that perform the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Obtains the current IP address of the server where it is running&lt;/li&gt;
&lt;li&gt;Looks up the most recent IP address of the server in a log file&lt;/li&gt;
&lt;li&gt;If the current IP address is different that the most recent IP address:

&lt;ul&gt;
&lt;li&gt;Updates the log file with the current IP address&lt;/li&gt;
&lt;li&gt;Send the new IP address in a customizable email to a configurable address&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If the IP address&amp;#8217;s are the same, it does nothing.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;strong&gt;The Solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It took a total of three Groovy classes/scripts to
solve this problem. We are not going to get in to the details of
the solution because I want to stay focused on Grape.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;You can find all of the code discussed in this post &lt;a href=&quot;http://github.com/slevine/my-ip&quot;&gt;on github&lt;/a&gt;.
Please feel free to download and use it. Feedback is welcome as well.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;This simple Groovy class first connects to a mail server, and then
sends the change of address message.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt; (Mailer.groovy)&lt;/span&gt; &lt;a href=&#8217;http://blue64.net/downloads/code/groovy/Mailer.groovy&#8217;&gt;download&lt;/a&gt;&lt;/figcaption&gt;
 &lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;12&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;13&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;14&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;15&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;16&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;17&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;18&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;19&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;20&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.mail.Session&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.mail.Message&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.mail.internet.MimeMessage&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.mail.internet.InternetAddress&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nd&quot;&gt;@Grapes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;([&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;nd&quot;&gt;@Grab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;javax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;nd&quot;&gt;@Grab&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;javax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;mail&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mail&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.4&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mailer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ConfigSlurper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;message&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parse&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;File&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MailProperties&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;groovy&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toURL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;deliverIpAddressChangeMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ipAddress&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subject&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;IP Address Changed to $ {ipAddress} &amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot; IP Address changed to $ {ipAddress}. \nPlease update your configurations.&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;n&quot;&gt;sendMail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;${s _config.message.to}&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;${s_config.message.from}&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;//&#8230;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The most interesting things to pay attention to are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;@Grapes&lt;/code&gt; block after all of the imports, you can see this
  groovy class depends on javax.activation and javax.mail jars.&lt;/li&gt;
&lt;li&gt;Thanks to Grapes, you can compile this class simply by invoking
  &lt;code&gt;groovyc Mailer.groovy&lt;/code&gt; as opposed to having to configure either
  Maven, Gant, Ant, or some other build tool to manage the
  dependencies and classpath for you.&lt;/li&gt;
&lt;li&gt;What&amp;#8217;s the big deal? Read more to find out!&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This next code snipped represents the &amp;#8220;main&amp;#8221; entry point of my
solution. It simply obtains the current IP address of the machine
it is running on, checks the current address against the most
recent known address stored in a log file, and then uses the
previous class to send an email if the IP address has changed.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt; (whereIsMyIp.groovy)&lt;/span&gt; &lt;a href=&#8217;http://blue64.net/downloads/code/groovy/whereIsMyIp.groovy&#8217;&gt;download&lt;/a&gt;&lt;/figcaption&gt;
 &lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;12&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;13&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;14&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;15&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;16&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;usr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;groovy&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;c1&quot;&gt;// IP Address Regex http://www.regular-expressions.info/examples.html&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;currentIp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;http://whatsmyip.us/&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toURL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=~/&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&#92;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;println&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentIp&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipLog&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;ip-log.txt&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;recentIp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ipLog&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;readLines&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tokenize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;,&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;currentIp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recentIp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;Mailer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;deliverIpAddressChangeMessage&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currentIp&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;IP Address has changed, it is now: ${currentIp}. Sending Message.&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;ipLog&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;${new Date()}, ${currentIp}\n&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The most interesting thing to pay attention to in this script is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;#!/usr/bin/env groovy&lt;/code&gt; on the first line on the script.&lt;/li&gt;
&lt;li&gt;This line enables the script to be called directly from the command
 line like: &lt;code&gt;./whatsMyIp.groovy&lt;/code&gt; instead of &lt;code&gt;groovy whatsMyIp.groovy&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;strong&gt;The Big Deal!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If Grape didn&amp;#8217;t exist the only way to invoke this
script would be to invoke it with a build tool such as Maven, GAnt,
or some other. If a build tool didn&amp;#8217;t suit you then you would have
to invoke &lt;code&gt;groovy -classpath=/path/activation.jar&#8230;&lt;/code&gt; and manage
the dependencies there. Both of these solutions work fine, but are
clunky.&lt;/p&gt;

&lt;p&gt;If you were to solve this problem using a language such as
Ruby, you would not have to worry about dependency management since
Ruby is so closely integrated with the OS. You would simply run
&lt;code&gt;gem install some gem&lt;/code&gt;, and this would install the dependencies at
the OS level. Thus allowing you to focus on your script and letting
the Ruby runtime focus on the dependencies. Invoking
&lt;code&gt;./someScript.rb&lt;/code&gt; is common in Ruby.&lt;/p&gt;

&lt;p&gt;Grape gives Groovy scripts the same clean dependency abstraction. It is possible to invoke
&lt;code&gt;./whatsMyIp.groovy&lt;/code&gt; without having to worry about any dependency
management. Once the groovy runtime comes across the Grape
annotations, it loads the dependencies on demand freeing the Groovy
script from having to be wrapped with a dependency management layer.&lt;/p&gt;

&lt;p&gt;This is a huge deal because now simple Groovy scripts can
leverage the entire Java ecosystem from the command line without
having to wrap the invocation with a build tool. Groovy Scripts are
now clean, simple, and easy. I hope this inspires you to go out and
convert some Ruby or Perl script to Groovy.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Time Machine over a Network Drive</title>
    <link href="http://blue64.net/2009/12/time-machine-over-a-network-drive/"/>
    <updated>2009-12-13T14:04:45-05:00</updated>
    <id>http://blue64.net/2009/12/time-machine-over-a-network-drive</id>
    <content type="html">&lt;p&gt;This post describes the steps involved when setting up Time Machine to backup to a Network Drive.  These steps are only required if you want to back up to a device other than a Time Capsule.  It is pretty quick and easy, so without further due, lets get started.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Enable network backups in Time Machine&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a terminal window cut/paste the following command:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8221;&gt;&lt;span class=&#8217;line&#8217;&gt;defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;blockquote&gt;&lt;p&gt;Update: Steps 2 &amp;amp; 3 are only required if you are not running Snow Leopard.  If you are, then all you need to do is mount the network drive you wish to use as a Time Machine destination, and then proceed to Step 4.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create Timemachine backup volume&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a terminal window cut/paste the following command:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8221;&gt;&lt;span class=&#8217;line&#8217;&gt;hdiutil create -fs HFS+J -volname &quot;Backup of computer-name&quot; computer-name_[mac address without&#8217;:&#8217;].sparsebundle&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;em&gt;The simplest way to obtain your mac address is to open a terminal window and type the command: &lt;code&gt;ifconfig -a&lt;/code&gt;, and look for the section of the output where it says: &lt;code&gt;ether 00:33:44:55:66:77&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The simplest way to obtain your computer name is to open a terminal window and type the command: &lt;code&gt;hostname&lt;/code&gt;, it will return the name of your computer, example, &lt;code&gt;my-hostname&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Putting it all together, based on the above examples, you would run the following command:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8221;&gt;&lt;span class=&#8217;line&#8217;&gt;hdiutil create -fs HFS+J -volname &quot;Backup of my-hostname&quot; my-hostname_003344556677.sparsebundle&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;strong&gt;Step 3: Copy file created in step 2 to network Time Machine backup destination&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;Using finder or terminal, copy the newly created .sparsebundle file to the place you want your Time Machine backup to reside.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Open Timemachine preferences, and the network drive should show up as a backup target&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If for some reason it doesn&amp;#8217;t, try opening and closing the Time Machine preferences, as it may take a moment for it to detect the newly available network drive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Rest easy knowing your mac is now backed up to a network storage volume.&lt;/strong&gt;&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Scala Sugar - Iteration</title>
    <link href="http://blue64.net/2009/11/scala-sugar-iteration/"/>
    <updated>2009-11-22T14:58:11-05:00</updated>
    <id>http://blue64.net/2009/11/scala-sugar-iteration</id>
    <content type="html">&lt;p&gt;In this second installment of Scala Sugar, lets put the lists that
we created in the
&lt;a href=&quot;http://blue64.net/2009/11/scala-sugar-lists/&quot;&gt;previous post&lt;/a&gt; to
use.&lt;/p&gt;

&lt;p&gt;How do we typically interact with lists when writing
non-trivial programs? We iterate over them! With that being said,
lets explore how iteration in Scala compares with iteration in
Java.&lt;/p&gt;

&lt;p&gt;Taking the lists in the previous post in to account, lets
assign ourselves a task of iterating over each element in the list
and converting them to uppercase.&lt;/p&gt;

&lt;p&gt;First, we all know how to do this in Java using a standard for each loop:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;There are so many different ways to iterate in Scala, thus we are only
going to talk about the most trivial ways.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;\&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;-or-&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;%s&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;em&gt;Download Source: &lt;a href=&quot;http://github.com/slevine/scala-training/blob/master/src/main/scripts/collections/simpleLists.scala&quot;&gt;simpleLists.scala&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As you can see, you can loop in Scala the same way that you do in
Java, namely, with a for each loop. There is nothing special about
that.&lt;/p&gt;

&lt;p&gt;The second loop is written in more a functional paradigm, as
it uses the Scala map function. It allows you to iterate over the
list without having to know anything about the details of the
iteration itself. With Scala you are working at a much higher
level. If we look at the Scala map function, it takes in a function
as an argument, in this case the function is &amp;#8220;toUpperCase()&amp;#8221;. The
map function then applies this function to all of the elements in
the List, thus you don&amp;#8217;t have to worry about the actual iteration
logic. In this scenario, all the caller needs to worry about is
that they have a List of elements, and they want some function f
applied to all of them.&lt;/p&gt;

&lt;p&gt;You can chain functions together on a List.
In this case, we changed a foreach to the end of the map. If we
were to describe the what is going on in plain english, it would
sound something like, take all the elements of l, apply
&amp;#8220;toUpperCase&amp;#8221; to all of them, then for each of them, print them.&lt;/p&gt;

&lt;p&gt;The final interesting thing to notice in the above line of code is
the &amp;#8220;_&amp;#8221; placeholder syntax. It looks strange to have a &amp;#8220;_&amp;#8221; there
as part of the code, but all it is doing is acting as a placeholder
for the function. It simply represents the current element of the
List being operated on. Even though there are two &amp;#8220;_&amp;#8220;&amp;#8216;s in this
example, they are completely independent of each other. The
placeholder is a very powerful advanced concept in Scala and this
example barely touches the surface of its usage. We will talk more
about it in a dedicated post.&lt;/p&gt;

&lt;p&gt;As you can see, Scala supports both the &amp;#8220;Java&amp;#8221; way of iterating and
a pure functional way. Again, this example is just one of the many different techniques for iterating
in Scala. In a future post we will look at other ways of iterating
in Scala.&lt;/p&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code found in this post is hosted at
  &lt;a href=&quot;http://github.com/slevine/scala-training&quot;&gt;github.com&lt;/a&gt; along with other sample Scala code.&lt;/li&gt;
&lt;/ul&gt;

</content>
  </entry>
  
  <entry>
    <title>Scala Sugar - Lists</title>
    <link href="http://blue64.net/2009/11/scala-sugar-lists/"/>
    <updated>2009-11-21T16:01:58-05:00</updated>
    <id>http://blue64.net/2009/11/scala-sugar-lists</id>
    <content type="html">&lt;p&gt;If I want to become a real Scala Ninja, I am going to have change
the way I think about coding. For the past 10 years I have been
programming primarily using the object oriented paradigm. Although
Scala supports pure object oriented programming, it is my desire to
learn to program in Scala in a complete functional paradigm.&lt;/p&gt;

&lt;p&gt;Of course, learning a new programming paradigm is not going to happen
over night, it is going to take a bit of time. Over the course of
the next few months, I am going to be posting a series of posts
documenting my progress learning Scala. They will be written from
the perspective of a &amp;#8220;hardcore&amp;#8221; Java programmer being enlightened
by a sweet new language. I will write about all of the &amp;#8220;sugar&amp;#8221;
Scala has to offer a Java programmer, and that is how I came up
with title &amp;#8220;Scala Sugar&amp;#8221;.&lt;/p&gt;

&lt;p&gt;In this first installment of Scala Sugar
lets discuss one of the most fundamental concepts required to write
any non-trivial program, namely Lists. For the longest time, Lists
in Java didn&amp;#8217;t bother me at all, as they seemed &amp;#8220;normal&amp;#8221; to me.
That was until I was introduced to dynamic languages a few years
back. Now, Lists in Java seem extremely verbose to me as I wonder
why in Java you can&amp;#8217;t just create and populate a list with a single
line of code. Lets look at our first Java vs Scala comparison.&lt;/p&gt;

&lt;p&gt;Lets create a trivial list of three lowercase strings.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.util.ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;java.util.List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;c&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;In Java it takes a minimum of 6 lines of code to declare a list of 3 strings!
Now lets declare the same list in Scala.&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;c&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Thats it, one line of code! The most important things to pay
attention to are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No need to import List in Scala, as most fundamental classes are imported by default in Scala&lt;/li&gt;
&lt;li&gt;You do not have to give &amp;#8216;l&amp;#8217; a type in Scala. Scala uses Type
  inference which means that it can infer its type from the object it
  is pointing to. It is important to realize that Scala is statically
  typed, it just doesn&amp;#8217;t require you to type your variables when you
  define them. The compiler is smart enough to figure it out.&lt;/li&gt;
&lt;li&gt;You can construct a list with elements in Scala, no need to
  &amp;#8220;add&amp;#8221; each element separately.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Even in this trivial example, you can start to see how clean and
concise Scala is.&lt;/p&gt;

&lt;p&gt;In my next post we will explore different ways to iterate over the lists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code found in this post is hosted at &lt;a href=&quot;http://github.com/slevine/scala-training&quot;&gt;github.com&lt;/a&gt; along with other sample Scala code.&lt;/li&gt;
&lt;/ul&gt;

</content>
  </entry>
  
  <entry>
    <title>Scala reduceLeft</title>
    <link href="http://blue64.net/2009/11/scala-reduceleft/"/>
    <updated>2009-11-19T17:52:02-05:00</updated>
    <id>http://blue64.net/2009/11/scala-reduceleft</id>
    <content type="html">&lt;p&gt;&lt;em&gt;This post was updated on November 19, 2009&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;As you know, during
the past few weeks (time permitting), I have been spending time
studying the Scala programming language. After reading the first
few chapters of
&lt;a href=&quot;http://www.artima.com/shop/programming_in_scala&quot;&gt;Programming In Scala&lt;/a&gt;,
I have come across the first feature of Scala that would have been
totally useful for me on one of my Java programming tasks a few
months ago.&lt;/p&gt;

&lt;p&gt;The actual programming task was quite complex, but for
the purposes of this post, we will work with a dramatically
simplified example. The simplified task is: given a list of Stocks
figure out which one has the highest earnings per share.&lt;/p&gt;

&lt;p&gt;Here is one way the problem can be solved in Scala:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;12&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;13&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;14&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;15&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;16&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;17&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;18&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Stock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ticker&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;price&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;earnings&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shares&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;toString&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ticker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot; has eps of: &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot; with a price of &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;price&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;earnings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shares&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;that&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Stock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Boolean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;that&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;portfolio&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Stock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;aapl&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;203&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.73E9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;878876000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Stock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;ibm&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;66&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.23E10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1384331000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Stock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;goog&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;465&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.19E9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;314754113&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;n&quot;&gt;portfolio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduceLeft&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;  &lt;span class=&quot;c1&quot;&gt;// goog has eps of: 13.31&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;em&gt;Download Source: &lt;a href=&quot;http://github.com/slevine/scala-training/blob/master/src/main/scala/net/slevine/scalatraining/stocks/model/stocks.scala&quot;&gt;Stock.scala&lt;/a&gt;, &lt;a href=&quot;http://github.com/slevine/scala-training/blob/master/src/main/scripts/stocks/reduceLeft.scala&quot;&gt;reduceLeft.scala&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have never seen Scala code before, this code will look quite
foreign to you because there are a lot of concepts in Scala that
are not present in Java. In a few weeks (again time permitting),
will begin writing a series on Scala for Java programmers, but for
right now, please bear with me, and try to follow along.&lt;/p&gt;

&lt;p&gt;The most interesting things to me about this code are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;del&gt;Line 04: You are reading it correct, that is operator overloading!&lt;/del&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Correction: Scala doesn&amp;#8217;t actually support &amp;#8220;operator overloading&amp;#8221;, in fact, &amp;#8220;operators are not special language syntax&amp;#8221;.
In Scala any method can be an operator. What makes an operator an operator is how you use it. Example:&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;scala&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//operator&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//not operator (but returns wrong type)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).+(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Correct return type&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;ul&gt;
&lt;li&gt;Line 07: It is great to be able to populate a list so easily
  without having to mess around with add methods.&lt;/li&gt;
&lt;li&gt;Line 14: This is the coolest feature of them all so far, the
  ability to reduce a list of elements down to one based on a binary operator.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;This is just a &lt;strong&gt;small&lt;/strong&gt; demonstration of the very basics of the
Scala language. If you find this at all interesting, you can find
out much more at &lt;a href=&quot;http://www.scala-lang.org/&quot;&gt;scala-lang.org&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: obviously, this is not the most optimal Scala solution so if your a Scala guru,
criticisms are welcome. I hope to come back to this example every few weeks, continually improving it as my knowledge of Scala grows.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;References The code found in this post is hosted at
&lt;a href=&quot;http://github.com/slevine/scala-training&quot;&gt;github.com&lt;/a&gt; along with
other sample Scala code.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Simple Applescript For The Traveling Mac</title>
    <link href="http://blue64.net/2009/11/simple-applescript-for-the-traveling-mac/"/>
    <updated>2009-11-12T22:46:45-05:00</updated>
    <id>http://blue64.net/2009/11/simple-applescript-for-the-traveling-mac</id>
    <content type="html">&lt;p&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You have a MacBook and a nice Apple Cinema display
(this doesn&amp;#8217;t sound like a problem so far), and you travel with the
MacBook every day. When you open the lid of your MacBook you like
to have the Dock on the left side of the screen giving you the most
top to bottom space, but when you come home and connect the MacBook
to your Cinema display and set up dual monitors, you want the dock
on the bottom of the Cinema, not on the left side of the MacBook.&lt;/p&gt;

&lt;p&gt;You don&amp;#8217;t want to have to go in to preferences every time to switch
the location. (or maybe you do?) For me it was becoming a very
tedious task, so I began researching ways to automate it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Leverage Apple&amp;#8217;s &amp;#8220;Language of Automation&amp;#8221;,
Applescript to handle the task. Applescript has lots of useful
hooks in to OS X.&lt;/p&gt;

&lt;p&gt;The requirements for the script are quite simple:
- Obtain the current resolution of the primary monitor
- if the resolution is &gt; 1900 (Cinema Display) configure the Dock for large display
- else configure the Dock for laptop display&lt;/p&gt;

&lt;p&gt;For the moment, that is my goal, simple, yet time saving.&lt;/p&gt;

&lt;p&gt;Step 1: Open AppleScript Editor
Step 2: Paste the following code in&lt;/p&gt;

&lt;div&gt;&lt;script src=&#8217;https://gist.github.com/232524.js?file=switch-displays.applescript&#8217;&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;&#8211; Obtain current resolution
tell application &amp;quot;Finder&amp;quot;
    set screenSize to bounds of window of desktop
    set screenWidth to item 3 of screenSize
    set screenHeight to item 4 of screenSize
    &#8211;display dialog &amp;quot;Screen resolution: &amp;quot; &amp;amp; screenWidth &amp;amp; &amp;quot; X &amp;quot; &amp;amp; screenHeight &amp;amp; &amp;quot; &amp;quot; &amp;amp; screenSize as Unicode text
end tell

&#8211; Configure the Dock based upon the current resolution
if screenWidth is less than 1900 then
    tell application &amp;quot;System Events&amp;quot;
        tell dock preferences
            &#8211;get properties
            set properties to {minimize effect:genie, magnification size:0.669312179089, dock size:0.0, autohide:false, animate:true, magnification:true, screen edge:left}
        end tell
    end tell
    tell application &amp;quot;WebKit&amp;quot;
        set the properties of front window to {bounds:{71, 22, 1350, 893}}
        &#8211;get properties of front window
    end tell
else
    tell application &amp;quot;System Events&amp;quot;
        tell dock preferences
            set properties to {minimize effect:genie, magnification size:0.632275104523, dock size:0.25925925374, autohide:false, animate:true, magnification:true, screen edge:bottom}
        end tell
    end tell
    tell application &amp;quot;WebKit&amp;quot;
        set the properties of front window to {bounds:{230, 59, 1612, 1050}}
        &#8211;get properties of front window
    end tell
end if


&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;


&lt;p&gt;Step 3: Run it to make sure it works as expected, if so, save the
script as application, so you don&amp;#8217;t need to open AppleScript Editor
each time you want to run it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Future requirements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Figure out how to hook the script in to sleep/wake events in Snow Leopard.&lt;/li&gt;
&lt;li&gt;Customize more than just the Dock.&lt;/li&gt;
&lt;li&gt;Migrate it in to a startup script that brings up all necessary
  applications based upon current mood (reading/blogging/coding).&lt;/li&gt;
&lt;li&gt;Others???&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Overall, I find Applescript a very easy way to automate things in
Snow Leopard.&lt;/p&gt;

&lt;p&gt;All of the source for this post can be found on
&lt;a href=&quot;http://gist.github.com/232524&quot;&gt;github.com&lt;/a&gt;. Please feel free to
fork and improve. Enjoy.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>iPhone 3.0 upgrade from beta 5 to final</title>
    <link href="http://blue64.net/2009/06/iphone-3-0-upgrade-from-beta-5-to-final/"/>
    <updated>2009-06-18T12:40:50-04:00</updated>
    <id>http://blue64.net/2009/06/iphone-3-0-upgrade-from-beta-5-to-final</id>
    <content type="html">&lt;p&gt;For the past few months, I have been running the iPhone OS 3.0
betas on my iPhone without too many issues. Thus I assumed that
when the final version of OS 3 was released, it would be easy to
upgrade to it. This has not been the case. The current beta on my
iPhone was beta 5, and then yesterday when the final version was
announced, I assumed after a simple plug in to iTunes, I would be
upgraded to the released version. Nope, iTunes kept telling me that
I have the latest version of iPhone OS 3. It seems as though it is
not checking the build number, only the OS level. The next thing I
tried was doing a restore, this didn&amp;#8217;t work either because it gave
me an error saying it could not connect to the site to download the
file.&lt;/p&gt;

&lt;p&gt;Finally, after some Googling around, I found that others with the
same problem circumvented it by
&lt;a href=&quot;http://support.apple.com/kb/HT1808&quot;&gt;putting their iPhone in recovery mode&lt;/a&gt;
and then upgrading from there.&lt;/p&gt;

&lt;p&gt;If you are currently running beta 5, you were supposed to upgrade
to the GM version posted last week. I skipped it thinking their
would be another release in the next few days with the final
version. I have now learned that the GM version is the same as the
final version. Unfortunately, now it is too late to download the GM
from the developer website, as Apple has taken it down, and
according to their documentation, you should obtain the final
version through iTunes. Thus if you are in the same boat as me, on
beta 5, you will have to install the latest version via recovery
mode.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Easy Upgrades</title>
    <link href="http://blue64.net/2009/06/easy-upgrades/"/>
    <updated>2009-06-15T17:49:31-04:00</updated>
    <id>http://blue64.net/2009/06/easy-upgrades</id>
    <content type="html">&lt;p&gt;Who doesn&amp;#8217;t like easy upgrades? Easy upgrades are great when
implemented correctly. There are two speciifiic easy upgrades I
have been (enjoying) using recently, namely, Wordpress and Ubuntu
Server.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href=&quot;http://wordpress.org&quot;&gt;Wordpress&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This blog has been powered by &lt;a href=&quot;wordpress.org&quot;&gt;Wordpress&lt;/a&gt; for a long
time, and has been upgraded many times along the way. Download the
latest tgz file, unzip it, save your wp-content directory, upgrade
your database, and then you are good to go. Keep in mind this was a
very simple procedure, as they call it their &amp;#8220;famous 5-minute
installation&amp;#8221;, which it typically was.&lt;/p&gt;

&lt;p&gt;Once upgrading to 1.7, the procedure became
&lt;a href=&quot;http://codex.wordpress.org/Tools_Upgrade_SubPanel&quot;&gt;as easy as clicking a link on the admin page&lt;/a&gt;,
and stepping through a wizzard This blog is now running 1.8, so it
has been through several automatic upgrades already, and each one
of them has been completely boring, which when talking about
upgrading an environment, is a very good thing. This magic does not
only apply to upgrading the entire platform, as you can
&lt;a href=&quot;http://codex.wordpress.org/Plugins_Installed_SubPanel#Upgrading_Plugins&quot;&gt;automatically upgrade your plugins as well&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href=&quot;http://ubuntu.com&quot;&gt;Ubuntu&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Many years ago I set up several Ubuntu Gutsy Gibbon&amp;nbsp;(7.10)
Servers in my basement to serve different purposes on my internal
network. These servers have been running without issue for the past
2 years. They would probably run seamlessly for another 2 years,
but unfortunately, I needed to install a new package that didn&amp;#8217;t
have a deb available for the current version of Ubuntu. At first I
was trying to find ways around this, installing the sources
instead, but that didn&amp;#8217;t work well for me, as it needed a newer
version of a dependent library. Even considered trying a different
software package, but it seemed like all of the software packages I
was trying to install, required a newer version of Ubuntu.&lt;/p&gt;

&lt;p&gt;What was I left to do? Either upgrade or rebuild from scratch.
Based on past experiences with other distros, upgrades have been
nothing but headaches for me. Since, I really didn&amp;#8217;t want to invest
the time to rebuild the server from scratch, decided to try my
first Ubuntu upgrade. Again, based on my past experiences, I
assumed I needed to download the media, and then hook up a monitor
to the server, and finally go through a set of upgrade screens.&lt;/p&gt;

&lt;p&gt;To my delight, after a little bit of research I found that you can
do the
&lt;a href=&quot;https://help.ubuntu.com/community/HardyUpgrades#Network%20Upgrade%20from%207.10%20for%20Ubuntu%20Servers%20(Recommended&quot;&gt;entire upgrade from the command line&lt;/a&gt;),
no user interface required, no media download required, no monitor
required! This was too good to be true right? Well, no. I ran the
&lt;em&gt;do-release-upgrade&lt;/em&gt;command, and now my server is running Hardy
Heron (8.04). Couldn&amp;#8217;t be any easier. Ubuntu&amp;#8217;s upgrade
infrastructure is very impressive.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;What is the moral of the story? If you want to build a loyal user
community, treat the members of the community well by providing
them with an easy upgrade path.&lt;/p&gt;&lt;/blockquote&gt;
</content>
  </entry>
  
  <entry>
    <title>Quicksilver is a timesaver</title>
    <link href="http://blue64.net/2009/01/quicksilver-is-a-timesaver/"/>
    <updated>2009-01-26T14:57:25-05:00</updated>
    <id>http://blue64.net/2009/01/quicksilver-is-a-timesaver</id>
    <content type="html">&lt;p&gt;It has been a while since a single program changed my workflow as
much as &lt;a href=&quot;http://docs.blacktree.com/quicksilver/what_is_quicksilver&quot;&gt;Quicksilver&lt;/a&gt; has.&lt;/p&gt;

&lt;p&gt;Over the years, it has been covered a lot on the Mac sites,
but it never really seemed like something that would help me. Guess
it was the stubborn side of me refusing to try something new. So
the question is what changed my mind now? Two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://oreilly.com/catalog/9780596519780/&quot;&gt;The Productive Programer&lt;/a&gt;: In his book Neil Ford describes several work flows involving
  Quicksilver that actually made sense to me, especially the
  Subversion plugin.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.pragprog.com/titles/ahptl/pragmatic-thinking-and-learninghttp://www.pragprog.com/titles/ahptl/pragmatic-thinking-and-learning&quot;&gt;Pragmatic Thinking And Learning&lt;/a&gt;:
Again, the virtues of Quicksilver described in such a way, that
  made a lot of sense to me.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;After reading these two excellent books, it was time for me to give
it a try, and see what it has to offer. It has been about a week
now, and it is hard to imagine using my Mac without it. It has
increased the efficiency of my workflow tremendously. The mouse has
become optional for most tasks. It has sped up common tasks for me
such as checking in/out files from Subversion, opening documents to
edit, quickly composing emails and attaching files, and my favorite
time saver is
&lt;a href=&quot;http://pragmactic-osxer.blogspot.com/2007/07/quickly-locking-your-computer-using.html&quot;&gt;this neat trick&lt;/a&gt;
that allows you to lock your computer with a simple keystroke.
Previously, it required clicking on the &amp;#8220;lock screen&amp;#8221; option in
keychain.&lt;/p&gt;

&lt;p&gt;If you are a Mac developer like me, and you are in to
efficiency, you need to at least give
&lt;a href=&quot;http://docs.blacktree.com/quicksilver/what_is_quicksilver&quot;&gt;Quicksilver&lt;/a&gt;
a try. You won&amp;#8217;t be disappointed.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Going Functional with Scala</title>
    <link href="http://blue64.net/2009/01/going-functional-with-scala/"/>
    <updated>2009-01-15T16:59:32-05:00</updated>
    <id>http://blue64.net/2009/01/going-functional-with-scala</id>
    <content type="html">&lt;p&gt;Lately it seems like functional programming has been the talk of
the town, the new (old) paradigm that is making a comeback in a
major way. For a while, I resisted the urge to follow the paradigm
de jour, being the hard core OO guy that I am, but now it is clear
to me that it is more than just hype. There are many reasons
functional programming makes sense as a paradigm for developing
software today, but the one that I am most interested in is the
fact that it handles concurrency so well, thus providing us the
building blocks to develop extremely scalable applications.&lt;/p&gt;

&lt;p&gt;FP(Functional Programming) has been on my mind for a while. The seed
was placed there in late 2007 by the Java Posse listening to their
&lt;a href=&quot;http://javaposse.com/index.php?post_id=289334&quot;&gt;interview with Martin Odersky&lt;/a&gt;.
They were not talking about FP per se, but more about a specific
JVM language called &lt;a href=&quot;http://www.scala-lang.org/&quot;&gt;Scala&lt;/a&gt; (pronounced
skah-lah not scale-la). Scala is not a pure functional language, it
is actually a fusion between functional and object oriented
programming. It brings together the best of both worlds. My
exposure to Scala did not end there, as I got another huge dose at
Java One 2008. Two of my favorite sessions there were either on
Scala, or Scala was a major discussion point.&lt;/p&gt;

&lt;p&gt;The first session was the Script Bowl. This session was not specifically about Scala, it
was more of a JVM scripting language battle, and in the end JRuby
was crowned king by the audience. In my mind, Scala was the clear
winner, because of the simple, yet elegant and powerfull
concurrency demonstrated. The presenter wrote what looked like a
relatively simple program, and it was capable of indexing tons of
RSS feed dumps in real time on a typical multi-core laptop
computer. I remember thinking to my self, wow, it would take me a
lot more time and effort to write a similar program in Java, and it
probably wouldn&amp;#8217;t have the same level of scalability as the one
demonstrated. The Scala code was able to fully utilize the
multi-core processors it was running on.&lt;/p&gt;

&lt;p&gt;The second session was on Scala presented by the father of Scala
him self Martin Odersky. During that session I was wowed again with
some of Scala&amp;#8217;s features like it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is scalable because it works for very small and very large systems.&lt;/li&gt;
&lt;li&gt;&amp;#8220;is the Java programming language of the future.&amp;#8221;&lt;/li&gt;
&lt;li&gt;is object oriented, functional, and a scripting language.&lt;/li&gt;
&lt;li&gt;leverages &lt;a href=&quot;http://www.scala-lang.org/node/242&quot;&gt;Actor&amp;#8217;s&lt;/a&gt; as the primary concurrency construct.&lt;/li&gt;
&lt;li&gt;fits seamlessly in to a Java environment.&lt;/li&gt;
&lt;li&gt;is a composition language, as it adds the notion of Traits.&lt;/li&gt;
&lt;li&gt;has an updated type system supporting type inference.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;After those two sessions, I headed over to the Java One bookstore
to check out the Beta version of
&lt;a href=&quot;http://www.artima.com/shop/programming_in_scala&quot;&gt;Programming in Scala&lt;/a&gt;.
I was impressed, but couldn&amp;#8217;t help thinking, is Scala for me? Can I
use it at work? Is it viable in the enterprise?&lt;/p&gt;

&lt;p&gt;Fast forward 7 months to today, and let me answer my own questions with one word:
yes!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is it for me? Yes, because Scala is not only something new, it
  is something advanced, because it was written in a no compromise
  academic environment. If nothing else it is a great way to broaden
  my horizons as a programmer.&lt;/li&gt;
&lt;li&gt;Can I use this at work? Yes, Scala code compiles down to Java
  byte code, so theoretically, if I was very evil, I could write all
  my code at work in Scala, run Scalac on it, and add the class files
  to the application, and no one would be any the wiser. Will I do
  that? No way, I am simply trying to illustrate a point that Scala
  is totally compatible with any existing Java environment.&lt;/li&gt;
&lt;li&gt;Is it viable in the enterprise? Yes, with its advanced
  concurrency model, I can only imagine the type of throughput you
  can achieve. (Looking forward to finding out)&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Now you know where my interest in Scala comes from. Hopefully after
reading this post, you will be a bit curious about it as well.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>New Webkit Feature</title>
    <link href="http://blue64.net/2009/01/new-webkit-feature/"/>
    <updated>2009-01-14T11:20:23-05:00</updated>
    <id>http://blue64.net/2009/01/new-webkit-feature</id>
    <content type="html">&lt;p&gt;Just noticed a new and important feature released with the latest
nighty build of Webkit, namely, an option to check for updates
automatically. Mozilla has had this feature in their browsers for a
long time, and it was hard not having it available in
&lt;a href=&quot;http://nightly.webkit.org&quot;&gt;Webkit&lt;/a&gt;. Just got used to downloading a
new version every so often.&lt;/p&gt;

&lt;p&gt;The days of remembering to update
&lt;a href=&quot;http://nightly.webkit.org&quot;&gt;Webkit&lt;/a&gt; are now over, the latest
nightly knows how to keep it self in sync with the current nightly
build.&lt;/p&gt;

&lt;p&gt;Here is the option you are presented with the first time you open
the latest Webkit.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blue64.net/images/uploads/2009/01/auto.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you choose to say no to the above dialog, or if you are like me
and you leave your browser open all the time, and you want to check
for updates manually there is that option too.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blue64.net/images/uploads/2009/01/autoupdate.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once the process is complete, you will see the following dialog
letting you know you are up to date.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blue64.net/images/uploads/2009/01/uptodate.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It is great to see this much needed feature has finally arrived in
&lt;a href=&quot;http://nightly.webkit.org&quot;&gt;Webkit&lt;/a&gt;. Hopefully this will entice
more people to jump on the &lt;a href=&quot;http://nightly.webkit.org&quot;&gt;Webkit&lt;/a&gt;
nightly bandwagon.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Keyboard Shortcuts</title>
    <link href="http://blue64.net/2008/12/keyboard-shortcuts/"/>
    <updated>2008-12-18T10:45:28-05:00</updated>
    <id>http://blue64.net/2008/12/keyboard-shortcuts</id>
    <content type="html">&lt;p&gt;Do you want to learn all of the keyboard shortcuts to your favorite
Java IDE, but just don&amp;#8217;t have time to study a cheat sheet? Are you
envious of your team lead because when you peer program with them,
they are able to get things done without using the mouse?&lt;/p&gt;

&lt;p&gt;You ask your self how did they do it? Do they memorize a cheat sheet while
on the Subway? Do they go home and practice coding every night? Are
they gifted with great memories? Well, they may be all of these
things, but you don&amp;#8217;t have to be!&lt;/p&gt;

&lt;p&gt;All you need to do is install a plugin called Key Promoter that is available for both Intellij and
Eclipse. The way it works is every time you use your mouse to
accomplish a task that can be accomplished via a keyboard shortcut,
it pops up a window and lets you know. In the Intellij version, it
actually keeps track of how many times you used your mouse for each
action. It becomes insulting after a while if you see you used your
mouse 20 times for the same action, knowing it has shown you the
corresponding keyboard shortcut each time.&lt;/p&gt;

&lt;p&gt;For more details you can find the Intellij plugin
&lt;a href=&quot;http://plugins.intellij.net/plugin/?id=1003&quot;&gt;here&lt;/a&gt;, and the
Eclipse plugin &lt;a href=&quot;http://www.mousefeed.com/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Simple Security for HTTP Based RESTful Services (Part 2)</title>
    <link href="http://blue64.net/2008/12/simple-security-for-http-based-restful-services-part-2/"/>
    <updated>2008-12-16T18:51:23-05:00</updated>
    <id>http://blue64.net/2008/12/simple-security-for-http-based-restful-services-part-2</id>
    <content type="html">&lt;p&gt;In &lt;a href=&quot;http://blue64.net/2008/11/simple-security-for-http-based-restful-services-part-1&quot;&gt;part one&lt;/a&gt;
of this series, we examined one potential solution that turned out
to be not so good. In this post lets try to find a solution using
the same design, but a better implementation.&lt;/p&gt;

&lt;p&gt;To recap, we decided to use the simplest solution which is to add a security hash to
each service call. The first approach we tried was adding the
security hash to the XML payload of each service call. The
conclusion was that approach was not very elegant, and actually
quite ugly.&lt;/p&gt;

&lt;p&gt;Let us now discuss a more elegant solution. Keeping the
same parameters in place, namely, we want to pass the hash along
with each service request, where can we place it where it will not
cause the implementation to be ugly? Why don&amp;#8217;t we try by add the
Hash as a field in the Http header?&lt;/p&gt;

&lt;p&gt;The request would look like:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;xml&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;PUT http://blah.com/services/someEntity/226 HTTP/1.1
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Accept: text/xml
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Type: text/xml
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;User-Agent: Jakarta Commons-HttpClient/3.1
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Host: somehost:port
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Hash: 53b383c67a03d23a38b6f52f4a732553
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Length: 380
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldOne&amp;gt;&lt;/span&gt; fieldOne &lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldOne&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldTwo&amp;gt;&lt;/span&gt; fieldTwo &lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldTwo&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;As you can see, the hash has been added to the Http header
(line 6). To verify the hash, we need to do create a Servlet Filter
that verifies the Hash.&lt;/p&gt;

&lt;p&gt;A very simple filter would look like:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VerifyHashFilter&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Filter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;   &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doFilter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServletRequest&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;            &lt;span class=&quot;n&quot;&gt;ServletResponse&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FilterChain&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;            &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ServletException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;     &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpServletRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getHeader&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;HASH&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;12345&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;doFilter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;     &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HttpServletResponse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setStatus&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;   &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Then our service code implementation would change to:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nd&quot;&gt;@Path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/services&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeEntityServiceImpl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nd&quot;&gt;@PUT&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nd&quot;&gt;@Path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/someEntity/{id}&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nd&quot;&gt;@Consumes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/xml&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateSomeEntity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SomeEntity&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;                &lt;span class=&quot;nd&quot;&gt;@PathParam&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;n&quot;&gt;dao&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;saveOrupdate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;To test our service, we can use &lt;em&gt;curl&lt;/em&gt;:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;xml&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;$ curl -i  -H &amp;#39;HASH:12345&amp;#39; http://localhost:9095/rest-test
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;HTTP/1.1 200 OK
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Type: application/xml
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Length: 71
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Server: Jetty(6.1.14)
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; standalone=&amp;quot;yes&amp;quot;?&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldOne&amp;gt;&lt;/span&gt;fieldOne&lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldOne&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldTwo&amp;gt;&lt;/span&gt;fieldTwo&lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldTwo&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;And the negative test:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;xml&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;$ curl -i  -H &amp;#39;HASH:54321&amp;#39; http://localhost:9095/rest-test
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;HTTP/1.1 401 Unauthorized
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Length: 0
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Server: Jetty(6.1.14)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;As you can see, the implementation code is much cleaner and reusable now.
The service code can be called from anywhere within the application
without having to worry about a security hash. It is safe to assume
that if the code makes it as far as the service, it passed through
the security filter thus making the request authorized. Having that
assumption as part of the design allows you to write much more
generic and reusable code.&lt;/p&gt;

&lt;p&gt;Please keep in mind this concept is nothing new, as this solution
is an over simplification of how the Spring Security module works.
The value in this solution is it doesn&amp;#8217;t have dependencies on any
external frameworks. When designing this solution, that was one of
the determining factors.&lt;/p&gt;

&lt;p&gt;Next time you need to implement a quick authorization mechanism for
your REST service, make sure you keep the Http header along with a
Servlet filter in mind.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>Simple Security for HTTP Based RESTful Services (Part 1)</title>
    <link href="http://blue64.net/2008/11/simple-security-for-http-based-restful-services-part-1/"/>
    <updated>2008-11-29T19:58:50-05:00</updated>
    <id>http://blue64.net/2008/11/simple-security-for-http-based-restful-services-part-1</id>
    <content type="html">&lt;p&gt;This is going to be the first in a series of posts discussing
potential ways of securing bi-directional RESTful based HTTP
services. For this series we are going to make the requirements
quite simple, namely, &amp;#8220;secure&amp;#8221; simply means the caller of the
service is authorized to invoke it. Lets assume that this solution
is being deployed along with a simple IP addresses restriction
mechanism. Since IP address&amp;#8217;s can easily be spoofed, this solution
is the next level of defense to ensure the identity of the caller.&lt;/p&gt;

&lt;p&gt;There are many different potential solutions to this problem, but
for this series, we are going to focus on the simplest solution
which is to add a security hash to each service call. This post
assumes you understand how to secure B2B communications with the
use of a security hash. If you need a refresher, please refer to
&lt;a href=&quot;http://blue64.net/2008/10/a-little-salt-with-that-hash/&quot;&gt;this post&lt;/a&gt;
on the subject.&lt;/p&gt;

&lt;p&gt;Since we know the design for securing our
communications, we need to decide on an implementation. The first
potential implementation we are going to examine is adding a
&amp;#8220;security hash&amp;#8221; to the XML payload for each service call.&lt;/p&gt;

&lt;p&gt;The embellished XML payload would look like:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;xml&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;hash&amp;gt;&lt;/span&gt;4723af11bef05fc6207bd22cd163d9db&lt;span class=&quot;nt&quot;&gt;&amp;lt;/hash&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldOne&amp;gt;&lt;/span&gt; &#8230; &lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldOne&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldTwo&amp;gt;&lt;/span&gt; &#8230; &lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldTwo&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;with the &amp;#8220;hash&amp;#8221; field (line 2) representing the security hash.&lt;/p&gt;

&lt;p&gt;The interface of the SomeEntity service, would look like:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeEntityService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateSomeEntity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SomeEntity&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SecurityException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;with an implementation that looks like:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;12&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;13&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;14&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;15&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nd&quot;&gt;@Path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/services&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeEntityServiceImpl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nd&quot;&gt;@PUT&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nd&quot;&gt;@Path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/someEntity/{id}&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nd&quot;&gt;@Consumes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/xml&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateSomeEntity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SomeEntity&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;                    &lt;span class=&quot;nd&quot;&gt;@PathParam&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;                        &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SecurityException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;validateHash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;            &lt;span class=&quot;n&quot;&gt;dao&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;saveOrupdate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SecurityException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Not Authorized&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;With the actual HTTP Client call looking like:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;7&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;8&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;9&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;10&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;xml&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;PUT http://blah.com/services/someEntity/226 HTTP/1.1
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Accept: text/xml
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Type: text/xml
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;User-Agent: Jakarta Commons-HttpClient/3.1
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Host: somehost:port
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;Content-Length: 380
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;hash&amp;gt;&lt;/span&gt;4723af11bef05fc6207bd22cd163d9db&lt;span class=&quot;nt&quot;&gt;&amp;lt;/hash&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldOne&amp;gt;&lt;/span&gt; fieldOne &lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldOne&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldTwo&amp;gt;&lt;/span&gt; fieldTwo &lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldTwo&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/someEntity&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;There you have it, our first potential solution to the
problem. We now need to examine if this solution is correct, and if
it is, is it elegant?&lt;/p&gt;

&lt;p&gt;First things first, is it correct? If you
trace through the code, you can see that before the service makes a
call to the DAO, it checks to verify the validity of the hash field
(line 10). If the hash it is not valid, it will throw an Exception,
which in turn would return a 401 to the user. It would not be
possible for a client to access the DAO without having the proper
hash as part of the XML Payload.&lt;/p&gt;

&lt;p&gt;Even though this approach would
perform as expected, it has two main issues, the first being it
mixes concerns. What we mean by mixes concerns is it mixes business
logic with security logic. This is a standard problem that applies
to layered architectures. The main side effect of this problem is
it makes the code very difficult to reuse. Lets me demonstrate this
with a simple code fragment:&lt;/p&gt;

&lt;figure class=&#8217;code&#8217;&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre class=&quot;line-numbers&quot;&gt;&lt;span class=&#8217;line-number&#8217;&gt;1&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;2&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;3&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;4&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;5&lt;/span&gt;
&lt;span class=&#8217;line-number&#8217;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&#8217;code&#8217;&gt;&lt;pre&gt;&lt;code class=&#8217;java&#8217;&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;c1&quot;&gt;// Code in a Web Controller class somewhere&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;n&quot;&gt;someEntitySvc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;updateSomeEntity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SecurityException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;    &lt;span class=&quot;c1&quot;&gt;// Nothing we can do&#8230; We don&amp;#39;t have the hash???&lt;/span&gt;
&lt;/span&gt;&lt;span class=&#8217;line&#8217;&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;As you can see from this simple example the implementation of the
service is not generic enough to be usable by a Controller in the
Web Tier. Firstly, even if the Web Tier knew about he hash, the
code becomes polluted with Exception handling for an Exception that
is not applicable to this client. Secondly, the Web Tier simply
doesn&amp;#8217;t have any knowledge of the hash, and putting logic here to
generate the hash just to use the service would not make any sense
from a design perspective.&lt;/p&gt;

&lt;p&gt;The second issue with this approach is
the fact that you can only apply it to Http methods that accept a
payload. What about methods that do not accept a Payload, I.e.,
GET? To make this work for the GET method, we would have to pollute
our GET method implementation with a @Consumes(&amp;#8220;text/xml&amp;#8221;) tag,
which functionally would work, but from a API design perspective,
it is quite ugly. As a consumer of a GET method, all you need to
know is the id of the entity you wish to &amp;#8220;get&amp;#8221;. Adding an XML
payload for &amp;#8220;security&amp;#8221; purposes is a bad and cumbersome design.&lt;/p&gt;

&lt;p&gt;With all that being said, it looks like this is not the best
solution to the problem. Stay tuned for part two of this series
where we will discuss a more elegant approach to solving this
problem.&lt;/p&gt;
</content>
  </entry>
  
  <entry>
    <title>A little Salt with that Hash?</title>
    <link href="http://blue64.net/2008/10/a-little-salt-with-that-hash/"/>
    <updated>2008-10-30T19:06:09-04:00</updated>
    <id>http://blue64.net/2008/10/a-little-salt-with-that-hash</id>
    <content type="html">&lt;p&gt;Was just presented with a unique (to me) requirement which is to
implement a single sign on across multiple domains within the same
page via a cookie. Huh? Let me elaborate, in simple terms it means
that there is a base site, lets call it foo.com, and then there is
a partner site, lets call it bar.com. On foo.com&amp;#8217;s main page we
want to be able to iFrame in bar.com with the credentials of the
current user logged in to foo.com transparently sent over and in
turn logged in to bar.com as well.&lt;/p&gt;

&lt;p&gt;In order to achieve this, the
first thing that needs to happen is the two sites need to have a
sub domain in common. The easiest way to accomplish this is to set
up an alias on the parent site (foo.com) to bar.com. The alias
would be something like bar.foo.com which points to bar.com. This
is required because if they do not have a domain in common there is
no way for them to share cookies.&lt;/p&gt;

&lt;p&gt;The next thing that needs to
happen is both sites have to agree on a way of authenticating a
user. In our case we used a cookie that consisted of the user id
followed by a hash of the user id which looked something like
&lt;code&gt;12345,jfk83jf835jfiie43&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your probably wondering how this simple
cookie can facility a single sign on across two different sites?
Let me explain, first lets talk about the how the values of the
cookie are generated. The first value, namely, the user id, is self
explanatory. The second part of the cookie, the hash, has a little
bit more to it. In order to effectively use the hash, the two sites
need to have a &amp;#8220;shared secret&amp;#8221; aka &amp;#8220;The Salt&amp;#8221; that only they know.&lt;/p&gt;

&lt;p&gt;In order to generate the hash, you append the salt to the user id,
and then run it through a one way hashing function. The result is a
unique key that can only be generated by the two parties who know
the salt (or generated by anyone who knows the salt, so make sure
the salt stays very secure). Since it is a one way function, it
would be very difficult for someone to reverse engineer it.&lt;/p&gt;

&lt;p&gt;Now that we have the id and the hash explained, lets talk about how
bar.com can use this to authenticate the user. When a user on
Foo.com first logs in, the shared cookie is created by foo.com, and
when they hit a page in the iFrame which is pointing to bar.com,
bar.com checks the cookie. If it is there, it has the user id and
hash of the remote user.&lt;/p&gt;

&lt;p&gt;Step one for bar.com is to take the user
id, hash it (based on the agreed upon salt), and first verify that
it matches the hash that is supplied as the second part of the
cookie. If it matches, it is safe for bar.com to assume the user id
in the cookie is the user id of the remote user, thus bar.com can
log the user in.&lt;/p&gt;

&lt;p&gt;This method is not the most secure method of doing
a single sign on as a hacker can easily do a man in the middle
attack and thus obtain the cookie, and hash, and then with the user
id and hash in hand, can easily log in to bar.com as the remote
user.&lt;/p&gt;

&lt;p&gt;There are a few precautions you can take if you choose to use
this method like adding values to the salt like, date, time,
request id, that would make the request unique not only to a user,
but to a specific time of day or a specific request. Another
approach you can take to make this more secure is to simply use SSL
across the wire.&lt;/p&gt;

&lt;p&gt;Keep in mind we used this method as a simple way
of verifying user identity, we were not concerned with either site
getting hacked as we had other security mechanisms in place.&lt;/p&gt;

&lt;p&gt;So, please take this approach with a grain of salt.&lt;/p&gt;
</content>
  </entry>
  
</feed>

