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

<channel>
	<title>JoeJag :: Tech</title>
	<atom:link href="http://code.joejag.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.joejag.com</link>
	<description>Joe Wright&#039;s technology blog</description>
	<lastBuildDate>Tue, 07 Feb 2012 22:06:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>How to send a raw HTTP request via Java</title>
		<link>http://code.joejag.com/2012/how-to-send-a-raw-http-request-via-java/</link>
		<comments>http://code.joejag.com/2012/how-to-send-a-raw-http-request-via-java/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 22:06:32 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=702</guid>
		<description><![CDATA[While trying to figure out how a service worked I recently had to put together a Java class that let you easily replay a http conversation which had been sniffed. I got bored of using telnet while making small changes to the payload and curl wants requests converted into an XML format it appears. This [...]]]></description>
			<content:encoded><![CDATA[<p>While trying to figure out how a service worked I recently had to put together a Java class that let you easily replay a http conversation which had been sniffed.</p>
<p>I got bored of using telnet while making small changes to the payload and curl wants requests converted into an XML format it appears.</p>
<p>This simple socket based class lets you send a captured http request to a service.  I&#8217;ve removed the exception handling for brevity.</p>
<pre class="sh_java sh_sourceCode">
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(args[0], 80);

        BufferedWriter out = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        sendMessage(out, new File(args[1]));
        readResponse(in);

        out.close();
        in.close();
    }

    private static void sendMessage(BufferedWriter out, File request) throws IOException {
        System.out.println(" * Request");

        for (String line : getContents(request)) {
            System.out.println(line);
            out.write(line + "\r\n");
        }

        out.write("\r\n");
        out.flush();
    }

    private static void readResponse(BufferedReader in) throws IOException {
        System.out.println("\n * Response");

        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }

    private static List<String> getContents(File file) throws IOException {
        List<String> contents = new ArrayList<String>();

        BufferedReader input = new BufferedReader(new FileReader(file));
        String line;
        while ((line = input.readLine()) != null) {
            contents.add(line);
        }
        input.close();

        return contents;
    }
}
</pre>
<p>For example.  Running this class with the parameters: &#8220;google.co.uk /path/to/stored/file&#8221; with the stored file being just:</p>
<pre>
GET /intl/en/policies/privacy/ HTTP/1.1
</pre>
<p>Will give you this output:</p>
<pre>
 * Sending
GET /intl/en/policies/privacy/ HTTP/1.1

 * Response
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: text/html
Last-Modified: Fri, 27 Jan 2012 17:53:03 GMT
Date: Tue, 07 Feb 2012 21:40:30 GMT
Expires: Tue, 07 Feb 2012 21:40:30 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
Server: sffe
X-XSS-Protection: 1; mode=block
Transfer-Encoding: chunked

// Body content of web page
<!DOCTYPE html>
</pre>
<p>For capturing payloads I recommend using <a href="http://afflib.org/software/tcpflow">tcpflow</a> running on the target host as &#8220;tcpflow port 80&#8243;</p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2012/how-to-send-a-raw-http-request-via-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking JMX with JRuby</title>
		<link>http://code.joejag.com/2011/checking-jmx-with-jruby/</link>
		<comments>http://code.joejag.com/2011/checking-jmx-with-jruby/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 11:02:33 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=669</guid>
		<description><![CDATA[I&#8217;ve recently been playing around with JRuby and found the switch from MRI to be hassle free. One of the advantages of JRuby is it&#8217;s portability. You can use nearly all Ruby code straight away on the JVM without having to do any code changes, you just include the jruby jar in your classpath. Another [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been playing around with <a href="http://jruby.org/">JRuby</a> and found the switch from MRI to be hassle free.  One of the advantages of JRuby is it&#8217;s portability.  You can use nearly all Ruby code straight away on the JVM without having to do any code changes, you just include the jruby jar in your classpath.</p>
<p>Another advantage is Java interoperability from JRuby (just <i>require &#8216;java&#8217;</i>).  I&#8217;ve found this useful when using JMX to check certain conditions are in place while performing releases and for general monitoring.  </p>
<p>The following script uses JRuby&#8217;s Java integration to walk a given ObjectName&#8217;s attributes within JMX.</p>
<pre class="sh_ruby">
require 'java'

# Return a JMX Connection
def connect_to_jmx service_url, credentials
	jmxUrl = javax.management.remote.JMXServiceURL.new(service_url)

	environment = java.util.HashMap.new
	environment.put(javax.management.remote.JMXConnector.CREDENTIALS,
                              credentials.to_java(:string))

	jmxCon = javax.management.remote.JMXConnectorFactory.connect(jmxUrl,
                                                                                              environment)
	return jmxCon.getMBeanServerConnection
end

# Walk the JMX tree and return the attributes.  Optionally limit to a single ObjectName
def walk_tree jmx_connection, limited_to=''
	names = jmx_connection.queryNames(
                                           javax.management.ObjectName.new(limited_to), nil)

	cached = Hash.new([])

	names.each do |name|
	   info = jmx_connection.getMBeanInfo(name)
	   info.getAttributes.each do | mbi|
		   attr = jmx_connection.getAttribute(name, mbi.getName)
		   cached.store(name.getCanonicalName,
                          cached[name.getCanonicalName] << { mbi.getName  => attr })
	   end
	end

	return cached
end

service_url = 'host:port'
credentials = ['username', 'password']
jmx_connection = connect_to_jmx(service_url, credentials)

results = walk_tree(jmx_connection, 'java.lang:type=OperatingSystem')

results.each do | key, attributes |
  puts ' * ' + key
  attributes.each do | attr |
	puts " ** #{attr.first[0]} :: #{attr.first[1]}"
  end
  puts
end
</pre>
<p>When invoked against a running JMX instance this will return something similar to:</p>
<pre class="sh_sh sh_sourceCode">
> jruby jmx_walker.rb
 * java.lang:type=OperatingSystem
 ** FreePhysicalMemorySize :: 51955511296
 ** TotalPhysicalMemorySize :: 151873998848
 ** Name :: Linux
 ** Arch :: amd64
 ** Version :: 2.6.18-194.11.4.el5
 ** AvailableProcessors :: 24
 ** SystemLoadAverage :: 0.49
</pre>
<p>Hopefully this is of use to you, or inspires you to investigate JRuby a bit further.  I&#8217;d highly recommend it for Java shops who want to take advantage of Ruby&#8217;s scripting ability.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/checking-jmx-with-jruby/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A DSL for collections in Java</title>
		<link>http://code.joejag.com/2011/a-dsl-for-collections-in-java/</link>
		<comments>http://code.joejag.com/2011/a-dsl-for-collections-in-java/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 15:50:24 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=541</guid>
		<description><![CDATA[When writing Java code I often find it laborious to create collections using the java.util.* collection classes. To avoid this, I&#8217;ve been using a mini-DSL to reduce my collections code. import static com.joejag.common.collections.Dsl.*; // A list List list = list("abc", "def"); // A set Set set = set("Sleepy", "Sneezy", "Dozy"); // A Map Map map [...]]]></description>
			<content:encoded><![CDATA[<p>When writing Java code I often find it laborious to create collections using the <i>java.util.*</i> collection classes.  To avoid this, I&#8217;ve been using a mini-DSL to reduce my collections code.</p>
<pre class="sh_java sh_sourceCode">
import static com.joejag.common.collections.Dsl.*;

// A list
List<String> list = list("abc", "def");

// A set
Set<String> set = set("Sleepy", "Sneezy", "Dozy");

// A Map
Map<String, Integer> map = map(entry("Joe", 28), entry("Gerry", 39));
</pre>
<p>Here is the underlying code.</p>
<pre class="sh_java sh_sourceCode">
package com.joejag.common.collections;

import java.util.*;

public class Dsl {
    public static &lt;T&gt; List&lt;T&gt; list(T... args) {
        return Arrays.asList(args);
    }

    public static &lt;T&gt; Set&lt;T&gt; set(T... args) {
        Set&lt;T&gt; result = new HashSet&lt;T&gt;(args.length);
        result.addAll(Arrays.asList(args));
        return result;
    }

    public static &lt;K, V&gt; Map&lt;K, V&gt; map(Entry&lt;? extends K, ? extends V&gt;... entries) {
        Map&lt;K, V&gt; result = new HashMap&lt;K, V&gt;(entries.length);

        for (Entry&lt;? extends K, ? extends V&gt; entry : entries)
            if (entry.value != null)
                result.put(entry.key, entry.value);

        return result;
    }

    public static &lt;K, V&gt; Entry&lt;K, V&gt; entry(K key, V value) {
        return new Entry&lt;K, V&gt;(key, value);
    }

    public static class Entry&lt;K, V&gt; {
        K key;
        V value;

        public Entry(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String args[]) {
        List&lt;String&gt; list = list(&quot;abc&quot;, &quot;def&quot;);
        System.out.println(list);

        Set&lt;String&gt; set = set(&quot;Sleepy&quot;, &quot;Sneezy&quot;, &quot;Dozy&quot;);
        System.out.println(set);

        Map&lt;String, Integer&gt; map = map(entry(&quot;Joe&quot;, 28), entry(&quot;Gerry&quot;, 39));
        System.out.println(map);
    }
}
</pre>
<p>I&#8217;ve noticed that the Google Collections project has morphed into <a href="http://code.google.com/p/guava-libraries/">Guava</a> which has great reusable code for collections and a lot of other common Java tasks.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/a-dsl-for-collections-in-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Start a web server from your pwd using Ruby and Thin</title>
		<link>http://code.joejag.com/2011/start-a-web-server-from-your-pwd-using-ruby-and-thin/</link>
		<comments>http://code.joejag.com/2011/start-a-web-server-from-your-pwd-using-ruby-and-thin/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 13:06:07 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=521</guid>
		<description><![CDATA[Occasionally I&#8217;m interested in grabbing files from a directory on a remote host. This could be for another process to consume or to look at on my local work station. The standard way of doing this is using scp, ftp or a file share. I prefer to start a short-lived web server that shares the [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally I&#8217;m interested in grabbing files from a directory on a remote host.  This could be for another process to consume or to look at on my local work station.</p>
<p>The standard way of doing this is using scp, ftp or a file share.  I prefer to start a short-lived web server that shares the file system.  </p>
<p>To make this simple I use a ruby script to allow me to start a webserver from the directory I&#8217;m currently in.</p>
<pre class="sh_ruby">
['rubygems', 'thin', 'rack', 'socket'].each {|file| require file }
Thin::Server.start(IPSocket.getaddress(Socket.gethostname), 7777) do
  use Rack::CommonLogger
  run Rack::Directory.new(Dir.pwd)
end
</pre>
<p>As an alias for ~/.bashrc it looks like this:</p>
<pre class="sh_sh">
alias rshare="ruby -rubygems -e \"['thin', 'rack', 'socket'].each {|file| require file };"\
" Thin::Server.start(IPSocket.getaddress(Socket.gethostname), 7777) {"\
" use Rack::CommonLogger; run Rack::Directory.new(Dir.pwd) }\""
</pre>
<p>This allows you to go to port 7777 on the host and retrieve the files you&#8217;re interested in.</p>
<pre class="sh_sh">
@joejag /tmp $ rshare
>> Thin web server (v1.2.7 codename No Hup)
>> Maximum connections set to 1024
>> Listening on 10.0.0.2:7777, CTRL+C to stop
10.0.0.8 - - [01/Mar/2011 07:45:15] "GET / HTTP/1.1" 200 2153 0.0038
</pre>
<p>You will need to have the &#8216;thin&#8217; and &#8216;rack&#8217; gems installed to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/start-a-web-server-from-your-pwd-using-ruby-and-thin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a simple Java RESTful service using Jersey and Maven</title>
		<link>http://code.joejag.com/2011/creating-a-simple-java-restful-service-using-jersey-and-maven/</link>
		<comments>http://code.joejag.com/2011/creating-a-simple-java-restful-service-using-jersey-and-maven/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 15:26:38 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=433</guid>
		<description><![CDATA[There has been a lot of interest in Resource Orientated Architecture (ROA) online which has resulted in an explosion of excellent libraries that make it simple to use this architectural style. The example in this article uses Java, Maven and a JAX-RS library called Jersey to create a very simple orders system. Our order consists [...]]]></description>
			<content:encoded><![CDATA[<p>There has been a lot of interest in <a href="http://en.wikipedia.org/wiki/Resource-oriented_architecture">Resource Orientated Architecture (ROA)</a> online which has resulted in an explosion of excellent libraries that make it simple to use this architectural style.</p>
<p>The example in this article uses Java, Maven and a JAX-RS library called <a href="http://jersey.java.net/">Jersey</a> to create a very simple orders system. </p>
<p>Our order consists of a reference number and a customer name.  The system has three entry points:</p>
<ul>
<li>HTTP PUT to create an order
<li>HTTP GET to see the details of an order
<li>HTTP GET to see all the existing orders
</ul>
<p>Download the full source of this article <a href='http://code.joejag.com/wp-content/uploads/2011/02/orders_restful_service.zip'>here</a>.</p>
<p>To make it simple to get it up and running I&#8217;ve added configuration for Jetty to run on port 9090.  You can compile the source code and run a web server to use it by issuing this single Maven command:</p>
<pre class="sh_sh sh_sourceCode">
mvn jetty:run
</pre>
<p>I recommend that you use <a href="http://curl.haxx.se/download.html">curl</a> to test your RESTful web services as it is easy to change the HTTP verb that you are using.  You can manipulate the orders system using these curl commands.  </p>
<pre class="sh_sh sh_sourceCode">
# Add a new order for Bob with ID 1
curl -X PUT http://localhost:9090/orders-server/orders/1?customer_name=bob 

# Check the status of the order
curl -X GET http://localhost:9090/orders-server/orders/1 

# See all the orders in the system
curl -X GET http://localhost:9090/orders-server/orders/list
</pre>
<p><i>Note: HTTP GET is the default verb used by curl, I&#8217;ve explicitly listed GET in the examples to make it clear which verb is in use.</i></p>
<h3>The code</h3>
<p>To use Jersey you need to add a servlet to your web.xml and create a resource implementation class.</p>
<h4>web.xml</h4>
<p>The WEB-INF/web.xml fragment sets up the Jersey Servlet with a parameter listing the package to search for RESTful classes.</p>
<pre class="sh_sh sh_sourceCode">
&lt;servlet&gt;
   &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
   &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
   &lt;init-param&gt;
      &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
      &lt;!-- Important bit --&gt;
      &lt;param-value&gt;com.joejag.code.orders.restservices&lt;/param-value&gt;
   &lt;/init-param&gt;
   &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
</pre>
<p>This reads any classes in <i>the com.joejag.code.orders.restservices</i> and looks for annotations declaring resources.  </p>
<h4>The Java class</h4>
<p>On the implementation class you can see the @Path annotation on the class signature indicating the resource we are handling.  This is used by Jersey to configure the URL used to interact with the resource.</p>
<p>Each method has a sub-path declared, an HTTP verb to respond from and the response type to produce.  The method parameters are bound by using either part of the path (with @PathParam) or a separate query parameter (with @QueryParam).</p>
<pre class="sh_java sh_sourceCode">
package com.joejag.code.orders.restservices;

import java.util.Map;
import java.util.TreeMap;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

@Path(&quot;orders&quot;)
public class OrdersService
{
   // Stores state simply in a static collection class.
   private static Map&lt;String, String&gt; orders = new TreeMap&lt;String, String&gt;();

   @Path(&quot;/{order}&quot;)
   @PUT
   @Produces(&quot;text/html&quot;)
   public String create(@PathParam(&quot;order&quot;) String order,
                                    @QueryParam(&quot;customer_name&quot;) String customerName)
   {
      orders.put(order, customerName);
      return &quot;Added order #&quot; + order;
   }

   @Path(&quot;/{order}&quot;)
   @GET
   @Produces(&quot;text/html&quot;)
   public String find(@PathParam(&quot;order&quot;) String order)
   {
      if (orders.containsKey(order))
         return &quot;&lt;h2&gt;Details on Order #&quot; + order +
                    &quot;&lt;/h2&gt;&lt;p&gt;Customer name: &quot; + orders.get(order);

      throw new WebApplicationException(Response.Status.NOT_FOUND);
   }

   @Path(&quot;/list&quot;)
   @GET
   @Produces(&quot;text/html&quot;)
   public String list()
   {
      String header = &quot;&lt;h2&gt;All Orders&lt;/h2&gt;\n&quot;;

      header += &quot;&lt;ul&gt;&quot;;
      for (Map.Entry&lt;String, String&gt; order : orders.entrySet())
         header += &quot;\n&lt;li&gt;#&quot; + order.getKey() + &quot; for &quot; + order.getValue() + &quot;&lt;/li&gt;&quot;;

      header += &quot;\n&lt;/ul&gt;&quot;;

      return header;
   }
}
</pre>
<p>Download the full source of this article <a href='http://code.joejag.com/wp-content/uploads/2011/02/orders_restful_service.zip'>here</a>.</p>
<h3>Where to learn more</h3>
<p>There are a number of great articles on how to develop JAX-RS REST applications.  I recommend you start with the <a href="http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e8">Jersey guide</a> for Java applications:</p>
<p>If you are using Ruby then take a look at the wonderful <a href="http://www.sinatrarb.com/">Sinatra</a> project.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/creating-a-simple-java-restful-service-using-jersey-and-maven/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Renaming MP3 tags with Ruby and the mp3info gem</title>
		<link>http://code.joejag.com/2011/renaming-mp3-tags-with-ruby-and-the-mp3info-gem/</link>
		<comments>http://code.joejag.com/2011/renaming-mp3-tags-with-ruby-and-the-mp3info-gem/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 22:14:06 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=419</guid>
		<description><![CDATA[Occasionally you get MP3s which have unconventional tags. I usually get this problem with compilation albums and it&#8217;s a bit dull to rename them in iTunes. There&#8217;s a ruby gem which let&#8217;s you easily edit id3 tags called &#8216;mp3info&#8216;. Here is a script to get you started: require "mp3info" dir = '/opt/music/album_name/' Dir.entries(dir).each do &#124;file&#124; [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally you get MP3s which have unconventional tags.  I usually get this problem with compilation albums and it&#8217;s a bit dull to rename them in iTunes.</p>
<p>There&#8217;s a ruby gem which let&#8217;s you easily edit id3 tags called &#8216;<a href="http://ruby-mp3info.rubyforge.org/">mp3info</a>&#8216;.</p>
<p>Here is a script to get you started:</p>
<pre class="sh_ruby">
require "mp3info"

dir = '/opt/music/album_name/'
Dir.entries(dir).each do |file|
   next if file !~ /.mp3$/ # skip files not ending with .mp3
   Mp3Info.open(dir + file) do |mp3|
      puts mp3.tag.title
      puts mp3.tag.artist
      puts mp3.tag.album
      puts mp3.tag.tracknum

      # You would perform your text transform here, here's a simple change instead
      mp3.tag.title = mp3.tag.title + ' with my change'
      mp3.tag.artist = mp3.tag.artist + ' with my change'
   end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/renaming-mp3-tags-with-ruby-and-the-mp3info-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing CouchDB 1.0.1 on RHEL4 as the local user</title>
		<link>http://code.joejag.com/2011/installing-couchdb-1-0-1-on-rhel4-as-the-local-user/</link>
		<comments>http://code.joejag.com/2011/installing-couchdb-1-0-1-on-rhel4-as-the-local-user/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 18:07:10 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=392</guid>
		<description><![CDATA[On some of the servers I use I don&#8217;t have root access. This means occasionally it can be a hassle to install packages when the dependencies aren&#8217;t available. Recently I had to install CouchDB and it&#8217;s dependencies from source. The tricky bit was having to edit the CouchDB configure script to specify the Curl libraries. [...]]]></description>
			<content:encoded><![CDATA[<p>On some of the servers I use I don&#8217;t have root access.  This means occasionally it can be a hassle to install packages when the dependencies aren&#8217;t available.</p>
<p>Recently I had to install CouchDB and it&#8217;s dependencies from source.  The tricky bit was having to edit the CouchDB configure script to specify the Curl libraries.</p>
<p>In this example I&#8217;ve assumed the user home directory is &#8216;/home/your_user&#8217;, with the Couch and it&#8217;s dependencies ending up in &#8216;/home/your_user/db/couch/&#8217;.</p>
<pre class="sh_sh sh_sourceCode">
# Erlang 5.6.5
wget http://www.erlang.org/download/otp_src_R12B-5.tar.gz
tar -zxvf otp_src_R12B-5.tar.gz
cd otp_src_R12B-5
./configure --prefix=/home/your_user/db/couch/erlang
make &#038;&#038; make install

# SpiderMonkey 1.7.0
wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
tar -zxvf js-1.7.0.tar.gz
cd js/src/
make -f Makefile.ref
make BUILD_OPT=1 JS_DIST=/home/your_user/db/couch/spidermonkey/ \
  -f Makefile.ref export

# ICU 4.6
wget http://download.icu-project.org/files/icu4c/4.6/icu4c-4_6-src.tgz
tar -zxvf icu4c-4_6-src.tgz
cd icu
./configure --prefix=/home/your_user/db/couch/icu
make &#038;&#038; make install

# Curl 7.21.2
wget http://curl.haxx.se/download/curl-7.21.2.tar.gz
tar -zxvf curl-7.21.2.tar.gz
cd curl-7.21.2
./configure --prefix=/home/your_user/db/couch/curl
make &#038;&#038; make install

# Add the programs to your $PATH
export PATH=$PATH:/home/your_user/db/couch/erlang/bin
export PATH=$PATH:/home/your_user/db/couch/spidermonkey/bin
export PATH=$PATH:/home/your_user/db/couch/icu/bin
export PATH=$PATH:/home/your_user/db/couch/curl/bin
export LD_LIBRARY_PATH=/home/your_user/db/couch/icu/lib

# CouchDB 1.0.1
wget http://mirrors.ukfast.co.uk/sites/ftp.apache.org//couchdb/1.0.1/apache-couchdb-1.0.1.tar.gz
tar -zxvf apache-couchdb-1.0.1.tar.gz
cd apache-couchdb-1.0.1
## edit configure.  Search for CURL_LDFLAGS=-lcurl.
## Replace with: CURL_LDFLAGS="-L/home/your_user/db/couch/curl/lib -lcurl"
./configure --prefix=/home/your_user/db/couch/couchdb \
  --with-js-lib=/home/your_user/db/couch/spidermonkey/lib \
  --with-js-include=/home/your_user/db/couch/spidermonkey/include \
  --with-erlang=/home/your_user/db/couch/erlang/lib/erlang/usr/include
make &#038;&#038; make install

# Start up CouchDB
/home/your_user/db/couch/couchdb/bin/couchdb
# Time to Relax.
# From this command you should see: {"couchdb":"Welcome","version":"1.0.1"}
curl http://127.0.0.1:5984/
</pre>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/installing-couchdb-1-0-1-on-rhel4-as-the-local-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding colour to your unix prompt (PS1) and some handy ls aliases</title>
		<link>http://code.joejag.com/2011/adding-colour-to-your-unix-prompt-and-some-handy-aliases/</link>
		<comments>http://code.joejag.com/2011/adding-colour-to-your-unix-prompt-and-some-handy-aliases/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 18:51:06 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=406</guid>
		<description><![CDATA[This is my standard .bashrc that I use on all my boxes with a bash prompt. It gives you colourful listings when you use &#8216;ls&#8217; and some shorthand versions of common &#8216;ls&#8217; parameters. The PS1 is set to just show the username, directory and the command in a brighter colour than the output from commands: [...]]]></description>
			<content:encoded><![CDATA[<p>This is my standard .bashrc that I use on all my boxes with a bash prompt.  It gives you colourful listings when you use &#8216;ls&#8217; and some shorthand versions of common &#8216;ls&#8217; parameters.</p>
<p>The PS1 is set to just show the username, directory and the command in a brighter colour than the output from commands:</p>
<pre class="sh_sh sh_sourceCode">
# ls aliases that use colour
alias la='ls -a --color=auto'
alias lla='ls -la --color=auto'
alias lt='ls -ltr --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias lsh='ls -lSh --color=auto'

# Unix prompt: /user /directory $
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\]'

# start a web server in this directory
alias rshare="ruby -rubygems -e \"['thin', 'rack', 'socket'].each {|file| require file };"\
" Thin::Server.start(IPSocket.getaddress(Socket.gethostname), 7777) {"\
" use Rack::CommonLogger; run Rack::Directory.new(Dir.pwd) }\""
</pre>
<p>Example in /bin:<br />
<img src="http://code.joejag.com/wp-content/uploads/2011/01/ps1.png" alt="ps1" title="ps1" width="364" height="78" class="alignnone size-full wp-image-410" /></p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2011/adding-colour-to-your-unix-prompt-and-some-handy-aliases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New language features in Java 7</title>
		<link>http://code.joejag.com/2009/new-language-features-in-java-7/</link>
		<comments>http://code.joejag.com/2009/new-language-features-in-java-7/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 13:43:48 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=299</guid>
		<description><![CDATA[I&#8217;m just back from the Devoxx conference in Antwerp. An update was given on the new language changes that will be in Java 7. The JDK currently has a release date of September 2010. Here are 7 of the new features that have been completed: Language support for collections ( Postponed to Java 8 ) [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m just back from the <a href="http://www.devoxx.com/">Devoxx</a> conference in Antwerp.  An update was given on the <a href="http://openjdk.java.net/projects/coin/">new language changes</a> that will be in Java 7.  The <a href="http://java.sun.com/features/jdk/7/">JDK</a> currently has a release date of September 2010.</p>
<p>Here are 7 of the new features that have been completed:</p>
<ul>
<li><del>Language support for collections</del> ( Postponed to Java 8 )</li>
<li>Automatic Resource Management</li>
<li>Improved Type Inference for Generic Instance Creation (diamond)</li>
<li>Underscores in numeric literals</li>
<li>Strings in switch</li>
<li>Binary literals</li>
<li>Simplified Varargs Method Invocation</li>
</ul>
<p>There is a lot more to Java 7 then just these language changes.  I&#8217;ll be exploring the rest of the release in future posts.  One of the big debates is currently around <a href="http://www.jroller.com/scolebourne/entry/closures_in_jdk_7">Closures</a>, which are a separate JSR.</p>
<h2>Language support for collections</h2>
<p><i>This has been postponed to Java 8!  You could use <a href="http://code.joejag.com/2011/a-dsl-for-collections-in-java">my simple alternative</a> until then.</i></p>
<p>Java will be getting first class language support for creating collections.  The style change means that collections can be created like they are in Ruby, Perl etc.</p>
<p>Instead of:</p>
<pre class="sh_java sh_sourceCode">
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
list.add("item");
String item = list.get(0);

Set&lt;String&gt; set = new HashSet&lt;String&gt;();
set.add("item");

Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();
map.put("key", 1);
int value = map.get("key");
</pre>
<p>You will be able to use:</p>
<pre class="sh_java sh_sourceCode">
List&lt;String&gt; list = ["item"];
String item = list[0];

Set&lt;String&gt; set = {"item"};

Map&lt;String, Integer&gt; map = {"key" : 1};
int value = map["key"];
</pre>
<p>These collections are immutable.</p>
<h2>Automatic Resource Management</h2>
<p>Some resources in Java need to be closed manually like InputStream, Writers, Sockets, Sql classes.  This language feature allows the try statement itself to declare one of more resources.  These resources are scoped to the try block and are closed automatically.</p>
<p>This:</p>
<pre class="sh_java sh_sourceCode">
BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {
   br.close();
}
</pre>
<p>becomes:</p>
<pre class="sh_java sh_sourceCode">
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
}
</pre>
<p>You can declare more than one resource to close:</p>
<pre class="sh_java sh_sourceCode">
try (
   InputStream in = new FileInputStream(src);
   OutputStream out = new FileOutputStream(dest))
{
 // code
}
</pre>
<p>To support this behaviour all closable classes will be retro-fitted to implement a <i>Closable</i> interface.</p>
<h2>Improved Type Inference for Generic Instance Creation (diamond)</h2>
<p>This is a particular annoyance which is best served with an example:</p>
<pre class="sh_java sh_sourceCode">
Map&lt;String, List&lt;String&gt;&gt; anagrams = new HashMap&lt;String, List&lt;String&gt;&gt;();
</pre>
<p>becomes:</p>
<pre class="sh_java sh_sourceCode">
Map&lt;String, List&lt;String&gt;&gt; anagrams = new HashMap&lt;&gt;();
</pre>
<p>This is called the <i>diamond operator</i>: &lt;&gt; which infers the type from the reference declaration.</p>
<h2>Underscores in numeric literals</h2>
<p>Long numbers are hard to read.  You can now split them up using an underscore in ints and longs:</p>
<pre class="sh_java sh_sourceCode">
int one_million = 1_000_000;
</pre>
<h2>Strings in switch</h2>
<p>Currently you can only use numbers or enums in switch statements.  String has been added as a candidate:</p>
<pre class="sh_java sh_sourceCode">
String s = ...
switch(s) {
 case "quux":
    processQuux(s);
    // fall-through

  case "foo":
  case "bar":
    processFooOrBar(s);
    break;

  case "baz":
     processBaz(s);
    // fall-through

  default:
    processDefault(s);
    break;
}
</pre>
<h2>Binary literals</h2>
<p>Java code, due to its C heritage, has traditionally forced programmers to represent numbers in only decimal, octal, or hexadecimal. </p>
<p>As quite a few domains are bit orientated, this restriction can introduce errors.  You can now create binary numbers using an <i>0b</i> prefix.</p>
<pre class="sh_java sh_sourceCode">
int binary = 0b1001_1001;
</pre>
<h2>Simplified Varargs Method Invocation</h2>
<p>When a programmer tries to invoke a *varargs* (variable arity) method with a non-reifiable varargs type, the compiler currently generates an &#8220;unsafe operation&#8221; warning. JDK 7 moves the warning from the call site to the method declaration.  This will enable API designers to use varargs due to the reduction of warnings reported.</p>
<p>This one is slightly more involved so you are better off looking at the <a href="http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000217.html">proposal</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2009/new-language-features-in-java-7/feed/</wfw:commentRss>
		<slash:comments>103</slash:comments>
		</item>
		<item>
		<title>Coding Dojo: Example Katas</title>
		<link>http://code.joejag.com/2009/coding-dojo-example-katas/</link>
		<comments>http://code.joejag.com/2009/coding-dojo-example-katas/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 13:04:50 +0000</pubDate>
		<dc:creator>Joe Wright</dc:creator>
				<category><![CDATA[process]]></category>

		<guid isPermaLink="false">http://code.joejag.com/?p=233</guid>
		<description><![CDATA[In my previous post I discussed the basics of what a Coding Dojo is and why you&#8217;d want to have one. I host a Coding Dojo at my work every month, so I&#8217;ve decided to periodically update an index page of example Katas. They are usually based off existing Katas which are available on the [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post I discussed the basics of what a <a href="http://code.joejag.com/2009/the-coding-dojo/">Coding Dojo</a> is and why you&#8217;d want to have one.  </p>
<p>I host a Coding Dojo at my work every month, so I&#8217;ve decided to periodically update an index page of <a href="http://code.joejag.com/coding-dojo-example-katas/">example Katas</a>.  They are usually based off existing Katas which are available on the main <a href="http://codingdojo.org/">Coding Dojo website</a>.  I&#8217;d also recommend the prolific <a href="http://translate.google.com/translate?hl=en&#038;sl=pt&#038;u=http://www.dojosp.org/&#038;ei=9k-mSrS6MoKanwO59rH0Dw&#038;sa=X&#038;oi=translate&#038;resnum=4&#038;ct=result&#038;prev=/search%3Fq%3Ddojosp%26hl%3Den%26client%3Dfirefox-a%26rls%3Dorg.mozilla:en-GB:official%26hs%3DTN0">Sao Paulo Coding Dojo</a>.</p>
<p>You should be able to reuse the same problems at your own Dojo.  If you have any other recommendations then please email/twitter me, or comment on this post.</p>
<h4>Current Problems</h4>
<ul>
<li><a href="http://code.joejag.com/coding-dojo-bowling-scores/">Bowling Scores</a></li>
<li><a href="http://code.joejag.com/coding-dojo-vending-machine/">Vending Machine</a></li>
<li><a href="http://code.joejag.com/coding-dojo-converting-between-different-numeral-systems/">Converting numeral systems</a></li>
<li><a href="http://code.joejag.com/coding-dojo-test-first-spreadsheet/">Test First Spreadsheet</a></li>
</ul>
<h2>Minesweeper Kata</h2>
<p>On a slightly related note, I just completed the common Minesweeper Kata.  </p>
<p>Matt Wynne, who has an excellent <a href="http://blog.mattwynne.net/">blog</a>, recently created a test harness using the Ruby BDD framework called <a href="http://cukes.info/">Cucumber</a>.  It acts as a great way to introduce you to BDD and Github if you haven&#8217;t used them before.</p>
<ul>
<li><a href="http://github.com/mattwynne/kata-minesweeper/tree/master">Matt&#8217;s Minesweeper Kata</a></li>
<li><a href="http://github.com/joejag/kata-minesweeper/tree/412433d53970cc6b052cad5235970ea0d6c3154d/lib">My Github fork with solution</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://code.joejag.com/2009/coding-dojo-example-katas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

