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

Friday, November 16, 2012

Kernel Upgrade Issue, Uninstalling 3.6.6 kernel from Ubuntu 12.10

I've upgraded my 12.04 to 12.10 and strangely, I've observed that the kernel version 3.5.x that ships as default with 12.10 has not been upgraded from 3.2.x which I was using in 12.04 LTS version. 

uname -a (for kernel details)

Hence I've tried to do a manual install of latest kernel version 3.6.x, which also failed with an error reporting unmet package dependencies. Not just with the manual kernel upgrade but I've been facing problems with the usual system updates too, that get pushed on a regular basis.

I've tracked this for a while and then realized that I had once made an edit to the default grub configuration file adding an additional "quite splash" line to boost up battery performance. The problem was that after the OS upgrade was done, somehow the simple double quote(") got messed up and was replaced by a smarter double quote (``) which was causing all the errors during my upgrades and updates. I've reverted back the double quotes 

sudo gedit /etc/default/grub

and I have done an update grub. 

sudo update-grub 

..only to realize that 3.6.x, 3.5.x and other older kernel versions were all present and installed but failed to show up because of the grub configuration file error. Moreover, I had realized that 3.6 being the latest didn't work that great at least on MY machine (perhaps because it is not as customized as 3.5 for 12.10) as it was consuming 42W of power as shown on powertop which was too damn high. Hence I've removed the 3.6 kernel using 

sudo apt-get purge linux-image-3.6.*

and I've reverted back to the default 3.5.x kernel that is shipped with 12.10 and everything seems fine now. 

Things I've learnt in the process:
  • It is ideal not to upgrade a kernel version that is more latest than the one shipped with the latest OS version. Even if you wish to do so, compile the kernel separately and then proceed.
  • Installing and uninstalling a kernel. 

Thursday, August 09, 2012

A stupid hack to open short urls blocked by proxy via Launchy

This script will be useful when short urls are blocked by a proxy within a network.

For example:

Lets say the link to an article or blog on the web is this:

LongUrl : http://something.com/somethingelse

Your friend reads the above article and mails the link to you in its shortened form:

ShortUrl : http://bit.ly/s1 or http://t.co/se1

Now when you are on a company network or a protected network, there is a good chance that bit.ly or t.co services are blocked by the proxy though the original site itself (something.com in this case) might not be blocked.

For such cases this silly vbscript can be handy. It basically talks to a longurl.org which lengthens the shortened url and opens it up on the browser.

lurl.vbs

set ws = CreateObject("WScript.Shell")
siteUrl = "http://longurl.org/expand?url=" & Wscript.Arguments.Named("u")

Dim objHttp
Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
objHttp.Open "GET", siteUrl, False
objHttp.Send
page = objHttp.ResponseText
Set objHttp = Nothing

longUrl = Split(Split(page,"Long URL:")(1),"""")(1)
ws.Run(longUrl)

I have saved the above lurl.vbs file under C:\Temp\Scripts.

From command prompt, running the below command will open up the link in the browser.

lurl /u:your-short-url        


                          
Further more, you can even configure it via the Launchy Runner plugin which makes it more fun.

                                   

Within launchy, typing lurl (tab) followed by site's short url will launch the site directly.

                                 








Wednesday, June 20, 2012

Design Patterns: Factory Method Pattern

/**
* A simple implementation of Factory Method Pattern
* using a real time CricketPlayer example
*/


//Base
public interface CricketPlayer{
    public String play();
}


//Subclass
class Bowler implements CricketPlayer{
    public String  play (){
        return "takes a wicket";
    }
}


//Subclass
class Batsman implements CricketPlayer{
    public String  play (){
        return "hits a six";
    }
}


//Factory class - contains the logic of instatiating players
public class CricketPlayerFactory{
    CricketPlayer player;
   
    public CricketPlayer getPlayerWho(String does){
        if(does.equals("bats"))
            player = new Batsman();
        else if(does.equals("bowls"))
            player = new Bowler();
        return player;              
    }
}


//Factory method implementation
public class FactoryImplementation{
    public static void main(String args){
        //create a factory
        CricketPlayerFactory factory = new CricketPlayerFactory();
        //ask factory to instantiate the player
        CricketPlayer player = factory.getPlayerWho("bats");
        //use the player
        System.out.prinln(player. play ());
    }
}

Design Patterns: Singleton Pattern

/**
* A simple Implementation of a singleton
*/
class MySingleton{
   
    static MySingleton singletonInstance;
   
   //****** instantiation ******


    static MySingleton getInstance(){
        if(singletonInstance == null)
            singletonInstance = new MySingleton();
        return singletonInstance;
    }
   
    private MySingleton(){
        //cannot be called public with new
    }


   //****** logic ******   
    ....
    ....
}


MySingleton.getInstance();


FYI, Points to remember

  • Singletons are bad. 
  • They are just glorified statics and hence against OOP principles. 
  • They should be rarely used. A classic example: for logging purposes.
  • Singletons are hard to unit test.
  • Also bad because it is NEVER a good practice to mix instantiation and logic in a single class. 




Saturday, June 09, 2012

How to know HBOOT version and S-ON or S-OFF on HTC phones


  • Switch off the mobile completely.
  • Hold the volume down button and simultaneously click the power-on button. This will login to the boot menu and display details like this:

    BUZZ PVT SHIP S-ON
    HBOOT-1.01.0002
    MICROP-0622
    TOUCH PANEL-ATMELC03_16ac
    RADIO-3.35.20.10
    Dec  2 2010, 17:14:26

    The first line displays the security flag status. For being able to play around with sudo root access on HTC phones the flag must be turned off (shows S-OFF if turned off). 
  • Click power off button and next hold the volume down button to reboot again into normal menu. 


To be continued.. 





Wednesday, May 09, 2012

Currying in Groovy

Currying is a process of transforming a function of n arguments into n partial functions of one argument each.

Although I was acquainted with the math part of it. I found it hard to implement it in code. I
couldn't help but posting Tim Yate's sample snippet to my question on SO about a simple implementation for currying in groovy.


def greet = { greeting, person -> "$greeting $person" }


// This takes a closure and a default parameter
// And returns another closure that only requires the
// missing parameter
def currier = { fn, param ->
  { person1 ->
     fn( param, person1 )
  }
}


// We can then call our currying closure
def hi = currier( greet, 'Hi' )


// And test it out
hi( 'Vamsi' )


Here's an other pointer for doing functional programming in groovy. 

Friday, May 04, 2012

Setting up Conky & Lua-Widgets

Installing Conky-Lua widget/theme on Ubuntu

1) Install conky and conky-all from Ubuntu Software Center or via apt-get on terminal.

2) Unzip the contents of the tar.gz or zip of Conky/Lua theme submitted by the developer into some folder named Scripts.

Most importantly, the unzipped file will have a conky text file and a .lua file in it.

3) Ensure Nautilus Browser -> View -> Show hidden files option is enabled and create a hidden folder in your /home/Your_Username directory with the name .conky

4) Within the Scripts folder, search for file with .lua extension. This file contains the widget script. Copy the .lua file into the .conky folder.

5) Rename the left over text file in the Scripts folder to a hidden file named .conkyrc and place it in /home/Your_Username directory.


6) Open the .conkyrc file in gedit and edit the portion of the .conkyrc file which refers to the  widget location path i.e the .lua script path. In this case should point to

/home/Your_Username/.conky/someWidgetScript.lua

On terminal run command

conky

which will start the Conky theme/widget.


Installing Multiple Conky-Lua widget/themes on Ubuntu

By default, Running the conky command will invoke the .conkyrc text file at /home/Your_Username location and checks for lua scripts placed in the .conky folder.

In case of setting up multiple widgets you need to have multiple conky text files and multiple lua scripts.

Place all the .lua files in the .conky folder in the /home/Your_Username directory.

Let the text files remain in the Scripts folder but ensure that the configuration path in them points to their corresponding .lua scripts in the .conky folder. (as shown in step 6 above)

In terminal, run

conky -c /home/Your_Username/Scripts/ConkyTextFileScript1
conky -c /home/Your_Username/Scripts/ConkyTextFileScript2
conky -c /home/Your_Username/Scripts/ConkyTextFileScript3

Each of these will take the conky text file path with -c option and individually start the widgets.


PS: You might face issues if any of the above actions, like the path configuration (in step 6) is not done correctly. Also, This requires little bit of scripting knowledge to work your way around. In a nutshell, these are the minimal steps required to setup Conky-Lua Widgets on Ubuntu.