Friday, September 5, 2014

New and Updated JSRs in JEE 7

Java EE 7 contains 14 new and updated JSRs. Java specifications are available at
  • JSR236:ConcurrencyUtilitiesforJavaEE1.0
  • JSR 338: Java Persistence API 2.1
  • JSR339:JavaAPIforRESTfulWebServices2.0
  • JSR 340: Java Servlet 3.1
  • JSR 341: Expression Language 3.0
  • JSR342:JavaPlatform,EnterpriseEdition7
  • JSR 343: Java Message Service 2.0
  • JSR 344: JavaServer Faces 2.2
  • JSR 345: Enterprise JavaBeans 3.2
  • JSR346:ContextsandDependencyInjectionforJavaEE1.1
  • JSR 349: Bean Validation 1.1
  • JSR352:BatchApplicationsfortheJavaPlatform1.0
  • JSR353:JavaAPIforJSONProcessing1.0
  • JSR356:JavaAPIforWebSocket1.0 

Thursday, March 27, 2014

Regular Expressions Reference Card

Title Reference Explanation Example Matches
Letters Inside Square Brackets [Ww] W or w [Ww]oodwork Woodwork, woodwork

[1234567890] Any digit

Letters outside square brackets ame Matches exact sequence ame -> amersohail794 matches ame

Ranges [0-9] 0 to 9 [0-2] -> amersohail794 matches 794

[a-z] all characters from a to z [a-z] -> amersohail794 matches amersohail

[A-Z] all characters from A to A [a-z] -> amersohail794 matches nothing

[ ] matches space [] -> amer sohail matches space only

Not [^a-z] Not characters from a-z [^a-z] matches 794

[^a-aA-Z] Not characters from a-z and from A-Z [^a-zA-Z] -> amersohail794 matches 794

Beginning of line ^ carret outside the square bracket shows that following character should be matched beginning of the line ^o -> oooohhhh OOOOhhh oooOOOOHHHH will match to first character only and that is "o"

End of line $ $ will match end of the line

Pipe Symbol | for or. A|a is same as [aA] W|w matches W or w

Special Characters *  0 or more occurences of previous character o*O*h*  matches oohhh, ooooooooohhhhh,OOOOOhhhhhh,oooOOOOOhhhhh

+ 1 or more occurences of previous character

? 0 or 1 occurrence of previous character

. Any character

\ is used as escape character [\.] -> This is sample sentence. will match the . In the line

Tuesday, January 28, 2014

Java Programming Terms

 Functional Interface

Its new term in JavaSE 8 for interfaces having just one method e.g. Runnable, ActionListener, Comparator, all are the examples of functional interfaces. Previous name was Single Abstract Method Type (SAM).

Anonymous Inner Classes

Its on the spot implementation of interface without explicitly requiring a name. Usually implementation of interfaces is done by classes and each class does have name but anonymous inner class does not have any name

Lambda Expressions

Short hand or concise notation for implementing functional interfaces than anonymous inner classes.

Immutable and Mutable Objects

Immutable objects are those objects which can not be changed after its creation. String is most famous example of immutable object. There are many pros of immutable objects like they can not be corrupted by multiple threads in concurrent application, least overhead for GC

Guarded Blocks

Its a code block waiting for a certain condition to be true for its execution. It happens in multi threading programs when different threads do different task and they coordinate each other by setting certain conditions / variables.

Generic Type

Generic Type is a generic class or interface that is parametrized over types.

Generic Type Declaration

 Definition of a class with type variables is know as Generic Type Declaration. This type variable can be used anywhere within a class.

The Diamond

In Java SE 7, we can replace the type arguments required to invoke a generic class, with empty set of type arguments (<>) as long as the compiler can determine or infer the type arguments from the content. This pair of angle brackets is known as "the diamond".

Box integerBox = new Box<>();
Raw Type
A Generic Class or interface without type arguments is known as raw type. e.g. List is a generic type but simple List is a raw type. We can assign generic types to raw types but assignment of raw type to generic type will give warnings and will be checked at runtime.


Friday, December 20, 2013

Create a KeyStore to save Secret Key

Secret key is used to encrypt and decrypt the strings like "password" and other secret information. KeyStore is the most secure place to keep secret key. 

Here are the main steps to create new key store and saving a secret key:
  • Get instance of KeyStore using KeyStore.getInstance method. It takes the name of the keystore
  • Once instance of KeyStore is available, load it as empty keyStore
  • Generate the secretKey and save it to keystore for future purposes.
    private final String KEYSTORE_TYPE = "JCEKS";
    private final String KEYSTORE_NAME = "CareKeyStore";
    private final String SECRET_KEY_NAME = "secretKeyAlias";
    private final String KEY_ALGO = "AES";

    private String final pwd ="mypass";

    KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
    ks.load (null,pwd.toCharArray());
    KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGO);
    SecretKey secretKey = keyGen.generateKey();
    KeyStore.ProtectionParameter protParam =
                            new KeyStore.PasswordProtection(pwd.toCharArray());
    KeyStore.SecretKeyEntry skEntry =
                            new KeyStore.SecretKeyEntry(secretKey);
    ks.setEntry(SECRET_KEY_NAME, skEntry, protParam); fos = null;
    try {
           fos = new;
 , pwd.toCharArray());
     }catch(Exception ex){
      }finally {
            if (fos != null) {


Thursday, July 4, 2013

JPA Reference Card

Collection of basic types

Tuesday, April 9, 2013

JSF 2 Tips & Techniques - JSF Messages, Resource Bundle, and Validation Messages

Overriding default JSF error / warning Messages

Message bundle is used to override jsf's default error / warning messages. Declare message-bundle in your faces-config.xml and write new messages for default keys. You can define list of JSF defined messages from JSF specification under "Localized Application Messages"

Resource Bundle

Localized Resource bundle is defined with resource-bundle tag inside faces-config at global level or it is defined at the page level with loadBundle tag. Define resource bundle properties file in src/main/resources folder (You use any pacakge hierarchy e.g. if you want to define your resource bundle something like then you needed to define corresponding folder hierarchy in your resource folder which will be src/main/resources/com/abc/view/ 

After defining your resource bundle, register it either on the page through f:loadBundle tag or globally in faces-config file using resource-bundle tag. 

Then Use it in your page like #{rb['app.title']}

Validation Messages using Bean Validation API

If you want to override default messages of Bean validation api or want to introduce new messages, then create file at root level i.e. at src/main/resources folder.