Developing Great Software

Thursday, May 21, 2009

Wicket + EJB + DI With Netbeans - Part 3

Part 3 - Using The @EJB Annotation To Provide Dependency Injection Support In The Web Tier And Consuming The Services Provided By Their Backing EJBs For this last part of the tutorial I am going to demonstrate how to use Wicket in an EE container environment such as Glassfish. wicket-contrib-javaee is a wicket module that provides integration through Java EE 5 resource injection. With wicket-contrib-javaee you can use the following 3 annotations in your wicket pages:
  • @EJB
  • @PersistenceUnit
  • @Resource
In particular, I will demonstrate how to use the @EJB annotation for dependency injection of the CustomerFacade ejb that was developed in Part 2 of this tutorial. Adding wicket-contrib-javaee-1.1.jar Begin by downloading the wicket-contrib-javaee-1.1.jar which can be found here on SOURCEFORGE.NET. Once you have downloaded the jar and saved it locally it then needs to be added to the WicketEETutorial1-war module:
  1. Right click the Libraries node and select Add Jar/Folder. This will open the Add Jar/Folder window.
  2. Navigate to the location where you saved the wicket-contrib-javaee-1.1.jar file and select it. Make sure that the Copy To Libraries Folder option is selected.
  3. Click Open and Netbeans will add the jar file to the module.
Now we need to add the wicket-ioc jar to the module. wicket-ioc.jar is included with the wicket distribution which we downloaded in Part 1 of this tutorial. Adding wicket-ioc jar
  1. Right click the Libraries node and select Add Jar/Folder. This will open the Add Jar/Folder window.
  2. Navigate to the location where you saved the wicket distribution that we downloaded in Part 1 of this tutorial. In the lib folder select the wicket-ioc jar file. Make sure that the Copy To Libraries Folder option is selected.
  3. Click Open and Netbeans will add the jar file to the module.
The wicket-ioc jar has a dependency on cglib jar so we need to include that in the module as well. Adding cglib jar The cglib jar is hosted on SourceForge and you can download it from here. Once you have downloaded the jar and saved it locally it then needs to be added to the WicketEETutorial1-war module:
  1. Right click the Libraries node and select Add Jar/Folder. This will open the Add Jar/Folder window.
  2. Navigate to the location where you saved the cglib jar file and select it. Make sure that the Copy To Libraries Folder option is selected.
  3. Click Open and Netbeans will add the jar file to the module.
The web.xml file needs to be modified to include a reference to the CustomerFacade ejb. Adding A Reference In web.xml To CustomerFacade EJB In the Projects pane expand the Configuration Files node and double click the web.xml file to open it in the editor and click on the References tab.
  1. Expand the EJB References panel and click the Add button. This will open the Add EJB Reference window.
  2. Enter WicketEETutorial1-ejb/CustomerFacade for the EJB Reference Name.
  3. Select Session for the EJB Type.
  4. Select Local for Interface Type.
  5. Enter ejbs.CustomerFacadeLocal for the Local Interface.
  6. Click the OK button to save the changes to the web.xml file.
Now we need to override the init method in Application.java. Modifying Application.java Open the Application.java file and add the following code: @Override protected void init() { super.init(); addComponentInstantiationListener((IComponentInstantiationListener) new JavaEEComponentInjector(this)); } Right click in the editor and select Fix Imports to add the needed import statements. Application.java file should now look like the following:Now we need to modify HomePage.java where we wiill include the @EJB annotation for the CustomerFacade ejb and consume its services. Adding The @EJB Annotation Open HomePage.java in the editor and add the following to the HomePage class definition: @EJB(name="WicketEETutorial1-ejb/CustomerFacade") private CustomerFacadeLocal customerEjb; *** Note that the name we are providing for the @EJB annotation is the same one we used when we added the EJB reference to the web.xml file. Now everything is in place for us to actually consume the data access services that are provided by the CustomerFacade ejb so lets add some more code to the HomePage constructor to get a list of Customer entities and get a count of how many entities there are in the list. Consuming The Data Access Services Provided By The CustomerFacade ejb Modify the HomePage constructor so that it looks exactly like the following: public HomePage() { add(new Label("sayhello","Hello, World!")); List customers = customerEjb.findAll(); int count = customers.size(); add(new Label("customercount",String.valueOf(count))); } Right click the editor and select Fix Imports to add the needed import statements. Your HomePage.java file should now look like the following; Open HomePage.html in the editor and modify its markup so that it looks exactly like the following; We have added a Wicket component that will display the number of Customer entities in the list of Customers returned from calling the findAll method of the CustomerFacade ejb. We are now ready to build and run our application. Running Our Application Rick click the WicketEETutorial1 node in the Projects pane and select Run to build and run the application. Your browser should open and render the following; As you can see from the above image, we were able to obtain a reference to the CustomerFacade ejb via the use of the @EJB annotation and call the findAll method which the CustomerFacade ejb exposes. findAll returned a list of Customer entities and from the list we were able to obtain a count of Customer entities. Summary Netbeans greatly simplifies the whole process of generating the Enterprise Application's modules. Netbeans wizards generate a lot of code that we would otherwise have to hand code ourselves. To a very large degree Netbeans trivializes Java EE5 applications allowing us to focus on the domain issues of the application and not its plumbing and wiring. Who said Java EE was too heavy and complex? I hope this tutorial demonstrated that neither is true. With the addition of the wicket-contrib-javaee jar file, Wicket applications gain the ability to use Java EE5 annotations that allow it to take full advantage of services esxposed in enterprise java beans when running in a Java EE5 container such as the one Glassfish provides. All in all, very cool, isn't it? And easy, too!And how cool is it to be able to use Wicket as the web framework in a Java EE application and have it be able to consume services exposed through ejbs using standard Java EE annotations? Though this application was trivial by design so as to keep it simple for demonstration purposes, the principles demonstrated here will work for applications of much greater size and complexity. Conclusion This concludes the tutorial and I hope you had as much fun following along as I had presenting it. Please feel free to provide your comments. I look forward to hearing from you.

Monday, May 18, 2009

Wicket + EJB + DI With Netbeans - Part 2

Part 2 - Creating Entity Classes That Map To An Existing Database And Exposing Data Access Services Through EJBs With Netbeans For this part of the tutorial I am going to use the Sample database that comes with the Java DB which should have been installed when you installed Netbeans and Glassfish. You can verify that the Sample database is recognized by Netbeans by expanding the Database node in the Services pane where you should see a connection listed there that looks like the highlighted one below. If you fully expand the connection node you can see that there are a number of tables already defined. For this tutorial we want to create one entity class for the Customer table. However, we will not have to hand code this entity class because Netbeans has excellent tooling support for generating entity classes that map to tables in a database. Creating Our Customer Entity Class Please follow these steps to create the Customer entity class for the Customer table in the Sample database:
  1. In the Projects pane right click the WicketEETutorial1-ejb module node and select New|Other from the context menu. This will open the New File window. In the Categories pane select Persistence and in the File Types pane select Entity Classes From Database and click Next.
  2. Expand the Data Source drop-down menu in the Data Tables Pane and select the jdbc/sample option. Netbeans will fill the Available Tables list with all the tables defined in the Sample DB.
  3. Uncheck the Include Related Tables option.
  4. Select the CUSTOMER table from the list of Available Tables.
  5. Click the Add button and then click Next.
  6. Enter entities as the package name in the Package field and the click the Create Persistence Unit button. In the Create Persistence Unit window accept all the defaults and click the Create button. Now click the Next button.
  7. In the Mapping Options pane select java.util.List for the Collection Type and click the Finish button.
Upon completing the above steps, Netbeans will create the Customer entity class and place it in the entities package. Netbeans will also generate the persistence.xml file and place it under the Configuration Files tab. With our entity class and persistence unit file created we can now go on and encapsulate our database access code in EJBs. Like our entity class we will not have to hand code our database access code. Netbeans has excellent tooling support for generating database access code that map to existing entity classes. Creating Our Customer Data Access EJB Please follow these steps to create the EJB that will encapsulate the database access code:
  1. In the Projects pane right click the WicketEETutorial1-ejb module node and select New|Other from the context menu. This will open the New File window. In the Categories pane select Persistence and in the File Types pane select Session Beans For Entity Classes and click Next. As you can see, Netbeans has filled the Available Entity Classes list with the Customer entity class we created.
  2. Select the Customer entity in the list of Available Entity Classes.
  3. Click the Add buttom and then click the Next button.
  4. In the Generated Session Beans pane enter ejbs for the package name. We want to create a local interface so make sure the Local checkbox is checked.
  5. Now click the Finish button.
Upon completing the above steps, Netbeans will create the local interface and implementation class files for the Customer entity that we selected and place these files in the ejbs package. Summary This concludes Part 2 of the tutorial. As you can see, Netbeans greatly simplifies coding the service tear in an Enterprise Application. We have taken advantage of the tooling available in Netbeans to generate our Customer entity class, persistence.xml and CustomerFacade EJB class files for us. In the next part of this tutorial, Part 3, we will expand the web tier to consume the services exposed by the CustomerFacade EJB that we have just created.

Wednesday, May 6, 2009

Wicket + EJB + DI With Netbeans - Part 1

Introduction This 3 part tutorial demonstrates how easy it is to create a Java EE5 Enterprise Application with Netbeans. The Web tier of the Enterprise Application will be based on the Wicket Web framework and the services tier -- the EJBs (Enterprise Java Beans) -- will serve up data from a database which will be consumed by the Web tier. In particular, it demonstrates how easy it is for the Web tier to consume the services exposed by EJBs and to manage references to the EJBs through DI (dependency injection) using the @EJB annotation and the wicket-contrib-javaee jar library. The tutorial has 3 parts covered in 3 separate articles:
  1. Part 1 will demonstrate creating the Enterprise Application in Netbeans and configuring the Web tier to use the latest build of the Wicket framework.
  2. Part 2 will demonstrate creating entity classes that map to an existing database and exposing data access services through EJBs.
  3. Part 3 will demonstrate using the @EJB annotation to provide dependency injection support in the Web tier and consuming the services provided by their backing EJBs.
At the conclusion of this 3 part tutorial we will have created a fully functional Wicket based Web application running in the Java EE5 container supplied by the Glassfish application server. The Web application will use standard Java EE5 based annotations such as @EJB to resolve references to EJBs at run time through the use of dependency injection. It is also my hope that this tutorial will serve to dispel the belief by many that the Java EE5 container is too heavy and complex to develop for. Part 1 - Creating A Java EE5 Enterprise Application With Netbeans Creating The Application Netbeans greatly simplifies the creation of Java EE5 based applications as the following demonstrates. Please follow these steps:
  1. Open Netbeans.
  2. Select File|New Project from the main menu. This will open the New Project window
  3. In the Categories pane select Java EE and in the Projects pane select Enterprise Application and click Next.
  4. Enter WicketEETutorial1 or any other name you chose for the Project Name as pictured below and click Next.
  5. From the Server drop-down selection list select Glassfish v2 or better. Accept the other options' default settings as pictured below and click Finish.
As a result of completing the above, Netbeans will generate and open 3 module projects in the Projects pane as pictured below.The 3 modules and their associated projects that were generated for us are summarized below:
  1. WicketEETutorial1 is an Enterprise Application module project and its ear (enterprise archive) file is what will be deployed to the server.
  2. WicketEETutorial1-ejb is an EJB module project and it is here where we will define our entity classes and encapsulate our JPA data access code in EJBs (Enterprise Java Beans). This module will serve as our service layer tier and provide both domain and business functionality to our Web application. Its jar file is included in the ear file mentioned above.
  3. WicketEETutorial1-war is a Web module project and it is here where we will define our presentation layer tier. Its war (web archive) file is included in the ear file mentioned above. As a default, Netbeans configures the web tier to serve up JSP pages. In the next step we will change this so that the web tier will use the Wicket web framework instead.
Configuring the Web tier to use Apache Wicket For this tutorial I am using the latest release candidate of the Wicket web framework which at the time of writing this was version 1.4-rc2 and which I downloaded from the Apache Wicket web site. A basic Wicket web application only needs the wicket-1.4-rc2.jar file so I added it to the module. Wicket has a dependency on the slf4j which is a logging API. I was able to download the SLF4J distribution from the SLF4J web site. This distribution also includes the jar file for log4j which is a popular logging implementation. I added 3 jar files from this distribution to the module. Now the web tier's Libraries node contains the following jars: To keep this part of the tutorial as simple as can be, the web application will for now only display the gratuitous Hello, World! In the web tier module I created the following source code package and 3 files: The contents of each of the above files is shown below: The web.xml file which is located under the Configuration Files tab in the Projects pane needs to be modified as well and should look exactly like the following highlighted text: As can be seen from the Home.html and Home.java files, the span element with the wicket:id of sayhello will have its contents replaced with Hello, World! Running The Application When we build and run the application the following is rendered in the browser:Summary This concludes Part 1 of the tutorial. As you can see, we have successfully created, built and run a Java EE5 Enterprise Application with a web tier using the Wicket web framework. The application as it now stands is trivial as it only displays the ubiquitous Hello, World! In the next part of this tutorial, Part 2, we will expand on the service side (the EJBs) and in Part 3, we will expand the web tier to consume the services exposed by the EJBs.

About Me

My photo
New York, NY, United States
Software Developer