Wednesday, December 24, 2008

Scripting in JDK6 (JSR 223) Part 2

Its Lunch Time!

After programmer 2 shown programmer 1 how to deal with JSR 223 or JDK6 scripting ( part 1) programmer 2 went for lunch as he didn’t eat anything since yesterday and left programmer 1 to meet his fate.

Programmer 1 began to apply what he learned from programmer 2 and stuff went so bad, after 30 minutes programmer 2 came back.

Programmer 2: Man, how is everything going? (While he was just about to finish his last bite from the sandwich)

Programmer 1: man the performance sucks, it is really really slow and that won’t be good thing, it is bottleneck now

Programmer 2: ok let me see what did you do. (Swallowed his last bite hardly)

The Crime!:

What programmer 1 wrote was the following:

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("javascript");
for(int i=0;i<100;i++){

engine.eval("function xReturner(){" +" x=x*10; return x;};xReturner();");


As we can see programmer 1 did nothing wrong , he just used what he learned but this code is a disaster, it will make performance issue , in our case we are using JavaScript which is interpreted with each request to the eval method ,this mean that every hit to this method and then calling the eval will do the following : read the file(if we are reading from file) , evaluate and then execute , oh man that so bad if we have a very complicated logic resides in a very big file or even a big function this will cause performance issue, lucky enough that there is something to over come this.

The Solution! :

There is an interface shipped with the script package named Compilable, from its name we can conclude that it might be used to compile our script, and yup that’s right it compiles our script so that we don’t need to evaluate and read it before executing it every time we need to invoke the script, this can be achieved as shown in the following code:

ScriptEngine engine = manager.getEngineByName("javascript");
if(engine instanceof Compilable)


Compilable compiledEng=(Compilable) engine;

CompiledScript script= compiledEng.compile("function xReturner(){" +" x=x*10; return x;};xReturner();");
for(int i=0;i<100;i++){


Object result =script.eval();}



engine.eval("function xReturner(){" +" x=x*10; return x;};xReturner();");


First we check to see if this engine is implementing Compilable interface or no (this is an optional interface so some engines might not implement it but in our case as we are using JS engine shipped with JDK which implements this interface) so if our engine implements this interface then everything will be smooth else we don’t have any other way except the normal eval method.

Ok so we will just check if it implements the Compilable interface so we will invoke the compile method of the engine which will return CompiledScript which we are going to use and invoke our eval method on it and get the result.

By invoking these two scripts on programmers 1 machine we got these results:

Case 1 (no compiling): 931 ms

Case 2(with compiling) 661 ms

*Oh yea the machine that was used is a little bit slow (very slow, in fact used for performance testing for sure :p)

After solving the disaster programmer 1 was very happy by this performance tip.

Programmer 1: oh man thanks a lot for this tip, but I have another thing in mind, does JSR 223 enable me to invoke a method of my choice from a list of methods?

Programmer 2: oh that’s nice questions, let me show you how.

The Invocable!

Yup as you figured out it is the same thing as Compliable an interface that is optionally implemented by the engine you are using.

It enables you to invoke a method by name and also pass parameters to this method (we won’t need any binding here as all the stuff is handled by concrete implementation of this interface).

Before we dig deep in it and see sample code we need to mention that there are two methods that are used for this purpose and they are “invokeMethod”,”invokeFunction” lol it has the same meaning but if you give it another thought you would figure out that the names is correctly used (Function is usually used to address functions in non Object oriented or in other words flat structure and Method are used to address methods in object oriented language) so what are the difference between them ?

The difference between them is as follow:

invokeFunction: used to invoke methods on the top level which means methods that doesn’t reside inside a class (as in Ruby for example)

invokeMethod: used to invoke methods inside a class or module

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");

engine.eval("function getWelcomeMessage(name){return 'hello '+name;};");
Object[] params = {"Wolrd"};
Invocable invEngine=(Invocable)engine;

As we can see in the previous code we just got instance from our engine and cast it to invocable and then call the invokeFunction method which will take the function we want to invoke and also the parameters we want to pass, nothing else we can say about the invokeFunction on the other side invokeMethod as we said it can be applied for JRuby, well as people say nothing is better than a nice simple example ;)

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
Object obj=engine.eval(new FileReader("c:\\JRubySample.rb"));
Invocable invEngine=(Invocable)engine;
Object[] params = {"Wolrd"};



As we can see above the code, it is the same as we did before but here we changed our scripting engine to be JRuby and not JavaScript as we used to do but here we read from a file named JRubySample which has the following contents:

# Class Scripting sample
class SampleClass

def show(message)
puts "hello "+message

def show(message)
puts "hello "+message +" outside"
sample =

return sample;

we can see that we have two methods with the name show, one is a top level (flat structure) and the other reside in a class so as we mentioned before the invoke function will work on top level functions so after invoking the invokeFunction we will get the value “hello world outside” on the other side we said that invokeMethod works on methods that resides in a class.

How can we decide which class we want to invoke its method?

This is determined in the first parameter of the invokeMethod as we can see in our JRuby sample we are returning an object from the class SampleClass and then this object is returned by the eval method and then we pass it to the invokeMethod which will take this object, look for show method in it and then invoke it so we will get this message “hello world”.

Nice, but is that the only thing Invocable interface can do? Nope it can do more than this.

Its Creation Time!

The title sounds strange (all titles in fact), well it is not that strange when you know what is it all about.

The other nice feature about Invocable interface is : with the usage of getInterface method you can create interface implementation from the methods in the script you are evaluating ,this mean that getInterface method returns an implementation of a specific java interface and the methods in this interface is implemented by the script being evaluated(the script has functions that have the same signature as the one in the java interface), so when I get the object returned by getInterface and invoke a method on it , it will just run the logic that were written in the script method, in the code below we can see that happens :

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");

engine.eval("function getAccountName(){var name='Account 1'; return name;};");
Invocable invEngine=(Invocable)engine;
AccountOperations operation=invEngine.getInterface(AccountOperations.class);

As we can see in the previous code sample we are going to evaluate the script which has one method named getAccountName (returns a string that represents the account name)
The new thing here is that we are using the getInterface method which would return an interface implementation to us, as we can see it takes the Interface that we want to get implementation for (it will return an implementation of that interface) after that we will call getAccountName method (the only method inside this simple interface) on the instance returned by the getInterface method and that’s it!

One last thing:

Ok I feel guilty as I didn’t mention something which is:” we can use java objects in JavaScript engine that is shipped with JDK6”

Oh yea you are talking about binding right? nope we are not talking about the bindings here I mean it literally, what I mean is that I can use java objects inside the script I am writing lets take a look at the following example :

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine egine = manager.getEngineByExtension("js");egine.eval("importPackage(javax.swing);var optionPane =JOptionPane.showMessageDialog(null, 'Hello!');");

As we can see we used this time getEngineByExtension instead of getEngineByName (both will do the same thing , no reason why I changed it here but as we said before I can use getEngineByExtension if we are parsing scripts for example so we don’t know what script we have now so we will just get engine instance based on its extension ) after that we actually imported java objects and used it in our script engine and when we call the eval method we will get a message box with Hello message

Back To Our Programmers:

Programmer 1: oh man that’s awesome thanks a lot. I owe you one, I want to do anything for you, tell me what can i do?
Programmer 2: it is ok, no need for that
Programmer 1: no no I insist
Programmer 2: well if you are, invite me on dinner.
Programmer 1: ok consider it done.

And in the restaurant programmer 1 forgot that he doesn’t have enough money and programmer 2 had to pay for it.

Despite this sad ending for programmer 2, programmer 1 were able to finish the task on time, thanks to JSR 223 which enabled him to do so and lets not forget programmer 2 as well


JSR 223 is really nice feature shipped with JDK 6

*We saw that JSR 223 enables us to pass data from java to JavaScript and vice versa.

*Also enable us to invoke scripts not only that but also enables us to return implementation of a specific interface when the script we are invoking have methods as the interface we want to get implementation for.

*it also enable us to use java objects in the script as in the JavaScript engine “rhino“ shipped with the JDK 6

Read more!

Saturday, December 13, 2008

Scripting in JDK6 (JSR 223) Part 1


For sure most of us (mm guess so) have heard about the Scripting provided in Java 6 (Mustang) or JSR 223, 1st time I saw that (just saw the title) I thought that the Java guys will enable us to compile and run JavaScript scripts and that’s the end, well nope that wasn’t what Scripting in java 6 about but actually it is about enabling scripting languages to access the java platform and get the advantages of using java and the java programmers to use such scripting languages engines and get advantages from that (as we will see ).after I got it I was thinking : oh man if the team who implemented the Scripting engine were living in the dark age and they came up with such idea they will be accused of The practice of witchcraft but thanks God we are open-minded now and they will live and the good thing is that they will continue to practice witchcraft :D
So for now java guys will stay alive and continue to do their voodoo stuff and we will be looking at this nice scripting engine.

A sad story (life before Scripting engine):

Programmer 1 is reading documents of an approved change request (while listening to Bon Jovi’s Have a nice Day) which was as follow:

“We need to change our Java Scripts in the client side and move it to server side and also our JRuby scripts to be converted into java classes and we need this by tomorrow”

Programmer 1:” Man, I’m going to have a bad day, how will I do this (develop, test and deploy) in one day this is insane, And that task is assigned to only me :S I don’t know JRuby and I am not that good at JavaScript, well better to move on and get some tutorials”

And in order to stick to the deadline programmer 1 had to get some advanced JavaScript, JRuby tutorials and didn’t have his lunch break L and he had to work till late hours in the next day morning in order to meet the deadline but as expected from programmer 1(that’s why they hired him) he completed his job on time and didn’t complain but he experienced a very bad day. TA DA end of story and it is one sad story, now lets rewind the tape and see how will the story end if programmer 1 had someone else to help him and to show him what is JSR 223.

A happy story (life with Scripting engine):

Same thing as previous (programmer 1 is reading CR document) and he got shocked

Programmer 1: man I’m gonna have a bad day, how will I do this (develop, test and deploy) in one day, this is insane.

A voice came from nowhere and said: what is going on? (That was programmer 2)

Programmer 1: man, check this change request we don’t have such time, we will have to work so hard to finish it in this time

Programmer 2: let me see… mmm well we could make a workaround and give them a build that everything is converted into java classes in no time.

Programmer 1: ok tell me more.

Programmer 2: we will give them what they want, which is all scripts are called and manipulated by java classes and called from server side (in JavaScript case for example) and by using the script engine we wont need to rewrite JRuby script or JavaScript in java we wont even need to know the logic or get any JRuby or JavaScript developer we will just call use the same logic and return the results and TA DA everything is going fine

Programmer 1: huh, seems to me that something hit you on the head, ok how we are going to do this?

Programmer 2: by using the scripting engine shipped with java 6

Programmer 1: huh !

Programmer 2: ok seems to me that you don’t understand a word, I will show you, just give me a paper

The Details:

What we need to know that the main class that we will need to interact with is ScriptEngineManager which will give us access to ScriptEngine which is the engine to any scripting language we want (in our case JavaScript and JRuby) by default JavaScript engine is shipped with JDK6 (Mozilla Rhino) but if we want other script engine we should download its engine implementation (there are some engines for, JRuby, PHP, Python, Groovy,…) ok lets see some JavaScript in action

In Action:

try {

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
engine.eval("var out='hi';var date=new Date();");

catch (ScriptException scrpEx)

By analyzing the previous code we can see:

Line 3 we make an instance from the ScriptEngineManager which is the entry point or in other words we use it to gain access to our script engine like line 4 shows , we note here that we got the scriptEngine by specifying the name of the engine ,we can also get our engine by specifying MimeType or Extension and that’s by calling getEngineByMimeType or getEngineByExtension instead of calling getEngineByName ,this can be useful if we want to get a scriptEgnine instance based on a file extension or something like that.

ScriptEngine instance we got is an instance of JavaScript engine which will apply all JavaScript rules to the script written or evaluated on this engine.

After getting the script engine in line 5 we just invoked a method called eval on this engine, this method evaluates the script giving for this method here as we can see we have supplied the script as a normal string we can supply it from a file for example but for the sake of simplicity we will just invoke the eval method on a string.

After invoking the eval method on the previous script, the rules for JavaScript will be applied while parsing this script, if anything went wrong an exception will be thrown and the type of the exception that will be thrown is ScriptException as line 7 shows.

When Stuff Goes Wrong:

What if we changed the parsed line demonstrated in previous code to be something that JavaScript wont recognize, what will show up? A parsing exception will be thrown indicating that this is not correct, something like the exception below (after changing var to vear)

javax.script.ScriptException: missing ; before statement (#1) in at line number 1

As we can see the exception will give you the line number that caused this exception with the column number and also the file name (in our case we are not using any file so we got Unknown Source)


Great now that we have known the basics of the scripting what else can I do with that? It doesn’t give me enough control yet, for example I can’t pass or get variable from java to JavaScript and vice versa, who said so? JSR 223 enables use to pass inputs and receive outputs, in the previous sample we’ve just shown how to acquire a ScriptEngine which is our entry to our selected script language but as we all know that this won’t be enough , we want to deal with the script itself in other words we need to pass variables to the script and get some output from that script to be manipulated by java. Fortunately JSR223 enables us to do this in an easy smooth way; the listing below shows us how we can get the return value from JS:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
System.out.println(engine.eval("var date=new Date(); date.getMonth();"));

The previous code just gets engine instance and after that it evaluates the expression and here eval will evaluate the expression against JS rules and then invoke this script which gives us back the value of the month but we are just printing the value, what if we wanted to take this value and use it somewhere else?

That’s easy we will just need to modify the last line to be like this:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Double month=(Double)engine.eval("var date=new Date(); date.getMonth();");

Cross Worlds (the direct way):

And now we can do whatever we want on the resulted month value.

Ok now we have seen that we can get back the date from the script by evaluating it but what if I wanted to get a specific variable value? Or assigning a specific value to a variable?

What we didn’t mention is the eval method has more than overload (the one we talked about is either pass a string or read the script form a file)

There is something called ScriptContext which enables us to define the scope of Biding Objects (we will see what does that mean shortly).

Bindings, this is what we have been looking for, bindings enables you to pass java objects to the script and also get script variable to java world.

The ScriptContext can either be GLOBAL_SCOPE or ENGINE_SCOPE which means that every object that we are going to bind in case of GLOBAL_SCOPE will be shared for all engine script and ENGINE_SCOPE will mean that it will be only visible for this engine.

Let’s look for some code now:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
Object a = bindings.get("a");
Object b = bindings.get("b");
System.out.println("a = " + a);
System.out.println("b = " + b);
Object result = engine.eval("c = a + b;");
System.out.println("a + b = " + result);

As we can see in the previous code we got the bindings of the engine and we’ve set its scope to be engine scope (only this engine).

After that we are calling bindings put method (which will allow us to pass java object to the script) bind the variable name “a” a value of 1 and variable “b” a value of 5so when evaluating the script, a will have value of 1 and b will have value of 5.

After that we are just getting the value of “a” and “b” back to the java world (we are just printing it but we could do anything with it and this is how we can get variable value from the script) and evaluating the script which will get us a result of 6

Cross Worlds (the indirect way):

We could have achieved the same functionality by using the indirect way as below:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
engine.put("a", 1);
engine.put("b", 5);
Object result = engine.eval("c=a + b;");
System.out.println("a + b = " + result);
System.out.println("a + b = " + engine.get("c"));

We just called put method on the engine which indirectly does the bindings for us same as the previous code and the get method also will get us the variable named “c” to our java world.


For now we have seen how to make a simple application and call a script, evaluate it and get its return and even pass any input to what ever we want in part two we will look into more interesting stuff, so stay tuned ;)

Read more!

Friday, October 10, 2008

Another Late Post

sorry sorry sorry sorry sorry sorry sorry sorry

believe me i am in the middle of something and once i am done (either did it or no) i will tell you

everything about what i am doing (or was doing in the future :D )

just wait till november ;) and i will tell you everything sorry for not posting for a long time :(
Read more!

Wednesday, July 23, 2008

Single Instance From Your Application ?


some people want to have only single instance running from their application and if the user clicked/started the executable file for the application again nothing will happen or the user will be prompted that there is an already running instnace(something like MSN you only have one instance an no matter how many time you click the MSN exe icon you will just get the same instance).

Chit Chat at a cafe':

one day four programmers were having a coffee break and one of them was having a bad day trying to make a single instance from his application

programmer 1: uffff i cant enjoy my java coffee :(

programmer 2: oh why is that ?

programmer 1: i want the user only to have one instance from my application and no matter how many time he run the application he will just get one instance.

porgrammer 2:oh come on it is not that hard i have done something like that and here is what i have done :why don’t we use file a config. for example and set value in the file to indicate that there is already running instance or even create a file named for example blabla.lock and by this way i can know that the application has an instance running already and when the application is done just change the value or delete the lock file to indicate that it is done and no instance is running.

programmer 3: opps hey programmer 2 i think you have a problem in your approach

programmer 1: oh really ! what is it ?

programmer 3 : well what if the machine crashed ?you will still have the value that indicates that there is an instance running from your application.

programmer 1 : mmm you are right , i was thinking to improve my solution by using registry entries :( ohh and how can we solve such thing programmer 3?

programmer 3: well you can use sockets for that ! how is that ? let me tell you ,just connect to a port and try to listen on this port and when you click on the application icon again to start up new instance it will just go and try to listen on the same port it will get exception (as there should be only one application listening ) .

programmer 1 , 2 : ohhh :O wow nice one

programmer 3: now we can enjoy our coffee :D

programmer 4: (shaking his head) well cant we just make a better solution ???

Make a room for Programmer 4:

programmer 1,2,3 : huh what do you mean ?

programmer 4: well didnt you hear semaphore thing ?

programmer 1,2,3: huh yea but what does it have to do with our case ?

programmer 4 : well it can solve your problem but i will you another way to do so

programmer 4: Semaphore this is the magic word , in old days C++ developers used to do so (use semaphore in order to make only one instance from you application) so when you start my application there will be lock acquired and when i try to start another instancei will check for this lock if it is not acquired so this mean that i am the first instance else am not and i should throw exception or so.

programmer 1,2,3 :oh so how will we handle this issue then we didnt get the solution and our coffee is getting cold :( ?

programmer 4: (while drinking his coffee) well we can achieve the desired functionality through locking mechanism from files introduced in java.nio package.

programmer 1,2,3 : ok tell us how can we do this

programmer 4: we will acquire a lock on the file (lets say our main class file) and when our main starts it will try to acquire lock on this file. if it succeeds this mean that it is the first instance of our program and if it fails (due to already exist lock ) this mean that it is not the first instance of our application note that we will release the lock once we end the application and there wont be any hurm if the application crashed or so as we can re-start it and it will work just fine.

programmer 4: ok does any one have a paper and a pen ?

programmer 1,2,3: (the three in the same voice) sure here you are

programmer 4: ok let me write the code for you

RandomAccessFile randomFile=new RandomAccessFile("C:/myMainClass.class","rw");
FileChannel channel=randomFile.getChannel();
if(channel.tryLock()==null) //we couldnt acquire lock as it is already locked by another program instance)
System.out.println("Sorry but you have an already running instnace from the application");

programmer 4: here you are

programmer 1,2,3 : WOOOW we didnt know that, this looks like a magic

programmer 1: yepiiii hey seems that i will leave early today from my work :D , although the coffee is cold now but i still enjoy it ;)

Read more!

Monday, July 7, 2008

Singleton Tips and Tricks


Singleton ? what is that ? i guess i have heard someone talking about it mmm but i don't know what does it mean :( , ok don't be sad cheer up we are going to talk about Singleton and some tips and tricks for it. so lets start by defining Singleton.

A singleton is a class that is instantiated exactly once no more no less, so this mean i only have one
unique instance from a specific class that is used by the whole application so it will be the same for all objects, in other words lets assume that i have a ball (red one :D ) and that ball is with me and i wrote my name over it after that my friend shout at me and say "ball !" i will just pass the ball i have to him, so now he has my ball (which i wrote on it my name) so this is what singleton like i will just have a single unique instance (ball in our case) from a specific class.

ok great now how can i achieve that?

in order to do so we need to keep our constructor (the class we want to get only a single unique instance) private and provide a public static instance that will allow the user of our class lets check the following example

public class myClass
private static final myClass myclass =null

pirivate myClass()

public static myClass getInstance()
if (myclass==null)
return myclass=new myClass();

ho ho now we are done right ? nope not exactly , you have created a singleton and congratulations for that ;) so when i need to get instance from myClass i will do so

myClass instance=myClass.getInstance();

You :ok ok now tell me that we are done ok?
Me :nope sorry there is a missing thing.
You: Missing thing?
Me: yes, what do you think if there are two threads invoked the getInstance at the same time? what will happen ?
You: explain more.

Meet Mr. Scheduler(The Problem):

ok now you are calling the getInstance and another guy is calling getInstnace so what will happen then ? well lets see Scheduler life and how it serve the threads.

Thread1:hello Mr.Scheduler !

Scheduler: hello thread1 how are you ?

Thread1:never been better( am the one who is going to be executed now yepiiii) and you ?

Scheduler:am fine thx, so are you ready for being executed ?

Thread1:yup i was born ready.

Scheduler:ok go ahead

...5 MilliSeconds

Scheduler:opps sorry i will have to suspend you now Thread1 .

Thread1:oh why :( i didnt finish i am still inside the if checking for the null and myclass is null so
i need just to create the object give me more time plzzzz.

Scheduler: come on be a good thread and leave space for others i promise i will give u another
chance till u r done

Thread2:hello hello my turn , my turn, my turn.

Scheduler:ok ok go ahead

Thread2: ok great am done create the myclass and i have returned a new object from myclass hehe thx Mr.Scheduler

Scheduler:you are welcome , Thread 1 you can resume your work.

Thread1:great thx, so now i will just create an object from myclass ( as it was null object last time i checked so i wont check it anymore), ok great am done and i have a my class object

Scheduler:great have a nice day thread :).

as we can see here that both threads , thread 1 and thread 2 have 2 different objects from myClass which destroy the singleton concept. so how can we overcome this ???

The Solution:

what if we initialized our object as a static initialization instead of checking if it is null or no ? but this way we are sure 100% that it iwll be initialized when our class is loaded by the VM, mmm ok let me show you and example:

public class myClass
private static final myClass myclass =new myClass()

pirivate myClass()

public static myClass getInstance()
return myclass ;

by this way we ensure that only one object will be created once the class is loaded and we wont have any problem if multiple thread called getInstance at the same time

Singleton and Serialization:

so if we made the Singleton class serializable through implementing serializable interface that wont do us any good coz this doesn’t guarantee that it will be single unique object (as each deserialization will result in a new instance from our class) so what we need is to provide the method named readResolve() so now we can write our code like this :
private Object readResolve() throws ObjectStreamException
{return myclass;}
so now we guarantee that only one unique instance will be returned.

Read more!

Monday, June 23, 2008

IoC (Inversion Of Control) and Dependency injection (DI)


Lots of us hear this word and also hear that this container is using IoC (inversion of control) so what is it all about and what does this mean? we will be working using Spring IoC through this article so lets start by checking the formal definition of the Dependency injection:“Dependency injection (DI) in Computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency.”Ho ho sounds little bit confusing, so lets make it more simple so first we need to know what is IoC and what is DI .Well we can say that IoC is a generic term and the DI is a more specific term so this mean that Dependency injection is more specific term than the IoC but still don't understand the IoC thing.

Life Without IoC and DI

In a standard component container,the component themselves ask the container for the resources they want for example lets check this segment of code (which you can find in most of EE application):

Context context=new InitialContext();
DataSoruce dataSource=(DataSource)context.lookup("jdbc/TestingDS");
Connection connection=dataSource.getConnection("yourUser","yourPassword");
Statement statement=connection.CreateStatment();
ResultSet results=statement.executeQuery("select * from users");
. //i didnt include all the code just the important thing

so in the above code we just initialize a context (JNDI)
and perform a lookup for a resource bounded at jdbc/TestingDS and then get the results and cast it as a DataSource after that we use it and continue with our processing.

oh yea so every time i want to do so i will have to make the lookup thingy to get the data source or any resource i might need.
mmm so i will keep asking my container to give me what i want? yup you will.
well lets see how would the container and the component interact with each other (without IOC)

A Day in the Container's Life !

Component: Hi Mr.Container.
Container:Hi Component.

Component:how is your day?
Container :oh i have been busy serving all other components and don't have time for myself :( .

Component:oh poor you Mr.Component.
Container:ahh what can i do this is my job :) , so how can i serve you ?
Component :oh yea i forgot , i want to you to serve me the resource bounded at bla bla bla
Container:ok sure no problem, just a min. here you are (hehehe now we got what we want :D )
Component:oh many thanks MR.Container, have a nice day.
Container:you too component bye.

so in here as we can see the component keeps asking the container to provide the needed resources.

Hollywood and IoC:

IoC as most of people say it apples Hollywood principle which is :

Component: Hello Mr.Container !Container :don't call Me, I will call you.TEET TEET TEET (the container hanged the phone :S )
Component : Shocked :|

This is what they call: don't call us , we will call you principle.

yup it is what you are thinking in , yes the container will take control over the resources for the
component but without involving the component in that, in other words the control of the resource will be inverted from component to container and its configuration(through XML as most of people do and as i do)

Life With IoC:

what if we want to apply IoC on the previous example?

Ok, so we will just need to modify some stuff (adding a property for the DataSource and also using spring framework)

//class that contains the code for spring IOC
private DataSource dataSource;

public void setDataSource(DataSource ds)

public static void man(String[] args)
//so now dataSource is set by Spring IOC
Connection connection=dataSource.getConnection("yourUser","yourPassword");
Statement statement=connection.CreateStatment();
ResultSet results=statement.executeQuery("select * from users");
. . . //i didnt include all the code just the important thing

//segments from XML configuration file

<%bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<%property name="driverClassName" value="youDriverClassName" />

<%property name="url" value="DBUrl" />

<%property name="username" value="yourUsername" />

<%property name="password" value="youPassword" />

<%bean name"ourComponent" class="com.springIoC.IoCTesting">

<%property name="dataSource" ref="dataSource">


as we can see in the above XML we just stated that we have a bean from class bla bla which has a property named dataSource so spring IoC will just think like that :

mmm (spring IoC thinking...)
oh yea i have a bean named ourComponent .
wait a minute this bean have a property named dataSource! .
this property refers to another bean named dataSource which have some property (url,username,password,driverclassname) .
oh great so now what i need to do is inject this bean property (data source property in the ourComponent ) with the Datasource ,TA DA now it is done.

so we can see here that we didn't make any lookup , so there is zero times component did ask the container about the DataSource.
in this case the control over deciding which DataSource to use is in the container not the component so once the container gets a DataSource it will pass it to the component by calling the setter (SetDataSource).when spring IoC container loads at runtime it will inject the dependency into our component (this is done at runtime by XML configuration as we can see above) so the Data Source will be injected into ourComponent and now we can make anything with this data source (with zero component to container call).

below is a diagram that shows the interaction in case we are using IoC or non IoC Container.

in the first we can see that the component ask the container to provide the resource it need (data source in our case ) but in the second image the container creates and inject the resource into the component

Types Of Injection:
We have seen in the previous sample that spring do the injection through setter (property) thats one way of injection , another way it to inject through the constructor (which spring also support)
this mean that i can inject all resource in the component through a constructor (and that constructor takes all the needed resource through arguments supplied)

so now we know what is Dependency injection and also the IoC concept , there are also some IoC containers other than spring IoC container for example :Pico container and also Avalon framework

Read more!

Wednesday, June 18, 2008


Difference between the following guys (we are talking about xml here :D ):


Guess that all of us knows what is DOM and some might know what is SAX but the major don’t know what is StAX and TrAX(most of ppl don’t know what is that)

As expected from the mighty JAVA it supports more things than the humble .NET :D as we can see in .NET 2003 it only supports DOM and don’t support any other

thing than DOM (I worked with .net 2003 I was .net develoepr at 1st but didn’t used 2005 extensivly so am not sure if MS supports it now or no but SAX was not

supported in the 1st palce in .NET )

So lets start with defining each of them :

DOM creates XML tree that is represented in the memory (for the document). It provides a very flexible API for creating, reading, updating, and deleting nodes within the tree, in general, an memory representation of the document is required; this is bad if we looked at the performance. This means that the DOM loads all the xml document into the memory so if we have 1 MB of xml then 1 MB of memory will be reserved for this document which is not good choice in large documents but note here that it is good in CRUD operations (we will know why later)

SAX is a "push"(take care from this we will see it later) type that provides an event callback interface. There is only a reading. SAX requires that an entire XML document be read. But , it doesn't require the entire document to be held in memory at any point in time. SAX is a very low level, efficient for parsing XML documents. this is a good choice if you have large Documents either with events or without events!!!(SAX is used for processing xml for events meaning that you can find an event in the xml that tells the code snippet that will handle the event to do something)

PUSH!!! So there must be a PULL then!!! Yup that’s right there is PULL and PUSH and the different is next time :p

StAX is a "pull" type o. there are a Cursor and an Event Iterator API. There are both reading and writing sides of the API. It is more developer friendly than SAX. StAX doesn't require an entire document to be held in memory. But you won’t need to read the whole document (the best part). Portions can be skipped. This improves the performance of StAx more than SAX.

TrAX is for transforming source documents into result documents using XSLT, rule-based language. A TrAX source document may be created via SAX or DOM. TrAX needs both Java and XSLT skills. Optimizing TrAX takes more time (to transform from one xml into another)

After that we can say that:

SAX and StAX are better in the performance issue and the memory thing
But in case of DOM and TrAX it depends (on how large the document is and on what you are doing)
Dom is the only one that supports CRUD operations

SAX and StAX are forward only but DOM and TrAX can be both ways (forward and backward)

Read more!