Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Friday, December 26, 2008

The Other side, the Ugly side - Slumdog millionaire

As statistical evidence suggests, most Indian movies are cheap imitations. Some (moi) feel that they drain the energy and test the patience limits. Bollywood's definition of sensuality is not something 'original'. The industry where dreams are sold has a strange appeal, which I rarely understood. Watching Indian movies is like watching a magician's show, the caveat, you already know the tricks.

But once in a while, when you expect the least, the rabbit is out , a real one which you would watch with child like amusement. Now that is a rarity. What is so intriguing! you tell me.

The "Slumdog millionaire" is one of those rare gems, not a bollywood movie though, it is based on the popular book 'Q and A' by Vikas Swarup. If you haven't read the book, don't miss this one. An uncouth movie with an arcane and predictable ending, but captivating it is. A bunch of lies, but beautiful ones they all are. A story teller's story which I have missed since a long time is what helped me stick. Go find out what is in for you. As the credits started rolling, I was in for another shock, so will you be when you see the name AR Rahman.

==spoiler==
A street rag, goes out to be a winner. Not much help is it, but remember that I am not going to be the one shouting kajol's name on the way out of the movie, Gupt. Wiki would definitely help, but why bother! See what I found on wiki after watching the movie,

"Rotten Tomatoes reported that 93% of critics gave the film positive write-ups, based upon a sample of 135, with an average score of 8.1/10.[20] At Metacritic, which assigns a normalized rating out of 100 to reviews from mainstream critics, the film has received an average score of 86, based on 35 reviews."

Thursday, December 4, 2008

Kick starting with hibernate

ORM is no rocket science, it is a very useful tool though. The idea of a tool to help manage transactions and bean bindings is a developer's paradise. So how do we start off? Download hibernate from internet, along with it the not so obvious slf4j-simple-xxx.jar and slf4j-api-xxx.jar files are needed.

Place these in you project class path. The next step is writing the mapping files. The file hibernate.cfg.xml is the one which spells out the all encompassing details like those of the database, connection pool etc. The < entity >.hbm.xml files contain the object mapping information along any operation specific information.

A sample hibernate config xml for MySQL


<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : hibernate.cfg.xml.xml
Created on : 02 November 2008, 19:31
Author : Kanthi Swaroop Rongala
-->

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hospital_care</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property>
-->

<!--
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
-->
<mapping resource="model/resources/Doctor.hbm.xml"/>
<mapping resource="model/resources/Patient.hbm.xml"/>
<mapping resource="model/resources/Appointment.hbm.xml"/>
</session-factory>

</hibernate-configuration>




Note that I have placed all my xmls in a different package called model.resources. What about the hibernate.cfg.xml location? We can move it as well.

See the following:
public class HibernateUtil {

private static SessionFactory sessionFactory = null;
private String CONFIG_FILE_PATH = "/model/resources/hibernate.cfg.xml";

private HibernateUtil(){
}

private SessionFactory create() {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure(CONFIG_FILE_PATH).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}

public static SessionFactory getSessionFactory() {
if(sessionFactory == null){
HibernateUtil utils = new HibernateUtil();
utils.create();
}
return sessionFactory;
}
}


Hope this helps in giving you that well deserved push

Dynamic proxy

Every one knows compiled code is type safe and is static. It is not possible to dynamically type the behaviour in such cases. C, C++, Java … they all fit the bill. Java has this interesting feature called Dynamic proxies which promises what it spells. Now how the hell can… well it is not exactly dynamic!. There is a provision for plugging functionality (not just behaviour) at run time without major code changes. So should we really be calling it Dynamic?

As life goes on, let’s see an example. Suppose I have come legacy code/tested code/production code/my own kitchen sink code/tutorial code…Tan(90). The purpose of the code can be as simple as that of a logger. Now I want to impart some extra functionality (not just behaviour), say an actionlistener, whoa!! On a logger!! (Bear with me for the sake of example). Now how would be do that:
  1.  Implement the interface
  2.  Create a wrapper class which uses composition
Not bad?, and where would be place this? In our code base of course! Dynamic proxies offer yet another way of doing things.

Think of a utility that creates a wrapper for the interfaces you want and lets you inspect the calls being made. Armed with this knowledge the lone ninja developer can render a killer app, well that is the idea at least. This utility is part of java since jdk1.3 and is called the Proxy.

Starring…coming to your nearest desktop…
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

What exactly are these? Proxy helps you by creating the implementing class alias proxy and InvocationHandler helps you with the inspection/introspection. Proxy needs a set of interfaces and InvocationHandler needs to be hand coded.

Some code snippets


public interface IAppLogger {

void logError(String txt);

void logInfo(String txt);

void logWarning(String txt);

}

public class GenericInvocationHandler implements InvocationHandler {


public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
StringBuilder sb = new StringBuilder();
sb.append(" Calling ---> ");
sb.append(method.getName());
sb.append("(");
sb.append(")");
System.out.println(sb.toString());
//donothing proxy
//return method.invoke(obj, args);
return null;
}

}

public class ProxyGen {

public Object createProxy(Class[] interfaces, InvocationHandler handler){
ClassLoader cl= ProxyGen.class.getClassLoader();
return Proxy.newProxyInstance(cl, interfaces , handler);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GenericInvocationHandler handler = new GenericInvocationHandler(new AppLogger());
ProxyGen factory = new ProxyGen();
ActionListener proxyObj2 = (ActionListener) factory.createProxy(new Class[]{IAppLogger.class, ActionListener.class}, handler);
proxyObj2.actionPerformed(null);
IAppLogger proxyObj = (IAppLogger) proxyObj2;
proxyObj.logError("abc");
proxyObj.logInfo("abc");
proxyObj.logWarning("abc");
}

}

As the whims of the butterfly go, in a flap of wing the requirements change. We now have a new technique under our belt.

I have seen a generic logging example on net as part of my learning, thank you Google, once again!

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.