tag:blogger.com,1999:blog-27635473302206617912023-06-20T22:00:41.473-07:00geekirikisubject orientedarbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-2763547330220661791.post-75152788025074934672012-06-14T04:32:00.000-07:002012-06-14T05:11:50.756-07:00test-driven infrastructure changes<p>I'm currently working a lot on infrastructure-related tasks. As a software developer I really appreciate TDD with all the certainty I get for code changes. Many changes of the infrastructure stack can be tested manually using curl. For instance to check if a request to a certain URL succeeds, this is all you need:<br />
</p><pre class="brush: bash; gutter: false">~ curl -I "http://10.0.0.1/"
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 14 Jun 2012 10:19:33 GMT
...
</pre><p>You can easily add cookies, do virtual host routing and pretend SSL termination:<br />
</p><pre class="brush: bash; gutter: false">~ curl -I -H"Host: awwsnap.io" -H"X-Forwarded-Proto: https" \
-b"cookie=value" "http://10.0.0.1/"
</pre><p>Ok, that's old news. But, how about automating those tests and spec'ing your stack?<br />
</p><p>To accomplish this, I use <a href="https://github.com/bmizerany/roundup">roundup</a> made by <a href="https://twitter.com/#!/bmizerany">Blake Mizerany</a>. It's a great testing framework that perfectly fits into UNIX environments. And its tests are written in plain bash. That's exactly what I need. Check it out, if you don't know it yet! <br />
</p><p>With roundup we can quickly put the example from before into a spec: <br />
</p><pre class="brush: bash; gutter: false">#!/usr/local/bin/roundup
### matchers
function returns {
cat | head -n1 | grep $1
}
### spec
it_should_redirect_requests_to_root() {
curl -I -H"Host: awwsnap.io" -H"X-Forwarded-Proto: https" \
-b"cookie=value" "http://10.0.0.1/" | returns 200
}
</pre><p>Roundup detects the success or failure of a test by its error status. In the previous example, the matcher function <code>returns</code> uses grep to find <code>302</code> in the first line of the response header. If grep finds that <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">http status code</a> it returns 0 otherwise 1.<br />
</p><p>This enables me to describe the wanted behaviour of a system up-front and "fix it" in a more focused way.<br />
One additional benefit during a rollout of a new configuration is that you can now run the test repeatedly against a host to validate the changes.<br />
</p><p>To stimulate your imagination a bit more, here are some additional examples:<br />
</p><pre class="brush: bash; gutter: false">#!/usr/local/bin/roundup
### matchers
function returns {
cat | head -n1 | grep $1
}
# check for rails-specific header
function is_rails {
cat | grep "^X-Runtime: "
}
function redirects_to {
res=$(cat)
echo $res | returns 302 && echo $res | grep "Location: $1"
}
### helpers
# using tee makes debugging easier
function get {
curl -I -H"Host: awwsnap.io" -H "X-Forwarded-Proto: https" \
-b"cookie=value" | tee /dev/stderr
}
### spec
it_should_serve_root() {
get "http://10.0.0.1/" | returns 200
}
it_should_pass_to_rails_backend_for_admin() {
get "http://10.0.0.1/admin" | is_rails
}
it_should_redirect_to_cdn_for_assets() {
get "http://10.0.0.1/assets" | redirects_to "cdn.awwsnap.io"
}
</pre><br />
<br />
<br />
<br />arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com1tag:blogger.com,1999:blog-2763547330220661791.post-44749308688160886942011-11-16T06:58:00.000-08:002011-11-16T11:26:13.252-08:00Ruby Heredocs in Array Constants<p>
While writing a <a src="https://www.relishapp.com/rspec">spec</a> that uses different chunks of csv and txt data I was wondering about the best way to define multi-line strings in array constants.</p>
<p>
Normally, I would use <a href="http://en.wikipedia.org/wiki/Here_document#Ruby">Heredoc</a> to define a single multi-line string like this:
</p>
<pre class="brush: ruby; gutter: false">
CSV_CHUNK = <<-CSV
10, "a", "b"
20, "c", "d"
30, "e", "e"
CSV
</pre>
<p>
Perfect. The unattractiveness starts when adding more chunk definitions. It usually ends up with having <i>CSV_CHUNK_0, CSV_CHUNK_1, CSV_CHUNK_3</i> and so on in place. Thats a bit unfortunately. For example this hinders to use normal array iteration like <i>each</i> and friends.
</p>
<p>
So, my question was if there is a way to simply add chunk after chunk to an array. Sure its possible:
</p>
<pre class="brush: ruby; gutter: false">
chunks = []
chunks <<<<-CSV
10, "a", "b"
20, "c", "d"
30, "e", "f"
CSV
chunks <<<<-CSV
40, "a", "b"
50, "c", "d"
60, "e", "f"
CSV
</pre>
<p>
This is valid Ruby syntax. Actually its just the << method of Array plus the Heredoc syntax. ( Yes, you can add a space inbetween :) )
</p>
<p>
But, since we are altering a variable we can't use a constant here. To use a constant, we have to do the Heredoc definition inline in the Array declaration:
</p>
<pre class="brush: ruby; gutter: false">
CHUNKS = [
<<-CSV ,
10, "a", "b"
20, "c", "d"
30, "e", "f"
CSV
<<-CSV ]
40, "a", "b"
50, "c", "d"
60, "e", "f"
CSV
</pre>
<p>
Although, this looks pretty scary, its again valid Ruby syntax. As many other languages Ruby allows a comma in front of the closing square bracket. We can use this to pretty up this construct and to make it more readable:
</p>
<pre class="brush: ruby; gutter: false">
CHUNKS = [
<<-CSV ,
10, "a", "b"
20, "c", "d"
30, "e", "f"
CSV
<<-CSV ,
40, "a", "b"
50, "c", "d"
60, "e", "f"
CSV
]
</pre>arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com1tag:blogger.com,1999:blog-2763547330220661791.post-18821264205169219312011-10-06T07:42:00.000-07:002015-11-09T02:39:28.216-08:00Flume and Hadoop on OS X<p>For a kick-ass <a href="http://en.wikipedia.org/wiki/Dev/null">webscale</a> big-data setup on your local mac, you'll want to have <a href="http://hadoop.apache.org/">Hadoop</a> and <a href="https://cwiki.apache.org/FLUME/">Flume</a> place.<br />
No seriously - this setup is especially useful, if you want to route your syslog output from your nodes to <a href="http://hadoop.apache.org/hdfs/">HDFS</a> in order to process it later using Map/Reduce jobs. </p><p>It took me a while to figure out, how Flume and Hadoop have to be configured so that receiving messages are getting written into HDFS. This is why I decided to write a quick tutorial to get things up and running. </p><p>I assume that you have brew in place. So, the first step is as easy as: </p><p><pre class="brush: bash; gutter: false;">brew install flume
</pre></p><p>But don't think that you can just install Hadoop with brew. The current version of Hadoop is pinned to 0.21 wich is an unstable version that, AFAIK, doesn't play together with Flume. We need version 0.20.2. I edited my Hadoop formula locally on my mac. But this should work, too: </p></p><p><pre class="brush: bash; gutter: false;">brew install https://raw.github.com/mxcl/homebrew/d0efd9ee94a55e243f3b10e903526274fc21d569/Library/Formula/hadoop.rb
</pre></p><p>After you finished the hadoop installation, we need to edit a bunch of files in order to configure hadoop for local single node setup. Go to /usr/local/Cellar/hadoop/0.20.2/libexec/conf and change the following files: </p><p>core-site.xml <pre class="brush: xml; gutter: false;"><?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>/tmp/hadoop</value>
</property>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:8020</value>
</property>
</configuration>
</pre></p><p>mapred-site.xml <pre class="brush: xml; gutter: false;"><?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:54311</value>
</property>
</configuration>
</pre></p><p>hdfs-site.xml <pre class="brush: xml; gutter: false;"><?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
</pre></p><p>Hadoop is now configured to use /tmp/hadoop as HDFS folder. Now, we need to create and format the directory. </p><p><pre class="brush: xml; gutter: false;">mkdir /tmp/hadoop
cd /tmp/hadoop
hadoop namenode -format
</pre><p>Hadoop will connect to localhost using ssh. To configure ssh in the way that it can connect from localhost to localhost without needing a password, we need to add you public key to your authorized keys. </p><p><pre class="brush: text; gutter: false;">cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
</pre><p>We can test this by trying to ssh into localhost without using a password: <p><pre class="brush: bash; gutter: false;">ssh localhost
</pre><p>You now should be able to start Hadoop. Fire up Hadoop by typing: <pre class="brush: bash; gutter: false;">start-all.sh
</pre></p><p>To check if HDFS is up and running, try to list the files of your brand-new distributed filesystem: <pre class="brush: bash; gutter: false;">hadoop dfs -ls /
</pre><p>To make Flume talk to HDFS we need to replace the hadoop-core.jar in the lib directory with the one that was shipped with hadoop. <pre class="brush: bash; gutter: false;">cd /usr/local/Cellar/flume/0.9.3-CDH3B4/libexec/lib/
mv hadoop-core-0.20.2-CDH3B4.jar hadoop-core-0.20.2-CDH3B4.jar.unused
cp /usr/local/Cellar/hadoop/0.20.2/libexec/hadoop-0.20.2-core.jar .
</pre><p>Now it's time to start a Flume master node: <pre class="brush: bash; gutter: false;">flume master
</pre><p>Go to a different terminal window and start a Flume node, too: <pre class="brush: bash; gutter: false;">flume node_nowatch
</pre><p>At this point, we should be able to start the "dashboard" of Flume in the browser. Open <a href="http://localhost:35871/">http://localhost:35871/</a>.<br />
In the config section we can now configure a sink that writes into HDFS. For testing purposes we can use a fake source that just reads a local file. Choose your local node from the drop-down list and enter <pre class="brush: text; gutter: false;">text("/etc/services")
</pre>as source and <pre class="brush: text; gutter: false;">collectorSink("hdfs://localhost/","testfile")
</pre>as sink.<br />
Now, check if the file was written to HDFS with: <pre class="brush: bash; gutter: false;">hadoop dfs -ls /
# cat it
hadoop dfs -cat /testfilelog.00000038.20111006-160327897+0200.1317909807897847000.seq
</pre><p>If everything worked well, you're fine to switch the source in Flume to: <pre class="brush: text; gutter: false;">syslogUdp(5140)
</pre>Now, Flume acts like an syslog server and it writes all log messages directly to HDFS. </p><p>Cheers, Arbo </p>arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com5tag:blogger.com,1999:blog-2763547330220661791.post-49095853154861856392010-12-17T08:55:00.000-08:002010-12-17T09:05:10.227-08:00AMQP: Integrating Spring and Rails with RabbitMQ<h4>Why?</h4>
<p>
Spring web applications tend to grow and become bigger and more complicated over the time. So it's often a good choice to source out a specific responsibility and process it in an other app made for the specific job. You can build a clean interface to the new app and maybe have an extra team just working on it. Plus you have a free choice of the programming language. I like Ruby.
</p>
<p>
Another big reason is that you don't want to do all computations in the time of one request of your Spring webapp. If something could be processed later, why not improve the response time and latency of your web app by asynchronously handing the job over to another app.
</p>
<p>
Maybe you guessed it: This can easily done with the AMQP protocol and one or many RabbitMQ broker. AMQP stands for <i>Advanced Message Queuing Protocol</i> which is an open standard and RabbitMQ is an excellent open sourced server or broker that implements this protocol. With AMQP you can asynchronously send messages in a standardized protocol that is supported by the vast amount of programming languages. There even is a spring project called <a href="http://www.springsource.org/spring-amqp">spring-amqp</a> and of course a ruby gem called <a href="https://github.com/tmm1/amqp">amqp</a> by tmm1. To get an idea how RabbitMQ works I encourage you to check the projects <a href="http://www.rabbitmq.com/">homepage</a>.
</p>
<h4>Getting started</h4>
<p>
First install RabbitMQ. Be sure to install RabbitMQ of version greater than 2. Otherwise you get "Protocol Mismatch" Errors.
</p>
If your on a Mac and have brew installed its easy like this:
<pre>
$ brew install rabbitmq
</pre>
<p>
Next install the amqp gem and start rabbitmq. You can use the following scripts to verify that sending and receiving messages over AMQP works:
</p>
<pre class="brush: ruby; gutter: false;">
# consumer.rb
require "rubygems"
require "mq"
AMQP.start do
queue = MQ.queue('hello.world.queue')
queue.subscribe do |word|
puts word
end
end
</pre>
<pre class="brush: ruby; gutter: false;">
# producer.rb
require "rubygems"
require "mq"
AMQP.start do
queue = MQ.queue('hello.world.queue')
i = 0
EM::add_periodic_timer(1) do
queue.publish "hello world #{i+=1}"
end
end
</pre>
<p>
The <i>Consumer</i> simply subscribes to the <i>hello.world.queue</i>. If a message arrives the subscribe block gets called and prints the message to the console. The <i>Producer</i> just pushes a message every second to the queue. Since AMQP already uses <a href="http://rubyeventmachine.com/">EventMachine</a>, you can use all features of EventMachine like <i>add_periodic_timer</i> and so on.
</p>
<p>
If everything works fine, you should see the <i>Consumer</i> receiving "hello world"-messages.
</p>
<p>
Now we build a new Rails 3 app and configure it to process messages.
</p>
<pre>
$ rails new consumer_app
</pre
>
You need to put the dependencies for tmm1's <a href="https://github.com/tmm1/amqp">amqp</a> gem and the <a href="http://code.macournoyer.com/thin/">thin server</a> in your gemfile:
<pre class="brush: ruby; gutter: false;">
gem 'amqp', :require => 'mq'
gem 'thin'
</pre>
<p>
Why do we use thin as webserver? Simply because thin provides a running EventMachine reactor. Without this reactor <a href="http://rubyeventmachine.com/">EventMachine</a> will block the current thread and no HTTP request would be processed. If you like to use an other server than thin you need to activate the EventMachine reactor manually. To do this, just put
<pre class="brush: ruby; gutter: false;">Thread.new { EM.run }</pre> in an initializer.
</p>
<p>
Now, create a Model called example_message.rb with following content:
</p>
<pre class="brush: ruby; gutter: false;">
# app/models/example_message.rb
class ExampleMessage
def self.start_listen
AMQP.start do
MQ.queue('hello.world.queue').subscribe do |msg|
puts msg
end
end
end
end
</pre>
<p>
The <i>listen</i>-method can be triggered by a Rails initializer. But the AMQP block will block during the boot process of Rails when we call <i>ExampleMessage.listen</i> directly. We can get around this by using <a href="http://rubyeventmachine.com/">EventMachine</a> to call it later in the next available time slot.
</p>
<pre class="brush: ruby; gutter: false;">
# config/initializers/example_message.rb
EM::next_tick do
ExampleMessage.listen
end
</pre>
<p>
<b>Notice:</b> My colleague <a href="https://www.xing.com/profile/Tobias_Sunderdiek">Tobias Sunderdiek</a> found out, that this only works for Ruby Version greater than 1.8.7 with a patchlevel around 320. It will <b>not</b> work with the current Ruby Enterprise Edition, which has a patchlevel of 253.
</p>
<p>
Now, if you start your Rails app and the <i>Producer</i> script you should see your app receiving messages.
</p>
<p>
Lets try to switch from the simple <i>Producer</i> script in Ruby to a simple <i>Producer</i> built in Java that uses the same queue. Luckily the Spring-AMQP project comes with an example that exactly does this called <i>helloworld</i>. You can fetch the Spring-AMQP sources using git as shown here:
</p>
<pre>
$ git clone https://github.com/SpringSource/spring-amqp.git
</pre>
<p>
To compile and run the <i>helloworld</i> example navigate to samples/helloworld and type:
</p>
<pre>
$ mvn compile
$ mvn exec:java -Dexec.mainClass="\
org.springframework.amqp.helloworld.async.Producer"
</pre>
<p>
Those Spring-AMQP examples are a great starting point to find out how Spring-AMQP works. From there it is a easy step to build a Service that does the message sending for a specific use case. Here is a simple Service that uses the RabbitTemplate to send messages:
</p>
<pre class="brush: java; gutter: false;">
@Service
public class HelloWorldMQService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessage(int i) {
rabbitTemplate.convertAndSend("hello world "+i);
}
}
</pre>
<p>
The RabbitTemplate must be configured before it can be used. To do this you need a AbstractRabbitConfiguration as shown below:
</p>
<pre class="brush: java; gutter: false;">
@Configuration
public class HelloWorldConfiguration
extends AbstractRabbitConfiguration {
protected final String helloWorldQueueName = "hello.world.queue";
@Override
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template =
new RabbitTemplate(connectionFactory());
template.setRoutingKey(this.helloWorldQueueName);
return template;
}
@Bean
public ConnectionFactory connectionFactory() {
SingleConnectionFactory connectionFactory =
new SingleConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}
</pre>
<p>
Now you can integrate this AMQP service with your Spring webapp.
</p>
<p>
If you like to dig deeper in AMQP, Ruby and Spring I encourage you to check out <a href="http://www.infoq.com/articles/AMQP-RabbitMQ
">this article</a> and <a href="http://www.infoq.com/presentations/Spring-AMQP">this presentation</a> on infoq.com.
</p>
<p>
Cheers,<br>
Arbo
</p>arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com0tag:blogger.com,1999:blog-2763547330220661791.post-45973409943832918402010-11-29T12:17:00.000-08:002010-11-30T00:53:20.669-08:00Testing SOAP Webservices with RSpec<p>
SOAP webservices are widely used in enterprise environments. Although they feel a bit clumsy in comparison to slim REST services, sometimes you have to deal with them.
<p>
The great thing is, to test such a service you are often free to use any tool you like. I like RSpec!
</p>
<p>
To query a web service you just need a few lines of code. I recommend <a href="https://github.com/rubiii/savon">Savon</a> as SOAP client. It is used as shown here:
</p>
<pre class="brush: ruby; gutter: false;">
require 'rubygems'
require 'savon'
WSDL_URL = 'http://www.webservicex.net/geoipservice.asmx?wsdl'
client = Savon::Client.new WSDL_URL
response = client.get_geo_ip do |soap|
soap.body = { "wsdl:IPAddress" => "209.85.149.106" }
end
puts response
</pre>
<p>
The response object can be converted to hash with the <i>to_hash method</i>, so you can fetch all values simply like you would do it with any other hash.
</p>
<p>
Now, the rest should be easy and is just a normal RSpec test:
</p>
<pre class="brush: ruby; gutter: false;">
require 'rubygems'
require 'savon'
WSDL_URL = 'http://www.webservicex.net/geoipservice.asmx?wsdl'
RETURN_CODE_OK = "1"
RETURN_CODE_ERROR = "0"
describe "Geo IP Webservice at #{WSDL_URL}" do
# helper method
def get_geo_ip_result ip
response = @client.get_geo_ip do |soap|
soap.body = {"wsdl:IPAddress" => ip}
end
response.to_hash[:get_geo_ip_response][:get_geo_ip_result]
end
before :all do
@client = Savon::Client.new WSDL_URL
end
it "should yield a country name" do
result = get_geo_ip_result "209.85.149.106"
result[:country_name].should_not be_nil
result[:return_code].should eql(RETURN_CODE_OK)
end
it "should return error for malformed ip address" do
result = get_geo_ip_result "not.an.ip.address"
result[:return_code].should eql(RETURN_CODE_ERROR)
end
it "should fail if no ip address is submitted" do
lambda { @client.get_geo_ip }.should raise_error
end
# ...
end
</pre>
Happy testing!
<p>
EDIT:
</p>
<a href="http://twitter.com/dbloete">@dbloete</a> pointed me to the <a href="http://relishapp.com/rspec/rspec-expectations/v/2-2/dir/matchers/expect-error">fact</a> that with
RSpec 2 you can expect errors even more readable:
<pre class="brush: ruby; gutter: false;">
it "should fail if no ip address is submitted" do
expect { @client.get_geo_ip }.to raise_error
end
</pre>arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com5tag:blogger.com,1999:blog-2763547330220661791.post-60961840917139820342010-08-18T11:45:00.000-07:002010-10-01T08:21:40.883-07:00CouchDB: Using List Functions to sort Map/Reduce-Results by Value<p>
I just found out that it is possible to sort the result of Map/Reduce
with a <a href="http://wiki.apache.org/couchdb/Formatting_with_Show_and_List">list</a> function.
</p>
<p>
Let's take the simple example that you want to count all documents grouped by a field called <i>type</i>.
The following map function emits the values of the <i>type</i> fields of all documents:
</p>
<pre class="brush: js; gutter: false;">
function(doc) {
emit(doc.type, 1);
}
</pre>
<p>
To sum up the documents with the same value in the <i>type</i> field, we just need this well-known reduce function:
</p>
<pre class="brush: js; gutter: false;">
function(key, values) {
return sum(values)
}
</pre>
By default CouchDB yields the result ordered by the keys. But if you want to order the result by occurrences of the type of the document you either have to sort it in your app or you use a list function like this:
<pre class="brush: js; gutter: false;">
function(head, req) {
var row
var rows=[]
while(row = getRow()) {
rows.push(row)
}
rows.sort(function(a,b) {
return b.value-a.value
})
send(JSON.stringify({"rows" : rows}))
}
</pre>
If you save the list function as <i>sort</i> and the Map/Reduce-functions as <i>count</i> together in a design document, you can fetch your sorted result like this:
<pre class="brush: bash; gutter: false;">
curl http://.../design-doc/_list/sort/count?group=true
</pre>
<p>
Of course there are other options to sort a view result. I didn't found much documentation on this topic, but <a href="http://stackoverflow.com/questions/2817703/sorting-couchdb-views-by-value">this thread</a> at stackoverflow is very informative.
</p>
Back to the couch - Cheers!arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com12tag:blogger.com,1999:blog-2763547330220661791.post-88859130244156243682010-08-05T08:43:00.000-07:002012-06-13T01:51:18.981-07:00jQuery meets CoffeeScript<p><b>Disclaimer: This article is pretty old.</b><br />
</p><br />
<p>I'm just amazed how brilliant <a href="http://jquery.com/">jQuery</a> and <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a> are working together. JQuery promises "write less, do more", but with the clean syntax of CoffeeScript you can be even more concise! </p><p>Here is a quick example: </p><pre class="brush: html; gutter: false;"><!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jQuery meets CoffeeScript</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
<script type="text/coffeescript">
show_message = (msg) ->
$('#message').hide().text(msg).fadeIn(2222,
-> $('#message').append('!')
)
$ -> show_message "world"
$('#message').click -> show_message "you"
</script>
</head>
<body>
<h1>hello <span id="message"></span></h1>
</body>
</html>
</pre><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script> <script type="text/coffeescript">
show_message = (msg) ->
$('#message').hide().text(msg).fadeIn(2222,
-> $('#message').append('!')
)
$ -> show_message "world"
$('#message').click -> show_message "you"
</script> <p>Here you see it running. Just click "world" to fire the click event. </p><h1>hello <span id="message"></span></h1><p>Just have a look to the JavaScript version:</p><pre class="brush: javascript; gutter: false;">var show_message = function(msg) {
return $('#message').hide().text(msg).fadeIn(2222, function() {
$('#message').append('!');
});
};
$(function() {
show_message("world");
});
$('#message').click(function() {
show_message("you");
});
</pre>Happy coding!arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com16tag:blogger.com,1999:blog-2763547330220661791.post-25564326249493308902010-07-15T13:43:00.000-07:002010-07-16T00:13:38.530-07:00Getting started with node.js and CoffeeScript<p>
After a couple of month the voices saying <a href="http://nodejs.org/">Node.js</a> is awesome getting louder and louder, I decided
to give it a try.
</p>
<p>
There is something different I never played with but I absolutly need to: <a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a>.
I was quite amazed to find out, that those two new shiny things fit and work perfectly with one another. If you like to get Node and CoffeeScript up and running just follow my little tutorial. Its written for Mac but it should work for Linux too.
</p>
<p>
To get started first install Node.js. So lets grep the latest version directly from github at <a href="http://github.com/ry/node">http://github.com/ry/node</a> or
download the tar-archive from <a href="http://nodejs.org/#download">http://nodejs.org/#download</a>. To build and install Node type:
</p>
<pre class="brush: bash; gutter: false;">
./configure
make
sudo make
</pre>
<p>
To install CoffeeScript go and clone the Node Package Manager from <a href="http://github.com/isaacs/npm">http://github.com/isaacs/npm</a>. </p>
<pre class="brush: bash; gutter: false;">
git clone http://github.com/isaacs/npm.git
</pre>
switch to the nmp directory and type
<pre class="brush: bash; gutter: false;">
sudo make
</pre>
<p>
The latest stable CoffeeScript-version will be installed to you computer. Now you have binary called "coffee", which
just starts your CoffeeScripts with Node.
</p>
<p>
If you're a TextMate fangirl or fanboy you may want to install the CoffeeScript TextMate bundle.
It can be found at <a href="http://github.com/jashkenas/coffee-script-tmbundle">http://github.com/jashkenas/coffee-script-tmbundle</a>.
</p>
<p>
Ok, now you ready to play. Here is a hello world server written in CoffeeScript:
</p>
<pre class="brush: javascript gutter: false;">
server = require('http').createServer (request, response) ->
response.writeHead 200, {'Content-Type': 'text/plain'}
response.end 'Hello World'
server.listen 8124
console.log 'Server running at http://0.0.0.0:8124/'
</pre>
Save it as hello_world.coffee and run it with:
<pre class="brush: shell gutter: false;">
coffee hello_world.coffee
</pre>
<p>
Open <a href="http://0.0.0.0:8124/">http://0.0.0.0:8124/</a> to see "Hello World" in your Browser.
</p>
Just take a look to the JavaScript version:
<pre class="brush: javascript gutter: false;">
var server = require('http').createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World');
})
server.listen(8124);
console.log('Server running at http://0.0.0.0:8124/');
</pre>
<p>
Although it's a short and dead simple script, for me the Coffee version is much cleaner and readable.
</p>
Cheers!arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com8tag:blogger.com,1999:blog-2763547330220661791.post-77384244608356986172009-12-10T06:35:00.000-08:002010-07-16T23:46:15.411-07:00Prime Factors Kata in Scala<p>
After practicing the <a href="http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata">Prime Factors Kata</a> in Ruby a couple of days, I tried the same exercise in Scala.
</p>
<p>
Except the fact that practicing Katas is a wonderful tool to learn your IDE and
optimize your workflow, I think Katas are also great to learn programming languages.
You just don't have to think about your programming-problem, you can just think about your language.
<br/>
Its been a while since I played around with Scala, so i started my daily Kata in Scala to refresh my skills.
</p>
<p>
The fact that Scala is not just an object oriented language but also a functional language, led me to a complete different solution of the prime factors kata as in Java or Ruby. I took the immutable List of Scala instead of a mutable Array, so I had to build the algorithm in a recursive manner. <br/>
Ok, enough talk. Here is my first solution:
</p>
<pre class="brush: java; gutter: false;">
// PrimeFactorsTest.scala
import org.junit.Test;
import org.junit.Assert._;
import org.hamcrest.CoreMatchers._;
class PrimeFactorsTest {
@Test
def shouldFactorNumbers = {
Map(
1 -> Nil,
2 -> List(2),
3 -> List(3),
4 -> List(2,2),
5 -> List(5),
6 -> List(2,3),
7 -> List(7),
8 -> List(2,2,2),
9 -> List(3,3),
10 -> List(2,5),
(2*2*5*7*13) -> List(2,2,5,7,13)
).foreach{ case (n, factors) =>
assertThat(PrimeFactors.of(n), is(equalTo(factors)))
}
}
}
</pre>
<pre class="brush: java; gutter: false;">
// PrimeFactors.scala
object PrimeFactors {
def of(n:Int) : List[Int] = tryFactor(2, n)
private def tryFactor(divider: Int, n: Int) : List[Int] =
if (n > 1)
takeIfFactorElseTryNext(divider, n)
else
Nil
private def takeIfFactorElseTryNext(divider: Int, n: Int) : List[Int] =
if (n % divider == 0)
divider :: takeIfFactorElseTryNext(divider, n/divider)
else
tryFactor(divider+1, n)
}
</pre>
<p>
EDIT:<br/>
A few days later, I came to a slightly better solution.
I now use a nested function <i>tryFactor</i> to do the recursion. It's nested
because in a different context it makes absolutly no sense.<br/>
As in my first solution I used <i>object</i> instead of <i>class</i> to create the singleton object <i>PrimeFactors</i>. Again it is not written in <a href="http://en.wikipedia.org/wiki/Tail_recursion">tail-recursive</a> style, but Scala obviously optimizes it.
</p>
<pre class="brush: java; gutter: false;">
// PrimeFactors.scala
object PrimeFactors {
def of(n:Int) = {
def tryFactor(n:Int, divider:Int) : List[Int] =
if (n == 1) Nil
else if (n % divider == 0) divider :: tryFactor(n/divider, divider)
else tryFactor(n, divider+1)
tryFactor(n, 2)
}
}
</pre>arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com0tag:blogger.com,1999:blog-2763547330220661791.post-50442928239818416662009-09-18T07:39:00.000-07:002010-07-16T23:43:00.149-07:00Creating anonymous classes from Java interfaces in JRuby<p>
Today I tried to create an anonymous class from an inteface in JRuby.
After a little bit of experimenting a collegue showed me that I need to alter
the <a href="http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html">singelton class</a> of an instance of the interface in order to implement a method.
Here I implement the Callable-Interface:<br/>
<p/>
<pre class="brush: ruby; gutter: false;">
callable = Java::java.util.concurrent.Callable.new
class << callable
def call
# implements method 'call' of the Callable-Interface
end
end
</pre>
<p>
So I tried this:
</p>
<pre class="brush: ruby; gutter: false;">
require 'java'
include_class 'java.util.concurrent.Callable'
include_class 'java.util.concurrent.Executors'
msg = "from thread"
callable = Java::java.util.concurrent.Callable.new
class << callable
def call
msg
end
end
executor_service = Executors.newFixedThreadPool 1
future = executor_service.submit callable
puts "hello #{future.get}"
# -> undefined local variable or method `msg'
</pre>
<p>
For my suprise, a variable defined in the outer context was invisible for
the implemented method. I expected that it would behave just like a Ruby block or a Java anonymous class which can "see" the outer defined variables.
</p>
<p>
We found a workaround for this problem by calling a method on the instance that
initializes a instance variable with the needed object:
</p>
<pre class="brush: ruby; gutter: false;">
require 'java'
include_class 'java.util.concurrent.Callable'
include_class 'java.util.concurrent.Executors'
msg = "from thread"
callable = Java::java.util.concurrent.Callable.new
class << callable
def init(msg)
@msg = msg
end
def call
@msg
end
end
callable.init msg
executor_service = Executors.newFixedThreadPool 1
future = executor_service.submit callable
puts "hello #{future.get}"
# -> hello from thread
</pre>
Please let me know if there a more elegant way to accomplish this!arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com3tag:blogger.com,1999:blog-2763547330220661791.post-33238920720119956502009-09-01T20:12:00.000-07:002010-07-16T23:40:48.882-07:00Pattern to low couple to proprietary frameworksMost Java programmers that are using proprietary libraries or other immutable frameworks are faced with a problem: Every time you need some functionality to work with a framework class you build procedural or even worse redundant code. This leads to high coupling between the framework and the rest of the application.<br /><br />Here are some thoughts to solve this problem:<br /><br /><span style="font-weight:bold;">Inheritance</span><br />The additional functionality is added to an inherited class. This often leads to many casts and instanceof in the source code.<br /><br /><span style="font-weight:bold;">Simple Wrapper</span><br />A class that holds the class of the framework. All additonal functionality is added by the wrapper class. Whenever the framework class is needed it can be retrieved by a getter of the wrapper class.<br /><br /><span style="font-weight:bold;">Decorator </span><br />Using the <a href="http://c2.com/cgi/wiki?DecoratorPattern">decorator pattern</a> often is not possible. The reason is that the framework class has to implement a shared interface with the decorater class but there is no way to change the framework. If the framework class does not implement such an interface the decorator pattern can not be used.<br /><br /><span style="font-weight:bold;">Monkey Patching</span><br />For Java-Programmers this is unusual, but often used in ruby and other dynamic languages. With the rise of JRuby and Groovy you can now patch your Java framework classes easily and follow <a href="http://www.pragprog.com/articles/tell-dont-ask">TDA</a> to get <a href="http://c2.com/cgi/wiki?DontRepeatYourself">DRY</a> code. In some static languages like Scala this is possible, too (see <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=179766">Pimp my Library</a>). <br />In my opinion monkey patching is the best way to work with unchangable frameworks.arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com2tag:blogger.com,1999:blog-2763547330220661791.post-57986934491358543572009-08-28T01:48:00.000-07:002010-07-16T23:36:42.712-07:00clojure parallel map<p>
Map is a function that takes two parameters. A list and another function that will be executed for every element in the list:
</p>
<pre class="brush: clj; gutter: false;">
(defn multiply-by-2[x]
(* x 2))
(map multiply-by-2 [0 1 2 3 4 5 6 7 8 9])
; => (0 2 4 6 8 10 12 14 16 18)
</pre>
<p>
The map funktion is a wonderful thing, because every computation for a list element can be parallelized.
</p>
<p>
Even though Clojure has the function <span style="font-style:italic;">pmap</span> that does exactly this parallelization, I built the same functionality on my own to understand multithreading with Clojure and java.util.concurrent.
</p>
Here it is: <br/>
<pre class="brush: clj; gutter: false;">
(import '(java.util.concurrent Executors))
(defn my-pmap [map-fn, l]
(let [len (count l)
pool (Executors/newFixedThreadPool len)
tasks (map
(fn [i](fn [] (map-fn (nth l i))))
(range len))]
(let [result (map
(fn [future](.get future))
(.invokeAll pool tasks))]
(.shutdown pool)
result)))
(my-pmap multiply-by-2 [0 1 2 3 4 5 6 7 8 9])
; => (0 2 4 6 8 10 12 14 16 18)
; it yields the same result as
(pmap multiply-by-2 [0 1 2 3 4 5 6 7 8 9])
</pre>arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com0tag:blogger.com,1999:blog-2763547330220661791.post-86327687700654050802009-04-24T00:39:00.000-07:002009-09-18T12:50:54.122-07:00Spring Security AuthenticationSuccessListener<p>
Ever asked how to do things on a successful authentication when using Spring Security?
</p>
<p>
It's very easy. You just need a ApplicationListener that listens for an InteractiveAuthenticationSuccessEvent.<br />
</p>
<p>
First create a <span style="font-weight: bold;">AuthenticationSuccessListener</span>
</p>
<pre class="brush: java; gutter: false;">
package example.web.security;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.security.event.
authentication.
InteractiveAuthenticationSuccessEvent;
public class AuthenticationSuccessListener
implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof InteractiveAuthenticationSuccessEvent) {
// do things on authentication ...
}
}
}
</pre>
<p>
Then declare a bean somewhere in Spring Application Context
</p>
<pre class="brush: xml; gutter: false;">
...
<bean class="example.web.security.AuthenticationSuccessListener" />
...
</pre>
Youre done!arbovmhttp://www.blogger.com/profile/18038605720685188267noreply@blogger.com0