Tuesday, August 10, 2010

Custom Findbugs Rule

Findbugs plugin architecture

You can create custom findbugs rules by creating a jar file in "plugins" directory of findbugs home (folder available when you extract findbugs).

Findbugs.xml file
At startup, each of those jar files is checked for a "findbugs.xml" file.
The XML file registers instances of
1. Detectors &
2. bug patterns that the detector reports.

At startup, FindBugs loads all plugin Jar files.  At analysis time, all detectors named in the findbugs.xml files from those plugins are instantiated and applied to analyzed class files.

Example findbugs.xml file

<DetectorPlugin>
  <Detector class="org.foobar.findbugs.FindUnreleasedLocks" speed="slow" />
  <Detector class="org.foobar.findbugs.ExperimentalDetector" speed="fast" disabled="true" />

  <!-- More Detector elements would go here... -->

  <BugPattern type="UBL_UNRELEASED_LOCK" abbrev="UL" category="MT_CORRECTNESS" />

  <!-- More BugPattern elements would go here... -->

</DetectorPlugin>

1.    <Detector> specifies a class which implements the edu.umd.cs.findbugs.Detector  interface and has a constructor that takes a single parameter of type edu.umd.cs.findbugs.BugReporter.  This element has three possible attributes:
a.     The required "class" attribute specifies the Detector class.
b.     The optional "disabled" attribute, if set to "true", means that by default, the detector will be disabled at runtime.
c.    The required "speed" attribute supplies a value to be shown in the "Settings->Configure Detectors" dialog.  It gives the user an idea of how expensive the analysis will be to perform.  The value of this  attribute should be one of "fast", "moderate", or "slow".
2.     <BugPattern> specifies a kind of bug that will be reported.  It has three required attributes:
a.    "type" is a unique code identifying the bug.  Only one BugPattern  can have a a particular type.
b.    "abbrev" is a short alphanumeric code for the bug.
c.    "category" can be one of categories defined in the core plugin's messages.xml:

1.    CORRECTNESS - code that was probably not what the developer intended
2.    BAD_PRACTICE - violations of recommended and essential coding practice
3.    STYLE - code that is confusing, anomalous, or written in a way that that leads itself to errors
4.    MT_CORRECTNESS - multithreaded correctness issues
5.    MALICIOUS_CODE - a potential vulnerability if exposed to malicious code
6.    PERFORMANCE - a performance issue
7.    I18N - internationalization and locale
or you may create your own category, in which case you should define it in a <BugCategory> element in _your_ messages.xml file.

Messages.xml file

<MessageCollection>

  <Detector class="org.foobar.findbugs.FindUnreleasedLocks" >
    <Details>
      <![CDATA[
        <p> This detector looks for JSR-166 locks that are not released on all paths
        out of a method.  Because it performs dataflow analysis, it is fairly slow.
      ]]>
    </Details>
  </Detector>

  <!-- More Detector nodes would go here... -->

  <BugPattern type="UBL_UNRELEASED_LOCK">
    <ShortDescription>Lock not released on all paths out of method</ShortDescription>

    <LongDescription>{1} does not release lock on all paths out of method</LongDescription>

    <Details>
      <![CDATA[
        <p> A JSR-166 lock acquired in this method is not released on all paths
        out of the method. This could result in a deadlock if another thread
        tries to acquire the lock.  Generally, you should use a finally
        block to ensure that acquired locks are always released.
      ]]>
    </Details>
  </BugPattern>

  <!-- More BugPattern nodes would go here... -->

  <BugCode abbrev="UL">Unreleased locks</BugCode>

  <!-- More BugCode nodes would go here... -->

</MessageCollection>


1.    <MessageCollection> is the top level element
2.    <BugCategory> elements optionally describe any categories you may have created for your bug patterns. You can skip these if you are using only the categories defined by the core plugin.
a.    <Description> child element has a brief (a word or three) description of the category.
b.    <Abbreviation> child element is typically a single capital latter.
c.     <Details> optional child element may describe it in more detail (but no markup).
3.    <Detector> holds meta-information about a Detector in the plugin.
a.    The required "class" attribute specifies the Detector class.
4.    Detector elements much have the following child elements:
a.    The <Details> child element has a brief HTML description of the Detector.
b.    It should have HTML markup that would be valid in a BODY element.
c.    It should be specified in a CDATA section so that the HTML tags are not misinterpreted as XML.
5.    <BugPattern> holds all of the human-readable messages for the bug pattern identified by the "type" attribute.  The type corresponds to the type attribute of the BugPattern elements described in findbugs.xml.
6.    BugPattern elements must have the following child elements:
a.    <ShortDescription> this is used for when "View->Full Descriptions" is turned off in the GUI, and it's also used as the title for descriptions in the Details window.
b.    <LongDescription> this is used for when "View->Full Descriptions" is turned on in the GUI, and for output using the command line UI.
7.    The placeholders in the long description ({0}, {1}, etc.) refer to BugAnnotations attached to the BugInstances reported by the detector for this bug pattern. You may also use constructs like {1.name} or {1.returnType}.

8.    <Details> this is the descriptive text to be used in the Details window.  It consists of HTML markup to appear in the BODY element of an HTML document.  It should be specified in a CDATA section so that the HTML tags are not misinterpreted as XML.
9.    <BugCode> is the text which describes the common characteristic of all of the BugPatterns which share an abbreviation.  In the example above, the abbreviation "UL" is for bugs in which a lock is not released. The text of a BugCode element is shown for tree nodes in the GUI which group bug instances by "bug type".

Writing hello world findbugs rule.

The rule will show an error on all static methods that you write in a class. This could help you start writing your own findbugs plugin.
1.    Extract the attached zip.
2.    Modify the build.xml to change the findbugs.home property to the directory where you have downloaded findbugs.
3.    Go to the extracted directory run ant plugin
4.    This would create a plugin.jar and copy it to findbugs.home/plugin directory
5.    Run ant test
6.    Check the html report in examples directory.

Tuesday, April 27, 2010

Thread.sleep

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Yes thats the kind of explanation they give in javadocs... Couldn't gather much from this.
So here's what it is in common mans language
A thread is like a process(Java gurus pls forgive me), the way we start each process on our machine like a firefox browser, a wordpad, media player etc... Now each of this process appears to be occuring simultaneously. Similar is the case of a thread in java.

You could have multiple threads and each of the threads would execute simultaneously or so it appears to be because of the time sharing it does.

Now getting to the topic of discussion what is Thread.sleep and other questions like isn't it a static method?

It is a static method but it gets the thread that is currently executing to sleep for the specified number of milli seconds.

How does it do it?
Using the concept of TLS or thread local storage. Each thread has a global memory which stores information corresponding to itself. So when Thread.sleep is called the stack which is running the thread is suspended and it waits for the time to complete or for some other method to interrupt it so that it can throw and interrupted exception and continue execution.

Difference between Thread.sleep and Thread.currentThread().sleep()
None. There is no difference between the two. Thread.currentThread returns the current thread that is executing don't confuse it with "this" this refers to object, Thread.currentThread refers to the thread being executed currently.

some more on it will come soon... but currently i'm feeling thread.sleeepy...

Sunday, January 10, 2010

Reliance Netconnect Usage

I have been using reliance netconnect night unlimited(so they say with a 10gb limit) plan for some time now.

The most irritating part of using it is that there is no way to find out how much you have used. They have a usage tab wherein they give you the kbs you have used, but do they expect us to add the usage depending on the time we used it and seperate it into night and day usage.

Here is the link where you can calculate the usage by just giving your mobile number and the dates.
It will give you a day and night split of your usage.

Friday, October 23, 2009

Use previously installed Ubuntu after installing XP or Vista

This post explains in a quick fashion how to restore your Ubuntu grub after installing any windows verstion(XP, Vista, 7(dint try but shud work)).

Steps:
1. Install the windows version you need on any partition other than the one on which ubuntu is installed.

2. When you restart the PC you will be automatically logging into the windows version, so you would have to boot using a ubuntu live cd.

3. Once in the ubuntu live cd env open a terminal( application-> accessories -> terminal).

4. Type in as given below without the quotes
"sudo grub"

5. You will log in into a grub shell
grub>

6. Type in "root (hd0,0)"
followed by "setup (hd0)"

7. Keep on trying this with other values

eg.
root (hd0,1)
setup (hd0)

root (hd0,2)
setup (hd0)

root (hd1,0)
setup (hd1)


root (hd1,1)
setup (hd1)

until you get a successful message and not
"Cannot mount selected partition"

8. Once you get the message it means the grub was restored. Type "quit"

9. You will come out of the grub shell.

10. Now restart the machine. You will see the grub at the point of booting, with the ubuntu partition.

Setting up to boot windows.

1. Boot into the ubuntu partition by selecting the ubuntu from the menu.

2. Open a terminal following process given above and type in gksudo gedit /boot/grub/menu.lst

and edit it to boot to your windows partition.

Please post comments or queries if any.

Tuesday, August 4, 2009

SCWCD certified

Long time since i wrote a post. Wouldn't have but this is a news i can't keep to myself. So finally I'm a sun certified web component developer. The preparation was pretty much long, but what really helped me is the experience in my company. This is one exam you need to give after you get a good lot of experience...

Sunday, April 19, 2009

Reliance Netconnect broadband on Ubuntu

Let me first of all make it clear that the activation of your card can be done only using windows so you need to find a windows machine and get your card activated.

Assuming you have activated your card the following are the steps to get it working on ubuntu


Open the Terminal (Application->Accessories->Terminal)

Type SU (Super User, gives administrative privilege will need it little later). Key in the password .

then,

lsusb

Bus 007 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 003: ID 12d1:1412 Huawei Technologies Co., Ltd.
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

This thing pops Out

Note down the no just before Huawei or quallcom depending on which card you use . To my experience all cards of the same vendor will have the same id. like for Huawei itll be 12d1:1412.

next,

modprobe usbserial vendor=0xAAAA product=0xBBBB ,

Where AAAA is 12d1 and BBBB is 1412 in my case.

Then you have to edit wvdial.conf file, type,

gedit /etc/wvdial.conf

This will open wvdial.conf.

(NOTE: wvdial might not be installed , to install it .
Insert original UBUNTU CD

Go to System->Administration->software sources. add cd as a source and close . itll update itself, Then on terminal type,
sudo apt-get install wvdial .

And key in Ur password , that shall do.

NOW coming back to wvdial .conf. Copy and paste this overriding the previous setting in .

[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Stupid Mode = 1
Modem Type = USB Modem
ISDN = 0
Phone = #777
New PPPD = yes
Modem = /dev/ttyUSB0
Username = 9876543210 (This is the no written in the back side and changes in every card).
Password = 9876543210 (This is the no written in the back side and changes in every card and is same as user name).
CBaud = 460800

Save it and exist .

Next,

Just enter terminal and sudo Wvdial
And your password to getconnected.

Best of Luck .

Wednesday, July 9, 2008

Installing Software in Ubuntu (.bin or .sh files)

Though there are many methods for installing files in Ubuntu like System--> Administrator --> Synaptic package manager. Many a times the websites offer .bin (binary) files for softwares. Here I am at giving a short and simple step by step process for installing from binary files.

1. Download the corresponding .bin file. Place it anywhere in your hard disk.
2. Navigate to the folder through the terminal...( using cd path)
3. Start installation by typing sudo ./filename.bin
4. Your root password will be asked. (if you haven't created a root account the password is the same as your login password).
5. Installation will occur on its own.
6. After the execution is over if you type command then the program should start.

eg. To install NetBeans IDE
1. Download the IDE from NetBeans IDE
2. Navigate to the folder containing the downloaded file.
3. Type sudo ./netbeans-6.1-ml-linux.sh
4. A GUI will guide you through the rest of the installation.
5. After the installation is over if you type
netbeans
NetBeans program will start. It also appears in the Applications--> Programming list in the ......

In case of any comments or suggestions please feel free to contact me
at anillraju@gmail.com