Search This Blog

Wednesday, December 15, 2010

jQuery UI tabs with ajax problem

Recently I was working on one project where I needed to add some tabs with dynamic loading content. After googling a while I found that jQuery UI has already tabs widget offering out of the box functionality.

To use jQuery UI download javascript and css files from http://jqueryui.com .

Idea is that I will have plain html with some url-s that will be loaded inside tab area when user clicks on tab.

So I have something like this:

<div id="tabs">
  <ul>
    <li><a href="someItems.html">Items</a></li>
    <li><a href="someItems2.html">Items2</a></li>
  </ul>
</div>


jQuery will turn this to nice looking tabs widget. When user clicks on first tab, content will be loaded by ajax from someItems.html page.

The problem is that that we want links inside the tab content to display external content inside tabs. To do that we add following:

$("#tabs").tabs({
  load: function(event, ui) {
    $("#tabs").find("a").live('click', function() {
      $(ui.panel).load(this.href);
      return false;
    });
  }
});

What we have done here is that we declared on load event for tabs widget and assigned click event through live method for every a element inside #tabs container.

So, after we made this after trying you will notice that only first tab is showing all link content in tab area. If you switch to second tab none of the links work.

To fix this we add:

$("#tabs").tabs({
  load: function(event, ui) {
    $("#tabs").find("a").live('click', function() {
      $(ui.panel).load(this.href);
      return false;
    });
  },
  select: function(event, ui) {
    $("#tabs").find("a").die('click');
    $("#tabs").find("a").live('click', function() {
      $(ui.panel).load(this.href);
      return false;
    });
  }
});
We added select event function where we unregister all click events and than again register them with all a elements.

And now we have fully functional dynamic loading tabs.

Tuesday, September 21, 2010

JSF 2 Exception Handling

JSF 2 introduced ExceptionHandler as central point for handling unexpected Exceptions that are thrown during the Faces lifecycle.

To avoid silent exceptions that are not catched in application implement your ExceptionHandler and do what ever you want with exception.

1. Create your implementation of ExceptionHandler:
public class MyExceptionHandler extends ExceptionHandlerWrapper {

  private static Log log = LogFactory.getLog(MyExceptionHandler.class);
  private ExceptionHandler wrapped;

  public MyExceptionHandler(ExceptionHandler wrapped) {
    this.wrapped = wrapped;
  }

  @Override
  public ExceptionHandler getWrapped() {
    return wrapped;
  }

  @Override
  public void handle() throws FacesException {
    //Iterate over all unhandeled exceptions
    Iterator i = getUnhandledExceptionQueuedEvents().iterator();
    while (i.hasNext()) {
      ExceptionQueuedEvent event = i.next();
      ExceptionQueuedEventContext context =
        (ExceptionQueuedEventContext)event.getSource();

      //obtain throwable object
      Throwable t = context.getException();

      //here you do what ever you want with exception
      try{
      //log error
        log.error("Serious error happened!", t);
        //redirect to error view etc....  
      }finally{
        //after exception is handeled, remove it from queue
        i.remove();
      }
    }
    //let the parent handle the rest
    getWrapped().handle();
  }
}
2. Create your ExceptionHandlerFactory and call your ExceptionHandler:
public class MyExceptionHandlerFactory extends ExceptionHandlerFactory {

  private ExceptionHandlerFactory parent;

  // this injection handles jsf
  public MyExceptionHandlerFactory(ExceptionHandlerFactory parent) {
    this.parent = parent;
  }

  //create your own ExceptionHandler
  @Override
  public ExceptionHandler getExceptionHandler() {
    ExceptionHandler result =
        new MyExceptionHandler(parent.getExceptionHandler());
    return result;
  }
}
3. Register your exception handler factory in faces-config.xml:
<factory>
  <exception-handler-factory>
    test.MyExceptionHandlerFactory
  </exception-handler-factory>
</factory>

Wednesday, September 15, 2010

Java heap dump

I recently worked on a project where I was faced with "out of memory" problem. Exception appeared usually after the application has worked for several weeks. I solved the problem with VisualVM tool that ships with Java 1.6 JDK.
VisualVM tool provides a direct connection to any Virtual Machine locally or remote, or can load heap dump or thread dump from a file.

To dump heap memory to a file type in console(linux):
  1. Get java process id
    ps -eaf | grep java
  2. Dump heap memory to a file using jmap ("jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server")
    jmap -heap:format=b PID (where PID i id of your java process)
  3. Run jvisualvm
    jvisualvm -J-Xmx512M
  4. Load heap.bin file
  5. Find biggest objects by retained size. After file loads you can see every object that was in memory at the time the dump was taken.Click on find biggest objects by retained size and you will get hierarchy of biggest objects in memory.
In this way, I discovered that I have HashMap object in some other singleton object, and the values were added to map, but never removed.