Wednesday, August 10, 2011

Hibernate Interceptors

Are you using hibernate and thinking of using Database Triggers? Then wait....

If most of your database operations are done via stored procedures then having triggers in place is the only choice to intercept any DML calls.

But since we are using hibernate we need a more object oriented way of handling things.
Hibernate interceptors is similar to triggers in database. Monitoring DML operations via triggers is the most efficient way, but it won't be easy to maintain, understand and manipulate business logic when all database operations are done via hibernate.

Coming straight to the point, hibernate provides "org.hibernate.Interceptor" interface to have the business logic to perform pre or post operations around DML hibernate calls. Following methods are required to be implemented against "org.hibernate.Interceptor".

public interface Interceptor {

public void afterTransactionBegin(Transaction arg0) {

}

public void afterTransactionCompletion(Transaction arg0) {

}

public void beforeTransactionCompletion(Transaction arg0) {

}

public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) {
return null;
}

public Object getEntity(String arg0, Serializable arg1)
throws CallbackException {
return null;
}

public String getEntityName(Object arg0) throws CallbackException {
return null;
}

public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
throws CallbackException {
return null;
}

public Boolean isTransient(Object arg0) {
return null;
}

public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
}

public boolean onFlushDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) throws CallbackException {
return false;
}

public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
return false;
}

public boolean onSave(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
return false;
}

public void postFlush(Iterator arg0) throws CallbackException {

}

public void preFlush(Iterator arg0) throws CallbackException {

}
}
Since you won't be interested in implementing all of the methods, hibernate provides "org.hibernate.EmptyInterceptor" class which has empty implementation of the Interceptor interface. Now, as per your need you can override any of the desired method. Below is the sample class which I plugged in hibernate configuration to print out the primary key of any object which is getting saved.

package com.test.interceptor;

import java.io.Serializable;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

public class InsertMonitorInterceptor extends EmptyInterceptor{

@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
System.out.println("Saving..." + id + ": " entity);
return super.onSave(entity, id, state, propertyNames, types);
}
}
Once you have your Interceptor class implementation ready it can be very easily plugged with any existing hibernate configuration as shown below.


new Configuration().configure(DBUtil.class.getResource("hibernate.cfg.xml"))
.setInterceptor(new InsertMonitorInterceptor())
.buildSessionFactory();
That's it. You can have different interceptor implementation plug it in to start using them.
Hope this article will be helpful. happy Intercepting.

Thanks.

Saturday, September 25, 2010

Blu Ray Player

!!! WOW !!!
That was the first expression when I played a Blu ray disc on my BDP-S370 connected to Bravia LCD.
It’s a feature packed Blu Ray HD player which comes at a decent price. I know the "Sony" and "Price" can't be decent but then it's worth.

Starting with the list of features
  1. Audio/Video formats: It supports a lot of audio and video format including MKV and MT2S which are basically high definition video format files. Apart from this it also supports DivX HD which is pretty common.
  2. USB Connectivity: It has got two USB ports. One can put up all A/V media and stills onto a USB drive and can enjoy them on a big screen.
  3. LAN Connectivity: I connected Blu Ray PLayer and my WB 2 TB disk via switch. And my player detects the Media Server (twonky) running on my WD Disk(DLNA Capability of the player) which hosts all my Audio, Video and Stills.
  4. Wireless LAN: It also supports wireless LAN but then you have to purchase Sony's wireless USB stick separately. It will gonna cost you around Rs. 4500/-. It's a big disappointment. Also it will not support any other wireless USB adapter.
  5. Misc: This player is can access Gracenote server and BD Live enabled. So if your disk player is connected to internet you can definitely make use of this feature. Also it can stream video from popular video sites like www.youtube.com. Well there couple of more sites listed from which videos can be streamed.
  6. Pretty good customer support just in case if you run into any issue (highly unlikely with Sony :) ).
Well those are the good points.
MKV File Problem
I had some issues with playing MKV files larger than 4 GB with Twonky media sever; I complained about this at Sony’s customer support. They kept me informed for two weeks, after that I didn’t heared from them. I guess they were not able to resolve the problem. Well I did some RnD on playing MKV files and here are some of my findings to play those on Sony’s BDP-S370.
Solution
When talking about USB support on BDP-S370, it only supports USB drives with FAT32 file system. It doesn’t support NTFS file system. Now if you have some high definition video files, mostly MKV, whose size mostly exceeds 4GB, you will gonna run into problem as you won't be able to copy those files to FAT 32 USB disk. [ A single file on FAT 32 file system cannot be larger than 4 GB ].
Well, now you can't play the MKV files using USB drive so you can go ahead with one the following options
  • Option 1: You can split those large files into smaller chunks and then copy it to USB disk. That will work. But then the splitting process will gonna take some time.
  • Option 2: Connect the player via LAN to a computer or other device hosting a Media Server. Now the player might not detect MKV files hosted by your media server. In my case it's Twonky. So I tried Serviio and Wild Media Server. Serviio didn’t work out but Wild Media Server works like a Charm. The Blu Ray player played my MKV file, which is of 11 GB is size, very smoothly.
Now, there is couple of things to be taken into consideration when you are going with second approach.
Wild Media Sever is not free. Trail version is available for download and test things out.
Wild media server basically does on-the-fly transcoding. So it will gonna eat a lot of CPU, in my case the CPU usage was 98%. So make sure that you have a decent CPU power. Also, when you are dealing with such large files over the network make sure that you running at least on 100 Mbps LAN.

Purchasing tips for a Blu Ray player.
  1. Check For Region Code: [Very Very important if you plan to buy Blu Ray discs from different regions of the world]

    • Blu Ray Region Encoding The World is divided into 3 regions A, B & C. The Blu Ray discs coming in the market are region encoded. The Blu Ray player sold will support only the region where it is being sold. So a Blu Ray player purchased from Region C will not gonna play a Blu Ray Disc purchased from Region A or B, until and unless it has got Multi-Region support. :)
      Now I'm not really sure if there are Blu Ray player available in market with Multi-Region support.

      Region Code Area
      A/1 The Americas, and their dependencies, Japan, East Asia (except the People's Republic of China and Mongolia), and Southeast Asia.
      B/2 Africa, Middle East, Southwest Asia, Europe (except Belarus, Russia and Ukraine), Australia, New Zealand, and their dependencies.
      C/3 Central Asia, East Asia (China and Mongolia only), South Asia, Eastern Europe, and their dependencies.
    • DVD Region Encoding: Don't get confused "Multi-region Blu Ray" with "Multi-Region DVD". For DVD's we have 7 Region codes.
      Well there are hacks available to make a player play "Multi-Region" DVD's. But these hacks may not work for Blu Ray discs. Google it and try it at your own risk.
      You can find the Region Codes on Blu Ray player manual. In case of Sony's BDP-S370, these code are mentioned at the back side of the player.

      Region Code Area
      0 Informal term meaning "worldwide". Region 0 is not an official setting; discs that bear the region 0 symbol either have no flag set or have region 1–6 flags set.
      1 United States, Canada, Bermuda, U.S. territories
      2 Europe (except Russia, Ukraine, and Belarus), Middle East, Egypt, Japan, South Africa, Swaziland, Lesotho, Greenland
      3 Southeast Asia, South Korea, Republic of China (Taiwan), Hong Kong, Macau
      4Mexico, South America, Australia, New Zealand, Oceania
      5Afghanistan, Ukraine, Belarus, Russia, Africa (except Egypt, South Africa, Swaziland, and Lesotho), Central and South Asia, Mongolia, North Korea
      6People's Republic of China, Hong Kong
      7Reserved for future use (found in use on protected screener copies of MPAA-related DVDs and "media copies" of pre-releases in Asia)
      8International venues such as aircraft, cruise ships, etc.
      ALLRegion ALL discs have all 8 flags set, allowing the disc to be played in any locale on any player.
  2. USB Support: Check for file system that a Blu Ray supports for the USB disks. Also check for the maximum size of the USB disk that will be supported by the player. This will come handy when you want to play high-def video files which are typically very large in size.
  3. Make sure that you get all your accessories comes with your Blu Ray player.
    FYI, Wi-Fi adapter doesn't come with Sony's BDP-S370 Blu Ray player.

That's It !!!
You might get couple of Blu Ray disk free with your purchase. :)
Opps … forgot to mention, I got two disks free. :)

Sunday, May 30, 2010

WD My Book World Edition 2TB

!!! I've got some space !!!

WD My Book World Edition 2TB White Light Edition (WDH1NC20000)
Yippeee!!! I got my disk.

This disk basically a NAS(Network Attached Storage) drive. It comes with a pre-installed Linux. It is a cut down version of Linux. That means Users, file permission groups etc etc basic features of Linux comes into the picture. So you can be pretty sure that only authorized users will be accessing your data. And its pretty good once you have your data on Network.

As I said Linux, no need to worry to all those who don't have knowledge of Linux. It's has got a pretty decent web interface to control & configure the disk properties. Thanks to the httpd service running on it. :)

Apart from that it has got a licensed version of Twonky media server. You can configure Twonky media server to share videos, music and pictures. And the same can be accessed by any PC, cellphone or any other device in the Network. Using web interface you can also enable services like NFS, AFP and FTP.

It has also got iTunes media sharing service running. So iTunes lovers will see this disk under "Sharing" section of iTunes. :)

Cons:
Although this unit Gigabit LAN capable, the data transfer rate is pretty slow over the network. I got max speed of 1350 kB/s while transferring data from my PC/USB Drive to the disk over the 100Mbps LAN card.

Initially I thought I didn't configure the disk properly but after going through blogs & forums I got to know that this disk is powered by ARM9 processor running at 200 MHz. Well I'm still not sure that this is the bottle neck in my case. I google on this a bit more and found that this processor can be over clocked to 500MHz. Probabilily this could improve the data transfer rate. But I didn't tried.

Anyways I got most of data on WD 160 GB USB disk. So I connect my USB disk to the USB port available at the back of the 2TB unit. This basically exposes the USB disk as a network share. To transfer the data logged in to the disk using SSH and fired "cp" command to transfer all my data. And guess what I transferred 50 GB of data in approximately 53 minutes. Pretty impressive...huh !!! ;)

Now since all my data is a one place I really don't need to do a lot of bulk transfer over the LAN. All that data dumping stuff was a one time task and I'm done with that.

To summarize, it's a pretty impressive unit to have your data at one place and share it in home with different PC's/devices(I use my cellphone a lot ;) ). With the Linux in place adds data security. Twonky & iTunes software makes this disk a perfect media server and I'm pretty happy with it after backing up my data.

And special thanks to Mini who brought this for me. :)