iDatamining.org

I am looking for projects to work on
Please contact with me at yiyu.jia@BostonInfoPro.com!

Wednesday, October 26, 2011

A simple tutorial about Exception handling in Jersey 1.1.5

There are at least three ways for us to deal with Exception in Jersey RESTful framework.

  1. Create a WebApplicationException instance and throw it in code.
  2. Extending your own Exception from WebApplicationException and throw your own exception in code
  3. creating an ExceptionMapper to have chance to further customize response upon exception.

This simple tutorial shows how to map our own defined exception in Jersey. Code has been created and tested in netbeans 6.9.1 with Jersey version 1.1.5 and Tomcat 6.

creating our dummy runtime exception class

package jia.blog;

public class JiaAppException extends RuntimeException{

    public JiaAppException( String message ) {
        super( message );
    }

}

Create our testing Web service

package jia.blog;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

/**
 * REST Web Service
 *
 * @author Yiyu Jia
 */

@Path("generic")
public class WSTester {
    @Context
    private UriInfo context;

    /** Creates a new instance of WSTester */
    public WSTester() {
    }

    /**
     * Retrieves representation of an instance of jia.blog.WSTester
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/xml")
    public String getXml() {        
         throw new JiaAppException("Sorry. You are not allowed to access this resource.");
    }

}

Creating Exception Mapper class

package jia.blog;

import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
 *
 * @author Yiyu Jia
 */

@Provider
public class JiaExceptionMapper implements ExceptionMapper< jiaappexception > {

    @Override
    public Response toResponse(JiaAppException jiaException) {
        
        GenericEntity ge = new GenericEntity < string > ( jiaException.getMessage ( ) ) { };

        return Response.status(Response.Status.FORBIDDEN).
                entity(ge).build();
    }
}


The web.xml file

Note: no servlet init param 'com.sun.jersey.config.property.packages'

    
        ServletAdaptor
        com.sun.jersey.spi.container.servlet.ServletContainer       
        1
    
    
        ServletAdaptor
        /resources/*
    
    
        
            30
        
    
    
        index.jsp
    


Test it

If you deploy web app on tomcat running on localhost and listen on port 8080, you test by pointing web browser to access URL http://localhost:8080/JerseyTest/resources/generic .

Wednesday, October 19, 2011

Why does DB2 not use Index I created?!

Recently, I heard a guy cried that the DB2 for iSeriese does not use index he created. Then, I asked him if he has check the size of table. The answer is NO!

So, it seems that some developers do not aware that a RDBMS' query optimizer may not use index though indexes are created to optimize SQL query speed. In fact, DBMS normally have its own algorithm to decide when to use index scan or table scan in a SQL query. In general, DBMS should use index scan instead of full table scan only when size of index is smaller than size of table. Otherwise, there is no point for DBMS to use index. This is true for DB2, MySQL, Oracle, MS SQL etc.

How to create index or combined index and how many index should be created for a table is big topic. But, we shall know that index may not be always used when SQL query optimizer thinks it is not worthy. This is most likely true for a new launched application, which has not received/created many data in tables. So, do not cry that DBMS is suck that it does not use indexes. Check your table size first. Then, check your DBMS configuration. For example, MySQL has configurable threshold value, which determine when query optimizer can use index instead of full table scan. But, you can not force SQL query optimizer to use index. The most we can do is configuring table to prefer index scan than table scan.

Here is a more detailed discussion about "magic number".

Tuesday, October 18, 2011

Java Class loader and static variable and JVM memory management.

We always say that there is only one copy of static fields of a class in JVM memory. This is incorrect. At least, it is not precise. The precise statement should be "there is only one copy of static fields of class for each class loader in one JVM". In other words, we will have multiple copies of static fields in one JVM memory if we use different ClassLoaders to load same class into one JVM.

Different JVM vendor may have different ways to do JVM memory management. But, I think it will be common for JVM to have class initializer and instance initializer. static fields in class is initialized in class initializer. Using hotspot as example, "instanceKlass" in JVM contains static fields. To understand more about java memory management, these articles may be helpful. Presenting the Permanent Generation , OpenJDK Storage management, and JVM spec.

But, we are not guys who are able to create JVM. So, let's use our ways to determine this. What we will do here are
  1. Create our own class loader.
  2. Create a simple Java class which has static field for testing.
  3. Create two instances of our own class loader and let them to load class.
  4. Changing static field value in one loaded class instance and see if the rest one has changed field or not. 
  5. We will say that static fields defined in same class have different copies in memory if their containing class is loaded into memory by different classloader.

getting a class loader from JavaBlogging and slightly modified it as below.

package jia.blog;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Our custom implementation of the ClassLoader.
 * For any of classes from "javablogging" package
 * it will use its {@link CustomClassLoader#getClass()}
 * method to load it from the specific .class file. For any
 * other class it will use the super.loadClass() method
 * from ClassLoader, which will eventually pass the
 * request to the parent.
 *
 */
public class JiaClassLoader extends ClassLoader {

    /**
     * Parent ClassLoader passed to this constructor
     * will be used if this ClassLoader can not resolve a
     * particular class.
     *
     * @param parent Parent ClassLoader
     *              (may be from getClass().getClassLoader())
     */
    public JiaClassLoader() {
        super(JiaClassLoader.class.getClassLoader());
    }

    /**
     * Loads a given class from .class file just like
     * the default ClassLoader. This method could be
     * changed to load the class over network from some
     * other server or from the database.
     *
     * @param name Full class name
     */
    private synchronized  Class getClass(String name)
        throws ClassNotFoundException {

        // is this class already loaded?
        Class cls = findLoadedClass(name);
        if (cls != null) {
            System.out.println("class " + name + "has been loaded.");
            return cls;
        } else {
            System.out.println("class " + name + " has not been loaded. Loading now.");
        }


        // We are getting a name that looks like
        // javablogging.package.ClassToLoad
        // and we have to convert it into the .class file name
        // like javablogging/package/ClassToLoad.class
        String file = name.replace('.', File.separatorChar)
            + ".class";
        byte[] b = null;
        try {
            // This loads the byte code data from the file
            b = loadClassData(file);
            // defineClass is inherited from the ClassLoader class
            // and converts the byte array into a Class
            cls = defineClass(name, b, 0, b.length);
            resolveClass(cls);
            return cls;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Every request for a class passes through this method.
     * If the requested class is in "javablogging" package,
     * it will load it using the
     * {@link CustomClassLoader#getClass()} method.
     * If not, it will use the super.loadClass() method
     * which in turn will pass the request to the parent.
     *
     * @param name
     *            Full class name
     */
    @Override
    public Class loadClass(String name)
        throws ClassNotFoundException {
        System.out.println("loading class '" + name + "'");
        if (name.startsWith("jia.")) {
            return getClass(name);
        }
        return super.loadClass(name);
    }

    /**
     * Loads a given file (presumably .class) into a byte array.
     * The file should be accessible as a resource, for example
     * it could be located on the classpath.
     *
     * @param name File name to load
     * @return Byte array read from the file
     * @throws IOException Is thrown when there
     *               was some problem reading the file
     */
    private byte[] loadClassData(String name) throws IOException {
        // Opening the file
        InputStream stream = getClass().getClassLoader()
            .getResourceAsStream(name);
        int size = stream.available();
        byte buff[] = new byte[size];
        DataInputStream in = new DataInputStream(stream);
        // Reading the binary data
        in.readFully(buff);
        in.close();
        return buff;
    }
}

Create a very simple class having static field.

package jia.blog;

/**
 *
 * @author Yiyu Jia
 */
public class AStatic {

    public static int yiyu = -1;

    public static int getYiyu() {
        return yiyu;
    }

    public static void setYiyu(int yiyu) {
        AStatic.yiyu = yiyu;
    }

    public static void printYiyu(String instanceName){
        System.out.println("The static member yiyu's value in " 
                + instanceName + " is " + AStatic.yiyu);
    }

}

Now, we write a testing class


package jia.blog;

/**
 *
 * @author Yiyu Jia
 */

public class TestStatic  {
    public static void main(String[] args) throws Exception {
        
        JiaClassLoader loader1 = new JiaClassLoader();
        JiaClassLoader loader2 = new JiaClassLoader();
        if(loader1.equals(loader2)){
            System.out.println("two classloaders are same");
        }
        else {
            System.out.println("two classloaders are different");
        }

        Class clsA = Class.forName("jia.blog.AStatic", true, loader1);
        Class clsB = Class.forName("jia.blog.AStatic", true, loader2);

        System.out.println("jia.blog.AStatic class: " + clsA);

        Object instanceA = clsA.newInstance();
        Object instanceB = clsB.newInstance();


        if (clsA.equals(clsB)) {
            System.out.println("class loaded in different customer classloader are same");
        }
        else {
            System.out.println("class loaded in different customer classloader are different.");
        }

        clsA.getMethod("setYiyu",int.class).invoke(instanceA, 1);
        clsA.getMethod("printYiyu", String.class).invoke(instanceA, "instanceA");
        clsB.getMethod("printYiyu", String.class).invoke(instanceB, "instanceB");
       
    }
}

Running testing code, we get result as below

two classloaders are different
loading class 'jia.blog.AStatic'
class jia.blog.AStatic has not been loaded. Loading now.
loading class 'java.lang.Object'
loading class 'jia.blog.AStatic'
class jia.blog.AStatic has not been loaded. Loading now.
loading class 'java.lang.Object'
jia.blog.AStatic class: class jia.blog.AStatic
class loaded in different customer classloader are different.
loading class 'java.lang.String'
loading class 'java.lang.System'
loading class 'java.lang.StringBuilder'
loading class 'java.io.PrintStream'
The static member yiyu's value in instanceA is 1
loading class 'java.lang.String'
loading class 'java.lang.System'
loading class 'java.lang.StringBuilder'
loading class 'java.io.PrintStream'
The static member yiyu's value in instanceB is -1

Having this knowledge in mind, we can explore more about how to use static and thread safe issue. In real world, we can see customized class loader in java application server, loading class from network, Web application is a born multi thread environment. It will be necessary to have a clear view about class loader architecture, thread safe, and static members.

Also, there are more interesting discussion on this link.

Thursday, October 6, 2011

Ext JS 4 reCAPTCHA widget

After password strength meter widget, I am asked to give a Captcha verify widget too. Well, it is not so difficult, below is a ReCaptcha widget for Ext JS 4.x. On ExtJS 2.x forum, there are detailed description about how to apply for ReCaptcha account.


Source code:

//define a reCaptcha widget.
Ext.define('yiyu.util.ReCaptcha', 
 { 
 extend : 'Ext.Component',
  alias : 'widget.recaptcha',
    

    onRender : function(ct, position){
     var me = this;
     me.callParent(arguments);
     
     me.recaptcha = me.el.createChild({
   tag : "div",
   'id' : me.recaptchaId
  });
     
     Recaptcha.create(me.publickey, me.recaptcha.id, {
            theme: me.theme,
            lang: me.lang,
            callback: Recaptcha.focus_response_field
     });
     
     //me.recaptcha.setWidth(me.el.getWidth(true));
        
    },
    
    getChallenge: function(){
     return Recaptcha.get_challenge();
    }, 
    
    getResponse: function(){
     return Recaptcha.get_response();
    }

});

//create a ReCaptcha instance and we can use it as item in Form.
var recaptcha = Ext.create('yiyu.util.ReCaptcha',{
  name: 'recaptcha',
        recaptchaId: 'recaptcha',
        publickey: 'Your public Key from ReCaptcha',
        theme: 'white',
        lang: 'en'
 });