Tuesday, July 10, 2007

Garbage Collection (with reference objects)

Part III

The Reference Object application programming interface (API) is new in the Java Development Kit (JDK) 1.2. This API allows a program to maintain special references to objects that allow the program to interact with the garbage collector in limited ways.

  • Unlike ordinary references, the reference in a reference object is treated specially by the garbage collector. A reference object encapsulates a reference to some other object, which is called the referent. The referent of a reference object is specified when the reference object is created.
  • Besides strongly reachable and unreachable objects, the Reference Objects API gives you strengths of object reachability. You can have softly, weakly and phantomly reachable objects and gain a limited amount of interaction with the garbage collector according to the strength of the object's reachability.
When an object is reachable from the root set by some chain of ordinary references (no reference objects involved), that object is said to be strongly reachable.

If, however, the only ways to reach an object involve at least one weak reference object, the object is said to be weakly reachable. An object is considered by the garbage collector to be in use if it is strongly reachable. Weakly reachable objects, like unreachable objects, are eligible for collection.




Fig.1: Adding reference objects to the heap

  • Unreachable objects (outside the strongly and weakly reachable areas) are not reachable from the root set.
  • Strongly reachable objects (inside the strongly reachable area to the lower left) are reachable through at least one path that does not go through a reference object.
  • Weakly reachable objects (inside the weakly reachable area shown enclosed with a dashed line to the upper-right) are not strongly reachable through any path, but reachable through at least one path that goes through a weak reference.

The diagram above shows two paths to the object on the heap with the X. It can be reached through the weak reference, and directly from the stack without going through a weak reference object. The object with the X is strongly reachable and not weakly reachable because one path leads to it from the root set without going through a reference object.

The garbage collection behavior for this object is the same as other strongly reachable objects until it is no longer reachable from the root set, at which time it becomes weakly reachable.

Friday, July 6, 2007

Garbage Collection ( Part II )

Garbage Collection process:

The garbage collector's job is
1)To identify objects that are no longer in use (An object is in use if it can be accessed or reached by the program in its current state)
2) Reclaim the memory.

How to identify the unreachable objects?

An executing Java program consists of a set of threads, each of which is actively executing a set of methods (one having called the next). Each of these methods can have arguments or local variables that are references to objects. These references are said to belong to a root set of references that are immediately accessible to the program. Other references in the root set include static reference variables defined in loaded classes, and references registered through the Java Native Interface (JNI) API.

  • All objects referenced by this root set of references are said to be reachable by the program in its current state and must not be collected. Also, those objects might contain references to still other objects, which are also reachable, and so on.
  • All other objects on the heap are considered unreachable, and all unreachable objects are eligible for garbage collection.
  • If an unreachable object has a finalize () method (means Resurrectable state), arrangements are made for the object's finalizer to be called.
  • Unreachable objects with no finalizers and objects whose finalizers have been called are simply reclaimed during garbage collection.

    Garbage collection algorithms vary, but they all have in common the task of identifying the objects that are reachable from the root set and reclaiming the space occupied by any other objects.

    In the diagram, which is a simplified view of the stack and heap for instructional purposes, objects inside the blue square are reachable from the thread root set, while objects outside the square in red) are not.

Thursday, July 5, 2007

Garbage Collection ( Part I )

Why Objects are called Reachable, Resurrectable, and Unreachable?

Prior to the release of Java 2 Platform, Standard Edition (J2SE) 1.2, an object could be in only one of three states

Reachable: An object is reachable if the garbage collector can trace a path from a root-set variable to that object. When the JVM creates an object, that object stays initially reachable as long as a program maintains at least one reference to the object. Assigning null to an object reference variable reduces the object's references by one. For example:

Employee e = new Employee (); Employee e2 = e; e = null;
In the above code fragment, the Employee object is initially reachable through e.
Then it is reachable through e2 as well as through e. After null assigns to e, the
Object is only reachable through e2.

Resurrectable: An object is resurrectable if it is currently unreachable through root-set variables, but has the potential to be made reachable through a garbage collector call to that object's overridden finalize () method. Because finalize ()'s code can make the object reachable, the garbage collector must retrace all paths from root-set variables in an attempt to locate the object after finalize () returns. If the garbage collector cannot find a path to the object, it makes the object unreachable. If a path does exist, the garbage collector makes the object reachable. If the object is made reachable, the garbage collector will not run it’s finalize () method a second time when no more references to that object exist. Instead, the garbage collector makes that object unreachable

Unreachable: An object is unreachable when no path from root-set variables to that object exists and when the garbage collector cannot call that object's finalize() method. The garbage collector is free to reclaim the object's memory from the heap.

Sunday, July 1, 2007

An Introduction To JSON

It’s a bird; it’s a plane, its JSON

A few months ago, I suddenly came across (actually tripped over) a word flying through the web – JSON. It’s pronounced the way it is written, but for people like me who’re trying to figure out how it is pronounced, its Jason. The name is fancy and friendly enough to catch one’s attention.. something like, “Hey JSON, what’s up today?”
Getting into the matter and keeping things short, JSON stands for JavaScript Object Notation. It’s an extremely simple way to represent objects that contain name-value pairs. Here’s a plain example:

{
“myself”:{
“profile”:[
“name”: “Clark Kent”,
“profession”: “Flying Around”
]
}
}
As of now, all you intelligent readers must have guessed what this thing is actually trying to say. The parent object is myself, which contains a child object profile. Now profile contains a list of keys: name and profession, and their corresponding values.
So, now you must have got an idea as to how a JSON text looks like. Let’s add a little extra to it:
{
“messengers”:{
“yahoo”:[
“username”: “cooldude”,
“password”: “p@$$w0rd”
],
“google”:[
“username”: “cooldude001”,
“password”: “$ecr3t”
]
}
}

The above example shows the parent object messengers containing two children – yahoo and google.


Things to be noted here:

  1. The curly brackets ({}) are the containers and they group together a set of related items.
  2. The square brackets {[]} are the array holders.
  3. Name-value pairs are separated by colons.
  4. All array elements and name-value pairs are separated by commas.

And now you can very well understand what the above JSON structure describes.

But beware! Professional programmers use even more complicated JSON structures for their application; not intending to say that they deliberately make it complicated, just that their complex application requires it. It’s a common practice to have nested JSON structures. Let’s have a look at it:

{“myself”:{“profile”:[“name”: “Clark Kent”,
“profession”: “Flying Around”, “messengers”: {“messengers”:{
“yahoo”:[“username”: “cooldude”,“password”: “p@$$w0rd”],
“google”:[ “username”: “cooldude001”,“password”: “$ecr3t”]}}]
}}

Assuming that you have not fainted yet and are still in your senses, lets try to understand what the above structure represents. Let me make it pretty for you:
{
“myself”:{
“profile”:[
“name”: “Clark Kent”,
“profession”: “Flying Around”,
“messengers”: {
“messengers”:{
“yahoo”:[
“username”: “cooldude”,
“password”: “p@$$w0rd”
],
“google”:[
“username”: “cooldude001”,
“password”: “$ecr3t”
]
}
}
]
}
}

So, now you find that this structure is a combination of the previous two structures. There is an extra key-value pair added to the profile of some Mr. Clark Kent.

Lesson to be learnt: You may not always get a pretty JSON structure :-). The ones sent/received by applications over the internet are generally compacted to save bandwidth usage. So before giving up on a JSON structure, be sure to arrange them properly to get a clear understanding of its meaning.

Ok, so what do I do with Mr. JSON?

There are pretty interesting things that you can do with JSON texts. You can parse them in JavaScript and even use them in AJAX calls.
Let’s see how we can use it in JavaScript. Let’s take the simplest example for a clear understanding. You can then build stuff over it yourself:

var myJSON = {
“messengers”:{
“yahoo”:[
“username”: “cooldude”,
“password”: “p@$$w0rd”
],
“google”:[
“username”: “cooldude001”,
“password”: “$ecr3t”
]
}
};

The above statement declares a JSON object myJSON. Now to access any object inside it, you can simply do a:

var yahooUser = myJSON.messengers[0].username;
var yahooPwd = myJSON.messengers[0].password;

Or, you can use a simple loop to iterate through the values:

for(var z = 0; z<myJSON.messengers.length; z++){
alert(“User: ” + myJSON.messengers[z].username +
“, Password: ” + myJSON.messengers[z].password);
}
This will show all the usernames and passwords in the JSON object in an alert dialog.

JSON with AJAX? Sounds Cool..

AJAX (ah, yet another divine technology) too uses JSON. The idea is to prepare the information in JSON format and send it across through AJAX calls. When received at the client end you just need to convert the JSON text into a JSON object using the friendly neighborhood eval() function.
But there is a catch in this methodology. My friends, who managed to stay awake during the Application Security presentation that I gave a few months ago, would be able to understand it better. There is something called as XSS (Cross Site Scripting). And blindly trying to eval() a received text can become a high security loop-hole. I’ll discuss it in details in some another article.
But as of now, if the JSON object is being received as a text from some external source, then it is highly recommended that JSON parsers should be used:
var myObject = myJSONtext.parseJSON(filter);


The optional parameter, filter, is actually a function that you’ll have to provide. This function will be called for each key-value pair in the JSON text. You might want to use it to alter the values depending on some conditions:


myData = text.parseJSON(
function (key, value) {

return key.indexOf('date') >= 0 ? new Date(value) : value;
});


On one hand where you can convert a string to JSON, you can also have a JSON object converted to string:

var myJSONText = myObject.toJSONString();



Nearly Done

JSON is new technology that is catching up quite fast. Some people say that it is an appropriate replacement for XML. Its quite easy to write and understand extremely easy to use. In XML where you need to load a document, painfully loop through it to extract the values before you can use it; in JSON, you just need the JSON string and eval() it.
All in all, JSON is a technology that is sure to make life easy. Using all the above mentioned information, I am sure that you are now pretty much prepared to face JSON in the real world.. :-)

PS: Please do post any corrections for the article that you might find. Suggestions are always welcome.

Friday, June 29, 2007

Configuring a standalone machine to install Sun One Directory Server

Requirement:

  • An NTFS partition with 250 MB disk space.
  • Administrator’s Username and Password.
  • A Windows Server with correct OS version.
  • *** Properly configured TCP/IP (IP address, subnet mask and optional default gateway)
  • A network connection (to a hub or another computer via a crossover cable). If you don’t have a network connection, you can finish the Active Directory installation by installing Microsoft Loopback Adapter.
  • Ethernet/LAN (NIC) card needs to be installed.
  • An operational DNS Server which can be installed in the Domain controller itself.
  • The Windows 2000 or Windows Server 2003 CD media (or at least the ‘i386’ folder)

Pre-Install Configurations:

Most of the standalone machines don’t follow the domain forest structure instead they have WORKGROUP structure. Now, Sun One Directory Server installation requires a domain structure for proper installation/functioning. To migrate from the existing WORKGROUP environment to the Domain environment you need to follow some steps. These are mentioned as follows:

  • Right click on My Computer.
  • ** Go to Computer Name tab and observe the WORKGROUP field. This is to confirm that there is no existing Domain in the LAN/intranet/computer.
  • Open a command prompt and type the command dcpromo. This will open an “Active Directory Installation Wizard” as shown below:
  • Now, follow the steps giving necessary inputs to complete the installation.
*** Before proceeding we must make sure to set a static IP and DNS IP. Refer the screenshot below as a sample TCP/IP setting:
  • In the “Domain Controller Type” panel select the option “Domain controller for a new domain”.
  • In the “Create New Domain” panel select the option “Domain in a new forest” and put a valid Domain Name and Domain NetBIOS name in the subsequent panels.
  • If all the entries are valid then installation of the Active Directory will be completed successfully.
Installation of Sun One Directory Server:

After installing and configuring the Active Directory in your local machine we have both the DNS Server and DNS client running in this same machine. Now, if we check the ** Computer Name tab as mentioned above, you can observe that your computer is being migrated from the WOORKGROUP environment to Domain Forest structure. It looks some thing like this:
Now we are ready to install Sun One Directory Server on a standalone machine. Note that the “Full Computer Name” required for installation can be retrieved from the “Computer Name” tab.

Thursday, June 28, 2007

NoClassDefFoundError vs ClassNotFoundException

Let us go through the definitions of the above throwables from the java doc

java.lang.NoClassDefFoundError

Thrown if the Java Virtual Machine or a classloader tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

java.lang.ClassNotFoundException
Thrown when an application tries to load in a class through its string name using:



  • The forName method in class Class.

  • The findSystemClass method in class ClassLoader .

  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

Here the most important part is that the error java.lang.NoClassDefFoundError is thrown when a class is not found in the class path at runtime but the class was present at the time of compilation.

While the exception java.lang.ClassNotFoundException is thrown by the methods which tries to load the class at runtime but the class is not required to be present in the classpath at compile time.

Let me explain the above using an example. Let us consider following class:

MyPrint.java

package test;
public class MyPrint
{
public
void print(String str)
{
System.out.println(str);
}
}

The method "print" is called from a class Test
Test.java

import test.MyPrint;

class Test
{
public static void main(String[] args)
{
MyPrint mp =
new MyPrint();
mp.print("
Class is loaded");
}
}


Here to compile the Test.java file you have to make sure that MyPrint.class is there in the classpath.

Now if you remove the MyPrint.class from the classpath (after compilation) and try to run “java Test” you’ll receive following error:

Exception in thread "main" java.lang.NoClassDefFoundError: test/MyPrint
at Test.main(Test.java:7)


Let us modify the Test.java file a bit:

//No Import required
//import test.MyPrint;

import java.lang.reflect.Method;

class Test
{
public static void main(
String[] args) throws Exception
{
Class cls = Class.forName("test.MyPrint");
Method meth = cls.getMethod("
print", new Class[]{String.class});
meth.invoke(cls.newInstance(),
new Object[]{"Class is loaded"});
}
}
Please note the changes. Here we are using reflection to call the method. At the time of compilation you don’t need MyPrint.class to be present in the classpath. Also as because “Class.forName” method throws java.lang.ClassNotFoundException (which is not error), you need to catch the exception. Here we have just re-thrown the exception.


Run if you run “java Test”, following exception will be thrown:

Exception in thread "main" java.lang.ClassNotFoundException: test.MyPrint

Now people who have worked with application servers (or even web servers) must have seen java.lang.ClassNotFoundException often. There your servlets, bean class etc are specified in the descriptors (mainly XML files). So the application class loader reads the descriptor and tries to load the classes. In case the class loader fails to load the class, the exception java.lang.ClassNotFoundException is thrown. You’ll find same exception often while working on JDBC, Spring etc in which involves similar situation.

Resolution (in general):
java.lang.NoClassDefFoundError

  • Make sure the class is there in the classpath.

java.lang.ClassNotFoundException

  • Make sure the class is there in the classpath.
  • Make sure your configuration files (specially where you are specifying the classes) are proper.

In case you have multiple class loaders you need to keep in mind that classes loaded by child class loader can't be seen by parent class loaders.

Wednesday, June 20, 2007

Diving Deep into Java (I):

I thought, let’s highlight some concepts of Java which many of us may know or may not. But those are important and interesting. So let dive deep into Java.


1. Can you declare a class abstract & final at the same time?

The answer is no. Because when you declare a class abstract, it means you can’t create an instance of that class directly. First you need to create a subclass of that class and then you can create an instance of that subclass. So, abstract class is made to be sub classed. On the contrary, final class can’t be sub classed. So, I think now it is clear that a class can’t be abstract & final at the same time.

2. We all know that private members are invisible to any code outside the member’s own class. So, you are free to declare a method with the same name as in the super class. This will not fall in the overriding or overloading category.
You all know that in case of overloading you can’t change only the return type of an overloading method. You have to change the signature of the method.
But in case of the above scenario you don’t need to worry about the overloading rule.

If the point is still not clear let’s take an example –
class A
{
private void printName(String nm)
{
System.out.println(nm);
}
}

class B extends A
{
public int printName(String nm)
{
System.out.println(nm);
return nm.length();
}
}

So, if you look at the above example it seems that it is breaking the overloading rule. But as the “printName” method is private in class A, class B is free to declare a method with the same name. To class B, it is as if class A does not have any such method.

3. What type of access do you have to a protected member of a class inside its subclass which belongs to a different package?

Protected members are inherited inside it’s subclass out side the package. So inside that subclass you can access that member using “IS A” relationship. But you can’t access it using a “HAS A” relationship.

Let me make it clear with an example –

package pkg1;
public class A
{
protected int count = 10;
}

package pkg2;
import pkg1;
public class B extends A
{
A a1 = new A ();
System.out.println (count);
System.out.println (a1.count); // this line is wrong
}

The line number 7 inside the class B is incorrect. We can’t use the (.) operator on an instance of class A to access the member “count” from inside class B. The member “count” is only inherited. So, we can think as if “count” is a member of class B. That’s why line number 6 is correct.

4. A very small point – never declare a local variable, inside any method, with public-private-protected access modifiers. You can only use “final” with local variables.

5. What can’t be changed and can be changed when you declare an object reference variable as final?

You can’t assign a new instance to the reference variable. But you can change the values of the members of that object.

6. We know that declaring a variable with final keyword makes it impossible to reinitialize that variable. But do we know that this is only true when we declare it with an explicit value? Please note, I have said explicit value, not default value.

7. One interesting point to remember – variables declared inside an interface is implicitly “public static final”. You can also explicitly declare it as “public static final”. No problem in that.
But methods declared inside an interface can’t be final, private, static, protected, synchronized, native or strictfp.