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

Saturday, December 22, 2007

Xbox 360

I bought a xbox 360, never thought that it should be blogged :) I am doing it anyway. The seventh generation of consoles are absolute number crunchers. If any one has worked on a graphics program like adobe photoshop or gimp, they would understand it. Most computers of yester years were good at things like browsing, drafting documents and playing media files. The graphic programers always went for a extra special offering called apple. Any good rendering involves operations that are cpu hungry and memory hungry. Now a days the chasm is being bridged but the divide still exists.

Coming back, the xbox sits like a cute little puppy in the drawing room just beneath the TV. It at time looks melancholic as well. All this changes in a spur of a moment, just switch on the power and insert your favourite game (mine being Gears of war). There is no thunder or lightning, apart from the noise made by the cooling unit. The graphics and visual effects at times dazzle me. I often wonder whose idea was the rocking controller. I have a strong feeling that the 3.2 Ghz 6 core machine will stay with me for a long time.

Jibx with Netbeans

There are times when we face the need to marshall and unmarshall java objects. What better than XML for this! Most programmers can write their own XML parsers and unparsers, why do that when we have a beautiful utility tool. Infact there are several tools. Jibx is one of them, it is offers better performance than many other rivals by using the pull-event mechansim, to top that it is free for use and opensourced. Jibx essentially asks the users to provide a mapping between java objects and xml data. This mapping is provided by users in form of xml file. Lets call it binding xml. Then the users are asked to compile the the project and compile the bindings. The byte code is modified (another reason for the high performance) during this process.

A sample code snippet:
##############################

package ajibx_learning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ResourceBundle;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;


public class Main {
/**
* Unmarshal the sample document from a file, then marshal it back out to
* another file.
*/
public static void main(String[] args) {
String ajibxProperties = "ajibx_learning.bizProperties";
String fileInput = "fileOutput";
String fileOutput = "fileInput";
String className = "className";
File fOut = null;
File fIn = null;
Object bean = null;
try {

ResourceBundle rbundle = ResourceBundle.getBundle(ajibxProperties);

fOut = new File(rbundle.getString(fileInput));
fIn = new File(rbundle.getString(fileOutput));
Class c = Class.forName(rbundle.getString(className));
bean = c.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
System.out.print("Exiting ...");
return;
}
System.out.print("Unmarshalling ...");
// unmarshal customer information from file
bean = unMarshall(fIn, bean);
// you can add code here to alter the unmarshalled customer
System.out.print("marshalling ...");
// marshal object back out to file (with nice indentation, as UTF-8)
marshall(bean, fOut);
}

public static Object unMarshall( File fileIn, Object bean){
boolean status = true;
try {
// note that you can use multiple bindings with the same class, in
// which case you need to use the getFactory() call that takes the
// binding name as the first parameter
Class beanClass = bean.getClass();
IBindingFactory bfact = BindingDirectory.getFactory(beanClass);
// unmarshal customer information from file
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
if(!fileIn.exists()) {
System.out.println("File doesnt' exist: "+fileIn.getAbsoluteFile());
return status=false;
}
FileInputStream in = new FileInputStream(fileIn);
bean = uctx.unmarshalDocument(in, null);
in.close();
} catch(Exception e){
status = false;
e.printStackTrace();
} finally {
if(status)
return bean;
else
return null;
}

}

public static boolean marshall( Object bean, File fileOut){
boolean status = true;
try {
// note that you can use multiple bindings with the same class, in
// which case you need to use the getFactory() call that takes the
// binding name as the first parameter
Class beanClass = bean.getClass();
IBindingFactory bfact = BindingDirectory.getFactory(beanClass);
// marshal object back out to file (with nice indentation, as UTF-8)
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
if(fileOut.exists()){
fileOut.delete();
}
if(fileOut.createNewFile()) {
System.out.println("Creating file: "+fileOut.getAbsoluteFile());
}
else {
System.out.println("Failed to create file: "+fileOut.getAbsoluteFile());
return status=false;
}
FileOutputStream out = new FileOutputStream(fileOut);
mctx.marshalDocument(bean, "UTF-8", null, out);
out.flush();
out.close();
} catch(Exception e) {
status = false;
e.printStackTrace();
} finally {
return status;
}
}
}
###############################

I decided to use the following properties file
#bizProperties.properties
# the input file is the source of xml data
fileInput = D:\\temp\\jibx-gen\\Example1.xml
# the outfile is written by the program
fileOutput = D:\\temp\\jibx-gen\\Gen-Example1_2.xml
# the bean which I intended to use
className = beandef.Person
###############################

I then decided to use the good old 'ant' script to compile this. The issue with ant scripts was that, the code cannt be debugged straight away in IDE. So, I decided to have look at the IDE generated ant script. This itself deserved another posting!!. The expressive power of ant is better illustrated in such examples. The modifications done to build.xml are given below. Note that I was using Netbeans5.5.1
##############################

blah blah of netbeans...
Please replace < with <

<!-- modified for the sake of Jibx bindings -->
<property file="nbproject/buildExample1.properties"/>
<!-- JiBX binding compiler task definition -->

<taskdef name="bind" classname="org.jibx.binding.ant.CompileTask" classpath="${file.reference.jibx-bind.jar}"></taskdef>
<!--
-->
<target name="-post-compile">
<echo message= "${file.reference.jibx-bind.jar}" >
</echo>

<!-- Run JiBX binding compiler -->
<bind verbose="true" load="true" >
<classpath path="${run.classpath}"></classpath>
<bindingfileset dir="${binding.dir}" includes="${binding.filenamepattern}" >
</bindingfileset>
</bind>

</target>
</project>
</quote>
###################################

The modifications are done to the "-post-compile" target. The prefix "-" indicates that this target cannot be called directly. This also uses a "buildExample1.properties" file. The binding information is given this file. The bindings are compiled using this information. In case you are perplexed, the actual information for build is given in build-impl.xml. The build.xml is just a wrapper. Some of the tasks are exposed to the user for allowing customisation and in this case "-post-compile" was choosen.
The contents of buildExample1.properties:
##################################
# The directory containing binding files
binding.dir= D:\\temp\\jibx-gen
# for nested files use **/*binding*.xml, for this example we dont' need it
binding.filenamepattern = *binding*.xml
###################################
The complete code for bean and xml used are also given
##############################
package beandef;

import beandef.Address;

public class Person {
private String name;
private String surname;
private Address address;
/** Creates a new instance of Person */
public Person() {
}

}

package beandef;

public class Address {

private String houseno, street1, street2;
/** Creates a new instance of Address */
public Address() {
}

}
##################################
A good source for learning jibx is http://jibx.sourceforge.net/tutorial/binding-tutorial.html. The above mechanism shows how we can debug projects using netbeans even when there is a need for custom ant tasks.

Saturday, December 1, 2007

The 'Stig' factor - Philosophy

What is Stig factor?
ZEN>>Isolation but observation. Visibility in Invisibility.

Why do I need to know this?
ZEN>> Because Top-gear has this. Because this is the ultimate truth (well a step short of if there is one). Because you asked me. And if you think zen doesnt' involve one thinking of Aston martins, you are wrong. It is always about things like Spider vs. Beetle in the parking lot. I like both of them by the way.

Master explain this in simple English to us 'not so crazy nuts'
ZEN>> Now that you have asked. In this world all entities are related. Some call it 'Butterfly effect'. This association is responsible for all joys and sorrows. Now wise men and truth seekers, who have realized this truth wanted to let others escape the miseries offered by this cruel life. They came up with an brilliant suggestion. That speaks of:
1. Association to disassociation
2. The hardships in breaking all bonds to keep a single bond.

Master but I am a simple programmer
ZEN>> Thats what they said when they came to me. They were simple tv program producers. I still remember that as if it were yesterday.

Master, but who said that
ZEN>> The top-gear chaps of course. They were very concerned about the presenters being more popular than the show. There was a bloke called Gerome clarkson who has nasty prejudice against general motors you know. They wanted some thing which is immune to change. So they brought in Stig. You can realise more about him from http://en.wikipedia.org/wiki/The_Stig . You see there was more than one stig but there is only one stig.

Oh, the zen factor. Things may change but the conception is unique. This what decoupling between modules means
ZEN>> Excellent, now meditate more about decoupling between modules and data bases. One can easily say that data bases are similar to modules with a different avatar. The trick is to store the mapping away from the modules. The modules change but the mapping rarely does. Even if it does you can easily adapt the mapping to the discovered truth

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.