Sunday, August 29, 2010

Facebook Chat with Pidgin

In a previous post, I wrote about how you can use Pidgin to replace Office Communicator (OCS). Now, I will show you how you can use Pidgin for Facebook chat, thereby making Pidgin a truly universal chat client!
  • Start the Pidgin client.
  • Add a new Account with the following details:
    • Protocol: XMPP
    • Username: yourfacebookusername
    • Domain: chat.facebook.com
    • Password: facebookpassword
    • On the Advanced tab, untick "Require SSL/TLS".

Friday, August 27, 2010

Leaving DB

Today is my last day at Deutsche Bank.

I've been at DB for six years. I started as a graduate and, over the years, have learnt so much, not just about technology, business and processes, but also about myself. I've had the opportunity to work with some of the best minds in the bank and have made some great friends. I would like to thank all my colleagues for their guidance and support and for making my time at DB so enjoyable. I will miss you all!

Leaving DB is by far the hardest decision I've ever had to make, but I feel that the time has come for me to move on to new challenges.

I'm certainly planning to keep blogging and tweeting, so stay tuned to find out what I am getting up to.

Journey onward!

Wednesday, August 25, 2010

Software Inventory of my Windows Machine

As I will be leaving my computer soon, I have decided to make a quick list of useful applications currently installed:
CategoryNameDescription
DatabaseAqua Data StudioDatabase Query Tool
DatabaseJaySQLJDBC Database Tool
DatabaseToadOracle Database Tool
DevAntBuild Tool
DevAxis 1.4Web Services Engine
DevAxis 2Web Services Engine
DevChainsawLog Viewer
DevCoberturaCode Coverage Tool
DevEclipseIDE
DevGroovyProgramming Language
DevGWTGoogle Web Toolkit
DevHermesJMSJMS Browser
DevHSQLDBJava Database Engine
DevJADJava Decompiler
DevJavaProgramming Language
DevMavenProject Build Tool
DevPerlProgramming Language
DevPuTTySSH Client
DevPython 2.6Programming Language
DevSciTESource Code Editor
DevsoapUIWeb Service Caller
DevSQLiteSQL database engine
DevTomcatServlet Engine
DevTortoiseCVSCVS Client
DevWinMergeDifferencing and Merging Tool
Devxmlbeans-2.4.0XML to Java type binding
EditorAltova XML SpyXML Editor
EditorEmacs 22.3Text Editor
EditorTextPad 4Text Editor
PerformanceCachemanXPWindows Tuneup Utility
PerformanceCCleanerPC Cleaner
PerformanceDefragglerDisk Defragmenter
PerformanceHijackThisSystem Scanner
ProductivityAlt-Tab Task SwitcherMS Powertoys
ProductivityCalculator PowertoyMS Powertoys
ProductivityClipXClipboard History Manager
ProductivityCmdHere PowertoyMS Powertoys
ProductivityLClockClock
ProductivityPassword SafePassword Manager
ProductivityProcessExplorerTask Manager
ProductivityPsToolsWindows Tools
ProductivitySlickRunQuick Application Launcher
ProductivityTaskixReorder taskbar items
ProductivityTweak UIMS Powertoys
ProductivityUnxUtilsGNU Utilities for Windows
Productivityxplorer² liteWindows File Manager
UtilitiesConvert Image To PDFPDF Converter
UtilitiesdoPDF 7PDF Converter
UtilitiesFoxitPDF Reader
UtilitiesWinRARArchive Manager
UtilitiesxmltidyTextPad addon
WebMozilla FirefoxWeb Browser
WebPidginInstant Messenger
And, ofcourse, browsing wouldn't be the same without my Firefox addons:
Firefox Addons
Adblock Plus
All-in-One Sidebar
British English Dictionary
Cache Status
Colorful Tabs
Delicious Bookmarks
Download Statusbar
DownThemAll!
dragdropupload
FaviconizeTab
Firebug
Fission
Flashblock
Forecastfox
FoxClocks
Greasemonkey
Mouse Gestures Redox
Tab Mix Plus
Tab Preview
Ubiquity
URL Fixer
If you think you have better alternatives for any of the applications above, please write a comment!

Saturday, August 21, 2010

Faster XPaths with VTD-XML

I've recently started using VTD-XML for applying XPaths on large XML documents. DOM is a memory hog and is too slow. However, VTD-XML allows you to run XPaths and provides random access to nodes, similar to DOM, but much more efficiently. You can't apply XPaths with a SAX parser nor can you access nodes randomly or traverse the document easily.

VTD-XML was 60 times faster compared to DOM when processing my XML document (20MB).

This post shows you how to use VTD-XML for fast XPath evaluation.

Sample XML:
I will use the following XML document in the examples below.

<?xml version="1.0"?>
<catalog>
 <book id="bk101">
  <author>Gambardella, Matthew</author>
  <author>Doe, John</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
 </book>
 <book id="bk102">
  <author>Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
 </book>
 <book id="bk103">
  <author>Corets, Eva</author>
  <title>Maeve Ascendant</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-11-17</publish_date>
 </book>
</catalog>
Loading the XML document:
The following code parses the XML file and creates the navigator and autopilot objects.
final VTDGen vg = new VTDGen();
vg.parseFile("books.xml", false);
final VTDNav vn = vg.getNav();
final AutoPilot ap = new AutoPilot(vn);
Selecting all titles:
Print out all the title nodes using an XPath expression of /catalog/book/title. First call selectXPath to compile the expression and then use evalXPath to move the cursor to the selected nodes in the result.
ap.selectXPath("/catalog/book/title");
while (ap.evalXPath() != -1) {
  int val = vn.getText();
  if (val != -1) {
    String title = vn.toNormalizedString(val);
    System.out.println(title);
  }
}
Selecting all book ids and authors:
This one is a bit more involved as a book can have many authors. In the code below, I first run an XPath to select the books and then iterate over the children, selecting the author nodes.
ap.selectXPath("/catalog/book");
while (ap.evalXPath() != -1) {
  int val = vn.getAttrVal("id");
  if(val != -1){
    String id = vn.toNormalizedString(val);
    System.out.println("Book id: " + id);
  }

  if(vn.toElement(VTDNav.FIRST_CHILD,"author")){
    do{
      val = vn.getText();
      if(val != -1){
        String author = vn.toNormalizedString(val);
        System.out.println("\tAuthor:" + author);
      }
    }while(vn.toElement(VTDNav.NEXT_SIBLING,"author"));
  }
  vn.toElement(VTDNav.PARENT);
}
The output is:
Book id: bk101
 Author:Gambardella, Matthew
 Author:Doe, John
Book id: bk102
 Author:Ralls, Kim
Book id: bk103
 Author:Corets, Eva
Related Posts:
Using XPath with DOM

Friday, August 20, 2010

Fixing a ConcurrentModificationException

Question:
The following code throws a ConcurrentModificationException. What additional code can you add between the <FIXME>...</FIXME> tags in order to prevent this exception from being thrown?
final List<String> list = new ArrayList<String>();
list.add("HELLO");
final Iterator<String> iter = list.iterator();
System.out.println(iter.next());
list.add("WORLD");
//<FIXME>

//</FIXME>
System.out.println(iter.next());
Solution:
In this example, a ConcurrentModificationException is thrown because the Iterator detects that the list over which it is iterating has been changed. If you look into the source code for these classes you will find that when an Iterator is created, it contains an int variable called expectedModCount which is initialised to the modCount of the backing list. Whenever the backing list is structurally modified (with an add or remove operation, for example) then the modCount is incremented. As a result, the iterator's expectedModCount no longer matches the list's modCount and the iterator throws a ConcurrentModificationException.

In order to prevent this exception from being thrown, we need to bring the expectedModCount of the iterator and the modCount of the list back in line with each other. Here are a couple of ways this can be done:

1. Reflection:
Reflection is the easiest way to change the internal counters of the iterator and list. In the fix below, I have set the expectedModCount of the iterator to the same value as the modCount of the list. The code no longer throws the ConcurrentModificationException.

final List<String> list = new ArrayList<String>();
list.add("HELLO");
final Iterator<String> iter = list.iterator();
System.out.println(iter.next());
list.add("WORLD");
//<FIXME>
/* Using Reflection */
try{
  //get the modCount of the List
  Class cls = Class.forName("java.util.AbstractList");
  Field f = cls.getDeclaredField("modCount");
  f.setAccessible(true);
  int modCount = f.getInt(list);

  //change the expectedModCount of the iterator
  //to match the modCount of the list
  cls = iter.getClass();
  f = cls.getDeclaredField("expectedModCount");
  f.setAccessible(true);
  f.setInt(iter, modCount);
}
catch(ClassNotFoundException e){
  e.printStackTrace();
}
catch(NoSuchFieldException e){
  e.printStackTrace();
}
catch(IllegalAccessException e){
  e.printStackTrace();
}
//</FIXME>
System.out.println(iter.next());
2. Integer Overflow:
Another approach is to keep modifying the list until the integer modCount overflows and reaches the same value as expectedModCount. At the moment, modCount=2 and expectedModCount=1. In the fix below, I repeatedly change the list (by calling trimToSize), forcing modCount to overflow and reach expectedModCount. This code took 38s to run on my machine.
final List<String> list = new ArrayList<String>();
list.add("HELLO");
final Iterator<String> iter = list.iterator();
System.out.println(iter.next());
list.add("WORLD");
//<FIXME>
for(int i = Integer.MIN_VALUE ; i < Integer.MAX_VALUE ; i++){
  ((ArrayList)list).trimToSize();
}
//</FIXME>
System.out.println(iter.next());