Bidvertiser

Wednesday, March 12, 2014

How to find IP address in mac

How to find internal IP address in mac
1.  Click on the apple icon on the upper left corner.


2.  Click on System Preferences.
3.  Click on Network under Internet & Wireless.
4.  Your IP address will be listed next to your connection status.



Monday, March 10, 2014

Getting started with RESTful webservice using Jersey

Jersey helps developing RESTful webservices seamlessly and makes your life easy. This article gives you a quick start on how to create your first RESTful web service using Jersey & Maven.  I assume you have a decent understanding of maven and J2EE.

Make sure you have
  1. Eclipse Kepler
  2. M2Eclipse plugin
  3. Java 1.7
  4. Tomcat 7
Step 1 - Maven Project
Create a new maven project from your eclipse.  Select maven-archetype-webapp archetype,  provide group id, artifact id, package in the following dialog. (See screenshot below)


Note: By default installed facets will not be Java 1.7, Dynamic web module 2.4.  You can edit this in org.eclipse.wst.common.project.facet.core.xml under .settings folder.  Also make sure to change the JRE from 1.5 to 1.7 in your project build path.

Step 2 - Maven Dependencies
Add the following jersey server and jersey servlet dependencies to your pom.xml
Step 3 - Deployment Descriptor
Add the Jersey servlet container to your web.xml.
Step 4 - REST Resource
Create your package and SampleRestResource class.  Add the path "/sample" at the class level, and add 2 GET resources. One with path "/hello" and the other with "/hello/{message}".  
Step 5 - Run your application
Right click on your project --> Run As --> Run on Server --> Select tomcat.  Now your first resource can be accessed from http://localhost:8080/jersey-rest-helloworld/jersey/sample/hello and the other resource from http://localhost:8080/jersey-rest-helloworld/jersey/sample/hello/your%20message

You can download the complete source here.

Monday, January 20, 2014

Why Singleton design pattern is still a good interview question to Java developers

Why every interviewer asks about singleton design pattern?
Well the answer could be either,
  1. That's what the interviewer knows to ask for.
  2. Or its a good way to start and branch into several other discussions.
  3. Or its one of the simplest design pattern and a controversial one in terms of usage.
What ever the reason may be, we better know it.

What is Singleton
Singleton is categorized under "Creational design pattern" and part of Gang of Four design pattern.  Singleton is a design pattern which suggests only one instance of a class for the entire application at any condition. There are several ways to achieve this, and be assured that most of answers you say may not convince the interviewer and he can over rule the solution.

Ways to do it
Before getting into the details are there any examples of Singleton to be used in real scenarios?  Yes, java Runtime, Logger, Service locators of JEE pattern follows singleton.  Now, we shall look into the most common ways to implement Singleton.

Singleton - Eager Initialization Example
As the name states in this approach the instance is "eagerly" created before it is expected/asked for.  It means the class will be initialized during the application startup.
In this example the interviewer would be interested in asking, What if the instance of this class is not required at all.

Yes it is a drawback that the instance is created even if it is not required. And that would be an issue only if this object is large, or else it is the best to leave it as is.  Lets say the interviewer says,  it is a large object and I do not want it to be eager loaded.  Well the next section describes you how you could write Singleton with lazy initialization.

Singleton - Lazy Initialization Example
Lazy loading is an interesting topic in programming, it means creating the object in a delayed(lazy) way. Generally any expensive process could adapt lazy style. To make our singleton lazy we have initialize the instance variable only when it is expected.
You could see the instance variable is assigned with the object when it is null and then on the same variable is reused. Take a closer look into line 10, it ensures thread safety.  If you are just writing just the pseudo code, there could be a question what would you synchronize as you haven't initialized the object yet. We synchronize the block with the class here.

Next is the volatile keyword for the instance variable, do we need that? If so why.
Yes it is absolutely required or you may run in into out of order write error scenario. ie. The reference is returned even before the object is constructed (in other words the memory is allocated but the constructor code isn't finished execution). Also volatile keyword helps as the concurrency control tool in a multi-threaded application.

Singleton - Enum way Example
Easiest of the ways, by default Enums are thread safe and ensures single instance.
For an interview I would not suggest this approach,  since it would finish up the question pretty quick.  You don't want to do that,  better hold the interviewer on the topic you know well. In terms of implementation I would suggestion the Enum way or the one that is discussed in the next section.

Singleton - Bill pugh way Example
William Pugh co-author of findbugs static code analyzing tool, on his research suggested the following way while implementing singleton.
You could witness  SingletonHolder, SingletonBillPughExample will not be initialized until getInstance() is invoked.

Friday, December 20, 2013

Spring MVC 3 + Spring Security + Hibernate 4 tutorial

This tutorial helps you to create a spring mvc application integrated with hibernate 4 and Spring security. Before continuing read through my previous article on Spring MVC 3 - Hello world example

The tutorial uses MySQL as database and source code enhanced from the previous Hello world example.

Changes to your pom
Add/modify the following to the properties section of your pom.xml. The spring version is increased to 3.2.3 to support transactions.
Add the following to the dependencies section

Now we are set to use hibernate and spring security in your application.

Create file application-security.xml inside your webapp >> WEB-INF >> spring folder and paste this content.
Replace the following to your servlet-context.xml. The changes include adding necessary namespaces and made beans as default namespace for convenience.
Create a new file controllers.xml Now all the configurations are done.  Create your controllers and views, I have not explained the views, web.xml for simplicity. The complete sample can be found here in github
Create User.java with the following content, make sure your test database has an user table with fields that matches the properties in the below
Create UserController.java with the following content
Create UserDAO.java with the following content
Download the complete code in github

Friday, November 15, 2013

Spring MVC 3 - hello world example - Quick start

This tutorial helps you to quickly create and run a spring-mvc hello world application.  Spring version used for this example is 3.2.3-RELEASE.

Prerequisites
  1. JDK 1.6+
  2. Eclipse Juno+
  3. Spring IDE 3.4+ for eclipse
  4. Maven 3
  5. Maven Integration plugin for eclipse
  6. Tomcat 7+
Initial Steps
Right click and create New --> Spring project.  In the next dialog, fill in the project name as "spring-mvc-spring-sample" and select "Spring MVC Project".  Please specify package name "com.ananth.spring" and click Finish.


Run your application
Right click on your eclipse project --> Run As --> Run on server --> Finish.
From your browser navigate to http://localhost:8080/spring/ you will see this screen.

The source can be download from github. Next chapter Spring MVC with Spring security & Hibernate.