Monday, 29 November 2010

Learning CDI (Contexts and Dependency Injection) Part 1

Please see the second part here


Contexts and Dependency Injection (CDI ) JSR 299 is a new feature in Java EE 6 Web Profile .  Its Enhance the JSR 330 Dependency Injection in Java. CDI are highly used annotation. CDI makes Loose coupling between classes. To use javax.inject.Inject annotation, we can inject any class into another classes 
  
In traditional way, we create the instance using 'new' operator in another class. here thats instance life time is dependent on target class. its tightly coupled. Here CDI makes to loosely coupled and change the life-cycle for that instance.

Thursday, 25 November 2010

Necessary Classes need run the Simple Java Application

     When we run Java program, then first load the JVM ie. its load the necessary startup classes are needed by all the programs.  its load 300 classes for load JVM.

Its make the initial delay for our program when we run first time. so latest jdk kit (1.6 update 14 or later) provide system tray tool. its load whenever system starts (Startup apps) its load the these nessary classes into cache . so when run the java program it run faster.

suppose i create simple "Hello World" program

public class HelloWorld {
 public static void main(String[] arg )
 {
  System.out.println("Hello World");
 }
}


when i run the HelloWorld.java file through -verbose command line option then we see what are the classes loaded

java -verbose HelloWorld


Output

[Opened /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.Object from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.io.Serializable from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.Comparable from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.CharSequence from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.String from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.reflect.Type from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from /ramki/eclipseHelios/jre/lib/rt.jar]
...
...
...
[Loaded java.security.ProtectionDomain$Key from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.security.Principal from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded HelloWorld from file:/ramki/workspace/sample/bin/]
Hello World
[Loaded java.lang.Shutdown from /ramki/eclipseHelios/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /ramki/eclipseHelios/jre/lib/rt.jar]


Its showing more than 300 classes are loaded.

Wednesday, 24 November 2010

SSL on Tomcat

update : i posted new video for enable ssl in tomcat.


please watch this video in 720p






Splash Screen in Java Application



Traditionally we are using splash screen with progress bar through JFrame or JWindow in Java swing application. But this approach, big disadvantage is splash screen is loaded after jvm is ready and necessary classes are loaded.

In Java 6 provides convient way to show the splash screen even before jvm is get ready.
It can integrate into JAR file also. When double click the jar the splash screen show right now. when  jvm start loading. after loading jvm we can procees the splash screen ie. we draw into splah screen, show progress bar , etc..

Java supports splash screen in gif, png, and jpeg format.


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SplashScreenDemo {

 private static JFrame wkFrame;

 public SplashScreenDemo() {

 }

 /**
  * @param args
  */
 public static void main(final String[] args) {

  

  SwingUtilities.invokeLater(new Runnable() {

   public void run() {
    Graphics2D g = null;

    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
     System.out.println("Splash not supported");
    } else {
     g = splash.createGraphics();
     renderSplashFrame(g, 1, "Iniitiating");
     splash.update();
    }

    // Create the top-level container and add contents to it.
    wkFrame = new JFrame("Simple Splash Screen Test");

    // SwingUtilities.updateComponentTreeUI(wkFrame);

    if (g != null) {
     renderSplashFrame(g, 2, "Loading Core Components");
     splash.update();
    }

    // Load and set an image as icon for the frame
    Image image = Toolkit.getDefaultToolkit().createImage(
      "images/wkicon.png");
    wkFrame.setIconImage(image);

    if (g != null) {
     renderSplashFrame(g, 3, "Loading Core Components");
     splash.update();
    }

    Dimension screenSize = Toolkit.getDefaultToolkit()
    .getScreenSize();

    wkFrame.setSize(screenSize);
    
    if (g != null) {
     renderSplashFrame(g, 4, "Set Dimension");
     splash.update();
    }

    wkFrame.setLocation(0, 0);

    if (g != null) {
     renderSplashFrame(g, 5, "Set Component Position");
     splash.update();
    }

    // do some work

    if (g != null) {
     renderSplashFrame(g, 6, "Loading MenuBar");
     splash.update();
    }

    // do some work

    if (g != null) {
     renderSplashFrame(g, 7, "Loading ToolBar");
     splash.update();
    }

    // do some work

    if (g != null) {
     renderSplashFrame(g, 8, "Loading Main part");
     splash.update();
    }

    // do some work

    if (g != null) {
     renderSplashFrame(g, 9, "Loading Status Bar");
     splash.update();
    }

    // Finish setting up the frame, and show it.
    wkFrame.addWindowListener(new WindowAdapter() {
     @Override
     public void windowClosing(WindowEvent e) {
      System.exit(0);
     }
    });

    wkFrame.setExtendedState(wkFrame.getExtendedState()
      | JFrame.MAXIMIZED_BOTH);

    if (g != null) {
     renderSplashFrame(g, 10, "Finish");
     splash.update();
    }
    
    wkFrame.setVisible(true);

   }

  });

 }

 private static void renderSplashFrame(Graphics2D g, int frame,
   String content) {

  g.setComposite(AlphaComposite.Src);
  g.setColor(Color.GREEN);
  g.fillRect(258, 349, 20 * frame, 10);
  g.setComposite(AlphaComposite.Clear);
  g.fillRect(258, 330, 200, 20);
  g.setPaintMode();
  g.setColor(Color.BLACK);

  g.drawString(content, 258, 340);

 }
}




We need to set command line parameter -splash as follows

java -splash:images/slpash.jpg SplashScreenDemo



When we make jar bundle that time we need to add Splash Screen entry into manifest.mf file in jar

Manifest-Version: 1.0
Main-Class: SplashScreenDemo
SplashScreen-Image: images/splash.jpg



Tip: This Feature only works latest Java 6 Version

final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
     System.out.println("Splash not supported");
    } else {
     g = splash.createGraphics();
     renderSplashFrame(g, 1, "Iniitiating");
     splash.update();
    }

Its retrieve the Splash Screen image mentions in command line parameter. and make the SplashScreen object.
If that object is null then Ur JRE cant support splash screen.
if its supports we construct the Graphics Object then we can process like normal paint method.
and we need to call update method, if anything is modified in graphics.
Related Posts Plugin for WordPress, Blogger...