Archive for the 'Java' Category

TextMate dark theme on Netbeans 6.1

I always like the cool dark theme Ruby/Rails editor in TextMate and now it’s available for Netbeans. Tor Norbye’s blog for detailed setup instruction HERE. This TextMate theme is not limited to RoR editor but it works with Java editor as well. Well worth check it out.

Netbeans dark theme for Ruby editor
TextMate style Ruby editor on Netbeans

Netbeans dark theme for Rails

Netbeans dark theme for Java
This theme also works with Java editor too! Neat!

Netbeans dark theme for XML editor
Dark theme for XML editor

No Comments »

Teera on March 26th 2008 in Software Development, Ruby, Netbeans, Java

Dynamic jar loading in JRuby Netbeans tutorial

Last month I got a feedback email from a developer on my earlier Netbeans tutorials. After successfully packaging JRuby desktop app in a jar, he had a problem with loading class from third party jar files locate outside the package.

The easiest solution was to package the third party jar file at two places: one inside the package for ruby script to access, another outside the jar package for Java class. But that wouldn’t be a good solution. After some thoughts and fiddling around, I came up with ruby script that can dynamically load a class and create class instance from external jar file. This way you only need to include one copy of the third party jar file outside the package.

I’ve created a Netbeans doc tips & tricks for this script, you can check it out here:

Netbeans Tips & Tricks: Dynamic Jar Loading in JRuby 

No Comments »

Teera on March 13th 2008 in Software Development, Ruby, Netbeans, Java

Netbeans 6 and Mac Java 6 Developer Preview 9 project wizard hangs problem

If you have downloaded and installed the recent release of Mac Java SE 6 Developer Preview 9, you might be experience a problem of Netbeans ‘New Project’ dialog hangs. The symptom could be varied, in my case, I could create new Java Web and Enterprise projects, but not Ruby or Java application projects. Once I clicked ‘Finish’ on Netbeans’ new project wizard dialog, the dialog would hang without new project being created.

Netbeans 6 on Mac new project wizard hangs with Java 6 Preview 9
This dialog hangs forever

This happens because AppleScriptEngine.jar bundled in Apple’s Java SE 6 Preview 9 package was originally compiled with JDK 6. Since the folder containing this file is shared across all installed JDK versions, the problem occurs.

The solution here is quite simple. Simply remove AppleScriptEngine.jar under /System/Library/Java/Extensions folder and Netbeans IDE will run fine.

2 Comments »

Teera on March 4th 2008 in Software Development, Ruby, Netbeans, Java

Bridging JRuby on Rails and Java with MVP

On my last post, I’ve explained how Model-View-Presenter (MVP) pattern and help you achieve extensibility on Java platform by decoupling your application from presentation technology. Yet, it’d be a shame if I stop the discussion there - I have not mentioned anything about the JVM! On this post, I’ll take it a step further. I’ll show you how you can leverage the power of Java Virtual Machine (JVM) and MVP pattern to transform your Java application to JRuby on Rails.

You can download the source code for this example HERE

Power of the JVM

Being on Java platform, developers can also reap another benefit from their best friend: the JVM. To demonstrate what good Java VM can bring is to look at this example. Assume that our bookstore site has become popular and our CEO just recently returned from a Ruby on Rails user group meeting. The CEO declares that we need to jump on the Rails bandwagon and get books data from XML file instead of hard coding it in the application.

The issue here is now we have to migrate our application across different technologies. To minimize cost and avoid writing a lot of code, we decide to leverage existing Java platform and go with JRuby.

JRuby allows Ruby class to implements Java interface. So we first start from creating a new data model where it reads books data from XML file.


Reading books data from XML file with REXML library


Sample XML books data

Rails controller = The ‘View’ controller

Now that we have the data model ready, we go ahead to implementing the view layer. This is similar to last example on servlet and JSP. We have Rails controller class implements the IBookDetailView interface. The controller is responsible for fetching data to .rhtml file to display.

The key idea here is that it’s the Rails controller that is the ‘View’ in MVP. The presenter is simply an instance of BookDetailPresenter Java class that we can use here with the magic of JRuby.

Rails controller implements IBookDetailView interface


Rails controller acts as the view and controls data that will get displayed on rhtml page

Running this application and there we have it on JRuby Rails

The virtual machine empowers Java and makes it a platform, not just a technology or programming language. Developers can benefit from MVP and, combining with the power of JVM, can take this pattern to another level.

No Comments »

Teera on February 3rd 2008 in Ruby, Netbeans, Web Dev, Java

Model-View-Presenter pattern in Java

Model-View-Presenter pattern has been around for quite some time. Though it seems that MVP is quite popular among .NET community, I don’t see much adoption in the Java camp. I’ve had quite a number of success on applying MVP to both Java and .NET projects, and have been a fan of this pattern since. On this blog post, I’m going to show you what MVP is, what it can offer, and why Java developers should pay attention to it.

Model-View-Presenter pattern

The key idea of MVP is the UI design that decouple the UI logic and UI representation. This will make the UI layer very thin and relieve it from any responsibilities as much as possible. Consider most of UI technologies come with the UI display and some logic attach to it (for example, binding event or data to Swing control). Having this coupling makes it difficult to automate the test for UI. Worse, if you decide to change the UI technology, you will have to rewrite this UI logic all over again.

MVP suggests the idea of separating UI logic to a separate class, the Presenter. The UI will have only small responsibilities of displaying data to the user and taking input. Two benefits of implementing this pattern are ease of UI testing and reuse of logic code on different presentation technology.

The Model

Model is where you store business logic, service, or data. It’s identical to the Model in Model-View-Controller architecture.

The View

The presentation layer of your application. In the context of MVP, the view should do nothing else but display data to and grab input from users.

The Presenter

You can think of presenter as an abstraction layer that glues the model and the view together. On the implementation level, this is done through the use of interface.

Model-View-Presenter with Swing

OK. Let’s see what the code looks like. Let’s say that I have a small book store application that does the following:

  • Grab list of books from data source and display in a drop-down list.
  • When user selects book from the drop down list, the detail of the book should be displayed on screen.

In normal Swing App, you’ll likely to see UI logic code stuck in actionPerformed method of an anonymous ActionListener class.


Traditional Swing application

However, with MVP, we try to remove UI logic from the view. So, the ActionListener will call presenter to perform this action.


Calling presenter on event

The presenter will join together the data from model and fetch it back to the view. As you can see, by wiring each component through interface, the view and the model are not aware of one another. You get a nice separation of concerns here.

Presenter: get data from model and display to the view
Presenter class: handle the logic, tying view and model together


Model interface: data model provider must implement this interface


View interface that view class must implements

The implementation

Martin Fowler recently broke up MVP to two patterns: Supervising Controller and Passive View (since it’s Mr.Fowler who declared MVP, it’s legit for him to retire it?). The main difference between the two is the level of interaction between each component.

In Passive View pattern, each component has clear boundary. The view contains very few or minimum logic code. Both view and model rely heavily on presenter, this includes the view updating logic.

MVP
Nikola Malovic’s diagram demonstrates a very clean separation between each of the component

Implementing clean separation passive view involves a lot of code. Supervising Controller is very similar to Passive View but more relax. Supervising Controller allows some logic code to be in the view. It also allows the model to talk to the view directly for updating display data.

There are also other flavors of MVP out there. They have the same key ideas of UI decoupling, only the minor difference in how it’s done. I prefer the implementation that can give me the cleanest separation of concern between the view, the model, and the presenter. Neither the view nor the model should have any dependencies on the presenter.

Don’t restrict yourself to the diagram though. There are cases where it’s necessary and better to have the view and model communicate directly to one another. For instance, if your application does not have strong object model, displaying a form with 200 fields from 20 tables to screen can be a daunting task. In this case, you’ll have to create a new object entity class to store all these fields to maintain a clean separation. It’d be much easier and less code involved if the model can update the view directly.

Benefits of MVP

As I mention above, the most evident benefits of MVP are making the UI testable and independent from presentation technology. The topic of testing is tied to Test Driven Development which I will go into detail in future posts.

Extensibility

Recently a client had asked me to port a Java desktop application to the web. Although quite a chunk of time was required to refactor the old code, the porting process was remarkably smooth with MVP. Let’s look at the earlier bookstore example. Assume that we are asked to port this application to web version. For simplicity, let’s say that our web platform is simple Java servlets and JSP.

First, I create a servlet that control a index.jsp. I have this servlet implements IBookDetailView. At this point you may ask why the servlet, isn’t the servlet supposed to be acting as controller/presenter? We already have the presenter logic in place. The servlet here serves as the output channel to the JSP file.

Then inside the servlet’s init() method, I initialize the presenter and the model the same way I do in Swing version.

After implementing all view methods, the servlet is ready to pass down the data in the form of Java Bean to JSP file for display. We now go ahead and add code to the JSP file to display data.

And that’s all we have to do. As you can see, the beauty here is we accomplish this migration task with out having to change any logic or model code. MVP benefits heavily from the use of interface.

Using MVP design pattern can give you the extensibility for building user interface application. MVP in a sense, is a good practice of layering and applying dependency injection to your application.

You can download the source code for the example HERE

For more information on the MVP pattern

No Comments »

Teera on January 29th 2008 in Ruby, Netbeans, Web Dev, Java

Packaging JRuby Swing application in a Jar

I just completed a new Netbeans JRuby tutorial. This tutorial will show you how you can easily package up your Ruby application into a single Jar file.
 

Check out the tutorial here,  Swing with JRuby part II: Packing JRuby Desktop Application

 

This tutorial focuses on how to package existing Ruby application. If you are looking for tutorial on how you can create JRuby desktop application from scratch, you may find the tutorial I had created earlier helpful:

 Swing with JRuby: Developing Desktop Application with JRuby and Java Swing API

 

No Comments »

Teera on December 16th 2007 in Ruby, Netbeans, Java

Swing JRuby Netbeans Tutorial

I created a Netbeans tutorial on how to develop desktop application with JRuby and Swing based on the demo I presented to Vancouver Ruby and Rails last month.

 Follow-up interview about my work on JRuby:

- Interview with James Branam

- Confession of a Netbeans Ruby user

I’m working on a new tutorial showing how to package JRuby-Swing desktop app with Netbeans. Stay tuned for more!
 

No Comments »

Teera on November 4th 2007 in Ruby, Netbeans, Java

JRuby Presentation @ Vancouver Ruby user group event

Earlier this month I had a presentation on JRuby for Vancouver Ruby user group. The 20-30 minutes presentation dragged on to about an hour or so. It’s very exciting to see that the audience joined in and started discussion on various aspects of JRuby. It found it intriguing and enjoyed it quite a lot myself.

The presentation slide can be downloaded in ODP format and PDF format. I’m cleaning up the demo code sample and will post it up very soon.

For more information

Special thanks to Brian Leonard, Peter Harvey, and Amanda Waite for advices and information =]

No Comments »

Teera on October 31st 2007 in Ruby, Netbeans, Java

Role of Architects

Ted Neward’s recent blog post provide significant insight into what it means to be an architect. Come to think of it, it’s quite amazing that we all often talk about IT or Software architect without having mutual agreement on what it means to be an architect. Lately I have also seen ‘Solution Architect’, ‘Infrastructure Architect’, ‘Service Architect’, and more. Ted’s answer on roles and responsibilities of architects compels me to re-think the entire concept over again.

So, what does an architect do?

For some companies I've worked for, the "architect" was as you describe yourself, someone whose hands were dirty with code, acting as technical lead, developer, sometimes-project-manager, and always focused on customer/business value as well as technical details. At other places, the architect (or "architect team") was a group of developers who had to be promoted (usually due to longevity) with no clear promotion path available to them other than management. This "architect team" then lays down "corporate standards", usually based on "industry standards", with little to no feedback as to the applicability of their standards to the problems faced by the developers underneath them.

What relevance do architects have today?

Well, this is a dangerous question, in that you're asking it of one who considers himself an architect and technologist, so take this with the usual grain of salt. Are we just overpaid out-of-touch developers? God, I hope not. Fowler talks about architecture being irrelevant in an agile project, but I disagree with that notion pretty fundamentally: an architect is the captain of the ship, making the decisions that cross multiple areas of concern (navigation, engineering, and so on), taking final responsibility for the overall health of the ship and its crew (project and its members), able to step into any station to perform those duties as the need arises (write code for any part of the project should they lose a member). He has to be familiar with the problem domain, the technology involved, and keep an eye out on new technologies that might make the project easier or answer new customers' feature requests.

What do you think? What would be your definition of an ‘architect’ ?

These excerpts are examples. Check out full blog post here.

Note: In this context, I mean ‘architect’ in terms of IT-related (solution, architecture, application, etc.) architecture role.

No Comments »

Teera on September 26th 2007 in Web Dev, .NET, Java

Notes on SCJP certification exam preparation

Recently a developer has emailed me for any suggestions and tips for SCJP (Sun Certified Java Programmer) exam preparation. I took SCJP exam about two years ago. Just as I typed up replying email I found that these tips are quite useful for anyone, especially students who plan to take SCJP certification exam.

Get an exam prep book

You can find a few SCJP prep book on amazon. I actually went to a bookstore near my house and skim through a few. The one I picked was “SCJP Sun Certified Programmer for Java 5 Study Guide by Kathy Sierra, Bert Bates” and it is simply amazing. This book covers all the topics. It’s the only main material I used for preparation. It does more than getting me through the exam, it did a great job laying solid foundation on Java as well. I still keeps it on handy on my bookshelf.

Is only one book enough?

Yes, one is enough. I’m not getting commission or anything advertising this book. I think any exam prep books can get you through the exam if you really work it, cover to cover that is.

Someone told me about ‘certification exam braindump’ ?

Check out some braindump websites. Don’t get confuse with those website that try to sell you their exam prep material. There are web forums where people post their study tips and exam experience. These braindumps won’t tell you what’s on exam but give you an idea of what it’s like.

Again, it’s really your choice if you want to purchase some sample test questions from many vendors advertising on Google (they are actually the same company). Going through enough braindumps sample test questions may help you passing the exam, but what’s missing will still be missing - the solid knowledge foundation.

What about Sun practice exam?

Sun practice exam will surely help. I got a chance to go through Sun SCJP practice exam questions after I got my cert. I found it to be much more informative and right on the exam topic than of other vendors. My advice on this though is, no matter how many preps you have, do not to take them for grant, use them as supplementary material to your learning.

Yes, I am ready to take on the exam!

This is in fact, my most critical tip for SCJP exam preparation. When you feel you’re ready, take a deep breath and slowly go through each of the exam objectives (in the book’s index or listed on Sun website). Without looking at any notes, write down everything you understand about each objective. Then check for correction. This will help making sure that you really have good understanding on all topics and boost your confidence.

Do I need to practice?

Practice makes perfect! I passed the exam with pretty good score for someone who doesn’t do Java everyday. If you work with real-world Java everyday, there should be no problem. If not, try out as many sample code (from prep book) as possible. Hand-typing the code will make it stick to your brain better than just reading through the text.

Ok, I’m a Java guru and I’ve read quite a number of Java book, I shouldn’t need the exam prep material right?

Studying normal Java book does not ensure that you’ll pass the exam. Regular Java book aims to give you enough knowledge to do real-world work, unfortunately, what you’ll find on the exam is a different story. To pass the SCJP, you also needs to know some non-common-sense stuffs, theoretical topics, in and out, and the “nice to know” of Java. That’s why I really suggest you grab a prep book, it help you with that.

One more thing(s):

Of course I won’t tell you what’s on the exam =). Here are few things I think you should get solid understanding of before taking the test. They are not as trivial as they seem to be!

  • Threading
  • Array, Generic, and collections
  • Object-oriented concept
  • Inner class and anonymous classes

Hope this help and good luck =]

1 Comment »

Teera on September 11th 2007 in Java, Personal