Monday, December 5, 2011

Manually install a maven dependency

execute the following command from the command line with the correct values for options. -Dfile option locates the downloaded jar file. Therefore make sure to specify the correct location or reach the jar location from the command line and execute the command.

mvn install:install-file -DgroupId=ojdbc -DartifactId=ojdbc6 \
     -Dversion= -Dpackaging=jar -Dfile=ojdbc6.jar -DgeneratePom=true

How to add Maven dependency from a relative path referencing a local jar

When you want to add a dependency which is not stored in maven repositories to your project this is the way!! You can manually download it and include inside the project and refer it from the pom.xml through a relative path.

This example assumes that you have downloaded and stored the jar inside ${project}/src/test/resources/lib. 
Here we must specify the scope of the dependency  as 'system'. There is another way which is to manually install the maven dependency to your local repository. You can find out how from here.

Friday, October 7, 2011

Oracle Stored Procedure to return multiple records and call it in SQLPLUS

To return multiple records we need a REF CURSOR type variable to assign each record. The following is a script which return a REF CURSOR  as OUT parameter.


  TYPE cursor_type IS REF CURSOR;
END Types;


CREATE OR REPLACE PROCEDURE getEmployeeInfo(p_recordset1 OUT  Types.cursor_type) AS

    OPEN p_recordset1 FOR
    SELECT empNumber, empName, phone, address FROM Employees;


We can run the above script in SQLPLUS as below.

SQL> START OracleStroredProc.sql

So procedure will be created by now. To call the procedure we have to create a variable of type REFCURSOR and pass it as a parameter to procedure call. Then we can
print the variable to see the result.

SQL> call getEmployeeInfo(:a);

Call completed.

SQL> print :a;

Executing the Oracle PL/SQL returns a number?

When you execute a PL/SQL script through sqlplus, if it returns a number at each line you press enter for a new line, it is beacuse you have forgot
to include '/' end of the script file for execute.


SQL> OracleScript.sql
       > 34
       > 35

So at this situation just enter '/'. Script will execute.

SQL> OracleScript.sql
       > 34 /

If you don't want to manually enter '/' here, place a '/' end of the script file. The script in here has a '/' end of the file which tells to execute it.

Oracle change the default date format before insert data

Oracle accepts date in the default format of YYYY-MON-DD. Here month is accepted as three characters of month name. Ex: January-JAN, February: FEB, etc

Now before executing a bulk records to a table with a required date format such as YYYY-MM-DD, we can alter the session to accept date in that format from the following query.

SQL> alter session set NLS_DATE_FORMAT='YYYY-MM-DD';

Now we can insert records with date represented in YYYY-MM-DD format.

Oracle drop a schema with all the objects (Similar to database drop in MySQL)

Here we have to drop the user who is the owner of the schema objects with CASCADE option which will result in droping user with the all objects created by that user.

SQL> Drop user username CASCADE;

Oracle PL/SQL script to drop a user if not exists

This script will run a query to count the number of users with a specified user name and if the count!=0 then drop user query is executed.

    u_count number;
    user_name VARCHAR2 (50);


        u_count :=0;
        user_name :='datauser';
        SELECT COUNT (1) INTO u_count FROM dba_users WHERE username = UPPER (user_name);

             IF u_count != 0
                 EXECUTE IMMEDIATE ('DROP USER '||user_name||' CASCADE');
              END IF;

              u_count := 0;
           WHEN OTHERS
                     DBMS_OUTPUT.put_line (SQLERRM);
                     DBMS_OUTPUT.put_line ('   ');



Include the above script in a file .sql (Ex: DropOracleUser.sql). Make sure to replace the username with correct name which is 'datauser' in above script. You can run the above oracle PL/SQL script in sqlPlus as follows.

sql> START DropOracleUser.sql

Monday, June 6, 2011

PHP-SOAP web service with out a WSDL

This article is going to describe how to create a simple web service with php-soap step by step.


    Server to run php (apache, xampp , wamp)
    PHP-SOAP extension


Let's write a simple web service to find the length of a given string.

So here is the code for StringLengthServer.php

# StringLengthServer.php
# Copyright (c) 2011 by Lakmali Baminiwatta

    function findLength($string) {
           return "Length of the string " . $string . "is : ".strlen($string);
    $server = new SoapServer(null,
              array('uri' => "urn://"));


  • First thing done here is to write a function to perform the action done by the service. So here function findLength($string) takes a string as the argument and prints back the length of it.
  • Then SoapServer is created. First argument null which means no WSDL used. Second argument is uri value is just a unique string as the namespace.
  • Then we have to add the function findLength to the server.
  • Now copy the file StringLengthServer.php to the server to run. Here it is assumed the url which service run is http://localhost/simple_server/StringLengthServer.php.
  • Now let's write a simple client program to get the service from the webservice.

    So here is the code for StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
   $client = new SoapClient(null, array(
      'location' => "http://localhost/simple_server/StringLengthServer.php",
      'uri'      => "urn:urn://",
      'trace'    => 1 ));

   $return = $client->__soapCall("findLength",array("lakmali"));
   echo("\nReturning value of __soapCall() call: ".$return);

   echo("\nDumping request headers:\n"

   echo("\nDumping request:\n".$client->__getLastRequest());

   echo("\nDumping response headers:\n"

   echo("\nDumping response:\n".$client->__getLastResponse());

In here the location argument of the SoapClient must be the URL to server program which we wrote above and added to run. Make sure it is correct.

Run this StringLengthClient.php to receive the service.

So the output will be:

Returning value of __soapCall() call: Length of the string lakmali is : 7
Dumping request headers:
POST /simple_server/StringLengthServer.php HTTP/1.1
Host: localhost
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.9
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:urn://"
Content-Length: 510

Dumping request:


Dumping response headers:
HTTP/1.1 200 OK
Date: Tue, 07 Jun 2011 13:24:06 GMT
Server: Apache/2.2.10 (Fedora)
X-Powered-By: PHP/5.2.9
Content-Length: 550
Connection: close
Content-Type: text/xml; charset=utf-8

Dumping response:

 Length of the string lakmali is : 7

Here the client program is written to display SOAP messages exchanged. But for a real client ony you need is return value from soapCall.

# StringLengthClient.php
# Copyright (c) 2011 by Lakmali Baminiwatta
   $client = new SoapClient(null, array(
      'location' => "http://localhost/simple_server/StringLengthServer.php",
      'uri'      => "urn:urn://",
      'trace'    => 1 ));

   $return = $client->__soapCall("findLength",array("lakmali"));
   echo("\nResult: ".$return);

Thursday, June 2, 2011

Java Initialization order

When a java class get loaded first all the static fields will be loaded with their default values.
Then static inits of the class begins in order, which means static fields will be initialized as given in order.

  • So in the given example code line 1 will load the class LakmaliClass with all its static fields assigning to default values.
  • Then static inits will begin executing line 5, 6 and 7.
  • Then instance inits will begin executing line 2, 3 and 4.
  • Line 2 will load the class Second with all the static fields default values (Here no static fields)
  • Then object will be created with passed values.
  • Line 3 will also do the same.

class Initializer{

    public static void main(String args[]){
        LakmaliClass o=new LakmaliClass(); // line 1


class LakmaliClass{

        System.out.println("Lakmali class instance init"); //line 2
        Second s1=new Second(s);            //line 3
        Second s2=new Second(number1);            //line4

    static String text1="text1"; //line 5
    static int number1=1;        //line 6

        System.out.println("Lakmali class static init");    //line 7

class Second{

    Second(String s){


    Second(int i){



Lakmali class static init
Lakmali class instance init

Let's see a different example, where instance will be created in static init.

class Initializer{

    public static void main(String args[]){
        LakmaliClass.getInstance(); // line 1


class LakmaliClass{

        System.out.println("Lakmali class instance init"); //line 2
        Second s=new Second(text1,number1);            //line3

    static LakmaliClass o=new LakmaliClass();            //line 4

    static String text1="text1"; //line 5
    static int number1=1;        //line 6
        System.out.println("Lakmali class static init");    //line 7

    static LakmaliClass getInstance(){
        return o;

class Second{

    Second(String s, int i){
        System.out.println("value1: "+s+"\nvalue2: "+i);



Lakmali class instance init
value1: null
value2: 0
Lakmali class static init

Here you can see that instance inits had run before static init. It happend in this order.

  • line 1 load class LakmaliClass with all its static fields default values.
  • Static inits begin.
  • First line 4 runs. It creates an instance of LakmaliClass. So instance init of LakmaliClass will be executed with current values. So text1 or number1 fields are not initialized to given values as line 5 and 6 have not executed yet. So text1 and number1 will have their default values.
  • Now line 5, line 6 and line 7 will execute in order.
  • Then getInstance method will be invoked.

Tuesday, May 24, 2011

Spring at a glance


Spring is an open source framework created to address the complexity of enterprise application development.

Advantages of Spring framework

Spring is a layered architecture. We can use what is required from those and leave others.

Spring enables POJO programming. POJO facilitates continuous integration, easier upgrades and switching.

The IOC feature of Spring simplifies JDBC

Open source framework.

POJO -Plain Old Java Object

Maintaining an enterprise java application for few years is even difficult to imagine due to their tightly coupled infrastructure frameworks which evolves rapidly.

Solution is using POJO. There is no magic in POJOs. There the classes don't implement infrastructure framework-specific interfaces. They are just plain Java classes. Therefore POJO decouples applications from changing frameworks. As a result, upgrading or switching to a new framework and integration will be easier and less risky.

Inversion of Control (IOC)

The concept of IOC pattern is that objects are not created,but described how they should be created. Here you do not connect your components and  services together on the code, but describe which services are needed by which components in a configuration file. In Spring, IOC container handles the hooking up all these. 
In Spring there are two types of IOC.

1. Constructor injection- Dependencies are provided as constructor parameters.
2. Setter Injection- Dependencies are given through javabean properties which are setter methods.

IOC benefits
  • Services can be easily connected by adding a new constructor or a setter method with less configuration.This minimizes the amount of code in an application.
  • IOC allows manually injecting any objects needed into the object under test. This makes unit testing more easy.
  • Loosely coupling is promoted as the dependencies are injected to the piece of requesting code.