Thursday, November 19, 2009

Excellent Code Display Tool for Technical Bloggers.

In my previous post, my code are displayed in ugly format. So that user will not read that post. I was really searching on net to find a Javascript which may help  me in displaying my code cleaner.
I came to know a SyntaxHighlighter which displayes your code exaclty what i was expecting. Thanks a lot for SyntaxHighlighter :).Below is the Step how you can integrate the  SyntaxHighlighter in your Blog/ Web Site.
For people who uses the Hosted Application like blogger.com, can use the online hosted javascript files from SyntaxHighlighter.
Step 01:
* Click Here to download the SyntaxHighlighter javascript code.
* For the people who uses blogspot and WordPress can wait :) (we can direcly use the code from their server)
STEP 02:
* Place the below code above <head> HTML tag of your HTML page.
* For  blogspot and WordPress users it will be  templete.xml file or the HTML column of the layout page.























Disable the language (here its computer language) which you may not use. For me i am java developer, i will not post code which are from VB, so  i am commented that. This disabling will give you performance in page load.
For the people who want to use online version should pre fix "http://alexgorbatchev.com/pub/sh/2.1.364/" in all your src attributes.
STEP 03:
Add your code inside the pre tag,and you have to define the class based on your code language.
public class AntHelloWorld {
 public static void main(String[] args) {
  System.out.println("This Project was build using Ant");
 }
}
As the above code is a java, so we are using class="brush: java", this decides which syntax coloring need to be used.

Wednesday, November 18, 2009

A Strange behavior about Java Resource Bundle


I am working in a project where I have to load two different property files (for example parent.properties & child.properties ) which are totally have different base name and it should also have inheritance relationship between this two bundle. So that if any keys are not found in child.properties it should get from parent.properties.
When I was analyzing the resource bundle code, I came to know there is method setParent(), this method is used to set which is your parent bundle (something like java super class). For example if we use bundle name called "child_en.properties" the my parent bundle should be "child.properties".
As this method was protected method in ResourceBundle Class, so to use this class I have sub classed my own ResourceBundle class and overridden the setParent Method like below.
public class P4JResourceBundle extends ResourceBundle {
protected void setParent(ResourceBundle parent) {
super.setParent(parent);
}
public void letsChangeParent(String parent){
this.setParent(ResourceBundle.getBundle(parent));
}
public Enumeration getKeys() {
return null;
}
protected Object handleGetObject(String key) {
return null;
}
So after changing the class I have executed with the below method, excepts that it should pick the key from parent.properties file if the key is not present in child.properties file.
public class ResourceBundleExample {
public static void main(String[] args) {
P4JResourceBundle myBundle = new P4JResourceBundle();
myBundle.letsChangeParent("com.passion4java.resourcebundle.parent");
ResourceBundle bundle = myBundle.getBundle("com.passion4java.resourcebundle.child");
System.out.println(bundle.getString("i_am_not_in_child_file"));
}
}
As usual J there is an error thrown saying the key was missing. 
Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key i_am_not_in_child_file
at java.util.ResourceBundle.getObject(Unknown Source)
at java.util.ResourceBundle.getString(Unknown Source)
at com.passion4java.resourcebundle.ResourceBundleExample.main(ResourceBundleExample.java:11)
So what's wrong?

I tried to debug the code in the resource bundle class and below code surprised me.
public static final ResourceBundle   getBundle(String   baseName,Locale   locale){
       return getBundleImpl(baseName, locale, getLoader());
}
private static ResourceBundle   getBundleImpl(String   baseName, Locale   locale,
                                            ClassLoader   loader){
         if (baseName == null) {
             throw new NullPointerException  ();
         }

 String   bundleName = baseName;
         String   localeSuffix = locale.toString();
         if (localeSuffix.length() > 0) {
             bundleName += "_" + localeSuffix;
         } else if (locale.getVariant().length() > 0) {
 //new Locale("", "", "VARIANT").toString == ""
 bundleName += "___" + locale.getVariant();
         }
 Locale   defaultLocale = Locale.getDefault();
          Object   lookup = findBundleInCache(loader, bundleName, defaultLocale);
         if (lookup == NOT_FOUND) {
             throwMissingResourceException(baseName, locale);
         } else if (lookup != null) {
             return (ResourceBundle  )lookup;
         }
Object   parent = NOT_FOUND; // **********THIS_LINE_OF_CODE**********
try {
             //locate the root bundle and work toward the desired child
 Object   root = findBundle(loader, baseName, defaultLocale, baseName, null);
             if (root == null) {
                 putBundleInCache(loader, baseName, defaultLocale, NOT_FOUND);
                 root = NOT_FOUND;
             }
…cont
As I was setting the value in setParent() of resource bundle to change the parent resource bundle, but when the bundle are created using ResourceBundle.getInstance(), internally it calls getBundleImpl() which set the parent as "NOT_FOUND" direcly.
I was really confused why the code was setting directly rather than using values from the setter method????
As user of this class this implementation really stops customization which users expects.
Please feel free to comment.

Monday, November 16, 2009

Right usage of Annotations in Java.

When I was reading the annotation in java, I can't really feel where it can be used. Most of them (like me J) think that Annotations are mostly used my IDE like Eclipse and other tools.

In one of my old projects they are using annotations for decision making purposes like permissions, I was not so convinced that is this is right use?.

But today I was learning Xdoclet (a code generation tool),where they use lots of javadocs kind of stuff to give the meta information's to Xdoclet tools. There we are J

*    @version 0.5

*    @web.servlet name="SimpleServlet"

* display-name="Simple Servlet"

* load-on-startup=" l"

*    @web-servlet-init-param name="table" value="production"

*    @web-servlet-init-param name="account" value="accounts"

In above code we are using lots of javadocs comments which are given inputs to Xdoclets, if they are using javadocs like comments it may not force you compile level syntax check.

If we are using Annotations, then we can achieve the compile check. In EJB3 they are using the Annotations for code generations for home interface and other classes.

As of I feel that the only usages of Annotations was it should be used when we do a code generations. I may not sure using Annotations for permission check for API classes is the right decisions?

Please feel free to add comments to this topic. Thank you.

Thursday, November 12, 2009

Add Sexy Bookmarks to Blogspot


I was searching for adding the Sexy Bookmarks to configure in blogspot.com, after long search I was finally configured using below URL. It has two versions of Sexy Bookmarks... 








SexyBookmarks v2 For Blogger






Add Sexy Bookmarks to Blogger




Configure which you like J

Wednesday, November 11, 2009

Something Fishy about JSTL Tags

I was using "Apache Standard Taglib Standard 1.1" for setting different resource bundle based on the user. I also added default resource bundle name in web.xml as below.

<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>com.passion4java.Localization</param-value>
</context-param>
I was using setBundle tag in my code so that it will set different bundle based on the user.

<fmt:setBundle basename="<%=partnerLevelBrandName%>"
scope="session"/>

I tried to set the scope in the setBundle tag, but in "Apache Standard Taglib Standard 1.1" it was throwing an exception saying that var argument need to be used when you are using the scope variable …

Error: passiontemplate1.jsp:31:1: The page failed validation from validator: "Illegal scope attribute without var in "fmt:setBundle" tag.".
Then I was running the code without scope variable, but the setBundle was not overriding the default build name (which is specified in the web.xml)

<fmt:setBundle basename="<%=passionLevelBrandName%>" />

When I was debugging the code of the JSTL Message Tag (org.apache.taglibs.standard.tag.rt.fmt.MessageTag), they are getting the bundle name from the pageContext .Below is the code which searches the bundle name.



Code Snipped from javax.servlet.jsp.jstl.core.Config



public static Object find(PageContext pc, String name){

    Object ret = get(pc, name, PageContext.PAGE_SCOPE);

    if (ret == null){

     ret = get(pc, name, PageContext.REQUEST_SCOPE);

     if (ret == null){

        if (pc.getSession() != null){

         // check session only if a session is present

         ret = get(pc, name, PageContext.SESSION_SCOPE);

        }

        if (ret == null){

         ret = get(pc, name, PageContext.APPLICATION_SCOPE);

         if (ret == null){

            ret = pc.getServletContext().getInitParameter(name);

         }

        }

     }

    } 
    return ret;
}
I have two questions which are running in my mind.
  • Why fmt:setBundle tag was not allowing to set the scope variable only?
  • As we defined the default resource bundle name in web.xml (which is application level), why the code was setting the bundle name in the pageContext?(it should set in the application scope).
  • And finally why they are started to search from pageContext à Application (I think it has to be reversed)?
Please let me know if you have any thoughts on this?

Friday, November 6, 2009

Google created Easy tools for developing JavaScript code.

Google has came up with 3 Tools which will really bring down you most of the JavaScript development efforts. They collectively called as "Closure Tools".

Closure Compiler:

    This tool will check your code and increases the performance of the code by removing unnecessary lines. It will also alert you with some common error pitfalls in java scripts.

    Google has created application (http://closure-compiler.appspot.com/home)     from which you can check you code. Just copy your code and past it and click the compile button.


Closure Library:
    It's a JavaScript Library from which you get most of the cross browser JavaScript API code. There are lots of JavaScript API in the internet,but most of them are not supported by more than 2 browsers and gives lot surprises (bugs) during integrations, but here it was supported by all the browsers and it form Google J
Closure Templates:
    This is similar to a template engine, which will have reusable HTML and UI elements. For example you can have a check box which customized with Closure Templates and it can been used in your entire page.


For more info Check out Introducing Closure Tools.


I am also planning to come up with simple example on this topic in future…





Thursday, November 5, 2009

How to Post your Blog using RSS Feed?

Here are the steps to Post your Blog using RSS Feed.



  • go blogspot.com and configure the email address by giving a "secretWords" and select the "Publish emails immediately" option and the save the settings.
    • Go the blogspot.com -> Login -> Settings -> Email & Mobile -> Email Posting Address




  • After adding the "secretWords" your email id will look like java2usuf.secretWords@blogspot.com. The reason for ""secretWords"" is that it only known by you.
  • Once you have your email id and settings has been enabled login to www.feedmyinbox.com
    where you can configure your email and the RSS feeds link.
  • So that if RSS feeds gets updated the tool will start sending email to the given address.




Wednesday, November 4, 2009

Casting an object if the object is NULL?

If you are casting an object which is null, then java will not throw any exceptions. What does is, it will simply assign the null to the casted reference type variable.

Object obj = null;

String a = (String) obj;

In above example "a" will be assigned as NULL.

ava.util.ArrayList al = session.get("xyzObject");

Object o = (Object) al;

String a = (String) o;

System.out.println(a);

If session.get("xyzObject") != NULL:

    If we are getting an arraylist object from session then the above code will throw Class Cast Exception.

If session.get("xyzObject") == NULL:

    If we are getting a null value from session then the above code will not throw any exceptions. The reasons because null can be cast to any object references.

What's a big thing in it:

    The main thing is if we are sending null to an object reference most of time, and some time if we are start sending a value in it, it may break the code...

Hope this post may give some thoughts. Please leave your comments if any.

 

This content comes from a hidden element on this page.

The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
Click me, it will be preserved!

If you try to open a new ColorBox while it is already open, it will update itself with the new content.

Updating Content Example:
Click here to load new content