Thursday, June 18, 2009

I was struggling with a stupid problem this week and it took me some time to realize what was going on, so I will post here the solution so nobody else needs to waste time on this matter again (and for future reference for myself as well ;)).

I wanted to read a .csv (comma separated value) text file that was inside one package in my application. Consider the simplified version of my code in the tree below.

The value of the variable url was null. And there is one simple (and quite obvious once you find out) reason for this. The resource file was indeed in my src folder, but it was not in my destination folder where the classes were generated. As I am using maven, this would be the target folder.And why is this obvious? Because the getResource method uses the ClassLoader of the calling class to find the resource you are looking for, and the realm of files the ClassLoader knows are in the classpath. As I was executing the code from my IDE, the classpath would be the compiled classes from sources. On top of that, the problem was that the text files not being copied to the destination folder.

Thanks to Maven the solution was quite simple. As it is based on convention over configuration, all I did was to move the resources folder to the root of the src/main folder like this:

The next tricky step is to set the prefered order in which your JVMs shoud be used. To set this up just edit the /etc/jvm file and include your recently installed JVM as the first one, so your file looks more or less like this:

# This file defines the default system JVM search order. Each# JVM should list their JAVA_HOME compatible directory in this file.# The default system JVM is the first one available from top to# bottom.

Ok, not ready yet, but I am not willing to return to Windows without doing this until the end! :)So, next step is setup the environment variables and if you want to set it system wide, edit the /etc/profile file and append the following lines at the end:

Thursday, June 4, 2009

Atomic Transactions should have a scoping mechanism that provides "all-or-nothing" semantics, enables shared resources to be protected from concurrent use and have ACID properties (Atomic, Consistent, Isolated, Durable)

WS-Transactions in the past vas very hard to achieve transactional interaction among WS services; even JTS implementations didn't interoperate but WS provide the opportunity to leverage unparalleled interoparability

On B2B interaction, one cannot affort to lock resources on behalf of an individual indefinitely and maybe you need to undo a subset of the worh that's been done

Possible Transaction Models for REST

Atomic Transaction (XA/XTA) - status: specified and implementd

Forward Compensation Base (WS-BP)- status: specified

Implementation for REST transaction for JBoss uses an uniform interface with HTTP and the usual methods (GET, PUT and POST) return the usual HTML code and some XML in the body

Example URLs:

GET <s>://<auth>/transaction-coordinator/active

POST <s>://<auth>/transaction-coordinator/begin

Demo is using the transaction-coordinator (with a Proxy in Tomcat) and some Web Services in Python, JBoss TM and RESTeasy

Andreas Egloff (Sun) and Bruce Snyder (SpringSource)Enterprise Integration Patterns in Practice"A Design Pattern is a formal way to document a solution to a design problem ina particular field of expertise" - from Wikipedia

Flexibility x Productivity: too little explicit support out-of-the-box but explicit support out-of-the-box is too rigid (does not fit your case or you are forced to go around) - the solution would be to extend those use cases

Camel supports routing of messages in a simple manner like, for instance, from("A").to("B") and much more complex stuff like taking decisions in the route and where to route the message to; it also suppors a pipeline routing much like from("file://myfile.txt").choice().when().method("MyBean","matches").to("Q").end().pipeline("B","C","D")

Project Fuji is the basis for OpenESB v3 and is a service platform to realize SOA based on Convention - Configuration - Code in that order; Light weight and OSGi based and not centred on JMS but on Services

The Project Fuji has a Web UI that allows the user to visually declare and configure services and program the connections and routing of services; it also offers a DSL (Integration Flow Language)

Bruce and Andreas went on some slides with examples of the patterns from EAI book and showed how those are implemented in Camel calls using Fuji interface to build them - it seems easy, but as they said in the beginning of the presentation, using out-of-the-box solutions might be too rigid; on the good side, you can write code using DSL and even pure Java, so it really might be an interesting solution for integration

After the slides, a quick live demo using Eclipse with some tools for Spring and Maven with archetypes for Apache Camel

Joshua Bloch in "Effective Java: still effective after all these years"First Edition of the famous book Effective Java was in 2001 and is now on its second edition from 2008 and it has new chapters on generics, enums, annotations, varargs, concurrency and Serialization

How to write a container with an arbitrary number of type parameters: typesafe heterogenous container pattern - parameterize selector instead of container; present selector to container to get data; data is strongly typed at compile time; allows for unlimited type parameters

Enum Types

double temp = thermometer.getTemp(true)

double temp = thermometer.getTemp(Temperature.FAHRENHEIT)

double temp = thermometer.getTemp(FAHRENHEIT)

which of the above would you prefer? :)

Synchronized collections are largely obsolete; use ConcurrentHashMap and friends and Never sinchronize on a concurrent Collection

The Serialization Proxy Pattern - don't serialize instances of your class; instead, serialize instances of a small, struct-like class that concisely represents it, then reconstitue instances of your class at read time using a constructor that uses only its public methods - Sun uses this pattern on its EnumSet there is a private static final SerializationProxy

The presentation is based on lots and lots examples of code, so it's kind of hard to resume them here! ;) In essence, it is probably a good idea to buy the new edition of the book! ;)

Different setup for selenium grid: using a selenium hub brings no complexity to the developers or to the already written tests; setting up differente remote controls for differente environments (IE on Windows, Firefox on Linux, Safari on Mac) only depends on having the Selenium hub

TestNG will allow to run the same tests in the different environments that were setup by Selenium Grid

Test Servers cost money: using the cloud allows to use on-demand servers, pay-per-hour, unlimited number of machines and start in a couple of minutes - no OS X offered by amazon

Create Amazon Machine Images for each different environment you need to test your application - The machines are killed after the tests are run and you only pay for the time you use those machines

Use Maven EC2 plugin to start amazon AMIs, stop at the end of test and pass user data

Accomodate large teams: enterprises like to scale by adding people and this makes things harder: easy-to-learn is critical, provide tooling (automated testing & integration) and keep the solution simple

Tooling is great, but don't let it get in the way!

In their words: "Consultants tend to think that copying and pasting code make them more productive. And this is not true.!: Prefer convetion and configuration over coding! Support Patterns as first class citizens!

Frameworks are poweful to reduce code duplication, but sometimes constrain flexibility: Interceptors are a powerful way to add flexibility

Developers spend a lot of time waiting. It is necessary to shorten the change-build-deploy-test cycle, by reducing build time, deploy time with smapper deployables or in-place editing; increase testability and debuggability with built-in tests and support automation

Modulartiy improves startuptime and performance by only loading what's necessary when it's needed. Modularity can help control complexity.

Reduce the artifacts per solution or make managing large numbers of artifats possible.

Connectivity Configuration: configuration belongs to run-time and not design or development time; deployables are used in different environments

Message-based services more robust in face of change so avoid rebuilding the world for small business logic changes: use scripting languages, externalize business logic (e.g. CBR), consider evolution, versions in handling messages, externalize cross-cutting concerns (e.g. Aspects)

Patch Management! OSBi allows to declare a dependency on a specific version of a component, and this way it is not necessary to test the whole enterprise application because you need to upgrade one library

JMS if often a bottleneck - component-component message-passing can also be done without JMS; in-memory message exchange allows performance improvement. Just use JMS if it is really necessary

Type Qualifiers improves documentation, find bugs in programs, guarantee the absence of errors and reduces the need for assertions and run-time checks

Checkers are designed as compiler plug-ins (i.e. annotation processors) and use familiar messages; many checkers are being implementd in Java 7

Michael said: "If you want to find some bugs in your code, Findbugs is ok. If you are a developer who cares, you'll want to find ALL your bugs, and this is the tool for that"

It is possible to create your own type checking annotations, and he showed us how to write a quick one to avoid SQL injection in our code that access the database

Michael showed some statistics comparing FindBugs and the new Java 7 type checking; type checking found 8 bugs (20000 LOC) and findBugs none; on the other hand, type chacking demanded 35 annotations to be written throughout the code

It is said that annotations like these would clutter your code, but in my opinion it is a good trade-off for having a "bug-free" code

Another interesting option that Type Checking offers, is that one can start doing the annotation of the code on a per-module or per-class basis, i.e. start with the most important parts of the code

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! :)

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, April 18, 2009

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::Baseend

class Transaction < ActiveRecord::Base belongs_to :contactend

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

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:

This last command will fail because rails does not recognize the Category model inside the subcategories model. So all you have to do is tell Rails that you want to include another model in the query, and this is done using the :include option from the find method:

Tuesday, March 3, 2009

If you need to set the default formatting of dates throughout your whole application in Ruby on Rails, just add the following line to your "<your application>/config/environment.rb"my_date_formats = { :default => '%d/%m/%Y' }

Tuesday, February 10, 2009

I was looking for a way to display currency data coming from the database as a long. I found out that Ruby on Rails has a buil-in Module Helper method called ActionView::Helpers::NumberHelper:number_to_currency which allows one to format a number according to some options:

This would work fine, but in this solution we are breaking the DRY (Don't Repeat Yourself) rule, because I would need to set the same parameters in every place I was displaying currency values.

A better solution would be to have a helper method which I could use in every piece of code where I need a currency. So I came up with the solution below, which consists in writing my own helper method in app/helpers/application_helper.rb:

Tuesday, January 20, 2009

A colleague at work just sent me this interesting list of the most dangerous programming errors.I think it a very thorough list considering the type of error the average programmer does and normally does not have the time (or interest, or knowledge, or whatever... :)) to care on a daily basis.

Thursday, January 15, 2009

Being a Certified Scrum Master and working with Scrum for over a year, people quite often come to me asking for directions about information on Scrum.I created this post to keep this information handy for me next time I am asked for this and to share it with the community.

Is, in their own words, "nonprofit organization committed to delivering articles, resources, courses, and events that will help Scrum users be successful". It was founded by Ken Schwaber, Mike Cohn and Esther Derby some of the founders of Scrum itself. There is one specific article which points the user to important fundamentals of Scrum like sprints and user stories. The Glossary of Terms of Scrum is very useful tool for anyone browsing through Scum information through the web or books.

Is "the home of the Certified ScrumMasters organization, offering training, gatherings, information, and materials regarding Scrum, including frequent articles". Amongst their resources you can find this brief overview of Scrum and

Videos

This video (with legend) from Ken Schwaber is also a great introductory resource. It is a speech given by him at Google.

Saturday, January 10, 2009

I downloaded and installed today the Netbeans 6.5 and for my surprise its IDE was in Portuguese. I prefer using the IDE in English, so I tried to change it using the way I remembered from previous versions, i.e. at the Tools menu. As it happens, it is not possible to change the language of the IDE through the interface, because it is based on the locale of your machine.

After some experimenting and searching I found out that it is necessary to pass the locale to the IDE and force it to use something different from your system. So, go to the shortcut which starts your IDE (yep, using Windows here) and pass "--locale en:US" in the command line.