Wednesday 30 May 2012

Steps to build debian package


Before go for this, need to know about debian package.

Debian package contains set of files that will install that package where we run this debain in ubuntu linux. Like exe in windows. So, Debian package is used to install particular software in ubuntu linux.

Now, Let start how to build this debian pacakge.

Requirements to build debian packages : Ubuntu System/VM

1. First we need some packages to build debain package in ubuntu. 
Run following command

sudo apt-get install build-essential autoconf automake autotools-dev dh-make debhelper devscripts fakeroot xutils lintian pbuilder

[ For ubuntu linux, we can use apt-get package manager to install any package ]

This command will install all packages specified.

2. Then, create source folder.  [ Folder name should be in lower case ]

Debian pacakage name have some naming convensions,  For Example. My source folder name is clientprojrct-1.0

This contains clientproject_1 folder. it contains all files need to be packed in debian package.

Folder structure  clientproject-1.0  -> clientproject_1

3.  Begin debianization
To build debian package, we need some files [ like control, postinst, postrm etc ] to define debian package.

After create source folder, we need to run this command.

cd clientproject-1.0

dh_make   [ This command will create one debian folder. This folder contains all neccessary files ]

After run this command,this will print

Type of package: single binary, indep binary, multiple binary, library, kernel module, kernel patch or cdbs?
[s/i/m/l/k/n/b]

Give option : s

4. Control file
Probably the most important step is how you edit the control file.

This file will be created after completing step 3, and you can find it in the "debian" folder in the directory containing your source files. It initially looks like this:

   1 Source: x264
   2 Section: unknown
   3 Priority: extra
   4 Maintainer: andrei <webupd8@gmail.com>
   5 Build-Depends: debhelper (>= 7), autotools-dev
   6 Standards-Version: 3.8.1
   7 Homepage: <insert the upstream URL, if relevant>
   8
   9 Package: x264
   10 Architecture: any
   11 Depends: ${shlibs:Depends}, ${misc:Depends}
   12 Description: <insert up to 60 chars description>
   <insert long description, indented with spaces>

For now, enter a hompage (line 7) and a description (line 12) for your package.

Now, a very important step: we must enter the build dependencies on line 5.

For example, Am created one application in java and I packed that application in debian package. To run that application, I need java.
Like this all needed packages, We should include in debian dependencies.

Then, if we need noacrh debian, then need to update "Architecture" as "all".
[ noarch : no architecture. We can install this debian package in both 32 and 64 bit system ]

5. The "copyright" file

In the same "debian" folder, you will also find a "copyright" file which you need to edit. The important things to add to this file are the place you got the package from and the actual copyright notice and license. You must include the complete license, unless it's one of the common free software licenses such as GNU GPL or LGPL, BSD or the Artistic license, when you can just refer to the appropriate file in /usr/share/common-licenses/ directory that exists on every Debian system.

6. The "changelog" file

You can fild this in the same "debian" folder, this file is very important. Here, You need to mention name and date deails.

x264 (0.1+svn20100107-1) unstable; urgency=low

* Initial release (Closes: #nnnn) <nnnn is the bug number of your ITP>

-- andrei <webupd8@gmail.com> Sat, 09 Jan 2010 22:40:24 +0200

7.  The "postinst" file

You can find this in the same "debian" folder. Here we need to mention where packed files are need to copy.

8. The "postrm" file

You can find this in the same "debian" folder. Here we need to mention what are the files are need to remove while uninstall this package.

9. Building the actual .deb package

After defined debian packages, we need to run this command to build debiam.

cd clientproject_1.0 -> clientproject_1

debuild -b     [ This will build debian packgae ]

After run this, you can get debian package.

Tuesday 29 May 2012

Singleton pattern


Singleton in java :

-> Singleton is one of the design pattern in java which is used to control the creation of class's instance. We can ensure only one instance of the class is created.

-> For this, we need to do something as follows,

1. Make class contructor as private, so that outside world can not access this class.
2. Need to create static variable for class instance.
3. Then, Need to create static method to return instance of singleton class. If class's instatnce is empty, then create new instance and return that instance. Otherwise return already created instance.
4. Make access method synchronized to prevent threading problems.


Example Code :
package javaexamples;

import java.util.Date;

public class SingletoneObjectDemo {

    private static SingletoneObjectDemo singletoneObj = null;

    private  SingletoneObjectDemo() {

    }

    public static synchronized SingletoneObjectDemo getInstance() {
        if (singletoneObj ==null) {
            singletoneObj = new SingletoneObjectDemo();
        }
        return singletoneObj;
    }

    protected void displayDate() {
        System.out.println("Today date is : " +(new Date()).toString());
    }
}


package javaexamples;

public class SingletoneObjImp {
    public static void main(String args[]) {
        SingletoneObjectDemo singletoneObj = SingletoneObjectDemo.getInstance();
        singletoneObj.displayDate();
    }
}

Friday 25 May 2012

User Defined Exception


1. Java provides an extensive set of in-build exceptions. But some cases we may need our own exception to handle some application specific error.

What is the neccessary to create our own exception?

1. The problem and Exception used may not have good relation.

2. If any unwanted condition or error occurs related to our Application, then we can throw our own application exception from any where.
[ While getting error in Helper classes, then we can not able to send that error directly to client.
That place, we can throw exception and send back to client ].


While defining an user defined exception, we need to take care of following,

1. The user defined exception class should extend from Exception class.
2. The toString() method should be overridden in user defined exception class inorder to provide meaningful information about the exception.


For Example,

public class UserDefinedException extends Exception {

    private String status;

    public UserDefinedException(String status) {
        this.status = status;
    }

    public String toString() {
        return "UserDefinedException : " + status;
    }

    public static void main(String args[]) {
        String str = null;
        try {
            checkInput(str);
        } catch (UserDefinedException ex) {
            System.out.println("Exception occured : " +ex);
        }
    }

    private static void checkInput(String input) throws UserDefinedException {
        if (input == null) {
            throw new UserDefinedException("Input is null");
        }
    }
}

In UserDefinedException class, We defined our own exception class.

In main method, we are checking whether the input is null, If its null then throwing 'UserDefinedException'.

If exception throws from called function, then need to catch that exception in caling function. [ Compiler will tell this ].


Difference between 'throw' and 'throws' :

throws :

1. It is used by the designer of a method to claim the exception. Claiming is nothing but informing the programmer about the potential problems that may arise while using the method.

2. The other way is to use as an alternative to try-catch block which is not a robust way.

throw :

1. "throw" is used by the programmer to throw an exception object explicitly to the system. Mostly used with user-defined exceptions.

Tuesday 22 May 2012

Break Statement in Java


Break statement is one of the several control statements Java provide to control the flow of the program. As the name says, Break Statement is generally used to break the loop of switch statement.

Please note that Java does not provide Go To statement like other programming languages e.g. C, C++.

Break statement has two forms labeled and unlabeled.

Unlabeled Break statement

This form of break statement is used to jump out of the loop when specific condition occurs. This form of break statement is also used in switch statement.

For example,

    for(int var = 0; var < 5 ; var++)
    {
            System.out.println(“Var is : “ + var);
   
            if(var == 3)
                    break;
    }

In above break statement example, control will jump out of loop when var becomes 3.

Labeled Break Statement

The unlabeled version of the break statement is used when we want to jump out of a single loop or single case in switch statement. Labeled version of the break statement is used when we want to jump out of nested or multiple loops.

For example,

    Outer:
    for(int var1 = 0; var1 < 5 ; var1++)
    {
             for(int var2 = 1; var2 < 5; var2++)
            {
                    System.out.println(“var1:” + var1 + “, var2:” + var2);
   
                    if(var1 == 3) {
                        break Outer;
                    }
   
            }
    }