Friday, October 24, 2008

ApacheDS

This is my masterpiece, yes, just feels like leonardo da vinci with his monalisa, it feels so proud when you complete something that you imagine it as difficult as building Nasa station in your neighborhood, first I ever heard about LDAP, they said that today every enterprises use LDAP for their applications, so they refer to one autentication server for some applications the have, its so scary thing such as object that came from another planet that used by big enterprises in earth. By the way, by little bit of passions and all the goodness of internet and i-friends you can call me master of LDAP right now, or LDAP guru sounds more attractive.

LDAP is Lightweight Directory Access Protocol, damned until right now I still think
this kind of technology is one of that those american people made just for fun and just as trend or mode in IT, but I just joining the trend, same as I joining java people, and enjoying in it. LDAP is just a text file that have username and password, it extension is ldif (LDAP Data Interchange Format), it is stored in a server, and many applications can call it, and match it, and it more faster rather than from database. Ok, perhaps I wrong to describe LDAP, it just my definition, CMIIW, and you can browse the correct definition on the net, and they have plenty of it.

match ---> enabled ---> welcome
---> disabled
---> null in Oracle
not match ---> wrong password ---> 3x opportunities ---> Oracle disabled
---> null in LDAP

Above is my LDAP scheme in my application, fyi, I use appfuse, SpringMVC, Acegi security,
oracle-hibernate, and now want to add LDAP authentication.

I use ApacheDS as LDAP server, there are many other servers such as openLDAP, Spring LDAP, but I use apache as same as other application, but I use the newest version one, rather than the old one that other used. http://directory.apache.org/ is the link, and choose the newest version, 1.5.4, the documentation is clear enough altough there needs more updates, but I try to tell you my way, the way that I think the good way for newbie as me.
  • download the installer, the different version, the server.xml is also different, I dedicate this post for 1.5.4 version, and there are many kinds of the installer, choose the most appropriate for your machine.
  • I've download the .bin installer for my linux-intel machine, if your machine is AMD, then download the AMD version.
  • In the folder of the installer, type: ./apacheds-1.5.2-i386.bin in the terminal. You can download the .deb version and just double click and it's automatically install just like .exe in windows
  • It will prompted, I forgot the exactly message, but by default there are three important places:
    • /opt/apacheds-1.5.4, the main program is here
    • /var/lib/apacheds-1.5.4, the variables of apacheDS is here, such as the server.xml
    • /etc/init.d/apacheds-1.5.4-default, the commad to start, stop or console mode is here
  • Add partition (name for scheme or database in oracle) in /var/lib/apacheds-1.5.4/default/conf/server.xml, inside the partitions tag, new jdbmPartition, we give id as sevenSeas just same as the manual.
<partitions>
...
<jdbmpartition id="sevenSeas" suffix="o=sevenSeas">
</partitions>

  • It needs to restart to apply the change of the adding of the
  • Download the ApacheDS studio for the UI for browsing, adding partition, http://directory.apache.org/studio/, and install it.
  • make new connection, feel with this entry:
    • hostname: 172.16.0.164
    • port: 10389
    • check network parameter
    • Bind DN or username: uid=admin,ou=system
    • Bind Password: secret
    • Check Authentication
  • Download the sevenSeasRoot.ldif and names_sevenSeas.ldif,
  • In the LDAP browser pane in the ApacheDS studio, expand the Root DSE, expand the o=sevenSeas, right click, import, LDIF Import, and then select sevenSeasRoot.ldif and then names_sevenSeas.ldif
  • In the tree, the o=sevenSeas now should have list of people and list of groups.
The LDAP is ready to use, and the next post I will describe in my application side.

Wednesday, October 15, 2008

Language Fundamental, Prepare for SCJP

A friend of mine ask me to get SCJP (Sun Certified Java Programmer) with him, but in his office, he will get change of the voucher if he pass the test, unfortunately not in mine. But in case I have nothing to do right now, so Im reading ebook "Java 2 : Sun Certified Java Programmer & Developer" by Kathy Sierra & Bert Bates. Both of them are SCJP team. I download it from esnips, if the link is broken, just try to google it,
I got the link also from googling first.

This post, and perhaps my next post are just as my summary of my reading, the first part of the book is Language Fundamental.

Java Programming Language Keywords







































































abstractbooleanbreakbytecasecatch
charclassconstcontinuedefaultdo
doubleelseextendsfinalfinallyfloat
forgotoifimplementsimportinstanceof
intinterfacelongnativenewpackage
privateprotectedpublicreturnshortstatic
strictfpsuperswitchsynchronizedthisthrow
throwstransienttryvoidvolatilewhile
assert




Keywords are special reserved words in Java that you cannot use as identifiers (names) for classes, methods, or variables. They have meaning to the compiler. None of them have capital letters,

Access Modifiers
- private Makes a method or a variable accessible only from within its own class.
- protected Makes a method or a variable accessible only to classes in the same package or subclasses of the class.
- public Makes a class, method, or variable accessible from any other class.

Class, Method, and Variable Modifiers
- abstract Used to declare a class that cannot be instantiated, or a method that must be implemented by a nonabstract subclass.
- class Keyword used to specify a class.
- extends Used to indicate the superclass that a subclass is extending.
- final Makes it impossible to extend a class, override a method, or reinitialize a variable.
- implements Used to indicate the interfaces that a class will implement.
- interface Keyword used to specify an interface.
- native Indicates a method is written in a platform-dependent language, such as C.
- new Used to instantiate an object by invoking the constructor.
- static Makes a method or a variable belong to a class as opposed to an instance.
- strictfp Used in front of a method or class to indicate that floating-point numbers will follow FP-strict rules in all expressions.
- synchronized Indicates that a method can be accessed by only one thread at a time.
- transient Prevents fields from ever being serialized. Transient fields are always skipped when objects are serialized.
- volatile Indicates a variable may change out of sync because it is used in threads.

Flow Control
- break Exits from the block of code in which it resides.
- case Executes a block of code, dependent on what the switch tests for.
- continue Stops the rest of the code following this statement from executing in a loop and then begins the next iteration of the loop.
- default Executes this block of code if none of the switch-case statements match.
- do Executes a block of code one time, then, in conjunction with the while statement, it performs a test to determine whether the block should be executed again.
- else Executes an alternate block of code if an if test is false.
- for Used to perform a conditional loop for a block of code.
- if Used to perform a logical test for true or false.
- instanceof Determines whether an object is an instance of a class, superclass, or interface.
- return Returns from a method without executing any code that follows the statement (can optionally return a variable).
- switch Indicates the variable to be compared with the case statements.
- while Executes a block of code repeatedly while a certain condition is true.

Error Handling
- catch Declares the block of code used to handle an exception.
- finally Block of code, usually following a try-catch statement, which is executed no matter what program flow occurs when dealing with an exception.
- throw Used to pass an exception up to the method that called this method.
- throws Indicates the method will pass an exception to the method that called it.
- try Block of code that will be tried, but which may cause an exception.
- assert Evaluates a conditional expression to verify the programmer’s assumption.

Package Control
- import Statement to import packages or classes into code.
- package Specifies to which package all classes in a source file belong.

Primitives

- boolean A value indicating true or false.
- byte An 8-bit integer (signed).
- char A single Unicode character (16-bit unsigned)
- double A 64-bit floating-point number (signed).
- float A 32-bit floating-point number (signed).
- int A 32-bit integer (signed).
- long A 64-bit integer (signed).
- short A 16-bit integer (signed).

Variable Keywords
- super Reference variable referring to the immediate superclass.
- this Reference variable referring to the current instance of an object.

Void Return Type Keyword
- void Indicates no return type for a method.

Unused Reserved Words

- const Do not use to declare a constant; use public static final.
- goto Not implemented in the Java language. It’s considered harmful.

The question might appear to be asking about, say, a runtime logic problem, but the real problem will be that the code won’t even compile because of the illegal use of a keyword. For example, the following code will not compile:

class Foo {
public void go() {
// complex code here
}
public int break(int b) {
// code that appears to break something
}
}

Meanwhile, you’re trying to figure out the complex code within the methods, when you needn’t look beyond the illegal method name and choose the “Code does not compile” answer.

According to the Java Language Specification, null, true, and false are technically literal values (sometimes referred to as manifest constants) and not keywords. Just as with the other keywords, if you try to create an identifier with one of these literal values, you’ll get a compiler error. For the purposes of the exam, treat them just as you would the other reserved words. You will not be asked to differentiate
between reserved words and these reserved literals. “Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants.”

Literals and Ranges of All Primitive Data Types
All six number types in Java are signed, meaning they can be negative or positive. The leftmost bit (the most significant digit) is used to represent the sign, where a 1 means negative (glass half empty) and 0 means positive (glass half full).

The positive range is one less than the negative range because the number zero is stored as a positive binary number. We use the formula -2^(bits - 1) to calculate the negative range, and we use 2^
(bits - 1)–1 for the positive range.

For boolean types there is not a range; a boolean can be only true or false. If someone asks you for the bit depth of a boolean, look them straight in the eye and say, “That’s virtual-machine dependent.” They’ll be impressed. :D

The char type (a character) contains a single, 16-bit Unicode character. Char is really an integer type, it can be assigned to any number type large enough to hold 65535.

Type Bits Bytes Minimum Range Maximum Range
byte 8 1 -2^7 2^7–1
short 16 2 -2^15 2^15–1
int 32 4 -2^31 2^31–1
long 64 8 -2^63 2^63–1
float 32 4 Not needed Not needed
double 64 8 Not needed Not needed

Literal Values for All Primitive Types
Literal = source code representation

Integer Literals
three ways to represent integer numbers in the Java language:
- decimal (base 10), int length = 343;
- octal (base 8), by placing a zero in front of the number
class Octal {
public static void main(String [] args) {
int five = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9
System.out.println("Octal 010 = " + eight);
}
}
- hexadecimal (base 16), including the prefix 0x or the optional suffix extension L
class HexTest {
public static void main (String [] args) {
int x = 0X0001;
int y = 0x7fffffff;
int z = 0xDeadCafe;
System.out.println("x = " + x + " y = " + y + " z = " + z);
}
}

All three integer literals (octal, decimal, and hexadecimal) are defined as int by default, but they may also be specified as long by placing a suffix of L or l after the number:
long jo = 110599L;
long so = 0xFFFFl; // Note the lowercase 'l'

Floating-Point Literals
Floating-point literals are defined as double (64 bits) by default, so if you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the suffix F or f to the number.

float f = 23.467890; // Compiler error, possible loss of precision
float g = 49837849.029847F; // OK; has the suffix "F"

Boolean Literals
Boolean literals are the source code representation for boolean values. A boolean
value can only be defined as true or false. Although in C (and some other
languages) it is common to use numbers to represent true or false, this will
not work in Java. Again, repeat after me, “Java is not C++.”
boolean t = true; // Legal
boolean f = 0; // Compiler error!

Character Literals

A char literal is represented by a single character in single quotes.
char a = 'a';
char b = '@';
You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with \u as follows:
char letterN = '\u004E'; // The letter 'N'

Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). For example, the following are all legal:
char a = 0x892; // octal literal
char b = 982; // int literal
char c = (char) 70000; // The cast is required; 70000 is out of char range
char d = (char) -98; // Ridiculous, but legal
And the following are not legal and produce compiler errors:
char e = -29; // Possible loss of precision; needs a cast
char f = 70000 // Possible loss of precision; needs a cast

You can also use an escape code if you want to represent a character that can’t be typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and double and single quotes.
char c = '\"'; // A double quote
char d = '\n'; // A newline

Literal Values for Strings
A string literal is a source code representation of a value of a String object. For
example, the following is an example of two ways to represent a string literal:
String s = "Bill Joy";
System.out.println("Bill" + " Joy");

Array Declaration, Construction, and Initialization
Arrays are objects in Java that store multiple variables of the same type.

For this objective, you need to know three things:
■ How to make an array reference variable (declare)
■ How to make an array object (construct)
■ How to populate the array with elements (initialize)

Declaring an Array
Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

Declaring an Array of Primitives
int[] key; // Square brackets before name (recommended)
int key []; // Square brackets after name (legal but less readable)
Declaring an Array of Object References
Thread[] threads; // Recommended
Thread threads []; // Legal but less readable

It is never legal to include the size of the array in your declaration. You might see a question or two that include code similar to the following:
int[5] scores;
The preceding code won’t make it past the compiler. Remember, the JVM doesn’t allocate space until you actually instantiate the array object.

Constructing an Array
Constructing an array means creating the array object on the heap—in other words, doing a new on the array type. To create an array object, Java needs to know how much space to allocate on the heap, so you must specify the size of the array at construction time. The size of the array is the number of elements the array will hold.

Constructing One-Dimensional Arrays
The most straightforward way to construct an array is to use the keyword new followed by the array type, with a bracket specifying how many elements of that type the array will hold. The following is an example of constructing an array of type int:
int[] testScores; // Declares the array of ints
testScores = new int[4]; //constructs an array and assigns it
//the testScores variable

You can also declare and construct an array in one statement as follows:
int[] testScores = new int[14];
This single statement produces the same result as the two previous statements.
Arrays of object types can be constructed in the same way:
Thread[] threads = new Thread[5];

The exam will expect you to know, for example, that the preceding code produces just one object (the array assigned to the reference variable named threads). The single object referenced by threads holds five Thread reference variables, but no Thread objects have been created or assigned to those references.

Remember, arrays must always be given a size at the time they are constructed. The JVM needs the size to allocate the appropriate space on the heap for the new array object. It is never legal, for example, to do the following:
int[] carList = new int[]; // Will not compile; needs a size

Multidimensional arrays, remember, are simply arrays of arrays. So a two-dimensional array of type int is really an object of type int array (int []), with each element in that array holding a reference to another int array. The second dimension holds the actual int primitives.
The following code declares and constructs a two-dimensional array of type int:
int[][] ratings = new int[3][];
Notice that only the first brackets are given a size. That’s acceptable in Java, since the JVM needs to know only the size of the object assigned to the variable ratings.

Initializing an Array
Initializing an array means putting things into it.

Look for code that tries to access an out of range array index. For example, if an array has three elements, trying to access the [3] element will raise an ArrayIndexOutOfBoundsException, because in an array of three elements, the legal index values are 0, 1, and 2.

Be sure to recognize that these cause runtime exceptions and not compiler errors! Nearly all of the exam questions list both runtime exception and compiler error as possible answers.
int[] x = new int[5];
x[4] = 2; // OK, the last element is at index 4
x[5] = 3; // Runtime exception. There is no element at index 5!

int [] z = new int[2];
int y = -3;
z[y] = 4; // Runtime exception.; y is a negative number

A two-dimensional array (an array of arrays) can be initialized as follows:
int[][] scores = new int[3][];
// Declare and create an array holding three references to int arrays
scores[0] = new int[4];
// the first element in the scores array is an int array of four int element
scores[1] = new int[6];
// the second element in the scores array is an int array of six int elements
scores[2] = new int[1];
// the third element in the scores array is an int array of one int element

Initializing Elements in a Loop
Array objects have a single public variable length that gives you the number of elements in the array. The last index value, then, is always one less than the length. For example, if the length of an array is 4, the index values are from 0 through 3. Often, you’ll see array elements initialized in a loop as follows:
Dog[] myDogs = new Dog[6]; // creates an array of 6 Dog references
for (int x = 0; x < style="font-weight: bold;">Declaring, Constructing, and Initializing on One Line

int[] dots = {3,6,9,8};
The size (length of the array) is determined by the number of items between the comma-separated curly braces.

Dog puppy = new Dog("Frodo");
Dog[] myDogs = {puppy, new Dog("Clover"), new Dog("Aiko")};
Four objects are created:
- 1 Dog object referenced by puppy
- 1 Dog [ ] object referenced by myDogs
- 2 Dog objects referenced by myDogs[1] and myDogs[2]

Cat[][] myCats = {{new Cat("Fluffy"), new Cat("Zeus")},{new Cat("Belbo"), new Cat("Legolas"), new Cat("Bert")}}
Eight objects are created:
- 1 2-D Cat[ ][ ] object referenced by myCat
- 2 Cat[ ] array objects
- 5 Cat objects

Constructing and Initializing an Anonymous Array
The second shortcut is called anonymous array creation and can be used to construct and initialize an array, and then assign the array to a previously declared array reference variable:
int[] testScores;
testScores = new int[] {4,7,2};
The use is for example, as an argument to a method that takes an array parameter. The following code demonstrates a just-in-time array argument:
public class Foof {
void takesAnArray(int [] someArray) {
// use the array parameter
...
}
public static void main (String [] args) {
Foof f = new Foof();
f.takesAnArray(new int[] {7,7,8,2,5}); //we need an array argument
}
}

Remember that you do not specify a size when using anonymous array creation syntax. The size is derived from the number of items (comma-separated) between the curly braces. Pay very close attention to the array syntax used in exam questions (and there will be a lot of them). You might see syntax such as
new Object[3] {null, new Object(), new Object()};
// not legal;size must not be specified

Arrays of Primitives
Primitive arrays can accept any value that can be promoted implicitly to the declared type of the array. Chapter 3 covers the rules for promotion in more detail, but for an example, an int array can hold any value that can fit into a 32-bit int variable. Thus, the following code is legal:
int[] weightList = new int[5];
byte b = 4;
char c = 'c';
short s = 7;
weightList[0] = b; // OK, byte is smaller than int
weightlist[1] = c; // OK, char is smaller than int
weightList[2] = s; // OK, short is smaller than int

Arrays of Object References
If the declared array type is a class, you can put objects of any subclass of the declared type into the array. For example, if Dog is a subclass of Animal, you can put both Dog objects and Animal objects into the array as follows:
class Car {}
class Subaru extends Car {}
class Honda extends Car {}
class Ferrari extends Car {}
Car [] myCars = {new Subaru(), new Honda(), new Ferrari()};

If the array is declared as an interface type, the array elements can refer to any instance of any class that implements the declared interface.

Array Reference Assignments for One-Dimensional Arrays
It's not talking about references in the array (in other words, array elements), but rather references to the array object. For example, if you declare an int array, the reference variable you declared can be reassigned to any int array (of any size), but cannot be reassigned to anything that is not an int array, including an int value. Remember, all arrays are objects, so an int array reference cannot refer to an int primitive. The following code demonstrates legal and illegal assignments for primitive arrays:
int[] splats;
int[] dats = new int[4];
char[] letters = new char[5];
splats = dats; // OK, dats refers to an int array
splats = letters; // NOT OK, letters refers to a char array

Arrays that hold object references, as opposed to primitives, aren’t as restrictive. Just as you can put a Honda object in a Car array (because Honda extends Car), you can assign an array of type Honda to a Car array reference variable as follows:
Car[] cars;
Honda[] cuteCars = new Honda[5];
cars = cuteCars; // OK because Honda is a type of Car
Beer[] beers = new Beer [99];
cars = beers; // NOT OK, Beer is not a type of Car

Array Reference Assignments for Multidimensional Arrays
- the array you’re assigning must be the same dimension as the reference you’re assigning it to.
int[] blots;
int[][] squeegees = new int[3][];
blots = squeegees; // NOT OK, squeegees is a two-d array of int arrays
int[] blocks = new int[6];
blots = blocks; // OK, blocks is an int array
- It must assigning array, not a primitif type
int[][] books = new int[3][];
int[] numbers = new int[6];
int aNumber = 7;
books[0] = aNumber; //NOT OK, expecting an int array instead of an int
books[0] = numbers; //OK, numbers is an int array

Using a Variable or Array Element That Is Uninitialized and Unassigned
- instance variable is declared within the class but outside any method or constructor
- local variable is declared within a method (or in the argument list of the method)
the compiler complains if you try to use a local variable before initializing it.

Primitive Instance Variables
public class BirthDate {
int year; // Instance variable
public static void main(String [] args) {
BirthDate bd = new BirthDate();
bd.showYear();
}
public void showYear() {
System.out.println("The year is " + year);
}
}
When the program is started, it gives the variable year a value of zero, the default value for primitive number instance variables.

Variable Type Default Value
Object reference null (not referencing any object)
byte, short, int, long 0
float, double 0.0
boolean false
char ‘\u0000’

Object Reference Instance Variables
The following modification to the Book code runs into trouble:
public class Book {
private String title;
public String getTitle() {
return title;
}
public static void main(String [] args) {
Book b = new Book();
String s = b.getTitle(); // Compiles and runs
String t = s.toLowerCase(); // Runtime Exception!
}
}
When we try to run the Book class, the JVM will produce the following error:
%java Book
Exception in thread "main" java.lang.NullPointerException
at Book.main(Book.java:12

Array Instance Variables
An array is an object; thus, an array instance variable that’s declared but not explicitly initialized will have a value of null, just as any other object reference instance variable.
But...if the array is initialized, what happens to the elements contained in the array? All array elements are given their default values—the same default values that elements of that type get when they’re instance variables. The bottom line: Array elements are always always always given default values, regardless of where the array itself is declared or instantiated.

If we initialize an array, object reference elements will equal null if they are not initialized individually with values. If primitives are contained in an array, they will be given their respective default values. For example, in the following code, the array year will contain 100 integers that all equal zero by default:
public class BirthDays {
static int [] year = new int[100];
public static void main(String [] args) {
for(int i=0;i<100;i++)
System.out.println("year[" + i + "] = " + year[i]);
}
}
When the preceding code runs, the output indicates that all 100 integers in the array equal zero.

Local (Stack, Automatic) Primitives and Objects
Local Primitives
In the following time travel simulator, the integer year is defined as an automatic variable because it is within the curly braces of a method.
public class TimeTravel {
public static void main(String [] args) {
int year = 2050;
System.out.println("The year is " + year);
}
}
Okay, so we’ve still got work to do on the physics. Local variables, including primitives, always always always must be initialized before you attempt to use them (though not necessarily on the same line of code). Java does not give local variables a default value; you must explicitly initialize them with a value, as in the preceding example. If you try to use an uninitialized primitive in your code, you’ll get a compiler error:
public class TimeTravel {
public static void main(String [] args) {
int year; // Local variable (declared but not initialized)
System.out.println("The year is " + year); // Compiler error
}
}
Compiling produces the following output:
%javac TimeTravel.java
TimeTravel.java:4: Variable year may not have been initialized.
System.out.println("The year is " + year);
1 error

Local Objects
Objects, too, behave differently when declared within a method rather than as instance variables. With instance variable object references, you can get away with leaving an object reference uninitialized, as long as the code checks to make sure the reference isn’t null before using it. Remember, to the compiler, null is a value. You can’t use the dot operator on a null reference, because there is no object at the other end of it, but a null reference is not the same as an uninitialized reference. Locally declared references can’t get away with checking for null before use, unless you explicitly initialize the local variable to null. The compiler will complain about the following code:
import java.util.Date;
public class TimeTravel {
public static void main(String [] args) {
Date date;
if (date == null)
System.out.println("date is null");
}
}
Compiling the code results in the following error:
%javac TimeTravel.java
TimeTravel.java:5: Variable date may not have been initialized.
If (date == null)
1 error

Instance variable references are always given a default value of null, until explicitly initialized to something else. But local references are not given a default value; in other words, they aren’t null. If you don’t initialize a local reference variable, then by default, its value is...well that’s the whole point—it doesn’t have any value at all! So we’ll make this simple: Just set the darn thing to null explicitly, until you’re ready to initialize it to something else. The following local variable will compile
properly:
Date date = null; // Explicitly set the local reference variable to null

Local Arrays
Just like any other object reference, array references declared within a method must be assigned a value before use. That just means you must declare and construct the array. You do not, however, need to explicitly initialize the elements of an array. We’ve said it before, but it’s important enough to repeat: array elements are given their default values (0, false, null, ‘\u0000’, etc.) regardless of whether the array is declared as an instance or local variable. The array object itself, however, will not be initialized if it’s declared locally. In other words, you must explicitly initialize an array reference if it’s declared and used within a method, but at the moment you construct an array object, all of its elements are assigned their default values.

Command-Line Arguments to Main
Now that you know all about arrays, command-line arguments will be a piece of cake. Remember that the main method—the one the JVM invokes—must take a String array parameter. That String array holds the arguments you send along with the command to run your Java program, as follows:
class TestMain {
public static void main (String [] args) {
System.out.println("First arg is " + args[0]);
}
}
When invoked at the command line as follows,
%java TestMain Hello
the output is
First arg is Hello

Tuesday, October 14, 2008

beanValidator Validates Pojo's Attributes Easily

It is powerful tool from spring, I guess it's only in spring because I never user the other framework such as tapestry or struts. It automatically validate application from filling fields of the pojos. And it is so simple because only add line above the attribute setters in its XDoclet, and small validator in validation-global.xml, and a line in action-servlet and application properties for the error message.

Ok, let's take a look in my pojo Lbuibp, which mean Reporter Bank, below is some code sniplet from it:


/**
* @spring.validator type="required" type="mask" msgkey="errors.char9"
* @spring.validator-var name="mask" value="${char9}"
*/
public void setId(String id) {
this.id = id;
}
/**
* @spring.validator type="mask" msgkey="errors.number8"
* @spring.validator-var name="mask" value="${number8}"
*/
public void setKursLaporan(Long kursLaporan) {
this.kursLaporan = kursLaporan;
}
/**
* @spring.validator type="required"
* @spring.validator type="mask" msgkey="errors.varchar30"
* @spring.validator-var name="mask" value="${varchar30}"
*/
public void setNamaBank(String namaBank) {
this.namaBank = namaBank;
}

Ok, those are setters of my attribute: id, kursLaporan, and namaBank, and their type is easily known which is char9, number8, and varchar30.

And then I add my own validator in validation-global.xml:
<constant>
<constant-name>char9</constant-name>
<constant-value>^[a-zA-Z0-9]{9}$</constant-value>
</constant>
<constant>
<constant-name>number8</constant-name>
<constant-value>^\d{1,8}$|^\d{1,2}((\,)?\d{3}){1,2}$</constant-value>
</constant>
<constant>
<constant-name>varchar30</constant-name>
<constant-value>^[\w\s.+*(),=:'"]{1,30}$</constant-value>
</constant>

- char9, it receives nine digits alphanumeric characters
- number8,
^\d{1,8}$ which receives number from 1 until 8 characters, or ^\d{1,2}((\,)?\d{3}){1,2}$ which receives 1 or 2 numbers, and then comma (or not), and then three numbers, comma and three number as a group, which is 1 or 2 of that group
- varchar30, it receives 1 or until 30 characters of set of word character, space, dot, plus, star, and etc characters.

And then in the action-servlet.xml we add a line in the lbuibpFormController bean which control the form of Lbuibp.
<bean id="lbuibpFormController" class="...webapp.action.LbuibpFormController">
...
<property name="validator"><ref bean="beanValidator"></ref>
...
</property>

And in the applicationResources.properties we add some message as below:

errors.char9={0} is invalid, it must 9 characters of string.
errors.number8={0} is invalid, it must 8 characters of number.
errors.varchar30={0} is invalid, it must maximum 30 characters of string.

And those error messages simply called if the user is inputing unreceivable syntax that will unpass of the beanValidator.

And this beanValidator is server side validation, we can add client side validation using javascript.

REGEX

Regex stands for Regular Expression, it also called re, I remember got it in automata or discrete math when bachelor. Basically, a regular expression is a pattern describing a certain amount of text. Their name comes from the mathematical theory on which they are based. Most of this post content is taken from http://www.regular-expressions.info, and I just copy and paste it.

Regex is a powerful tool when we already learn it, in my project, I used regex as field validator of my pojos and as validator of the uploaded file name, fyi, in my application there is a part that user upload files, and that file is filtered first by application, because my appliaction is monthly bank reporting, so it needs to know that the file is report in what month, I mean the file must named year and month in the last of its name.

11 Special Charactes
(metacharacters)
- the opening square bracket [
- the backslash \
- the caret ^
- the dollar sign $
- the period or dot .
- the vertical bar or pipe symbol |
- the question mark ?
- the asterisk or star *
- the plus sign +
- the opening round bracket (
- the closing round bracket )

If we want to use one of these metacharacters, we need to escape them with a backslash, or double backslash in java.

Non-Printable Characters
- \t to match a tab character (ASCII 0x09),
- \r
for carriage return (0x0D) , and
- \n for line feed (0x0A).
- More exotic non-printables are \a (bell, 0x07), \e (escape, 0x1B), \f (form feed, 0x0C) and \v (vertical tab, 0x0B).

And once again, we need double backslash in java, one backslash is for the regex commonly.

Regex Engine
A regular expression "engine" is a piece of software that can process regular expressions, trying to match the pattern to the given string.

There are two kinds of regex engines: text-directed engines, and regex-directed engines. Jeffrey Friedl calls them DFA and NFA engines, respectively.
http://www.regular-expressions.info is based on regex-directed engines, and that sites is based on pearl language. First, I thought java is also regex-directed, but surprisingly is not, I create mini program as told by the site to find out is java text-directed engines or regex-directed, and surprisingly it's text-directed. The regex is: regex|regex not and the string is regex not, Below is the code, If the resulting match is only regex, the engine is regex-directed. If the result is regex not, then it is text-directed. Below is the code, and the output is "Found 'regex not' at position 0":

public static void main(String[] args) {
Pattern p = Pattern.compile ( "regex|regex not", Pattern.CASE_INSENSITIVE ) ;
String text = "regex not";
Matcher m = p.matcher ( text ) ;
if ( m.matches() ) {
System.out.println ( "Found '" + m.group ( 0 ) + "' at position " + m.start ( 0 ) ) ;
}
else {
System.err.println("No found match");
}
}


Regex-directed always returns the leftmost match, if we applying regex cat to He captured a catfish for his cat, the engine will try to match the first token in the regex c to the first character in the match H. This fails. There are no other possible permutations of this regex, because it merely consists of a sequence of literal characters. So the regex engine tries to match the c with the e. This fails too, as does matching the c with the space. Arriving at the 4th character in the match, c matches c. The engine will then try to match the second token a to the 5th character, a. This succeeds too. But then, t fails to match p. At that point, the engine knows the regex cannot be matched starting at the 4th character in the match. So it will continue with the 5th: a. Again, c fails to match here and the engine carries on. At the 15th character in the match, c again matches c. The engine then proceeds to attempt to match the remainder of the regex at character 15 and finds that a matches a and t matches t.

The entire regular expression could be matched starting at character 15. The engine is "eager" to report a match. It will therefore report the first three letters of catfish as a valid match. The engine never proceeds beyond this point to see if there are any "better" matches.


But in java which is text
-directed, he report no match for that situation, I already try regex cat to He captured a catfish for his cat, and it report no match found, it only receive String cat.

Character Set or Character Class "[ ]"
With character set we can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. We can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9.


- \d is short for [0-9]
- \w stands for "word character", usually [A-Za-z0-9_]. Notice the inclusion of the underscore and digits.
- \s stands for "whitespace character".
- The negated versions, \D is the same as [^\d], \W is short for [^\w] and \S is the equivalent of [^\s].

Be careful when using the negated shorthands inside square brackets. [\D\S] is not the same as [^\d\s]. The latter will match any character that is not a digit or whitespace. So it will match x, but not 8. The former, however, will match any character that is either not a digit, or is not whitespace. Because a digit is not whitespace, and whitespace is not a digit, [\D\S] will match any character, digit, whitespace or otherwise.

If we repeat a character class by using the ?, * or + operators, we will repeat the entire character class, and not just the character that it matched. The regex [0-9]+ can match 837 as well as 222. But If we want to repeat the matched character, rather than the class, you will need to use backreferences. ([0-9])\1+ will match 222 but not 837.


Negated Character Classes
Typing a caret after the opening square bracket will negate the character class. The result is that the character class will match any character that is not in the character class. It is important to remember that a negated character class still must match a character. q[^u] does not mean: "a q not followed by a u". It means: "a q followed by a character that is not a u". It will not match the q in the string Iraq. It will match the q and the space after the q in Iraq is a country. Indeed: the space will be part of the overall match, because it is the "character that is not a u" that is matched by the negated character class in the above regexp.

Metacharacters Inside Character Classes
Note that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.

To include a backslash as a character without any special meaning inside a character class, you have to escape it with another backslash. [\\x] matches a backslash or an x. The closing bracket (]), the caret (^) and the hyphen (-) can be included by escaping them with a backslash, or by placing them in a position where they do not take on their special meaning. I recommend the latter method, since it improves readability. To include a caret, place it anywhere except right after the opening bracket. [x^] matches an x or a caret. You can put the closing bracket right after the opening bracket, or the negating caret. []x][^]x] matches any character that is not a closing bracket or an x. The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. Both [-x] and [x-] match an x or a hyphen. matches a closing bracket or an x.

The Dot Matches (Almost) Any Character
The dot matches a single character, without caring what that character is. The only exception are newline characters. So by default, the dot is short for
the [^\n].

Anchors
Anchors are a different breed. They do not match any character at all. Instead, they match a position before, after or between characters. And they are Zero-Length Matches.
- The caret ^ matches the position before the first character in the string.
- $ matches right after the last character in the string.
- \b matches at a position that is called a "word boundary"


Applying ^a to abc matches a. ^b will not match abc at all, because the b cannot be matched right after the start of the string, matched by ^. While c$ matches c in abc, while a$ does not match at all.

There are four different positions that qualify as word boundaries:
* Before the first character in the string, if the first character is a \w.
* After the last character in the string, if the last character is a
\w.
* Between a
\w and a non-word character following right after \w.
* Between a \W and a
\w following right after \W character.

In the regex-engine,
\bis\b will match This island is beautiful. Space is non word character, and the others are. The engine has successfully matched the word is in our string, skipping the two earlier occurrences of the characters i and s. If we had used the regular expression is, it would have matched the is in This.

Alternation "|"
The alternation operator has the lowest precedence of all regex operators. That is, it tells the regex engine to match either everything to the left of the vertical bar, or everything to the right of the vertical bar. If we want to limit the reach of the alternation, we will need to use round brackets for grouping.

Optional "?"
It is greedy. The question mark gives the regex engine two choices: try to match the part the question mark applies to, or do not try to match it. The engine will always try to match that part. Only if this causes the entire regular expression to fail, will the engine try ignoring the part the question mark applies to.

The effect is that if you apply the regex Feb 23(rd)? to the string Feb 23rd, the match will always be Feb 23rd and not Feb 23. You can make the question mark lazy (i.e. turn off the greediness) by putting a second question mark after the first.

"?,*, +,{}"
- ? is optional, 1 or 0
- * is 0 or more repetition
- + is 1 or more repetition
- {} is limiting repetition, The syntax is {min,max}
, if we want exactly n reeptition so the syntax is {n}

Again, they are greedy, suppose we want to receive html tag with this regex:
<.+>. when we test it on a string like This is a first test. we might expect the regex to match and when continuing after that match, . But it does not. The regex will match first. Obviously not what we wanted. The reason is that the plus is greedy. That is, the plus causes the regex engine to repeat the preceding token as often as possible. Only if that causes the entire regex to fail, will the regex engine backtrack. That is, it will go back to the plus, make it give up the last iteration, and proceed with the remainder of the regex.

To fix the problem:
- laziness, by adding "?", so the regex will becomes <.+?>.

- better solution without backtracking: We can use a greedy plus and a negated character class: <[^>]+>.

Grouping and Backreference "()"
Besides grouping part of a regular expression together, round brackets also create a "backreference". A backreference stores the part of the string matched by the part of the regular expression inside the parentheses.

([a-c])x\1x\1 will match axaxa, bxbxb and cxcxc. \1 is calling backreference ([a-c]). and in java we need double backslash rather than single backslash.

The other example is <([A-Z][A-Z0-9]*)\b[^>]*>.*? that want to match a pair of opening and closing HTML tags, and the text in between, such text bold italic.


Java also support forward reference, that is: we can use a backreference to a group that appears later in the regex. Obviously only useful if they're inside a repeated group. The example is (\2two|(one))+ will match oneonetwo.