Trying out Eclipse Kepler

Yesterday I downloaded the new version of Eclipse IDE; Eclipse Kepler.


So far the experience is great. It is much faster than Juno. Main problem I had with Juno is that it is damn slow even with the updates provided later by eclipse to fix the slowness issue. But Kepler seems to have fixed all those issues properly.

Most notable improvement for me so far is the improved Eclipse Marketplace. Now we can select multiple plugins once and do the installation together.  Previously we had to install plugins one-by-one. And also conflict resolution related to plugin installation had been improved. Previously when there is a conflict, eclipse only tell that installation cannot be completed, but now it says exactly which plugin causes the problem and gives option to continue installation without it.

One problem I had was that there is no official support for Scala-IDE for Kepler. But managed to find a scala-ide build from scala-ide google group. It was working fine, so until official version is available I'll be able servile with it.
 

Alternative for Google Reader, Feedly

Google has announced that they are going to discontinue Google Reader from 1st of July 2013. It is really sad news since I use reader everyday to keep up-to-date with various blogs.

After seeing this news, first thing I wanted to do was to find and alternative. First one I came across is Feedly.  It is a web based reader and has plugins for chrome, firefox, safari and apps for android and iOS. Best thing with Feedly is that you can login with Google account and it will automatically import all your feeds from Reader. After getting used to the UI, it seems to be really nice tool. For the moment I'm going to use Feedly.

I'm also thinking about finding a good desktop reader as well. Since I'm using Linux there are lot of readers available. Have to try some and find a good one. 
 

Changing System Time in Xen VM

Yesterday I wanted to change the system time in one of our test servers. Server was a Xen VM running redhat linux.  I used date --set="20 Dec 2012 00:30:00" command, but when I checked the date again it showed the previous date not the date I've set using date command. 

After doing some searching I found out that by default VM clock is synchronized with its host Xen Server; so changing date is not possible. We need to disable this time synchronization before updating date in the VM. To do that I had to execute following command in VM linux console.
/sbin/sysctl -e xen.independent_wallclock=1

After that I was able to change the system time using date command.

 

Stubbing Asynchronous Http Services using Wiremock

Wiremock is simple library which can be used to stub Http Services. It is a handy tool when you want to write unit tests against some Http service(eg: Http REST web service). It has direct support for JUnit using @Rule feature. We can write unit tests to stub different http requests and verify those requests and responses very easily.

One weakness of wiremock was it only supported synchronous  http request mocking(i.e: You send the request and stubbed wiremock server send the response). But I wanted to mock Asynchronous REST server. In asynchronous scenario in addition direct http response, server is initiating another call back request to the server. This method is commonly used by web service operations which take lots of time to process. Flow of such operation would look like follows.

When a client want to get some operation done;
  1) Client sends the request to the server
  2) Server accept that request and give that accepted response as direct http response
  3) Server do the processing and send the actual result as a new http request to the client

This feature is implemented in my wiremock forked repository. Still this is not being merged to main wiremock repository. So if you are interested in this you'll have to clone my forked repo, build it locally and publish the jar file to your local maven repository.

Stubbing asynchronous request is easy.


In most asynchronous http services, some kind of Id is being used to map original request with the asynchronous response. There can be two ways of doing this.
  1) Server generates transactionId and sends it with the immediate response
  2) Client sends the transactionId and server echo that when it sends back asynchronous response

If you want to use the second method with wiremock, you can use setEchoField method.

At the moment there is a one limitation with this. You can only use this facility with JSON requests and responses.

You can find the complete test class at github.
 

Useful GIT Commands

This is collection of useful GIT commands I use in my day today work. Just posting here to keep as my own reference.

Revert changes made to your working copy:

git checkout .

Revert changes made to the index (i.e., that you have added):

git reset

Revert a change that you have committed, do this:

git revert ...

View commits not yet pushed to remote:

git log --branches --not --remotes

Not pushed most recent commit with branch name:

git log --branches --not --remotes --simplify-by-decoration --decorate --oneline

Difference between two branches:

git diff --stat --color master..branch 

Here the order of branch name is merge-target..merge-source.

Create remote tracking branch:

git branch --track feature1 origin/my-remote-branch

Here git fetch/pull from feature1 branch will get the updates from my-remote-branch

Create new local branch from remote branch:

git branch --no-track feature2 origin/master

Push local branch to remote repository:

git push origing feature2

Checkout specific file from differrent branch:

git checkout remote/branch path/to/file

 

Exclude resource files from jar file created with SBT

I use SBT to build my scala projects. In addition to scala source files I have some resource files in my project. Those are some configuration files and localization resource files.

When I build the jar file using SBT package command all of those resource files also get included in the jar file. When those resource files are included inside the jar, modifying them becomes difficult. I have to extract the jar file, edit them and create the jar file again.

Ideal way of handling resource files are not to include them in the jar file, rather keep them separately and include them in the class path when running the application. How to exclude them from SBT package task was the problem I had. After some googling I found out that this can be achieved by including mapping task in the build. Following is sample build file which uses mapping to exclude various types of resource files.


val excludeFileRegx = """(.*?)\.(properties|props|conf|dsl|txt|xml)$""".r

  lazy val myapp = Project(id = "myapp", base = file("myapp"),
    settings = baseSettings ++ Seq(
      name := "My App",
      mappings in (Compile, packageBin) ~= { (ms: Seq[(File, String)]) =>
        ms filter {
          case (file, toPath =>{
            val shouldExclude = excludeFileRegx.pattern.matcher(file.getName).matches
           // println("===========" + file + "  " + shouldExclude)
            !shouldExclude
          }
        }
      },
        libraryDependencies ++= Seq (dispatch),
      libraryDependencies ++= testDependencies))

 

Solving no-interface issue in Wireshark on Ubuntu 11.10

Recently I switched to Ubuntu 11.10 as OS in my development machine. Previously I was using Fedora 12. When I installed Wireshark in ubuntu and run it, wireshark didn't show any interfaces which can capture packets. I think it is because wireshark was not run as root user. In fedora when I start wireshark it  ask to run as root and get the password. In ubuntu it was not like that.
So after doing some googleing found a solution at a blog post by tavshed.

sudo groupadd wireshark
sudo usermod -a -G wireshark YOUR_USER_NAME
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 750 /usr/bin/dumpcap
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap
sudo getcap /usr/bin/dumpcap
sudo chmod +xs /usr/bin/dumpcap
 

Creating Java Command Line Applications using jLine

Most common way of writing a command line application in java is to use BufferedReader to read user input. But if you want more features like tab completion, you have to write code to handle it from the scratch in BufferedReader method.

There is a nice library called jLine http://jline.sourceforge.net/ which can be used to write nice CLI apps without much effort. It has out of the box support for;

  • Command History 
  • Tab completion 
  • Line editing 
  • Custom Key Bindings 
  • Character masking
I've written simple sample application on how to use jLine. You can check that out in github https://github.com/sandarenu/sample-apps/tree/master/java-cli-app
 

Another Tech Genius Passed Away...













Dennis Ritche, co-author of C programming language and one of the creators of Unix operating system has passed away last week. His inventions changed the way how we do things in the computer industry.
Thank You Dennis....

It is really sad to see that two of the great minds in technology world passed away in a month...
 

Remembering Steve Jobs

Steve Jobs, one of the greatest visionaries of our time has passed away on Wednesday(5th Oct 2011). He was 56.

At 2005 Stanford address quated on his own mortality, saying:
“Remembering that I’ll be dead soon is the most important tool I’ve ever encountered to help me make the big choices in life. Because almost everything — all external expectations, all pride, all fear of embarrassment or failure - these things just fall away in the face of death, leaving only what is truly important.

 

How to Use Multiple GIT Accounts

Yesterday at HMS we moved some of our projects to GIT from SVN repository. So in order to access that office GIT repository I had to create a new ssh key using my office email address. I already had a ssh key which I used to access my GitHub account. So once I created new key I was unable to access GitHub since in normal configuration ssh can only have one public key.
But I wanted to find a way which I can use both office GIT server login and my GitHub login. After doing some searching on google I found a solution to that. For that we'll have to create ssh config file. So I created following config file at my .ssh folder
vi ~/.ssh/config

# hms-git
Host hms-git
  HostName git.myoffice.com
  User git
  IdentityFile /home/sandarenu/.ssh/id_rsa

# github
Host github.com
  HostName github.com
  User git
  IdentityFile /home/sandarenu/.ssh/id_rsa_github


Once this config file is saved I could clone office git projects using
  git clone git@hms-git:myproject

To clone GitHub project I could use
  git clone git@github.com:sandarenu/task_tracker.git



 

Using SoapUI for Web Service Testing

SoapUI is a great tool which can be used to test web services. In this post I'll show you how to set up SoapUI project for WS testing.

Create new project from File ->New SoapUI Project, and in "New Project Dialog" give name and the location of the WSDL file.


You can find the sample wsdl file I used for testing in github https://github.com/sandarenu/soapui-test.

Web Service Mocking

Service mocking is required when you are writing a web service clients and either you don't have the web service implemented or you don't have the access to the real service. Here's how to do that using SoapUI.

Create Mock of the web service by right clicking on the project and selecting 'Generate MockService'.

Once mock service is created you can mock the operations by clicking on the small icon in the service mock dialog.




Double click on the operation you just mocked and you'll get a dialog to configure mock responses.
You can configure any number of mock responses and then select how those response to be dispatched when our mock service receives WS request. There are many dispatching strategies available such as "Script", "Sequence", "Query Match", etc...

When you do some integration testing you need your mock service to respond based on different parameters of the requests you send. In such cases you can either use "Script" or "Query Match" dispatch mechanism.

Web Service Testing
We can use SoapUI to create test cases to test a Web Service we developed. To create a test suite right click on the project and select "New TestSuite" from the context menu. Then right click on the test suite we just created and select "New TestCase".



Once we have created the test case we can add test steps to it. Lets add a test step to test time for time zone 'lk'. Right click on the test steps dialog and select "Test Request" and create the request you want to test.
Once test request is created we can add assertion step to it. Click the "Assertions" button at the bottom of the test request editor.


Right click on the assertion editor and add "xPath Match"

 Put //Time as "xQuery Expression" and put value you are expecting as "Expected Result"

Now you can test this request using the Mock WS you created before. Start the mock service by clicking on the small green arrow found in the top of the "TimeServiceMock". Then open the test case you created above by double clicking it. Then run the test case again by clicking small green arrow in the to of the dialog box.


If every thing went well you'll be able get success result for your test case.

This is just the start. You can then add more test cases and assertions and improve your test suite. SoapUI site has good documentation on how to do functional testing. http://www.soapui.org/Getting-Started/functional-testing.html
You can refer that and get more information.

You can also find the SoapUI project for this example at https://github.com/sandarenu/soapui-test. Download it and open it using "File -> Import Project"

Happy Testing.....
 

Solving 'symbol lookup error' in new Eclipse 3.7 Indigo in Fedora

When I downloaded and run the latest eclipse 3.7 Indigo release, it crashed with a strange error.

symbol lookup error: /usr/lib/libwebkit-1.0.so.2: undefined symbol: soup_content_decoder_get_type

After some searching in the web I found out that it is due that I'm having an older version of libsoup library. I had libsoup-2.28.1-1.fc12.i686 in my fedora 12.
Issue was solved by updating that library using yum.
yum update libsoup

 

Hibernate HQL Select Query Based on Item in a One-to-Many Relationship

Few days back I had a situation where I have to fetch some data based on a value of an item stored in one-to-many relationship. I my project I had two classes like below. I had to fetch all the Oders that has particular item.

class Order {
    private List<Item> items;
    //more code ...
}

class Item {
    private String itemId;
    private String name;
    //more code ...
}

With native SQL it was not that difficult, but since I've used Hibernate I wanted to find a way to do it in HQL. After some searching in Google I found out how to do it.

select o from Order o left join o.items its where its.itemId = :itemId
 

Different views for your blog

Recently Blogger started providing different views for the blogs created in it. They are very cute way to view our blogs in different angles. Following is snapshot view of my blog.
 

After Long Time.......

Its being long time since my last post......
Hopping to do some posts more frequently in coming months. So keep your fingers crossed :)
 

MySql - Ms Sql Server Database Portability

Few weeks back I was given a task to migrate a Java application using MySQL as its database to MS SQL Server. It was actually not a full migration, rather application should be able work with both MySQL and MS SQL Server. Since we had used Hibernate in our application I thought it will be a easy thing. But when I dig deep in to the application and started migration I was proved to be wrong. I encounter lot of issues and hence thought of documenting them so any one can get some help. Major headache was the difference in some direct SQL commands used in the application. Following are the list of issues I encounter and how I solved them.


1. JDBC Driver Issue with the Microsoft JDBC Driver
    First thought of using JDBC driver provided by Microsoft for MS SQL Server 2005.  But when I ran the test cases I got few issues with it.
    1). Gave execption "org.hibernate.MappingException: No Dialect mapping for JDBC type: -9 "
        After some investigation found out that it was due to NVARCHAR datatype. There is another opensource driver called jtds. Which is more actively developed as well. With that driver I did not get that MappingException.

2. Data type differences
    For queries like "SELECT count(id) From myTable", MySql JDBC driver returns datatype java.math.BigInteger and with MsSQL jtds JDBC driver we get datatype java.lang.Integer. So I had to convert it to string first and then convert to long
    String countStr = summaryRow[1] == null? "0": String.valueOf(summaryRow[1]);
    long count = Long.parseLong(countStr);

3. Nullable Unique columns not supported in MsSql Server. In Sql Server NULL is taken as a value and hence can't have multiple rows with null, but in MySql we can have Nullable unique columns. So when table schema is generated through Hibernate we have to execute additional update query to perform a workaround for this issue. Here we add new column called 'my-table_id_add' to the table.

declare unique_key_list_my-table cursor for
    select OBJECT_NAME(OBJECT_ID)
    FROM sys.objects
    WHERE type_desc LIKE '%CONSTRAINT' and OBJECT_NAME(OBJECT_ID) LIKE 'UQ__my-table%'

OPEN unique_key_list_my-table
    FETCH NEXT FROM unique_key_list_my-table INTO @keyName
    set @RowNum = 0
    WHILE @@FETCH_STATUS = 0
    BEGIN
      set @RowNum = @RowNum + 1
      print cast(@RowNum as char(1)) + ' ' + @keyName
      if not @keyName is null
      begin
           select @sql = 'ALTER TABLE [my-table] DROP CONSTRAINT [' + @keyName + ']'
           execute sp_executesql @sql
      end
        FETCH NEXT FROM unique_key_list_my-table INTO @keyName
    END
CLOSE unique_key_list_my-table
DEALLOCATE unique_key_list_my-table

ALTER TABLE my-table ADD my-table_id_add AS (CASE WHEN my-table_id IS NULL THEN CAST(id AS VARCHAR(30)) ELSE my-table_id END);
ALTER TABLE my-table ADD CONSTRAINT UQ__my-table_my-table_id UNIQUE(my-table_id_add);


4. TIMESTAMP in MS Sql server is not functionally same as MySQL. Have to use GETDATE() function for that.
    e.g: Create table timestamp_test DATETIME default(GETDATE())

5. In Ms Sql Server we can't use 'user' as table/field name, but that is possible in MySql.

6. LIMIT is not supported in Ms Sql Server and there is no easy way if we want go select some range of data. In MySql we use "SELECT * FROM my-table LIMIT 10, 20" to select records from 10 to 20, but in Ms Sql Server we have to use something like "SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY id) as row FROM my-table ) a WHERE row > 10 and row <= 20"
 

Integrating YUI Compressor to your Maven Web Project

Size of the javascript files and css files in a website has a big affect on its performance. Especially in slow connections lot of time will be spent on downloading those files. We recently came across such situation. Our website was using jQuery and some other javascipt libraries. Some of them were more than 100kb. Sometimes it took more than 10 seconds to load a page. When we view the loading statistics using developer-tools in Chrome it showed that browser had downloaded more than 500kb of scripts and css.

One way to reduce this script size is to compress them. Our script files contained lot of comments, licencing statements, nice formattings, long meaningful variable names etc... which increases the file size. YUI Compressor is a free utility by Yahoo which can be use to compress our scripts by removing above mentioned things. It is jar file and you can use that and compress file by file.

But that is not very practicle to keep those minimized scripts in develepment since after minimizing you won't be able read them and edit them when needed. Best method is to do the minization when you create the war file. Luckly there is a maven plugin for this. Following is the plugin component you can add to your maven pom.


<plugins>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>
${project.build.directory}/minimized
</directory>
<targetPath>/</targetPath>
<filtering>false</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>0.7.1</version>
<executions>
<execution>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<webappDirectory>
${project.build.directory}/minimized
</webappDirectory>
<nosuffix>true</nosuffix>
</configuration>
</plugin>
....
</plugins>



When you run mnv clean package YUI compressor will compress the scripts and put them in target/minimized folder and those will be used to create the war file.
 

8 Stages of Programming a New Feature

This is a really nice comic I came across few days back. It is really nice and explains every thing what we generally going through as software engineers.....




 

Happy New Year 2010


Wish You All a Very Happy and Prosperous 
New Year!
May All Your Dreams Come True in This Wonderful Year...