Netbeans IDE 6.7 has added support for Qt. Qt developers including me can enjoy with our great IDE in developing some great applications with the QT framework. Get a Netbeans 6.7 executable from Netbeans.org and give it a try.Enjoy Qt funs
July 3, 2009
June 1, 2009
PART I: Dependency Injection with Guice
Introduction
Guice(pronounced “juice”) is an ultra-lightweight, next-generation dependency injection container for Java 5 and later.In this article we’ll look at Guice in Action and Plain Old Factory for dependency injection. We will decide what and when to use dependency injection in our applications(desktop or web).
Why do you need Guice ?
Guice enables and provides the best of all worlds:
- Easy unit testing
- Maximal flexibility and maintainability
- Minimal repetition.
Can’t this be done without Guice?
Yes you can with factory design pattern, but as the your application thens to grow Guice will be of great beneficial to your application.
Factory Patten
Before the term “Dependency Injection” was discovered, developers make use of factory design pattern to achieve the same effect in some aspects.In this example, a client depends on a UserAuthentication service interface for authenticating a user either by via JDBC (Database Server) or LDAP (LDAP Server).We’ll call it UserAuthentication.

We have two implementations of the service with JDBC as the default implementation of this service which the client should not depend directly on. If we decide to use a different service implementation in the future, we don’t want to go around and change all of our clients.
Implementations of UserAuthentication service :
- JDBC Implementation
-
LDAP Implementation

Next, we will need to implement the factory class for our clients to use in getting an implementation of the service as well as a means of testing in a mock service.
Logic of UserAuthentication factory :
Pass the name of implementation to the factory service and the factory returns an instance of that implementation.By default, a JDBC implementation will be handed to the client if name of implementation cannot be found or is null.

The client uses the factory directly to access the service if it needs one. Additionally you can add extra logic so that service implementations can be registered to the factory. The factory then have knowledge of all different implementations (Registry, you call it) and can access an implementation from the registry.
A simple client for our UserAuthentication Service

What is happening?
Get an instance of factory and use factory to get an implementation based on the name. Use the implementation to test a dummy user by creating an instance of user and calling the login method of the service interface via which you pass the dummy user.
Running the Factory Client

Dependency injection by Hand
From Guice 1.0 Guide, I quote:
“The dependency injection pattern aims in part to make unit testing easier. We don’t necessarily need a specialized framework to practice dependency injection. You can get roughly 80% of the benefit writing code by hand.”
While the client asked the factory for a service in our previous example, with dependency injection, the client expects to have its dependency passed in. Don’t call me, I’ll be there when you need me.

Dependecy injection by hand will require us implementing a factory for the client so that each implementation of the service will be returned by the client factory.
Summary on Plain Old Factories and Dependency Injection by Hand
You will notice that both (plain old factories and injection by hand) require barely the same amount of code to accomplish.
Dependency injection by Guice
Writing factories and dependency injection logic by hand for every service and client can become tedious. Some other dependency injection frameworks even require you to explicitly map services to the places where you want them injected.
From Guice 1.0 Guide, I quote:
“Guice aims to eliminate all of this boilerplate without sacrificing maintainability.”
-
In using Guice, you implement modules, com.google.inject.Module.I prefer AbstractModule class.
- Guice passes a binder to your module, and your module uses the binder to map interfaces to implementations.
- Guice allows you to scope services (Singleton:one instance or Prototype: an instance for each injection by default).
As a proof of concept, we will create a module called SimpleAuthenticationModule to bind UserAuthentication to JdbcUserAuthentication.

A module tells Guice what you want to be injected. Guice can inject to fields, contructors and methods.
How is the service injected to the client?
By annotating your fields, methods and constructors with @Inject.
public class Client {
@Inject
public void Client(Service service) { this.service=service;}
}
Use of @ImplementedBy annotation
Instead of implementing the AbstractModule, you can annotate the service with @ImplementedBy. This annotation takes the implementation class as its parameter. It eliminates even more boilerplate code.
Use of @Singleton annotation
Instead of binding a scope to your implementation as done in the SimpleAuthenticationModule, you can annotate your implementation with this annotation to single instance of your service.
Architectural Overview of Guice
There are two distinct stages in the architecture of Guice as enumerated in Guice 1.0 Guide. They are startup and runtime. Injection Points are created by the Injector after supplying it with modules and injection class.Guice can look at the classes you tell it about during this startup stage and any classes those classes know about, and tell you whether or not you’re missing any dependencies.
Guice Runtime model:
see Guice 1.0 User Guide for more details about the runtime stages.

Source: Guice 1.0 User Guide
Annotation Binding
For the UserAuthentication service, there are two implementation of it namely JdbcUserAuthentication and LdapUserAuthentication. How do we differentiate between the two implementations? Guice has an answer to that and this is what the annotation binding is to solve.
For this example, we will bind the annotation @LdapAuthentication to the service LdapUserAuthentication implementation and @JdbcAuthentication annotation to JdbcUserAuthentication implementation. Refer to Guice 1.0 Guide on how to create Binding Annotations.

Guice provides production-worthy implementation called @Named but we will implement our own as proof of concept.
Bootstrapping your application
The idea of bootstrapping is fundamental to dependency injection. Always explicitly asking the Injector for dependencies would be using Guice as a service locator, not a dependency injection framework.
Implementation of UserAuthentication using Guice
-
Implement the AbstractModule and configure all your services for injection.

-
Bootstrap your application and provide an access to Injector class.

-
Use Injector object from the bootstrap stage and call injectMembers method for dependency injection.

Summary
We implemented factory pattern, dependency injection by hand and dependency injection by Guice ,and truly Guice gives you easy unit testing, minimal repetition and maximal flexibility and code maintainability.
In Part II, we will look at Dependency Injection with Guice in Spring.
Source code for this article can be downloaded at http://groups.google.com/group/jaccra/web/GuiceExample.zip. To run the application you will need:
- IDE (Netbeans/Eclipse or just Ant)
- Guice Library
Further Reading
http://code.google.com/p/google-guice/
May 18, 2009
JAccra : Introduction to Java Concurrency
Last week, we had the JAccra meeting and the topic for discussion was “Introduction to Java Concurrency”.
I talked on the following topics:
- Locks
- Atomic Variables
- Threads :ThreadPoolExecutor Implementation
The slides for the presentation can be downloaded at Java Concurrency and grab the Source Code
.
March 24, 2009
Ja.NET = Java + .Net
In development, sometimes interoperability becomes an inportant factor.One will think of Web Service to bridge the gap of platform dependency.How about Java and .Net running together in the (language) platform ecosystem. Ja.NET is what you will need to realize that.Check it out guys.
March 9, 2009
JACCRA :First Meeting
In a quest to aggregate Java developer in Ghana. Edem Morny, myself and other members started a group called JACCRA (pronounced JAK KRA). We had the first meeting on 7th March, 2009, which i talked on the J2SE specifically on
“WHAT’S NEW IN J2SE 5 ?” . This was an emphasis on the java programming language in general. For more information about JACCRA visit our page and if you are in Accra try register.
The topics discussed included Generics, Annotations, Auto-boxing, Enumeration and what these can do for you as a developer.
The presentation can be downloaded at J2SE 5.
The source code for the topics discussed can be downloaded :
1. Reflection
2. Annotation
3. Enumeration
December 15, 2008
Creating an order service application with JMS and JBoss Server 4.2.2 in Eclipse
Introduction
This is a simple J2EE application using JMS for asynchronous processing of
an order sented by a client. The message broker use is JBossMQ already in JBoss Server, so there is no other configuration required to develop a simple JMS application.
To follow this tutorial, you will need the following:
1. JBoss Server 4.2.2
2. Eclipse 3.3
Configuring the IDE with the JBoss Server.
In the right-bottom, Click on the Servers Tab, then Right click in the view and choose New > Server as shown below

Choose JBoss AS 4.2 and make sure the “Default” configuration is selected,then click on Next and Finish.

Finally, Click on the Run icon ![]()
Output:

Stop the server and lets continue to the actual development.
Creating the an EJB Project.
1. Select File > New > Project… , then Expand EJB and select EJB Project.

2.Click on Next and Set the
Project Name: OrderService as shown below.

3.Click on Next, then Finish

Finally the project with name OrderService is created.

Creating the messaging service
1.Create class “Order” with fields as shown below

2.Then create setter and getter methods for the fields.
3.Because the object Order will transmitted over the network it needs to serialised.
4.Next create another class “OrderProcessingMDB” as shown below.

This class needs a bit of explanation.This is a message driven bean
because of the annotation @MessageDriven.
The activation configuration properties will created by the server during
deployment.
How does this works?
This class is called by the server itself when a message arrives at queue point
with the destination name: “queue/OrderRequestQueue”.The class implements the MessageListener which has only one method called onMessage(Message m),which is called by the server when a message arrives.
To complete the Message Driven Bean
add this method which contains the logic to be processed on message arrived

The final project structure in Eclipse looks as shown below

Running the OrderService
Right click on the project node,then choose Run As > Run on Server.
This will make the project deployed to the server folder “default/deploy”

Developing the OrderService client
1.Goto File > New, then Project…
2.Under node J2EE, select Application Client Project and click on Next.

3.Set the Project Name: OrderClient. Make sure JBoss Runtime and Default Configuration for JBoss Runtime 4.2.2 is set correctly.
4.Now lets create the client class “OrderClient”.

5.Edit the class to reflect as shown below:

Walking through the code:
We use the javax.naming.Context interface to do a lookup for the Destination and the ConnectionFactory.In order for the client to know the connection to the JBossMQ the provider url is set in the Properties class.
In the @MessageDriven, there is destination name “queue/OrderRequestQueue”, this name is used
for the lookup of the destination.
ConnectionFactory by default can be look up by “ConnectionFactory”.
For further reading goto JBoss.org
Continuation of code:
6. Add the code below to what was done in Step 5.

Walking through the code:
Once we lookup for the destination and the connectionFactory.We can move on what is happening in step 6.In order for the client to send a message (message types: Object, btye, text) we ned a MessageProducer that is created from the session,which is in turn created from the Connection.
The steps iterated here is basically the same in sending a message to message borker in EJB pespertive.
Running the project:
Finally lets run the project:
1.Make the sure the JBoss Server is running.
2.Right click on the OrderClient node,choose Run As > Application
Turn to your server to see the output:

October 23, 2008
Happy Birthday Netbeans
It has a been a great experience for me in the pass three years and when i started building my skills with java and the IDE in mind of other developers was Netbeans. It has been a nice IDE for developers and the upcoming one. Bravo to the guys at Netbeans community for a wonderful IDE for the past ten years.
October 13, 2008
Fiji: Adobe Flex & JSF Integration
Guy, I am in a hurry to tell you that there is an integration library for adobe flex with JSF called Fiji. Yeah i know, you are happy too. But you know what, go get it by the horn and see what is in there for you.
This will trigger Edem Morny: =JBoss Seam Preacher and Selasie.
Blazeds: In search of Java & Adobe Flex Integration
I was trying to find a way of getting Java to work with Adobe Flex. Then I got LCD :Livecycle Data Services but it was not free sorry guys. In my search I made a discovery Blazeds,yes i did hahaaa.. Go grab it and give it a shoot.
Overview
BlazeDS is the server-based Java remoting and web messaging technology that enables developers to easily connect to back-end distributed data and push data in real-time to Adobe® Flex® and Adobe AIR™ applications for more responsive rich Internet application (RIA) experiences. (Source: Adobe)
October 5, 2008
Developing a simple organiser using CakePHP
Introduction
CakePHP is a PHP library for RAD using design patterns like
MVC, Association Data Mapping like Hibernate,JPA etc.
The organiser will consist of four basic functionality:
Notes, Tasks, Contacts and Appointments.Lets get organise now.
To follow the tutorial,you need the following software and resources.
1.Netbeans IDE ver 6.5- PHP module
2.JDK version 6 or 5
3.MySQL version 5.0
4.Xampplite
5.CakePHP
Setting up the resources downloaded
Install all the executables except CakePHP.
For more information on how to set up netbeans visit (www.netbeans.org)
Creating a PHP project with the IDE
1.Select File>New Project… ,PHP >PHP Application
Click Next.
2. Set the
Project name : Organiser
Change project location to where ever

Click Next
3.Run Configuration:
Set -:
Run As: Local Web Site (running on local web server)
Check :Copy files from Sources Folder to another location.
Copy to Folder : browse to the location of xampp installation and select “htdocs” folder
Setting the project up
Unzip the cake_1.2.0.7692-rc3.zip
Copy all the files in the unzip file to your project_directory/web

This is the structure for most application.
The project will be as shown:

Setting up the database for the project
In the IDE,under Source Files select file “database.php.default” as shown

Rename the file to “database.php”.
Now edit the database.php file as shown

This is the settings i used, change to reflect yours. (login ,password ,database,host).
Codes in the application
All the php source files will be placed in the structure as shown above.
Take a moment and look at the struture under /app (models, views, controllers => MVC design pattern)
All the models of the application will be placed under /app/models
All the views of the application will be placed under /app/views
All the controllers of the application will be placed under /app/controllers
Lets have the first cake:
Scripting the models:

The figure shows us the models we need to create.
Under Source Files>app/models, Right click and then select New> PHP file..
Set the filename: task
Check if the Folder is set to “web/app/models”
Edit the file to reflect as shown:

NB: CakePHP has a convention used to bake the application based on the filename and class name of the models.You must follow the naming as i have done to make it work.Go to http://www.cakephp.org for more information on the conventions.
Create the other three models as we did above.
Edit appointment.php to reflect

Edit note.php to reflect as shown below

Edit contact.php to reflect as shown below

What just happened?
We just created four models to reflect our tables in the database.
Each model inherits from AppModel.This is a class from CakePHP to help model the tables.
Since we are following the conventions, CakePHP will get all the attributes from the tables for each model for us.
Scripting the controllers:
Every model needs a controller and the basic CRUD will be generated at runtime for us via CakePHP scaffolds.
Under Source Files>app>controllers, Right click and select New>PHP file..
Set the filename: notes_controller
Make sure that the folder is set to web/app/controllers
Edit the file to reflect as shown below.

Create the other three controllers with filenames(contacts_controller, notes_controller, appointments_controller).
Edit the contacts_controller.php file to reflect as shown below

Edit the appointments_controller.php file to reflect as shown below

Edit the tasks_controller.php file to reflect as shown below

What just happened?
We just created four controllers to manage and control our models.Open any of the controller files
,you realise there are no methods.You only see two variables.
var $name=’Appointments’ for the appointment controller.
var $scaffold;
The variable declare above does the trick. This informs CakePHP that we want a basic CRUD operation and so should take care of it for us.
What CakePHP does is to bake a read, create, update, and delete operation for all the models that have the variable $scaffold declare.What we get is a well bake cake ready to be eaten.
Testing
Open your browser and Enter this url : http:localhost/Organiser/contacts

You will notice that the page has “CakePHP : The rapis application development” header.
Adding a home page for the application
Create an empty file with filename home.ctp into app/views/pages.That is where pages without controllers are kept.
Edit to as show below:
Changng the default layout
Create an empty file with filename default.ctp into app/views/layouts.
Edit the file to reflect:
Add a css to help in laying out the pages well.
Get the file stylesheet and create a css file with name stylesheet into folder /app/webroot/css.
Running the final project
After all this alteration the final page.

Summary
We just created a simple organiser in a short time.



