Archive

Posts Tagged ‘Java’

What Java Look and Feel do you use?

November 9th, 2009 James 3 comments

Quite some time back, I read an article titled “20+ Free Look and Feel Libraries For Java Swing“. I have evaluated most of the libraries mentioned in that article. Personally, I  prefer the System look and feel that is bundled in the JRE. However, I like Substance, PGS and JGoodies as well. Which one do you use? Curious to know.

What Java Look and Feel do you use?

View Results

Loading ... Loading ...
 
Categories: Java, Polls Tags: , , ,

Developing A Simple Java Application With Spring

November 4th, 2009 James 14 comments

Introduction
Spring is a powerful application framework that can be used across any layer in your application. For example, you can use Spring to manage only your data access layer or you can use Spring to provide remote services for your swing client. In this article, I will explain how to get started with Spring by developing a simple java application.

Requirements
1. Your favorite IDE
2. Latest Spring framework.

(Note: This article makes use of Spring framework 2.5.6 which is the current production release)

The Application
We are going to develop a simple application that fetches and display the list of registered users. The application consists of just two interfaces, their implementation.

The DAO layer
Let us now develop the DAO layer of the application. This layer consists of just one interface “UserDao” and it’s implementation “UserDaoImpl”. The interface consists of just one method named “getUsers()”. Let us quickly develop them.

Listing 1. UserDao

package com.springapp.dao;
import java.util.Iterator;

/**
 *
 * @author James
 */
public interface UserDao
{
    Iterator<String> getUsers();
}

Listing 2. UserDaoImpl


package com.springapp.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author James
 */
public class UserDaoImpl implements UserDao
{
    public Iterator<String> getUsers()
    {
        List<String> users = new ArrayList<String>();
        users.add("Gavin King");
        users.add("Geertjan");
        users.add("Mike Keith");
        users.add("James");
        return users.iterator();
    }
}

Nothing fancy here. The implementation is pretty straight forward, though in a real environment you might fetch those details from the database using ORM frameworks like JPA.

The Service Layer

We will encapsulate the service layer from the dao implementaion by writing code against the interface UserDao. We will follow the same pattern we used in developing the dao layer and write the interface UserService and it’s implementation UserServiceImpl.

Listing 3. UserService


package com.springapp.service;

import java.util.Iterator;

/**
 *
 * @author James
 */
public interface UserService
{
    Iterator<String> getUsers();
}

Listing 4. UserServiceImpl


package com.springapp.service;

import com.springapp.dao.UserDao;
import java.util.Iterator;

/**
 *
 * @author James
 */
public class UserServiceImpl implements UserService
{
    private UserDao userDao;

    public Iterator<String> getUsers()
    {
        return userDao.getUsers();
    }

    public void setUserDao(UserDao userDao)
    {
        this.userDao = userDao;
    }
}

Kindly notice that both the service layer classes and dao layer classes are Spring agnostic. That’s the beauty of Spring. Spring is less invasive. Most of the application code can be developed without knowing anything about Spring.

The Client

Now we need someone to make use of our service layer and we will waste no time in developing the client for our service. We can just write a junit test class to invoke our service but I prefer to write a simple java class to be the client. Though our client will be a simple POJO class, you can easily replace it with a Swing or Web front end. Enough talking, let us dive into action! We will first develop our client as a normal java class without using Spring.

Standard Client

Listing 5. StandardUserServiceClient


package com.springapp;

import com.springapp.dao.UserDao;
import com.springapp.dao.UserDaoImpl;
import com.springapp.service.UserServiceImpl;
import java.util.Iterator;

/**
 *
 * @author James
 */
public class StandardUserServiceClient
{

    private UserServiceImpl userService;

    public StandardUserServiceClient()
    {
        userService = new UserServiceImpl();
        UserDao userDao = new UserDaoImpl();
        userService.setUserDao(userDao);
    }

    private void fetchUsers()
    {
        Iterator<String> users = userService.getUsers();
        while (users.hasNext())
        {
            System.out.println(users.next());
        }
    }

    public static void main(String[] args)
    {
        StandardUserServiceClient client = new StandardUserServiceClient();
        client.fetchUsers();
    }
}

Hmm, there you can see our client code is “coupled” tightly with the service and dao implementations. That’s where Spring comes to our rescue. Spring will dynamically “inject” the implemenations so our application will remain “loosely” coupled.

Spring Client

But how will Spring know about our implementations? We need to inform Spring a little about our application and define the “hotspots” where it can dynamically “inject” the dependencies or implementations. Spring expects these details in a configuration file and let us quickly write that. Create a package called “resources” and create a xml file called “applicationContext.xm” inside it.

Listing 6. applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="userDao" class="com.springapp.dao.UserDaoImpl">
 </bean>

 <bean id="userService" class="com.springapp.service.UserServiceImpl">
     <property name="userDao" ref="userDao"/>
 </bean>
</beans>

In the “applicationContext.xml” file we defiend two beans named “userDao” and “userService”. We also specified their implementations using the “class” attribute. Also notice that we are setting the property “userDao”  in the “UserServiceImpl” class with “UserDaoImpl” by referencing it’s name “userDao”. Pretty straighforward!

Now it’s time to churn out our Spring client.

Listing 7. SpringUserServiceClient


package com.springapp;

import com.springapp.service.UserService;
import java.util.Iterator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author James
 */
public class SpringUserServiceClient
{
    private UserService userService;

    public SpringUserServiceClient()
    {
        //initialize the spring container
        ApplicationContext context = new ClassPathXmlApplicationContext("resources/applicationContext.xml");
        userService = (UserService) context.getBean("userService");
    }

    private void fetchUsers()
    {
        Iterator<String> users = userService.getUsers();
        while(users.hasNext())
        {
            System.out.println(users.next());
        }
    }

    public static void main(String[] args)
    {
        SpringUserServiceClient client = new SpringUserServiceClient();
        client.fetchUsers();
    }
}

A couple of details about this class:

  • The ApplicationContext interface helps us to plug into the Spring container and lookup the classes we need by using the name we defined in the xml file.
  • There are many implementataions of ApplicationContext available in Spring. One of the most widely used implementation is “ClassPathXmlApplicationContext”. It is used to load Spring configuration files found in classpath.

As seen from Listing 7, Spring helps us to program to interfaces and develop loosely coupled applications. As a result, applications become more testable as there are no external dependencies like application server. It is also very easy to switch the implementations effortlessly. For example, in our sample application you can easily write a UserDaoMockImpl and use it in your test cases effortlessly.

Project Structure

Here is the complete application looks like:

screenshot1

 
Categories: Java Tags: ,

Developing A Simple Pluggable Java Application

September 20th, 2009 James 13 comments

Most of the applications we use on daily basis are pluggable. Popular applications like Firefox, Eclipse, NetBeans, JEdit, Wordpress, Hudson are all pluggable. In fact, pluggability has played a major part in the success of most of these applications. Why not make the Java applications we develop pluggable as well? Yes, we get pluggability out of the box, if our applications are based on a rich client platform like NetBeans or Eclipse. But for some reasons if you decide not to use those platforms, it doesn’t mean that they should not be pluggable. In this article, we will learn how to write a simple pluggable application that will load it’s plugins dynamically.

The API
First, let us define a plugin interface that should be implemented by all the plugins of our application. We are going to keep it very simple. Create a project called “plugin-api” in your favorite IDE and create the interface “ApplicationPlugin”.


package com.pluggableapp.plugins.api;

public interface ApplicationPlugin
{
    String getName();
    void init();
}

Read more…

 

Five different uses of Java Applets

August 22nd, 2009 James 2 comments

In a world where everyone is using technologies like Flash, Silverlight etc to present rich content, are Java Applets still used? Are they still relevant? The answer is – “Yes”. Apart from being used primarily for playing online games, Java Applets are still used in many different ways. Here I would like to highlight a few applications that put Applets to good use.

1. Online Office Suite
ThinkFree is a very popular and professional online office suite based on Java Applet and Ajax.

2. Virtualization
JPC or Java PC Emulator is a pure java based virtualization software that can be used to boot your virtual computers right inside the browser. Yes they run as “Java Applets” inside the browser.

3. Remote Desktop Viewer
Products like TightVNC and UltraVNC provide a java applet based client to view remote desktops.

4. (Web) Operating System
iCloud, is an web operating sytem that makes use of XML and Java Applet Technology (atleast for Firefox).

5. File Upload

Facebook uses a beautifully designed Java Applet to upload photos to facebook photo albums.
net2ftp is a web based ftp client that makes use of tiny Java Applet to “drag and drop” files from local computer to the browser and upload them to the remote ftp server.

If you have come across any website that make use of Java Applets, please share your thoughts in the comments.

 

Hudson CI Server – A quick start guide

July 24th, 2009 James 7 comments

Introduction

Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day.
- Martin Fowler

Hudson is a popular open-source continuous integration server used by many organizations like Redhat JBoss. Though there are many well known and well established open-source projects like CruiseControl, Continnum and some commercial offerings like Bamboo, what makes Hudson special is it’s powerful yet easy to use web interface, it’s simplicity and it’s extensible architecture with many plugins.

Read more…

 

NetBeans 6.7 – A quick glance

July 7th, 2009 James 6 comments

NetBeans 5.0 – Simplified Swing development
NetBeans 5.5 – Simplified Java EE development
NetBeans 6.0 – Made the NetBeans editor and other core infrastructure on par with competitors
NetBeans 6.5 – Looked beyond Java development by supporting languages like PHP
NetBeans 7.0 6.7 – Tries to make collaborative team development seamless.

I was quick to download the “All Java” pack of NetBeans IDE for linux. Installation, as usual was pretty smooth on my Ubuntu 9.04. The installation didn’t give me much surprises and it was very much similar to version 6.5. I customized the installer to install Glassfish v2.1 and Tomcat 6.0.18 for me.

Read more…

 

Ubuntu 8.10 – A Productive Java Development Environment

December 13th, 2008 James 18 comments

I recently started using Ubuntu 8.10 at my workplace as well. Till then, I have been using Ubuntu only at home. For me, Ubuntu@Work was very different from Ubuntu@Home. I mostly surf, blog, listen to music and play some games at home. But Ubuntu@Work was a completely different scenario.

Since I’m new to this linux stuff, it took me some time to configure things like static ip address, host names etc. But once everything was setup, things started moving quickly. I initially had doubt in my minds about the font rendering of NetBeans (or any swing app for that matter) under linux. I even wrote an post showing my frustration with NetBeans font rendering when compared to Eclipse. But with jdk.1.6.10, font rendering is smooth and NetBeans works like a champ! You can see some samples here:

Read more…

 

Subversion and NetBeans – A quick start guide

April 7th, 2008 James 22 comments

Introduction:

Subversion is arguably the most popular version control system as of now. No wonder NetBeans has very good support for Subversion. I personally feel that a java developer must be familiar with both these tools. This article shall help you to get started with both these tools.

Objectives:

- To create a simple java project in NetBeans.

- To import the java project into the subversion repository.

- To commit the changes made in a java source file.

- To view the revision history of a java source file which was changed.

- To rollback to the previous revision of the java source file.

Read more…

 

Must have tools for a Java Developer

March 19th, 2008 James 23 comments

Apart from your favourite IDE, I feel, a Java Developer might be very productive with the following tools (in no particular order):

- Firefox (Do I need to say anything about it?)

- Apache Ant (Not needed, if you use NetBeans. NetBeans has got bundled ant)

- JEdit (Mainly for it’s wide range of plugins. I use it’s LogViewer and HexViewer plugin frequently. Also it has got excellent syntax highlighting for your properties file, java files, nsis scripts etc)

- Subversion Version Control System(Got excellent integration with NetBeans and Eclipse. You must consider it atleast for your personal development.) You can read more about installing subversion here.

Read more…

 

Connecting to a database from a java web application

March 12th, 2008 James 8 comments

In these days of numerous java frameworks, we often forget or don’t care about some simple things. Though this post might not be very interesting to most of you, it might help some of those to whom this might be the information they are looking for. So bear with me.

Pre-requisites:

- Latest version of Tomcat (currently 6.0.16)

- A database :-) (In my case, it’s mysql 5.0)

- Appropriate jdbc “driver” jar file for your database. (In my case, it’s mysql jdbc driverr)

Read more…

 
Categories: Java Tags: ,