Thursday, December 25, 2008

Guicing JSF with Guice

Recently I read some of presentation of the WebBeans. This encourage me to learn JSF. When learning the JSF there are Managed Bean and I think I need some injection, Guice cross on my mind, so I search on the internet about integrating Guice and  JSF. Some of article and one project found, but none of fits on what I need and I want.

So I'm trying to learn how this JSF works so I can use Guice on it.

The case of login on a web page. User need to login to a web page using username and password. Below is the login bean

public class LoginBean {
    private String username;
    private String password;
    
    public String login() {
        return "success";
    }
    
    public void reset(ActionEvent event) {
        username = null;
        password = null;
    }
}

we need some logic when login button execute. So I add some logic.

public class LoginBean {
    private String username;
    private String password;
    
    @Inject
    private UserDao userDao;
    
    public String login() {
        User user = userDao.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            // we found the user on our d   atabase
            return "success";
        } else {
            // there is no user with specified username and password
            return "failed";
        }
    }
    
    public void reset(ActionEvent event) {
        username = null;
        password = null;
    }
}

The @Inject marked will be injected by the Guice injector. So we are going to create the Guice injection works. Mojarra, the JSF Resource Implementation, we can find com.sun.faces.spi.InjectionProvider, here are the injection will be held.

public class InjectionProvider {
    void inject(Object managedBean) throws InjectionProviderException;
    void invokePostConstruct(Object managedBean) throws InjectionProviderException;
    void invokePreDestroy(Object managedBean) throws InjectionProviderException;
}

We are iterested only on "inject" method, this are executed everytime the Managed Beans are about to use.

public class GuiceInjectionProvider implements InjectionProvider {
    private final Injector injector;
    
    public GuiceInjectionProvider() {
        // create injector
        injector = Guice.createInjector(new InMemoryModule());
    }
    
    public void inject(Object managedBean) throws InjectionProviderException {
        // inject managed beans
        injector.injectMembers(managedBean);
    }
    
    public void invokePostConstruct(Object managedBean)
            throws InjectionProviderException {
    }
    
    public void invokePreDestroy(Object managedBean) throws InjectionProviderException {
    }
}

Above are simple implementation for the injection, actually for now on we need the simple one. The creation of injection is on the constructor.

We already have the java class, now is the configuration part. Add context-param on web.xml




<context-param>
lt;param-name>com.sun.faces.injectionProvider</param-name>
<param-value>uudashr.guicejsf.guice.GuiceInjectionProvider</param-value>
</context-param>



It tells the mojarra to use custom injection provider.

Thursday, September 4, 2008

V8 Benchmark

Google Chrome is rock.
One things that make it a rock star is the JavaScript engine, "V8". Then I try to do a benchmark by opening http://code.google.com/apis/v8/run.html on different browser.

Here are the result:

Google Chrome 0.2.149.27
Score: 1362
DeltaBlue: 1543
Crypto: 1201
RayTrace: 1022
EarleyBoyer: 1820

Safari 3.1.2
Score: 165
DeltaBlue: 115
Crypto: 113
RayTrace: 194
EarleyBoyer: 296

Firefox 3.0.1
Score: 174
DeltaBlue: 204
Crypto: 110
RayTrace: 173
EarleyBoyer: 237

Opera 9.52
Score: 247
DeltaBlue: 160
Crypto: 110
RayTrace: 315
EarleyBoyer: 666

Well, where is the IE7?
When I open the URL on IE7, it shows alert window when the progress reach 33%, but I finally got the result.


Internet Exprorer 7
Score: 41
DeltaBlue: 18
Crypto: 36
RayTrace: 57
EarleyBoyer: 78

At first, the score is only 18, and I try it again, hoping a better result. It only can reach 40 to 41.

Well, IE7 has the slowest JavaScript engine. The browser war is started.

Wednesday, August 6, 2008

jEdit: Basic Setting of My Favourite Text Editor

Not much people knows jEdit is a great tools, most of I know it can compete with existing commercial and free Text Editor (or even Programmer's Editor), and the great thing it is multiplatform and open source, many people give contribution on jEdit.

I'll introduce best practice of using jEdit, it based on my needs as software developer.

You can download jEdit on www.jedit.org. Get the jEdit distribution and install it.
Start the jEdit, and you will see splash screen like this



Some platform such windows has jEdit server, it runs on the background, so there is no need to do initialize things again and when you need to open jEdit, it will start quicker.

This are the main screen



Since version version 4.3 pre 14 it has new splash screen and little fancy icons.
Straight to the point, what do programmers usually need is?
  1. Line numbers
  2. Indentations
  3. Shortcut to do things
Line numbers
To show line numbers, simply by click on menu bar: "Utilities -> Global Options..."
Alternative of this you can click "Global Options..." on the toolbar.
Select "gutter" on the left tree and check the "Line numbering" on the right view.

Indentations
Indentation has been set by default, but it use 8 as tab and indent width. I'm not conform with this options and usually use 4 as tab and indent width . On the same "Global Options..." dialog, select "Editing" on the left tree. Fill all the desirable value.

Shortcut
Programmers need to do things fast. So this are list of some usefull shortcut
  • Open... (ctrl + o)
  • New (ctrl + n), it will create new editor of new buffer. Buffer is just like tab.
  • Show Buffer Switcher (alt + back_quote), it just open the combo box, you can do this by clicking on the Buffer Switcher.
  • Go to Next Buffer (ctrl + page_down)
  • Go to Previous Buffer (ctrl + page_up)
  • Find... (ctrl + f)
  • Find next (ctrl + g)
  • Incremental search (ctrl + comma)
  • Hypersearch (ctrl + period), this will show list of all occurrence word on different view
  • Incremental search for selected word (alt + comma)
  • Hypersearch for selected word (alt + period), this will show list of all occurrence word on different view
Well, start using the free and powerful jEdit. It has many other functionality and you can make it more powerful by using plugins.

Thursday, May 29, 2008

ConcurrentHashMap

Why use the ConcurrentHashMap? The traditional synchronized HashMap or Hashtable, use a single lock to read and write. When you try to write new entry while other try to read the different entry, the locking happened. Shouldn't be like this. The simple implementation is using a N lock for N entries. The modification for entry X will be use the Y lock, where Y is lock for X. On ConcurrentHashMap, they use segmentation for the lock, they have 32 lock (or depends on the configuration). So a different write or read can be done concurrently without locking if they have a different lock/segment.