Tuesday, June 17, 2008

Spring : Inversion of Control (IoC) / Dependency Injection (DI)

We have heard a lot about Spring in recent years. Specially it is one of an indespensable tool widely employed by Java developers. It has a kind of defacto standard as a container. Though Spring has lot of features to delve into, I am specially talking about the inversion of control and dependency injection in this blog. Here I am trying to define inversion of control/ dependency injection because what I found over the years is that eventhough developers are using IOC and DI they are little aware of what actually they mean. Though nobody needs a precise definition to use the features, I believe understanding them would be better.

Dependency Injection (DI) and Inversion of Control (IoC) are interchangable terms but I am going to define them from Injection and Control perspective. For example look at the class below:-


public class UserManager {
private UserDao userDao;

// code involving useDao

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

}


Lets assume that userDao is a spring bean injected into UserManager (I am assuming some familiarity with spring because I just want to make concepts of DI and IoC clear). Now interpret it this way: UserManager is dependent on UserDao for some operation> In traditional programming we might create the UserDao instance for use in UserManager like this UserDao userDao=new UserDao() but not as such with Spring. The UserManager is not responsible for creating the dependency that it requires itself but rather depends on Spring to inject its dependency.
Hence the name Dependency Injection.

Now for IoC, see it this way: the control of creating the object necessary for UserManager has been transferred to Spring. The control that traditional objects use to have has been lost and control has inversed. The owner uses the object created by something else. Hence the name Inversion of Control.

Monday, June 16, 2008

Setting up SVN+SSH Protocol in Eclipse

In order to be able to use SVN+SSH with Eclipse Subversion plugin you need to do the following:-

1. First open following file named config in the text editor. The config file is located in the user's Application Data\Subversion directory.

For example if the user for which the subversion client is installed is svnuser then the file path should be:-

C:\Documents and Settings\svnuser\Application Data\Subversion\config

2. In the file locate commented #ssh and uncomment it and set the path to the path of TortoisePlink.exe (you need to have TortoiseSVN installed). You can either set the path as an environment variable or directly put the path in the config file. If you put the path in environment variable you need to point to the variable in config file like as shown:-

ssh= $SVN_SSH ssh

where $SVN_SSH is the environment variable name

If you want to directly point to the executable in the config file do it as follows

ssh=E:\\TortoiseSVN\\bin\\TortoisePlink.exe

TortoiseSVN is not the only option. Actually, we can use any ssh connection tool that supports same command line parameters as the SSH executable available in Unix.

To sum it all here is an extract from the config file
### as:

#ssh = $SVN_SSH ssh
ssh=E:\\TortoiseSVN\\bin\\TortoisePlink.exe
### If you wanted to define a new 'rsh' scheme, to be used with
### 'svn+rsh:' URLs, you could do so as follows:



This is what we need to do at the config file to start using svn+ssh. But sometimes I have found that eclipse plugin continuously keeps on asking for password. This occurs when the the SVN client used in eclipse is Default Native Java HL . So change it to Java SVN.

If you don't know where those changes are to be made follow following detailed steps:-
a. In Eclipse go to windows menu.
b. Click Preferences and expand Team option.
c. After expanding Team you will find SVN option click on it.
d. On the right side you will find the SVN client setting. Select the option as stated above and restart the eclipse (though restart might not always be necessary).

Wednesday, February 13, 2008

Setting up SSL in web application and tomcat

In order to setup SSL in java web application we need to follow following steps:-

1. First of all generate a certificate by using keytool. Keytool comes with java and is located in bin directory of the JRE. If java has been configured properly it should be directly available via command prompt. Here is an example of its uses.

keytool -genkey -keypass password-storepass password-keystore E:\ks.bin

This will generate a certificate named ks.bin which can be used in our web application. When we hit enter on this command it will asks several questions . The questions asked are as follows:-

What is your first name and last name?
What is the name of your organizational unit?
What is the name of your organization?
What is the name of your city or locality?
What is the name of your state or province?
What is the two letter country code for this unit?

After answering all these questions it will ask you to confirm it. Once you confirm it the certificate file will be generated.

2. Configure the tomcat server to use the certificate. In order to configure it open the server.xml file that is located in conf directory of tomcat. Find a SSLEnabled connector. If you haven’t been using SSL before then an example connector is commented in the server.xml file. Uncomment it and make changes as follows or simply use the connector below.

<Connector SSLEnabled="true" clientAuth="false" keystoreFile="e:\\ks.bin" keystorePass="password" maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLS"/>

Actually, it’s better to put keystore file in tomcat directory and use a relative reference than an absolute reference.

3. Modify web.xml to incorporate SSL in the web application. A sample is given below

<security-constraint>
<web-resource-collection>
<web-resource-name>securedapp</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>

url-pattern can be modified to limit the pages that are to be SSL secured. In the above example all the pages are SSL secured.