Tuesday, April 8, 2008

Using anonymous class for command strategy!

In my small consulting bit, I recently came across a client’s requirements, part of which is mentioned below:
1) Data comes in a flat file, in comma delimited format.
2) Each line represents some sort of actions. The server makes sure that the actions represented in each line are sequential.

But he now needs further interpretation and more “actions” and his serverside coders have extended the interpretations and hence requirement for extension at clientside. They have given him a set of “reusable lib” to interpret the textual lines and returning into meaning objects! And conveniently left for the client to decide what to do with those “meaningful” objects. The previous clientside design just took each line and parsed the info, and did some stuff like executing a work flow, persisting the interpreted line details in db, notification to clientele etc.
Many of the older objects and methods are still much applicable even now. Like earlier execution of workflow, persistence and notification codes.

The older “IntegratorObject” (I didn’t name it!) was full of “if-else” conditions and a huge block of code. Would definitely like to refactor that! Factory method, strategy .. all came rushing onto my mind.

Then came my client, “oh! Java is oops, so these would be easily fit using reusable components and codes from older program, right?” (Advice: never tell a client that his existing code is crap! And I needed the money) ... so, I told him that I would make his “reusable” code into extensible code so that he can add and reuse stuff but not have to break exiting. “Whatever! should work as you promise and as efficiently as earlier. And not break anything!”... Okkkayyy!
Step1: I decided that each line must interpret and a “command object” created.

public interface IntegratorCommand {
void execute();
}

Step2: subclassed “IntegratorObject”, since some of the states and methods can only be used from within for sake of the “reusable methods”!! Overrode the onMessage(), and added a method for command object creation which would act as factory method and made the new Class as abstract.

public abstract class AbstractIntegrator extends IntegratorObject {
....
....

@Override
public void onMessage(BufferedReader reader) {
String lineInput;
try {
lineInput = reader.readLine();
while (lineInput != null) {
IntegratorCommand cmd = getIntegratorCommand(lineInput);
cmd.execute();
lineInput = reader.readLine();
}
reader.close();
} catch (RuntimeException e) {
...
}
}
protected abstract IntegratorCommand getIntegratorCommand(final String textInput);

}


[[NOTE: If you are wondering, why I subclassed and broke the onMessage() which is clear violation of all OOD principles, well firstly I didn’t want to do any modification/refactoring to their existing codebase (saving my neck!), secondly, yes I could have used a wrapper class delegating the needed instructions, but I also wanted to show to the client whys and hows. Served my purpose later!]]

Step3: So far, so good, I now derive a new class out of “AbstractIntegrator” and implement the “getIntegratorCommand()”right? Therein lies the problem, because of my client wanted to “reuse” the existing codebase which dependent heavily on states already present on older implementation (and I didn’t want to do touch their code). Building any extrinsic and sharable strategy objects would be difficult, and I would be forced to create intrinsic strategies which must be passed the reference of the “IntegratorObject” so that it can read its various states. And then pre-create strategies in constructor of IntegratorObjects (or use setter method!):



public class IntegratorStrategy {
public IntegratorStrategy(IntegratorObject integrator);
....
}
//in constructor of IntegratorObject types
IntegratorStrategy strategy = new IntegratorStrategy(this);


Now such predefined “typed and named” intrinsic strategies are helpful but can have following limitations
a) Cannot be loosely couple and shared (extrinsic)
b) Must be created within the scope of parent.
c) Circular reference. If you do manage to create sharable instances outside, and use setter methods for IntegratorObject ref and used in multi-threaded applications, you can run into problems because of programming error. Surely if you do not document the ordering of synchronized object method calls.
[Ok, I am thinking too much, just trying to cover possibilities]

Now, I could start thinking of some mindboggling extrinsic strategy design which would still require me to pass “IntegratorObject” states to each strategy making them more and more complicated and prone to breakage during extensions! I want the strategies to have access to the “IntegratorObject” instance and yet be defined concisely and be used in the factory method of “getIntegratorCommand()”. {{thinking! thinking....}} What about using “Anonymous Inner classes”?

Step4:

public class IntegratorV2 extends AbstractIntegrator {
....
....

@Override
protected IntegratorCommand getIntegratorCommand(
final String textInput);
IntegratorCommand cmd = new IntegratorCommand() {
@Override
public void execute() {}
};

//depending on textInput, different command objects are created
if (textInput.startsWith(“WF”)) {
cmd = new IntegratorCommand(){
public void execute() {
//serverside provided utils!!!
Object obj = IntegratorUtils.getWorkflowVO(textInput);
//superclass method
executeFlow(obj);
}
};
} else if (textInput.startsWith(“SND”)) {
cmd = new IntegratorCommand(){
public void execute() {
Object obj = IntegratorUtils.getNotifyMsg(textInput);
//superclass methods
notify(obj);
}
};

}
//and so on...
return cmd;
}
}


[[NOTE: I didn’t like the “if” statements and I fixed them other ways later, but the above should carry the point I am trying to drive]]
Everything works perfectly. Note the use of keyword “final” in getIntegratorCommand()for the String argument passed. This is required for call stack persistence of argument values. Otherwise, you can always use a property (and a setter method) to achieve that.
A nice use of abstraction. (and old way of forming “Closures” although the created command objects never lives outside the scope of the creator, in this case. But nothing stops the commands to be deferred calls. Isn’t that the idea of a command pattern anyway?)
The client finally saw the point and I did get the opportunity to refactor bit of their older code (and earn a bit extra money. Who said consulting was easy!)
I have renamed and changed the contexts a bit and omitted other irrelevant details. Sorry I couldn’t give much detailed information. Secrecy! Blame it on the rain!

P.S: I eventually used mix of anonymous class and genericized strategies.

Sunday, April 6, 2008

I love Spring!

I love Spring! The season as well as the framework. :)
For example, for one of my clients I had to install XPlanner as a project tracking and monitoring tool. (Yeah! I know there maybe a better XP tool available but XPlanner is simple and client likes it!) Anyway .. .

I downloaded xplanner 0.7b7 and tried to install under Vista Premium, running JRE 6 and Tomcat 6. This version of XPlanner is built on Spring 1.2 and I got this error

“org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.apache.commons.collections.map.LinkedMap] to required type [java.util.HashMap]”

Checking onto source file showed that “MetaRepositoryImpl” takes a “java.util.Hashmap” as its “repositories” property. However for some reason Spring was constructing a LinkedMap instance for “< Map >” declaration in bean configuration. Hmmm .. needed further investigation but I did not have time!
Now I had two options:
1) I could modify the source of “MetaRepositoryImpl”.
2) Think about spring way - Below is what I did.
Previous=>

< property name="repositories">
< map>
< entry>
....
< /entry>
......
< /map>
< /property>



Changed to=>

< property name="repositories">
< bean class="java.util.HashMap">
< constructor-arg>
< map>
< entry>
....
< /entry>
......
< /map>
< /constructor-arg>
< /bean>
< /property>


Done! Spring saves the day!

Monday, March 24, 2008

A lil homework

As part of my lil homework yesterday, I had this 'kahani'.

'A toy is controlled remotely over some grid like terrain. Along with its positional coordinates, the toy also maintains its facing (North, South, East, West) so that when remotecontrol commands 'move' - it moves in its facing direction. On turnLeft command, if facing North it would face West. On turnRight command, facing East then would be South. And so on .. '

Well smarty pants, I, declared an enum type.
public enum Facing {North, South, East, West;}


Writing a function to turn things right seemed simple enough!

public void turnRight() {
switch (this.currentFacing) {
case East : this.currentFacing = Facing.South; break;
case West : this.currentFacing = Facing.North; break;
case North: this.currentFacing = Facing.East; break;
case South: this.currentFacing = Facing.West; break;
default: break;
}
}


the similar way to implement the turnLeft() method!!! Now, this didn't seem to tough, eh? Ehrrrrrr! too many conditional statements? If say a new facing (NorthEast) is added, then for things to work, existing code would have to be broken and modified?

Now, Java 5 came with linguistic support for enumerated types enum. All enums implicitly extend java.lang.Enum and allow you to add typical class constructs. 'enum' is also Comparable and Serializable. Now, I could write some complicated program to solve the conditional expressions above. But hey all java enum return an array of declared enums through enum.values(). Voila!

So I wrote:

public enum Facing {North, East, South, West;} //change in order!

public Facing getFace(Facing refFace, int factor) {
int currentIdx = 0;
Facing[] faces = Facing.values();
for (int i=0; i < faces.length; i++) {
Facing face = faces[i];
if (face.equals(refFace)) {
currentIdx = i;
break;
}
}
currentIdx = currentIdx + factor;//the next facing index
if (currentIdx < 0) {
while (currentIdx < 0)
currentIdx = currentIdx + faces.length;
} else if (currentIdx >= faces.length) {
currentIdx = currentIdx - faces.length;
}
return faces[currentIdx];
}


if I consider turning left/anticlockwise as negative ..

getFace(Facing.North, -1); //=>Facing.West
getFace(Facing.North, +5); //=>Facing.East


So in the end, my turnRight() and turnLeft() turned to

public void turnRight() {
this.currentFacing = getFace(1, this.currentFacing);
}
public void turnLeft() {
this.currentFacing = getFace(-1, this.currentFacing);
}

Even if I go and change the Facing declaration to something like
public enum Facing {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest;}
things would still work as expected if you want to turn left from say North Facing 1 step to NorthWest, 2 steps to West or even turn 720 deg! :)

Think about implementing the same for Days of week, or Months and do similar mathematics on them.
 
day = add(Day.Monday,4);
day = add(Day.Monday,-3);
month = add(Month.January,-3);


You can further generalize things further using
day.getClass().getEnumConstants()

enum is Tiger introduced. Prior to that java, standard way was like declaring a public static final constant usually in an interface/abstract class! For example, how do you add days, month or years to a calendar object?
calendarInstance.add(Calendar.DATE, 2);

Notice the similarity of definition but the differentiation? Declared in Calendar class
public static final int DATE = 5; 

if only java had operator overloading instead of the exotic darker beans and means for the riches! :((

Sunday, March 23, 2008

VS2005 installation on Vista!

finally managed to install Visual studio 2005 on Vista Home Premium. Slow-ish internet bandwidth didn't help. After installing VS, I had to apply Team suite SP1. and then run VS2005 Sp1 for vista, which is kinda like service pack for service pack. These aren't just the patches, I still have to run and fix for various other patches toolsets and extensions, but I will take them on need basis. If you have UAC enabled, it can be frustratingly slow and irritating

There are plenty of articles thankfully on MSDN and on internet, which does provide detailed information.

http://mvolo.com/blogs/serverside/archive/2006/12/28/Fix-problems-with-Visual-Studio-F5-debugging-of-ASP.NET-applications-on-IIS7-Vista.aspx (ASP.NET problems)
http://support.microsoft.com/kb/937523
http://support.microsoft.com/?kbid=929470

oh, VS.NET 2002 and 2003 aren't supported on Vista, but VB6.0 supported??? I am also facing problem with windows explorer. Crashes and restarts once in a while. I am guessing that its a incompatible shell extension. No time to debug and fix .. My vista experience has been bumpy alright! (Humpty dumpty sat on a wall, humty dumpty had a great fall?). I like to adopt softwares early and its fun sometimes finding ways around blocks, but it can be maddenning when pressed for time.

Gonna watch F1 (Sepang) and then got some 'homework' to do ..

hasta la vista!

Friday, March 21, 2008

YASST (Yet another SQL scripting tool)

I was trying to revive my almost broken down PC, when I unearthed this program, SQLScripter.
What does it do? It reads excel spreadsheets, and generates SQL scripts for each row. Simple rules guide the format of XLS file:
1. Spreadsheet name decides the TABLE name.
2. Within each spreadsheet, first row denotes the COLUMNS. By default first column is taken as primary key, unless you mention
3. The Last column denotes action to be performed. (INSERT, UPDATE or DELETE)
How it works?
1. Pick up the excel file. The UI helps you choose the spreadsheets you want to script. Clicking a button generates the SQL scripts and displays in a text area (optionally multi panes for each TABLE).
2. Edit, save as SQL file(s).

Deploy: Single exe and a properties file on Windows 2000 and above (checked on vista .. works!) . Oh, you should have excel or equivalent extentions (eg Microsoft works)

I wrote it initially in Delphi 5. (I recompiled it today on D7, no change required). It uses OLE Automation to connect to MS Excel and read contents. I wrote it for Netkraft, who wanted a pilot for certain client. I went to Netkraft’s office one afternoon, took me about 5/6 hours or so, for the first version. (Never really got paid for that, but got something else going .. different story!)

Later on, I added few other features. As simple it maybe, 4 organizations still use it extensively – two of them in production and support!! So I thought maybe, just maybe, this old outdated thing can be of some use to somebody! :))
Now, where do I upload the source codes?

Wednesday, February 27, 2008

my new notebook!

I recently upgraded from my old notebook - an Acer AMD Turion TL-50. I got one Dell XPS, Centrino T7500, Dual Core, 2.2GHz, 4 MB L2 cache, 800MHz FSB, 3GB Ram, 160GB Sata HDD, a basic NVidia GeForce 8000 with 128 MB memory and other regular stuff.

1. Most of the brands come packaged with no scope for upgrading components, other than dell.
2. I have used AMD throughout and I abused my acer notebook (TL-50) and home PC to the max. so no complaints. But I couldnt find a notebook with TL-60 or above (within my budget), and AMD has been lagging in the notebook processor compared to Intel. Hence the decision to go with a Intel Centrino.
3. Most branded and packaged notebooks, came with hi end graphic and audio cards, and additional multimedia, of which I have no use. Only dell allowed me to adjust the configuration, and I cut down on video and audio cards.
4. Pricing: Any decent notebook with a T7300 or above came close to 100,000 INR or above. Like Mac, Ferrari, Fujitsu, HP, Lenovo and Sony included. Sony did have one with T7250 costing about 56K, but the notebook seemed like a tin-case.
5. India Models: Many of the brands will market the same model with downsized configuration and still equivalent to Euro prices. For example Acer 5920 Gemstone (yeah - the beige keyboard one), US model comes with T7300 processor but in India its T5450 processor. Same for some HP pavilion notebooks. This is blatant coning!
6. I could not wait for someone to come down from US to get me a notebook. I needed now.

In the end I found, none other than dell provided value for money and option for configurability. Upgraded, it cost me ~63K. It would have been only about 54/55K, if not for the excise duty on components, which for other brands are not applicable I guess. Anyway, I do not think I would have got such a config with other brands for less than 100K. Btw, whats the need for a 2MP webcam??

Saturday, February 9, 2008

now devcamp ..

I went to attend the first devcamp 'unconference', held at ThoughtWorks , Bangalore premises.

Devcamp is offshoot from Barcamp, (foocamp->barcamp->devcamp!), intent towards the developers. As Barcamp India went on a different tangent, Devcamp intended to connect and capture the imagination of the developer community.

Martin Fowler was present and set the context of unconferencing mode of ideating, brainstorming. The ‘unconference’ took place at TW office. Organized in terms of talks, discussions, and Lightning talks, sessions happened simultaneously at TW’s various discussions/conference rooms and open working spaces. Subjects were varied from semantic web to android programming to LINQ to ruby for gaming, symbian PIPS etc to discussions of ‘if facebook an enterprise app’. ?? (Whatever..)


Discussions were sometimes too loosely-coupled and I used the extra time to hop at other talks and discussions!

Microformats and Semantic web talk was very interesting, although I am skeptical about feasibility of standardization process and implementations. Checkout Poshzone (Plain Old Semantic Html zones) .

I attended an interesting session by Karthik Ramachandra on Load testing with Erlang. Some more work and will cut jmeter like butter. :)

I missed out Ravi Mohan‘s talk on ‘Monads’. The session was on placeholder, and I went to attend another session. I wanted to speak to him about scala and osgi, but he left soon after.

Lightning talks were interesting. 3 minutes of talking about virtually anything, and people demonstrated/spoke about handheld gaming device, to online video mashups to just ideas etc.

For many developers, focus seemed to be in RIA development. Bringing the web to the desktop! Lots of activity around AIR, Silverlight, Django.

Time allocated per talk/discussion was 30 mins, which hardly left scope for detailed discussions. For example, talking about say advanced Eclipse Plugin development, within 30 mins? Nor is the time enough for forthcoming technology talks on Adobe AIR, for eg.

I should have done a talk on OSGi or Spring dynamic modules, as there turned out to be some slots available by absence, but I wasn’t prepared with demo-able code samples or slides.


Sidu did a commendable job in organizing the whole meet with other Barcamp volunteers. Although it had its share of photography-‘enthusiasts’, web 2.0 innovators, publicists etc, I look forward to attending Devcamp next year.

info: LinuxAsia is conducting an event of open source technologies at Bangalore next week. Check out www.osiweek.com



Btw remembered this .. “For a successful technology, reality must take precedence over public relations, for Nature cannot be fooled.” - Richard Feynman

Monday, January 21, 2008

Freerice

[November 14th, 2007]
A friend of mine (Paul) sent me the link. http://www.freerice.com. The endeavor is by “John Breen”, who is a US fundraiser. The idea is to use a game to build vocabulary, to donate rice to the needy. For every correct answer, the website donates 10 grains of rice. Companies advertising on the website provide the money to the UN’s World Food Programme (WFP) to buy and distribute the rice. http://news.bbc.co.uk/2/hi/europe/7088447.stm

While, I do not know why go by grains-of-rice, rather than grams-of-rice, I was free to check the website and contributed to about 500 grains of rice. For many words, (and they keep getting tougher), I had to refer to dictionary.com, but for once there was no guilt factor for cheating. While I was at it, I was thinking that .. this is a great way to increase someone’s vocabulary but this is a game that requires time, and my objective is also to donate more and more rice, which I can achieve only by correct answers. If technology can be used to raise money, I can also use technology to manipulate towards helping the eventual goal.

I can think of writing a program so that I can just keep running it the whole day. J How? Think of writing a simple selenium test case to run through the website, capture the element that contains the word requested, and also capture the choices given. Once I know the requested word, an Ajax call to any semantic search system can give you the results, and run a simple regular expression to match against the choices to come up with a rating. Again, use selenium to select the best possible answer and submit the request. Put this in a loop and keep running through out the day. With java 6’s new scripting apis, you can probably do without a selenium test case. More on java scripting api later.

to begin ..

The purpose of this blog, is to jot down my occasional ramblings on IT and software and sometimes other technological aspects, F1 aerodynamics including.

Yip Harburg wrote "Don't get smart alecksy, With the galaxy, Leave the atom alone" .. well I thought that way once, but once I experienced the "kick in the discovery", I moved on to find a different meaning altogether, and software long ceased to exist as extrasomatic knowledge to me.

Over the years, I worked on varied platforms, programming languages on different domains. I never really swore loyalty to any single software technology as such. Every time I was looking forward to working on things I have never worked before, and thereby posing the biggest challenge. Of course you would want to carry forward, utilize and maximize your learning and understandings, and thereby maybe I am little more proficient in some technologies than others. Essentially I am a jack of many trades, but probably not mastered many! :)

Many of my codes, components, ideas are lost in transition, should I say. Some are somewhere dangling on the web, some I can’t find, and many are obsolete. Many I had to take off for they would be so simple to do now and many don’t work in modern environments.

I will try updating with the older works here but I will have to figure out a way to share the bigger chunks of code. I am soon going to get a domain and probably rent a virtual server. Till then let this be the source of the one! :)