Tuesday, June 2, 2009

JavaOne 2009: Clojures for the JVM by Rich Hickey


  • Fundamentals: it is a dynamic language and a new LISP, Functional, Supporting Concurrency, Compiles to JVM bytecode, not OO

  • Why not OO? Because OO encourages mutable state and Mutable stateful objects are the new "spaghetti code". His words, not mine! :)

  • Supports a REPL (read-eval-print-loop)

  • Load/change code in running program

  • On Data Types it has, for example, Arbitrary Precision Integers, Ratios (no precision loss?), doubles, BigDecimals...

  • On Composite Data Structures it has, for example, Lists, Vectors, Maps, Sets and everything nests

  • Data structures are the code, not text-based syntax and everything is an expression

  • All data structures are immutable; performance problems are avoided by two rules: new versions are not full copies and make use of a structural sharing of data (this last one is kind of hard to put in a few words here)

  • Abstraction-based library built on concrete classes for structures with a Java Interface

  • Clojure supports prallel computation, mostly because it focuses on concurrency, which is made easy by using immutable data; no locks needed

  • The only thing that mutate are references themselves, in a controlled way

  • Clojure uses MVCC - Multiversion Concurrency Control through the command dosync which is the transactional control done by databases

  • Clojure integration with Java is natural beacause the data types are the same as Java; it has debugging support and all the Java bytecode infrastructure works with Closure


Why is it that everytime I see a brand new language or programming paradigm it reminds me of everything I learned 15 years ago at College? My words, not his! :)

JavaOne 2009: EJB 3.1Overview

EJB 3.1 Technology Overview by Kenneth Saks
He spoke about the EJB 3.1 Spec which should be ready by end of the year.


  • Session Bean with Local Business Interface; "No-interface" view client - it calls directly the business method from the EJB and it is not generated by the API

  • Java EE Platform packaging will change to a simplified version; same behavior withoud need for ejb-jar

  • EJB 3.1 "lite" API; a small subset of EJB 3.1 API required by JavaEE pPlatform 6 Web Profile, which is made possible by the simplified .war packaging mentioned earlier (local session beans, transactions, declarative security and interceptors)

  • Portable global JNDI Names for Remote and Local session beans; portable naming syntax with unique names defined globally, within application and per module

  • EJB Component Testing tries to solve the problem that it is hard to test Local session beans and client-side EJB component; it is possible to run the EJB in an embeddable container in regular JSE

  • New features: Singletons (new Session Bean component type, Container and Bean Managed Concurrency), Startup/shutdown callbacks, Caledar-based timers (created progammatically or automatically), Asynchronous session bean invocations (available for Stateful, Stateless and Singleton beans), JAX-RS Integration


Glassfish v3 has the implementation for all these new features.

JavaOne 2009 General Session

The Opening General Session for Java One 2009 has just begun. We had presentations from Verizon, Intel, Sun itself with JavaFx. The first two were a little bit commercial, but the JavaFX quick presentation and introduction was interesting in the way that it showed what this technology is able to do!!

Saturday, May 16, 2009

Lower Case, Upper Case and Capitalize in Ruby

Ruby has some very straightforward methods to change the case of your strings.

Here are some of them:

"HoMer".downcase # returns homer
"lisa".upcase # returns LISA
"margie".capitalize # returns Margie
"BaRt".swapcase #returns bArT

Just a quick one! :)

Find this article also on definenull.com.

Search an Array in Ruby

If you want to search for a value in an array in Ruby you could do something like this:


array = ['Bart','Lisa','Maggie']
array.include?('Homer') # returns false
array.include?('Lisa') # returns true

Another possibility is to do this:


array = ['Bart','Lisa','Maggie']
array.index 'Homer' # returns nil
array.index 'Lisa' # returns 1

Both methods should work for searching an array.

For more information check the Ruby API for the Array Class.


Cheers,

Dani


Find this article also on definenull.com.

Wednesday, April 29, 2009

Which binary file am I running in Cygwin?

If you want to find out which of the binary versions of some file you are running under Cygwin you can try two commands: "which" and "type".

type -a ruby

Should return something similar to:

16:55:17 ~$ type -a ruby
ruby is /usr/bin/ruby
ruby is /bin/ruby
ruby is /usr/bin/ruby


So would this:

which ruby

Cheers!

Find this article also on definenull.com.

Saturday, April 18, 2009

Ruby on Rails Model Auto Completer

When developing web applications nowadays, caring about user interaction is mandatory. Using Ajax technology to allow your system to show information without reloading the whole page is one of the features every web user is getting used to.

One good example on this kind of interaction are those text boxes with auto completion. While the user types some name the system will automatically search for matches to those names in the database, offering sugestions on possible existing values.

However, if such a text edit box is part of a form you also need to populate some hidden fields with the returned ids for the text you found on the database.

Ruby offers a text edit option that will perform auto completion by searching in the database, but it does not populate another field.

The model_auto_completer plugin is perfect for this job! If you have, for example the following models, you can type (and lookup) the name of a contact directly in the Transaction form:

class Contact < ActiveRecord::Base
end

class Transaction < ActiveRecord::Base
belongs_to :contact
end

First thing you gotta do is to install the model_auto_completer plugin using the command:

script/plugin install http://model-ac.rubyforge.org/svn/trunk/vendor/plugins/model_auto_completer

After doing this go ahead and install the default auto_complete plugin from rails:

script/plugin install auto_complete

Then, in your view file, add the call to the method that will render the text_edit for the contact name and the one for the contact_id (which is hidden) will also be automatically added:

<%= belongs_to_auto_completer :transaction, :contact, :name %>

The method in the Transaction controller which gets called by the ajax method must be named accodingly:

def auto_complete_belongs_to_for_transaction_contact_name
@contacts = Contact.find(
:all,
:conditions => ['LOWER(name) LIKE ?', "%#{params[:contact][:name]}%"],
:limit => 10
)
render :inline => '<%= model_auto_completer_result(@contacts, :name) %>'
end

One problem you might run into is to get an error messsage regarding the forgery protection. Something like this:

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/request_forgery_protection.rb:79:in `verify_authenticity_token'

This message is telling you that the POST command you are sending is not being understood by the gorgery protection mechanism. To overcome this problem, just add the command below to your Transactions Controller:


protect_from_forgery :except => [:auto_complete_belongs_to_for_transaction_contact_name]


Well, this is pretty much everything you need to make this plugin work. Have fun!!

You find this article also on definenull.com.