Spring Basics

Framework:
A framework is a collection of components that can used to build well organized applications.

Spring Framework:
Spring is a framework, because it provides components to build common parts of applications, such as data access support, MVC support, and many others.

The core of the Spring Framework is based on the principle of Inversion of Control (IoC). Applications that follow the IoC principle use configuration that describes the dependencies between its components. It is then up to the IoC framework to satisfy the configured dependencies. The “inversion” means that the application does not control its structure; it is up to the IoC framework to do that.

IoC:
IoC provides services through which a component can access its dependencies and services for interacting with the dependencies throughout their lives. In general, IoC can be decomposed into two subtypes: DI and dependency lookup

DI:
Dependency Injection (DI) in computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency.
Three types
1. interface injection
2. setter injection
3. constructor injection

Dependency Lookup:
It’s the reverse process of DI where a component must acquire a reference to a dependency, whereas with DI, the dependencies are literally injected into the component by the IoC container
two types are
1. Dependency pull
2. Contextualized dependency lookup (CDL

AOP:
Aspect-oriented programming (AOP) is one of the technologies of the moment in the programming space. AOP lets us to implement crosscutting logic—that is, logic that applies to many parts of your application—in a single place, and then have that logic automatically applied right across the application

there are two types of AOP

1. Static AOP, such as AspectJ.
2. Dynamic AOP, such as that in Spring, allows crosscutting logic to be applied arbitrarily to any other code at runtime


Swing application for Windows CE and CrE-ME

This is same as Writing Applet Application. The Only thing we need to the followings

Step1:
Take the javax.swing package from http://www.nsicom.com/Default.aspx?tabid=295.

You will get a CAB file, put it on the PDA device and click on it to install. It will fire a dialog box. Select the root folder of the CrE-ME.

Step2:
The write a sample Swing program:

Bellow program also shows how to set a user defined icon left most of the Title bar.

import javax.swing.*;
import java.awt.*;

public class Sample
{

public static void main(String[] args)
{

JFrame frame = new JFrame("Setting an Icon for a frame");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("icon_confused.gif"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}

NB: keep your image icon in the same package where the program is.

Finally produce the jar file and keep it in the PDA and run it.

Java in Windows CE with CrE-ME

Developing CDC application with CrE-ME VM’s:
Recently I just worked with PDA devices and got to develop an application for that PDA which holding Windows CE Operating System. Let us know all that steps to be followed for this circumstance.

NB: For CDC devices there are two JV’s are well known which requires license to work. Both are

  • IBM J9
  • CrE-ME
CrE-ME can be used for a month of trial. IBM J9 does not give trial any more but earlier it used to.

Step 1:
Download Microsoft Active Sync from the bellow link, You may need to sign in to your MSN id to download

http://www.microsoft.com/windowsmobile/en-us/help/synchronize/activesync45.mspx

Step 2:
Install the Active Sync then connect the PDA and try to access the PDA device.

Step 3:
Download JVM CrE-ME 4.12 and CrEme 3.29 from http://nsicom.com/, free evaluation. And install both with having PDA connected to the PC . U will get confirmation in the PDA while installed

Step4:
Write your Applet and produce the .jar of it.
Here is my sample program

//MainTest.java
import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;


public class MainTest extends Frame implements ComponentListener
{

private Label StepLabel ;

private MenuBar mbar;

private Menu menu;
private MenuItem mitem, msolve;

AboutGSL about;
NewForm newForm;


public void init ()
{

setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());

setLayout (null);

about=new AboutGSL();
newForm=new NewForm();

mbar = new MenuBar();
menu = new Menu("File");
mbar.add(menu);

mitem = new MenuItem("New");

menu.add(mitem);

mitem = new MenuItem("Update");

menu.add(mitem);

msolve = new MenuItem("Delete");

menu.add(msolve);
mitem = new MenuItem("Exit");
menu.add(mitem);
menu = new Menu("Help");

mbar.setHelpMenu(menu);

mitem = new MenuItem("About");
menu.add(mitem);
setMenuBar(mbar);

show();

start();

}


public void start () {}

public void stop () {}

public MainTest (String s) { super(s);
}


public synchronized boolean handleEvent (Event event)
{

if (event.id == Event.ACTION_EVENT)
{

if ("About".equals (event.arg))

about.setVisible(true);
if ("New".equals (event.arg))

newForm.setVisible(true);
if ("Exit".equals (event.arg))

System.exit(0);
else{
}
}

else if (event.id == WindowEvent.WINDOW_CLOSING)

System.exit(0);

return true ;
}

public void componentResized(java.awt.event.ComponentEvent e)
{

if (e instanceof java.awt.event.WindowEvent)
{

setVisible(false);

stop();

init();
}

}

public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}

public void componentHidden(ComponentEvent e) {}


public static void main(String args[]) {

MainTest app = new MainTest(" GSL Sample Application");
app.init();
app.addComponentListener(app);
}

}

//
NewForm.java
import java.awt.*;
import java.awt.event.*;

class NewForm extends Frame
{

Button save=new Button(" Save ");

Button OK = new Button (" Close ") ;

Label nameL = new Label ("Enter Name",Label.LEFT) ;

TextField nameT = new TextField (30) ;

Label fnameL = new Label ("\nEnter Father Name",Label.LEFT) ;
TextField fnameT = new TextField (30) ;
Label addL = new Label ("Enter Address") ;

TextField addT = new TextField (30) ;

Label contactL = new Label ("Enter Contact Number") ;

TextField contactT = new TextField (30) ;


public NewForm ()
{

super ("About Grameen Solutions") ;

setSize (Toolkit.getDefaultToolkit().getScreenSize().width/2, Toolkit.getDefaultToolkit().getScreenSize().height /2) ;
setResizable (false) ;

setLayout (new FlowLayout ()) ;

add(nameL);

add(nameT);

add(save);

add (OK) ;

}


public synchronized boolean handleEvent (Event event)
{

System.out.println( event);

if (event.id != Event.ACTION_EVENT)
{

if (event.id == WindowEvent.WINDOW_CLOSING)
{

dispose ();
return true;

}
else
return false ;
}

if (" Close ".equals (event.arg))

dispose () ;
return true ;

}

}


//
AboutGSL.java
import java.awt.* ;
import java.awt.event.* ;

class AboutGSL extends Frame
{

Button OK = new Button (" Close ") ;
Label Caption = new Label ("Passbook V1.0", Label.CENTER) ;

Label c1 = new Label ("developed for Sample Demo", Label.CENTER) ;

Label c2 = new Label (" ", Label.CENTER) ;

Label c3= new Label (" ", Label.CENTER) ;


public AboutGSL ()
{

super ("About Grameen Solutions") ;

setSize (Toolkit.getDefaultToolkit().getScreenSize().width/2, Toolkit.getDefaultToolkit().getScreenSize().height /4) ;

setResizable (false) ;

setLayout (new FlowLayout (FlowLayout.CENTER, 500, 0)) ;
add (c2);

add (c3);

add (Caption);

add (c1);

add (OK) ;

}

public synchronized boolean handleEvent (Event event)
{

System.out.println( event);

f (event.id != Event.ACTION_EVENT)
{

if (event.id == WindowEvent.WINDOW_CLOSING)
{

dispose ();

return true;

}
else
return false ;
}

if (" Close ".equals (event.arg))

dispose () ;
return true ;

}

}


Step5:
Put yor .jar file inside the
Windows\creme\demos\ directory of your PDA and try to run it by double clicking on it.

Its worked fine for me.

JSP Live Validation

Its being interesting to have the validation of web application with each tab movement which also called Live Validation. The concept natively came with Ruby on Rails frame work. I felt to do the same for a JSP page.

What we need?
1. Download livevalidation_standalone.js file from bellow location
http://www.livevalidation.com/javascripts/src/1.3/livevalidation_standalone.js

2. Download consolidated_common.css file from bellow location
http://docman.cn/docs/livevalidation/css/consolidated_common.css

3. keep both the file in the context of your web application

4. And link both .js and css file to the Jsp page with in tag

<head>
<link rel="stylesheet" type="text/css" href="css/consolidated_common.css">
<script type="text/javascript" src="jscript/livevalidation_standalone.js"></script>
</head>

then beside the text box have the given code as given bellow.

Required Validation:
<tr>
<td width="462"><span class="style5">Required Validation:</span></td>
<script type="text/javascript">
var sayHello = new LiveValidation('sayHello', { validMessage: 'Thanks!', wait: 500});
</script> </td>
</tr>

same way follow other code for several validation

Numeric Only :
var f3 = new LiveValidation('f3');
f3.add(Validate.Numericality);

Integer Only:
var f4 = new LiveValidation('f4');
f4.add(Validate.Numericality, { onlyInteger: true } );

Specific 2000 Only:
var f5 = new LiveValidation('f5');
f5.add(Validate.Numericality, { is: 2000 } );

Higher than or equal to 2000:
var f6 = new LiveValidation('f6');
f6.add(Validate.Numericality, { minimum: 2000 } );

Lower than or equal to 2000:
var f7 = new LiveValidation('f7');
f7.add(Validate.Numericality, { maximum: 2000 } );

Within a range of 2000 - 3000:
var f8 = new LiveValidation('f8');
f8.add(Validate.Numericality, { minimum: 2000, maximum: 3000 } );

Should be between 2000 and 2003, and also be an integer:
var f9 = new LiveValidation('f9');
f9.add(Validate.Numericality, { minimum: 2000, maximum: 2003, onlyInteger: true } );

Should be 4 or more characters in length:
var f11 = new LiveValidation('f11');
f11.add(Validate.Length, { minimum: 4 } );

Confirm password:
var f19 = new LiveValidation('f19');
f19.add(Validate.Confirmation, { match: 'myPasswordField'} );

Should be an email address:
var f20 = new LiveValidation('f20');
f20.add(Validate.Presence);
f20.add(Validate.Email );

output of the page look like with each event like this



Building Blocks of Android Applications

There are four building blocks to an Android application:
• Activity
• Intent Receiver
• Service
• Content Provider

Its of course not necessary each application need all these 4 block but some how some of these will be followed
The XML file AndroidManifest.xml contains all components to be used in the application. All the componets need to declare here before used.

Activity:
Each function or activity that happens in a screen of the application is party of activity.
All the Android class extends a base class Activity. In screens we see are Activity class

Intent and Intent Filters:

Android uses a special class called an Intent to move from screen to screen. An intent describes what an application wants.
There is a related class called an IntentFilter. While an intent is effectively a request to do something, an intent filter is a description of what intents an activity (or intent receiver, see below) is capable of handling.

Intent Receiver:
It’s a some kind of background process. IntentReceiver to receive the data from external events.

Content Provider:
Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if we want our application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

Life Cycle of Android Activity: Life cycle methods sequence are
  1. onCreate()
  2. onStart()
  3. onResume()
  4. onFreeze()
  5. onPause()
  6. onStop()
  7. onDestroy()


Ref:
http://www.anddev.org/lifecycle_of_an_activity-t81.html

Android Simple Login Demo

This demo gives an idea of designing Relative Layout. Its a simple Log In demonstration. Which looks pretty as shown here.







main.xml looks as





Source code as comes here

package my.com.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MyAndroid extends Activity implements OnClickListener
{

private EditText tName;

private EditText tPass;
private Button btnEnter, btnExit;
private AlertDialog.Builder alert;

@Override
public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initComponents();
}

private void initComponents()
{

tName = (EditText) findViewById(R.id.tName);
tPass = (EditText) findViewById(R.id.tPass);
btnEnter = (Button)findViewById(R.id.btnEnter);
btnExit = (Button)findViewById(R.id.btnExit);
btnEnter.setOnClickListener(this);
btnExit.setOnClickListener(this);
}

@Override
public void onClick(View v)
{

if(v == btnEnter) { alert = new AlertDialog.Builder(this); alert.setTitle("Welcome"); alert.setMessage("Your Name: " + tName.getText().toString()+ "\nYour Pass:"+tPass.getText().toString());
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{

public void onClick(DialogInterface dialog, int whichButton)
{

setResult(RESULT_OK);

}
});
alert.show();
}
else if(v == btnExit)
{

finish();
}
}
}

Java SE from sketch - Part 2

Java Primitive Data Types:

Data types are to define various types of data in the program to be used, there are 8 primitive data types in java.



Java Variable or Identifier:
  • Variables are to hold different type of data to be used in the program
  • Java is strict case sensitive language
  • Identifiers must start with a letter, a currency character ($), or a connecting
  • character such as the underscore ( _ ). Identifiers cannot start with a number!
  • After the first character, identifiers can contain any combination of letters,currency characters, connecting characters, or numbers.
  • Key words should be ignored to be an identifier
Legal Identifiers:
int _a;
int $c;
int ______2_w;
int _$;
int this_is_a_very_detailed_name_for_an_identifier;

Illegal Identifiers:
int :b;
int -d;
int e#;
int .f;
int 7g;


Java Code conventions:
Sun defined a coding standard and style programmer. Here are the naming standards that Sun recommends as follow,

Classes:
The first letter should be capitalized, and if several words are linked together to form the name, both the words should have first letter uppercases. Classes, the names should typically be nouns like,
System
Account
StudentProfile

Interfaces:
The names should be adjectives like,
Runnable
Serializable

Methods:
The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs like,
getName ()
setFullName()
validateUser()

Variables:
Like methods, the camelCase format should be used, starting with
a lowercase letter like,
maxWidth
accountBalance
formName

Constants:
They should be named using uppercase letters with underscore characters as separators like,
YEAR_RATE
FINAL_VALUE




Writing First Java Program:

Being an OOP language each Java Program has to be written with a class. Let us write a simple program to begin,

public class MyFirst
{
public static void main(String[] args)
{
System.out.println("New Line");
System.out.print("After New Line");
System.out.println("Not in new Line");
}
}
output:
New Line
After New LineNot in new Line


Here we created a public class called MyFirst having a main method,

public static void main(String[] args) {
}

This main method happens to be the starting point of the program and also all other methods will be called from this method.

System.out.println("New Line");

Here System is a built-in class, out is an Object of output Stream and println(); is the method to display data in the console.

Println(); prints data in a line and moves the cursor to new line. Please remember it never starts in a new line.

Print(); print along with current line.

Output of the program should give the complete idea.

NB: about public, static, void and String[] we will discuss soon.

to be continued...