The Source for Java Technology Collaboration
User: Password:



Inderjeet Singh's Blog

May 2007 Archives


Pet peeve with the Java 1.5 enhanced for loop

Posted by inder on May 24, 2007 at 04:15 PM | Permalink | Comments (14)

I love the enhanced for loop construct introduced in Java 1.5. It makes the code look clean, and saves us from dealing with iterators or array indices. But since it is essentially syntactic sugar, it does create iterators behind the scenes as necessary. This can result in insidious bugs since if the passed collection or the array is null, your for loop will throw a NullPointerException. Here is an example:

import java.util.List;
public class ForLoopTest {
  public static void main(String[] args) {
   List<String> items = null;
   for (String item : items) {
     System.out.println("Item:" + item);
   }
 }
}

When this program is run, it will throw a null pointer exception.

I think it would have been better if the for loop did a null pointer check on the collection before trying to iterate over it. Granted, it will have a little bit of overhead, but I think the cost is justified because it is really common for people to not check for null values before invoking the for loop. I dont expect the semantics to get changed now because of backward compatibility concerns, so dont hold your breath expecting a change. But still, what do you think? Did the language designers make a mistake? Share your thoughts as comments to the blog.



Using Google Checkout SDK with Glassfish

Posted by inder on May 21, 2007 at 11:28 PM | Permalink | Comments (0)

Google Checkout Logo Google Checkout is a Google service that enables a faster, safer and more convenient way to shop online. Online merchants can implement Google checkout on their websites, and then they do not need to worry about managing credit-cards information and privacy of user accounts. Besides, Google provides free credit-card processing until the end of 2007 and will later provide credit for adwords expenses in free credit card processing. It is trivial to implement an HTML version of the Google checkout process. However, Google also provides a REST-based XML API that can be used to automate order processing. In this blog, I will discuss how to use the Java SDK for Google Checkout to easily implement the XML API integration with Google checkout in your Web applications. You can then deploy your application on GlassFish or Tomcat to handle REST events.

The Checkout SDK provides a library jar and an XML configuration file that you will need to embed in your Web application. You can then configure your action handlers that will receive notification of various order processing events from Google as a user places an order. Here are the steps that you will need to follow:

  1. Download and install Glassfish.
  2. Download checkout-sdk-0.5.zip or a later version from its download page.
  3. Unzip checkout-sdk-0.5.zip file. This will create a directory named checkout-sdk-0.5.
  4. The top-level index.html has instructions on using the SDK. You can also browse the Javadocs for the classes contained in the SDK at docs/javadocs/index.html
  5. Copy checkout-sdk/checkout-sdk.jar to your WEB-INF/lib
  6. Copy checkout-sdk/web/WEB-INF/checkout-config.xml to your WEB-INF/ directory
  7. Edit your WEB-INF/web.xml and cut-and-paste the contents from checkout-sdk/web/WEB-INF/checkout-fragment-for-web.xml
    1. If you need to add additional handlers to process checkout notifications, edit checkout-config.xml and change default value of the handlers to your classes.
    2. Use Checkout SDK Javadocs to extend notification handlers
  8. Copy over the contents of checkout-sdk/web/checkout to a suitable directory in your application. For example, web/checkout
  9. Your application is now ready to use Google Checkout APIs. Deploy it on glassfish to test the integration
  10. Login to your Google checkout merchant account, click on Settings tab, and then click on Integration on the left menu. register the URL for your notification service in the API callback URL textfield. For example, if glassfish was running your application at http://foo.bar.com:8080/myapp then, the registration URL will be http://foo.bar.com:8080/myapp/notification

That is it! Try it out today, and share your experiences as comments to this blog. We will be try our best to improve the checkout SDK to enable easy implementation by your Web applications. Thanks for reading.



JavaOne sessions on Java EE 5 puzzlers and Google Checkout

Posted by inder on May 02, 2007 at 09:14 AM | Permalink | Comments (0)

JavaOne Logo
JavaOne 2007 is right around the corner, and I am participating in two sessions there. The first one is based on my work at my previous employer, Sun Microsystems. This session will present tips and tricks on using Java EE 5, and is patterned after the popular Java Puzzler sessions by Josh Bloch.

My other session is related to using Google Checkout API in Java applications. In this session, we will describe how Java developers can use this API to enable payments for selling products on their websites. The integration can be as simple as an HTML FORM post, or a heavy duty one for integration with order processing systems using a REST based API. We will also demonstrate how the popular Web 2.0 Pet Store application can be modified to use Google Checkout.

When neither presenting or attending other sessions, I will be handing out goodies at the Google booth in the pavillion! So if you are coming to the conference and would like to meet up, drop by the booth or at either of the sessions. If there are any specific topics you would like us to cover in the sessions, you are welcome to share them as comments to this blog. Thanks for reading.



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds