Ubuntu insights, Programming in groovy, java, et als!

Thursday, December 22, 2011

Running Cobertura as Ant Script

I posted about configuring eCobertura plugin for eclipse in the previous blog post. In this post, I will describe the bare minimum steps to run Cobertura as an ant build script from eclipse. Cobertura unlike eCobertura plugin, provides advanced code coverage reporting in xml and html formats. This is much more flexible for custom configuration as it can be controlled via the ant script. Replicate the steps below :

1) Download cobertura zip.

2) Create a java/groovy project named "Sample" in eclipse.

3) Create a simple java/groovy class and a corresponding unit testcase class to test it.. Place them in seperate or same packages as you wish.

4) Create a folder called lib under the Sample project folder (as Sample/lib).. Place junit.jar, apache ant.jar, and all the jars in the downloaded Cobertura zip (most importantly cobertura.jar) into the lib folder and include the jars in the project's classpath.

5) From the downloaded Cobertura zip, pick up the two files build.properties and build.xml and place them in your Sample project base. (as Sample/build.xml and Sample.build.properties)

6) The build.xml is the ant script to run Cobertura and it uses the configuration in build.properties file.

7) Change the contents of build.properties as shown below:


# The source code for the examples can be found in this directory

# The path to cobertura.jar

# Classes generated by the javac compiler are deposited in this directory

# Instrumented classes are deposited into this directory

# All reports go into this directory

# Unit test reports from JUnit are deposited into this directory                          

# Coverage reports are deposited into these directories


8) Now change the contents of build.xml as shown below:



<?xml version="1.0" encoding="UTF-8"?>

<project name="Sample" default="coverage" basedir=".">

    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner &lt;thekingant@users.sourceforge.net&gt;
    Copyright (C) 2006 Dan Godfrey
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY

 <property file="build.properties" />

 <path id="cobertura.classpath">
      <fileset dir="lib">
           <include name="*.jar"/>

 <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
 <target name="init"> 
  <mkdir dir="${classes.dir}" />
  <mkdir dir="${instrumented.dir}" />
  <mkdir dir="${reports.xml.dir}" />
  <mkdir dir="${reports.html.dir}" />
  <mkdir dir="${coverage.xml.dir}" />
  <mkdir dir="${coverage.summaryxml.dir}" />
  <mkdir dir="${coverage.html.dir}" />

 <target name="compile" depends="init">
  <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
   <classpath refid="cobertura.classpath" />

 <target name="instrument" depends="init,compile">
   Remove the coverage data file and any old instrumentation.
  <delete file="cobertura.ser"/>
  <delete dir="${instrumented.dir}" />
   Instrument the application classes, writing the
   instrumented classes into ${build.instrumented.dir}.
  <cobertura-instrument todir="${instrumented.dir}">
    The following line causes instrument to ignore any
    source line containing a reference to log4j, for the
    purposes of coverage reporting.
   <ignore regex="org.apache.log4j.*" />
   <fileset dir="${classes.dir}">
     Instrument all the application classes, but
     don't instrument the test classes.
    <include name="**/*.class" />
    <exclude name="**/*Test.class" />

 <target name="test" depends="init,compile">
  <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
    Note the classpath order: instrumented classes are before the
    original (uninstrumented) classes.  This is important.
   <classpath location="${instrumented.dir}" />
   <classpath location="${classes.dir}" />
    The instrumented classes reference classes used by the
    Cobertura runtime, so Cobertura and its dependencies
    must be on your classpath.
   <classpath refid="cobertura.classpath" />
   <formatter type="xml" />
   <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
   <batchtest todir="${reports.xml.dir}" unless="testcase">
    <fileset dir="${src.dir}">
     <include name="**/*Test.java" />

  <junitreport todir="${reports.xml.dir}">

   <fileset dir="${reports.xml.dir}">

    <include name="TEST-*.xml" />


   <report format="frames" todir="${reports.html.dir}" />



 <target name="coverage-check">

  <cobertura-check branchrate="34" totallinerate="100" />


 <target name="coverage-report">


   Generate an XML file containing the coverage data using

   the "srcdir" attribute.


  <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />


 <target name="summary-coverage-report">


   Generate an summary XML file containing the coverage data using

   the "srcdir" attribute.


  <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />


 <target name="alternate-coverage-report">


   Generate a series of HTML files containing the coverage

   data in a user-readable form using nested source filesets.


  <cobertura-report destdir="${coverage.html.dir}">

   <fileset dir="${src.dir}">

    <include name="**/*.java"/>

    <include name="**/*.groovy"/>




 <target name="clean" description="Remove all files created by the build/test process.">

  <delete dir="${classes.dir}" />

  <delete dir="${instrumented.dir}" />

  <delete dir="${reports.dir}" />

  <delete file="cobertura.log" />

  <delete file="cobertura.ser" />


 <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>



9) Running the above build.xml as an ant script in eclipse will create a folder hierarchy. The generated xml and html format reports can be found at Sample/cobertura/complete folder.

PS: Instead of running the script from eclipse you can also run the build.xml from a command line using the ant -[options] command.

Sunday, December 18, 2011

Eclipse : Configuring eCobetura for Code Coverage

Recently, I stumbled across cobertura while searching for a decent code coverage utility for my java and groovy units. Major parts of Cobertura are under the GNU public license which I found would suffice my need for basic code coverage. You can read more about cobertura here at http://cobertura.sourceforge.net/

eCobetura is a comfy eclipse plugin that generates a code coverage report within the eclipse console itself..Here is how you can configure it.

Eclipse update site for eCobertura


In eclipse, Under Help menu -> Install New software, add up the above (url) update site..

Check eCoberture code coverage option and proceed with Next until all the dependencies get installed..

Restart eclipse after installing the eCobertura plugin..

Sample unit test code coverage using eCobertura
Create a sample Java/Groovy project.

Create a java/groovy class PrintUtil

public class PrintUtil {                        
public String printHello(String msg){      
return "Hello "+msg;                    

public String printGoodday(String msg){    
return "Good day "+msg;                 

Likewise create a JUnit/Gunit testcase class that will test the functionality of PrintUtil class.

import junit.framework.TestCase;                
public class PrintUtilTest extends TestCase {   
PrintUtil util = new PrintUtil();           
public void testPrintHello(){               
String out = util.printHello("World");  
assert(out=="Hello World");             

public void testPrintGoodday(){             
String out = util.printGoodday("World");
assert(out=="Hello World");             

In eclipse, Under Window menu -> Show View -> Other, Select the Coverage Session View option to view the eclipse console for the code coverage results of the above unit test..

Select the PrintUtilTest testcase in the eclipse package explorer, Find the new shorcut for eCobertura in the eclipse toolbar (probably just best the debug menu icon).. Execute the PrintUtilTest as a junit under the Coverage As -> JUnit Test option.. The complete code coverage details will be shown in the Coverage Session View similar to the one shown below..

PS: This post focuses only about eCobertura, an eclipse plugin for Cobertura which can be used in tandom while running junits in eclipse.. However the original, Cobertura has more advanced features to generate a complete XML/ HTML reports on code coverage, etc..It can be run from command line or as an ant or Maven build.. You might want to exploit it for advanced reporting options.. 

Monday, December 05, 2011

Pharo Beginner : My first Smalltalk Program

DockingBarMorph new
  position: 0@225;

        addMorph: (SimpleButtonMorph new
                          label: 'Close';
                          target: [DockingBarMorph allInstances last delete];
     height: 55;
                          actionSelector: #value);

        addMorph: (SimpleButtonMorph new
            label: 'Open Transcript';
                          target: [Transcript open.
                                        Transcript show: '*** Default text in Transcript ***'
                          actionSelector: #value);

addMorph: (SimpleButtonMorph new
                        label: 'Open Browser';
     target: [Browser open.];

       addMorph: (SimpleButtonMorph new
                            label: 'New Workspace ';
                            target: [Workspace new open.];

addMorph: (SimpleButtonMorph new
                          label: 'Dock';
                          target: [UIManager inform: 'Hello world.. This is a sample Dock..'];
                         height: 55;
                          actionSelector: #value);


Tried to add up custom launchers for pharo development utilities. Currently implemented new Workspace open, System Browser, Transcript, etc.. Will have to make it complete so that the dock should be able to launch every component under the conventional right click popup in the pharo environment..

A simple pharo starter program which you can fiddle and extend, after a thorough understanding on ProfStef go.

Friday, December 02, 2011

Tutorial : List Operations in Python

""" All text within triple quotes is treated as comments in python """
""" This tutorial explains lists in python with the simplest operations as examples"""
""" Standard string concatenation using + operator """
""" Prints the message in a new line """
def printMessage(str):
print ">>>>> "+str

""" For loop : A similiar equivalent of each closure in groovy """
""" Note that indents are the only way to tell the interpreter about the blocks """
"""Also note that the below print it, prints elements in same line with space separated i.e a typical equivalent of System.out.print in java"""
def printList(aList):
for it in aList :
print it,

""" ********************** Start scripting : list operations ********************** """
list = [10, 1, 2, 3, 4, 5, 6]

"""*** Print the elements of the list *** """
printMessage("Initial elements in the list : ")

"""*** add an element to the end of the list *** """
lastVal = 9
printMessage("Elements after appending a new element "+str(lastVal)+" at the end of the list : ")

"""*** insert element at index i *** """
insertVal = 8
indice = 7
list.insert(indice, insertVal)
printMessage("Elements after inserting : "+str(insertVal)+" at index : "+str(indice))

"""*** sort the elements ascending order by default *** """
printMessage("Elements after sort : ")

"""*** Reverse the elements : same as groovy *** """
printMessage("Elements after reversal : ")

"""*** Removes the last element *** """
printMessage("Elements after removing last element : ")

"""*** Removes element at specified index *** """
"""Note the string cast below..A typical toString() equivalent in java"""
printMessage("Elements after removing element of index at : "+str(index))

"""*** Removes element with the value specified *** """
printMessage("Elements after removing the value : "+str(value))

""" *** number of times the element 1 occurs in a *** """
countFor = 1
printMessage("The number of times element : "+str(countFor)+" occurs in the list")
print list.count(countFor)

""" ****************** Some more looping and branch conditions ****************** """

""" *** Find the smallest element in the list using a typical for equivalent of eachWIthIndex groovy closure*** """
small = list[0]
smallestElementIndex = 0
for index, item in enumerate(list):
if item < small :
small = item
smallestElementIndex = index
print  "The smallest element of the list is "+str(small)+" at index "+str(smallestElementIndex)

""" *** While loop implementation *** """
printMessage("A simple while loop in python to convey : ")
sizeOfList = len(list)
while i<sizeOfList:
print "Python is fun \m/"
i = i + 1


Output for the above list operations performed :

>python -u "PythonBasicListOps.py"
>>>>> Initial elements in the list : 
10 1 2 3 4 5 6 9
>>>>> Elements after appending a new element 9 at the end of the list : 
10 1 2 3 4 5 6 9 9
>>>>> Elements after inserting : 8 at index : 7
10 1 2 3 4 5 6 8 9 9
>>>>> Elements after sort : 
1 2 3 4 5 6 8 9 9 10
>>>>> Elements after reversal : 
10 9 9 8 6 5 4 3 2 1
>>>>> Elements after removing last element : 
10 9 9 8 6 5 4 3 2
>>>>> Elements after removing element of index at : 3
10 9 9 6 5 4 3 2
>>>>> Elements after removing the value : 9
10 9 6 5 4 3 2
>>>>> The number of times element : 1 occurs in the list
The smallest element of the list is 2 at index 6
>>>>> A simple while loop in python to convey : 
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
>Exit code: 0

Tuesday, November 22, 2011

Why Linked List is a Linear Data Structure?

Reminiscing my textbook definitions during graduation, all I ever read about linear data structures was that, they have elements placed adjacent to each other. Now, I curse myself for not being able to understand the inners of the concept rather than trying to perceive the whole thing at superficial level.

As I dig through this, I see that there are two aspects to the term linear. One is at the physical level (in bits and bytes of memory), other at the logical level (concerning the data structures used). At the logical level we talk about data structures as being linear or non linear. But in reality, at the physical level, computer memory is always linear i.e one memory block adjacent to other.

The concept of non linearity is (usually) implemented with the help of pointers that connect other memory chunks by storing their addresses. Technically, implementing pointers at the physical level is somehow helping us to understand non linearity at the logical level implementation of data structures. Assuming so, I was stumped at this point, wondering why a linked list is considered linear in spite of the nodes never being physically adjacent.

After an overwhelming head-breaking session, I started to see this differently. The terms linear and non linear are purely meant to be viewed at the logical level when used along side data structures. If a list is being used, irrespective of whether it is an array implementation or a linked list implementation, it should only be perceived as a data structure that stores elements adjacently (logically, abstract picture) and hence it is linear.

If the confusion still lasts, there is this thumb rule that you can take help of, to recheck if a data structure is linear or non linear. If you are required to sequentially traverse through all the elements of a data structure to access its nth element, then the data structure is linear. Else it is non-linear.

Example : Stacks, Queues, Lists are always linear. Irrespective of whether they are implemented using pointers or arrays, you need to sequentially traverse through the whole data structure to access the nth element which is not so in the case of trees and graphs wherein to access one element, traversing a specific branch might suffice.

Tuesday, November 15, 2011

Solve Graphic Driver Errors & Unity 3D Display Issues in Ubuntu 11.10

Graphic card compatibility issues with Ubuntu are very annoyingly frequent during installation. I've been a sufferer of the same recently when my graphic card drivers crashed and Unity 3D failed to render correctly, Instead it reverted to Unity 2D version which pretty much sucks. Here are some insights into Unity panel issues w.r.t the non-availability or incompatibility of your graphic driver.

Test if unity is supported  :
Type in terminal

/usr/lib/nux/unity_support_test -p 

You need to get an output saying Unity is Supported : Yes. If you don't, there are things that need to be answered which of course will be, in the below steps.

How do you know if Unity 3D is running :
In the terminal, type in


If the output of the above command is Ubuntu, Unity 3D is enabled and running.
If the output of the above command is Ubuntu-2D, Unity is running in 2D mode.

PS: Make sure you are logged into Ubuntu and not into Ubuntu 2D. Verify this prior to login.

Test if Graphic card is detected :
In the unity dash, search for system Info. The System info will show you Processor, Memory, Graphics and other details. If the graphics details displayed is empty, it means that either the graphic card installed is not recognized or you might not have installed a graphic driver at all.

Moving On :
There are three ways of looking at any issue relating to graphic driver installation.

1) Install default binaries provided by Ubuntu (FGLRX drivers) (or)
2) Install Open source drivers to get the graphic card detected & running (or)
3) You either install the ATI binaries from the official ATI site.

You must stick to only one among the above three aproaches, out of which the Second option to install third party open source drivers is the best in my opinion. Here I've explained the first two approaches in detail.

Prior to taking any of the above approach, run this command to install dependencies :

sudo apt-get install build-essential cdbs fakeroot dh-make debhelper debconf libstdc++6 dkms libqtgui4 wget execstack libelfg0 dh-modaliases

1) Install FGLRX drivers provided by Ubuntu (I don't recommend)
To install the FGLRX drivers, search the Unity dash for Additional Drivers, Click on the appropriate FGLRX driver suggested and activate it.

2) Install Open source Graphic Drivers (Recommended)
The below command will remove all traces of Ubuntu's default fglrx drivers (if installed).

sudo apt-get remove --purge fglrx fglrx_* fglrx-amdcccle* fglrx-dev*

Remove the existing xorg.conf 
sudo rm /etc/X11/xorg.conf

Reinstall the xorg.conf
sudo apt-get install --reinstall xserver-xorg-core libgl1-mesa-glx:i386 libgl1-mesa-dri:i386 libgl1-mesa-glx:amd64 libgl1-mesa-dri:amd64

Configure xorg

sudo dpkg-reconfigure xserver-xorg

Reboot the system
sudo reboot

The above series of commands are most important, necessary and typically are the installation steps involved in using open source Graphic drivers to detect your graphic card.

PS: These open source drivers worked spot on with my Dell system with an AMD Radeon™ HD 6470M - 1GB (For ICC) video card. And should hopefully do the job for you too :)

Count the Lines of Code in Your Project

I was searching through eclipse if there was an option somewhere within the IDE that could count the total lines of code in my project. After 5 min of Google struggle, I got to know that I need to install a metrics plugin to be able to do so. Reluctant to install any additional plugin, I resorted to groovy, spending an equivalent 5 min of time for this piece of code that counts the total number of lines in all the files within a directory at any depth.

Run the below script with dirPath variable pointing to your project base and addup comma seperated extensions in the filterFileExtensions variable list to specify the filetypes you wish to index , as in java, groovy or any text rendering file extensions like txt, html, etc. 

//------------------- Start Groovy Script --------------->

def filterFileExtensions = ["groovy", "java"] 
def dirPath = 'C:\\VKWorks\\Sampleproject'

def base = new File(dirPath)
def obj = new FileUtil(list:filterFileExtensions)
//------------------- End Groovy Script --------------->

class FileUtil{
def lineCount = 0
def filesProcessed = 0
def list 

def recurseCountCodeLines(file){

    def fileExtn = file.getName().tokenize(".").last()
        println "processing file $file"
            file.getText().eachLine{ line ->
            def isCommentedCode = line.startsWith("/") ||
                if(line.size()>0 && !isCommentedCode)
            filesProcessed ++
def printResult(){
    println "---------------------------------"
    println "files processed : $filesProcessed "
    println "Code line count : $lineCount"
    println "---------------------------------"

PS on performance: 12,000 lines of code in 150 odd files within various depths of the specified directory were retrieved in less than a second. 

Tuesday, November 08, 2011

Design Patterns : A prelude from a learner's viewpoint

“What do you understand about design patterns from the past nine months of code that you've written?”

This was the question put to me by my manager in today's meeting. The question was sudden and to be honest, I fumbled for words. I uttered few random sentences including the phrases “style of programming”, “the best way to apply a logic out of all available ways to do so”, etc. (which were pretty lame of course)

Now, I sit back in peace and try to fathom about design patterns based on my nine months of programming experience in the corporate world. When I try to rephrase the answer for the question, I find that I am really out of words to exactly describe what a design pattern exactly is, which is So in contrast with the ease with which the Gang of Four describes them to be.

From my understanding, I put it this way,

“A design pattern applied is THE possibly best fit (or the most optimized if not the best) approach that you adhere to while programming to find solution to a problem that you are trying to solve.” It is more generic a term and has got to do little or less with the specifics of a programming language used except for that it is widely used within the scope of Object Oriented Programming.

From what I understand, I believe and will continue to believe so, that, a design pattern is not limited to the few available standard textbook patterns like the factory pattern, singleton pattern, et al as suggested by the GoF. There can always be a pattern that is not a standard, is nameless and self defined, yet best suited for the task that you are working on. All the standard textbook patterns available can be safely regarded as hints to the problem you solve. Why I call it a hint more than the solution itself is that sometimes the pattern might just fit in so perfectly to a business scenario but sometimes it might not, Nevertheless it will take you closest to implementing a best solution to that problem.

My experience limits me to very few design patterns that I've worked with till date like the observer pattern, factory pattern, the yuckiest singleton pattern, etc but I guess this is what a design pattern is if you ask me, of course, from a noob's perspective.

Good that this post will not allow me to embarrass myself again by fumbling for words to phrase an answer when asked the same question and hopefully, at least in an year or two, I hope that I'd be in a position to answer any design pattern related question even in the midst of the midnight hour. Amen!

Saturday, November 05, 2011

Automount NTFS drives on Ubuntu

Well, here's for the lazy ones out there. I came across this simple tweak to fix the problem of manually mounting disks (either external/NTFS) on Linux.

If you have frequently accessed files placed on several drives or partitions that are external to Linux, you need to manually mount these partitions manually from Nautilus or terminal prior to accessing files/directories in them. To avoid this, you can change the contents etc/fstab file in such a way that they are auto mounted on startup.

Prior to editing the fstab content, you need to know the partition label of your drive. Partition labeling (alphabets followed by a number) is quite different in Linux when compared to Windows. Your NTFS partition might look something like sda2 or sda3.

For the geek

Step 1: Running the

fdisk -l

command will give you details of all the partitions like the one shown above.

Step 2: If you fail to decipher anything out of the above output, Just run the below command against the device names whose System column of the above output contains NTFS in it.

fdisk -s /dev/sda6

Running the above command will give you the size of the partition sda6 in bytes which gives you a better chance of identifying the drive (presuming that you might at least know the approx size of the NTFS drive you want to mount).

Step 3: Run the below command below 

sudo mkdir /media/Windows
sudo echo "/dev/sda6 /media/Name_of_Your_Partition ntfs-3g defaults,locale=utf8 0 0" >> /etc/fstab
sudo mount -a

which will add up the details of your partition to etc/fstab.

For the noob

1) Install mount manager via Ubuntu software center, open the app and identify the partition label of the drive to be mounted based on the disk size and partition details shown in it.

2) Hit Alt+F2 and type in
    gksudo nautilus

3) Open the file named fstab under etc folder of file system.

4) Add the below line to the file and save. 

/dev/sda6 /media/Name_of_Your_Partition ntfs-3g defaults,locale=utf8 0 0

Note: Replace sda6 by your partition label and Name_of_Your_Partition should be the name of your NTFS drive. 

Sunday, October 30, 2011

Ubuntu 11.10 - First Thing's First

 What I did after Ubuntu 11.10 installation, the journey so far!
  1. Installed Oneiric Ocelot (64 bit) with partitions spaces 50GB for Ubuntu root / and 150 GB for /home .

  2. Ubuntu restricted areas for audio and video codecs. (Can be installed either from Ubuntu software center or from the terminal using the
    sudo apt-get install ….

  3. Downloaded Launchy, an opensource keystroke launcher, my all time favorite system utility to easy index files. (Comes as a deb package by default, one can open the deb package with Ubuntu Software Center to proceed for installation or from the terminal using..
    cd .. //to the downloaded directory location
    dpkg -i packagename.deb
  4. Cairo Cock : Another personal favorite of mine for easy access. Cairo dock overrides few things that unity lack in terms with ease of access. Well, for everything else, you've the terminal. Nevertheless, I find Unity a lot more complete than what it was in Ubuntu 11.04. I personally like Unity, it is just a matter of breaking conventionality to realize the potential of what something new and innovative could offer.
  5. Chromium, a much better and faster web browser than Firefox.
  6. Rhythmbox, to manage audio. I am not a big fan of the music players suggested in the Linux world, although I haven't tried out all of them. The newer Winamp like interface is what I personally love. Have been looking around for such but again, Rhythmbox seems to be the better among the bad lot. (And nothing can beat itunes for managing music on my ipod. One reasons why I also have windows as a dual boot. All other ipod sync plugins provided by any of the Linux players suck.)
  7. VLC player for videos.
  8. Was also looking at installing Tweetdeck but still need to look through this as Tweetdeck requires Adobe AIR and sadly AIR has stopped its support for Linux compatibility. May be version 2.6 will do, but then, even Tweetdeck appears to have some installation errors on Ubuntu offlate.

Other stuff
Programming in Ubuntu has always been heaven, cannot really give you reasons why but I just love it on Linux!
  1. Installed Oneiric Ocelot (64 bit) with partitions spaces 50GB for Ubuntu root /, 150 GB for /home .
  2. Java and groovy for Linux.
  3. Eclipse Galileo with groovy plug ins.
  4. SCITE and scintilla, an open source text editor that I've been recently introduced to, highly powerful and capable of supporting many languages like C, C++, python, ruby, perl, et al. Highly customizable for a programmer and lot more powerful than the standard gedit of Ubuntu.
  5. Changed etc/xdg/autostart to startup apps like Launchy on Ubuntu login, so that I don't have to do it manually each time.
  6. I am also looking at auto mount of external disks that share space between Windows and Ubuntu to escape the manual labor each time at startup. A simple bash script or etc/fstab may be? Need to look through..

Well, That's it for a brief start of a long journey with Oneiric Ocelot.

Launch Applications on Ubuntu Startup

You can always add programs/applications to linux startup without having to launch them maunally, meaning, the application you wish for will launch itself automatically as soon as you boot into ubuntu after logging in.

For instance, let us say you are looking at adding firefox web browser to your start up list, all that is needed to be done is, to copy the firefox.dekstop file from user/share/applications folder to the etc/xdg/autostart folder.

Via Terminal :

  1. Ctr+Alt+T to open terminal
  2. Type in the following command and hit eneter
    sudo cp /usr/share/applications/firefox.desktop /etc/xdg/autostart

Via GUI :

  1. Hit Alt+F2, type in
    gksudo nautilus

    //This will ensure deliberate copy of the desktop file albeit the file permission issues if any.

  2. Browse through Filesystem -> usr -> share -> applications
  3. Copy the desired .desktop file (firefox.desktop in this case)
  4. Browse through Filesystem -> etc -> xdb -> autostart and paste it in the folder.
This will start firefox on ubuntu startup. Similar approach can be adapted for any other application installed.

Wednesday, September 21, 2011

Groovy Based RSS Reader - Intro

A desktop application with the help of which a user can keep track of his favorite blogs or the frequently visited websites.
Pre-requisite to be able to work with:
The website/blog should necessarily have an option for RSS feed subscription.
Rough Ideas/Steps involved :
1) Fetch the URL of the website/blog from the user as input.
2) Search the metatags of the page for existence of an rss feed.
3) If an rss feed for the website/blog exists, fetch the rss content from the feed URL, parse it and perform the display logic.
4) On exit, save state of UI i.e of all the blogs subscribed. On reopen, update feeds.
Genuinely started with the aim to target bloggers i.e users of blogspot/wordpress and other famous blog hosting sites. Should be in a position to extend the functionality to all websites that provide for an RSS subscription.

Monday, September 19, 2011

Serializing objects in Groovy

Never came across a more sexier way to serialize objects. Thanks to groovy goodness.

def map1 = ['1':'one']
def map2 = ['2':'two']

def file = new File("C:/serializedObjects.txt")

file.withObjectOutputStream { out ->
    out << map1
    out << map2          
file.withObjectInputStream(getClass().classLoader){ ois ->
        println it                  

Monday, August 22, 2011

Retrieve the RSS Feed URL of a Webpage

/* A dirty code tweak that can search the meta tags
* of a webpage for its rss feed url.
* If a feed exists for the page,
* the rss feed link portion of the page source
* will be extracted using regex.

def getRSSFeedURLFor(String urlStr){
def url = urlStr.toURL()
def result = []
def str
def lookForStart = '''<link rel="alternate"'''
def lookForEnd = '''>\n'''

url.eachLine {
result << it + "\n"

if(it.contains(lookForStart)) {
strtIdx = it.indexOf(lookForStart)
lastIdx = (it.indexOf(lookForEnd)+2)
str = it.substring(strtIdx,lastIdx)

str = str.replaceAll(/(.*)(href.+)"(.*)"(.*)\n/,'$3')
str = str.trim()
return str

def urlStr = "http://www.ashes-phoenix.blogspot.com"
def rssURL = getRSSFeedURLFor(urlStr).toURL()
println rssURL

PS: For most of the standard websites, if an RSS feed exists for the page, it will be specified in one of the metatags of the page source that looks like:

<link rel = "alternate" type=atom/application/rss+xml href=''...>

The logic lies in extracting the link within the href attribute of this tag which is actually the rss feed of the page we are trying to retrieve

Friday, August 19, 2011

Build a Binary Tree & Perform an Inorder traversal

* A simple groovy implementation to create a Binary Tree
* and to perform inorder traversal

class Node{

    Node left, right
    int data
        this.data = data
        left = null
        right = null

class BTree{

    static def insert(Node node, val){
      if(val < node.data){
          if(node.left == null){
              node.left = new Node(val)
              println "inserting $val to the left of $node.data"
          else insert(node.left, val)
      else if(val > node.data){
          if(node.right == null){
              node.right = new Node(val)
              println "inserting $val to the right of $node.data"
          else insert(node.right, val)
    //left, root, right
    static def printInOrder(Node node){
        if(node == null) return
            println node.data

//Shift to main if you aren't running this as a groovy script
def root = new Node(25)
BTree.insert(root, 10)
BTree.insert(root, 30)
BTree.insert(root, 24)
BTree.insert(root, 299)
BTree.insert(root, 266)
BTree.insert(root, 121)
BTree.insert(root, 920)

//Inorder traversal of the entered elements