Miles to go …

November 22, 2010

Screencast #35: JDK 7 Project Coin Features in NetBeans IDE 7.0 Beta

Filed under: netbeans — arungupta @ 12:45 pm

NetBeans IDE 7.0 Beta was released last week – download here. JDK 7 build 118 was released last week as well – download here. The New and Noteworthy page of NetBeans 7.0 highlights the support for some of the Project Coin features. This screencast highlights how to get started with JDK 7 in NetBeans IDE and use some of the Project Coin features, specifically:

  • Strings in switch
  • Binary integer literals and underscores in numeric literals
  • Multi-catch
  • Improved type inference for generic instance creation (diamond operator)
  • Automatic resource management

Enjoy!

Share feedback about Project Coin at coin-dev and about NetBeans at NetBeans Forums.

Technorati: screencast netbeans jdk7 project coin

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

November 19, 2010

Java EE 6 & GlassFish @ JavaOne Brazil & CEJUG

Filed under: glassfish, netbeans — arungupta @ 8:53 pm

The first ever JavaOne outside the USA is going to be in Brazeeeeel from Dec 7-9th … yeeeee haw! Java EE 6 & GlassFish will have a strong presence there in terms of technical sessions, general session, exhibitor floor, hands-on labs, and of course you’ll find us all around the hallways.

Here is a quick summary of different Java EE 6 related sessions …

When ? Where ? What ? Speaker ?
Tuesday
11:45 – 12:30pm
Salon 2 S319438
HK2: Oracle WebLogic Serve, Oracle GlassFish Server, and Beyond
Jerome Dochez

Wednesday
10:15 – 11am

Salon 1 S313557
The Java EE 6 Programming Model Explained: How to Write Better Applications
Jerome Dochez
Wednesday
10:15 – 11am
Salon 2 S313431
Java Persistence API 2.0: An Overview
Ludovic Champenois
Wednesday
11:15 – 12pm
Salon 1 S314168
What’s New in Enterprise JavaBean Technology
Arun Gupta
Wednesday
1:30 – 2:15pm
Salon 3 S313265
Advanced Java API for RESTful Web Services (JAX-RS)
Chuk-Munn Lee
Wednesday
2:30 – 3:15pm
Salon 3 S320002
Creating RESTful Web Services using JAX-RS
Ludovic Champenois
Thursday
10:10:45
Salon 1 S313189
Complete Tools Coverage for the Java EE 6 Platform
Ludovic Champenois
Thursday
11:15 – 12:00pm
Salon 1 S320004
Hyperproductive JavaServer Faces 2.0
Arun Gupta
Thursday
2:00 – 2:45pm
Salon 1 S313937
Using Contexts and Dependency Injection (CDI) in the Java EE Ecosystem
Jerome Dochez
Thursday
4:15 – 5:00
Salon 1 S320003
Servlet 3.0 Extensible, Asynchronous and Easy to Use
Arun Gupta
Thursday
4:15 – 6:00pm
Salon 4 S314285
Using Oauth with RESTful Web Services (Hands-on Lab)
Chuk-Munn Lee
Thursday
4:15 – 6:00pm
Salon 6 S313277
Beginning with the Java EE 6 Platform (Hands-on Lab)
Ludovic Champenois
& Gang

And prior to JavaOne, I’ll also be speaking at CEJUG on Java EE 6.

Looking forward to enjoy Caipirinha, Churrascaria, Guarana, Brazilian coffee, beautiful beaches, and my first summer time visit to Brazil :-) Where will I see you ?

For the first time travelers to Brazil, here are some travel tips and 4 things not to miss in Brazil.

Technorati: conf javaone brazil javaee6 glassfish brazil cejug

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

November 18, 2010

TOTD #150: Collection of GlassFish, NetBeans, JPA, JSF, JAX-WS, EJB, Jersey, MySQL, Rails, Eclipse, and OSGi tips

This is the 150th tip published on this blog so decided to make it a collection of all the previous ones. Here is a tag cloud (created from wordle.net/create) from title of all the tips:

As expected GlassFish is the most prominent topic. And then there are several entries on NetBeans, JRuby/Rails, several Java EE 6 technologies like JPA, JAX-WS, JAX-RS, EJB, and JSF, and more entries on Eclipse, OSGi and some other tecnhologies too. Here is a complete collection of all the tips published so far:

  • #149: How to clean IntelliJ cache, preferences, etc on Mac OS X ?
  • #148: JPA2 Metamodel Classes in NetBeans 7.0 – Writing type-safe Criteria API
  • #147: Java Server Faces 2.0 Composite Components using NetBeans – DRY your code
  • #146: Understanding the EJB 3.1 Timer service in Java EE 6 – Programmatic, Deployment Descriptor, @Schedule
  • #145: CDI Events – a light-weight producer/consumer in Java EE 6
  • #144: CDI @Produces for container-managed @Resource
  • #143: Retrieve Twitter user timeline using using Jersey and OAuth
  • #142: GlassFish 3.1 – SSH Provisioning and Start/Stop instance/cluster on local/remote machines
  • #141: Running GlassFish 3.1 on Ubuntu 10.04 AMI on Amazon EC2
  • #140: Moving GlassFish Installation – Referenced file does not exist "osgi-main.jar"
  • #139: Asynchronous Request Processing using Servlets 3.0 and Java EE 6
  • #138: GlassFish 3.1 Milestone 1 – Clustering and Application Versioning Demos
  • #137: Asynchronous EJB, a light-weight JMS solution – Feature-rich Java EE 6
  • #136: Default Error Page using Servlets 3.0 – Improved productivity using Java EE 6
  • #135: JSF2 Composite Components using NetBeans IDE – lightweight Java EE 6
  • #134: Interceptors 1.1 in Java EE 6 – What and How ?
  • #133: JPA2 (JPQL & Criteria), JavaDB, and embedded GlassFish – perfect recipe for testing
  • #132: Servlets 3.0 in Embedded GlassFish Reloaded – lightweight Java EE 6
  • #131: Dynamic OSGi services in GlassFish – Using ServiceTracker
  • #130: Invoking a OSGi service from a JAX-WS Endpoint – OSGi and Enterprise Java
  • #129: Managed Beans 1.0 in Java EE 6 – What and How ?
  • #128: EJBContainer.createEJBContainer: Embedded EJB using GlassFish v3
  • #127: Embedding GlassFish in an existing OSGi runtime – Eclipse Equinox
  • #126: Creating an OSGi bundles using Eclipse and deploying in GlassFish
  • #125: Creating an OSGi bundles using NetBeans and deploying in GlassFish
  • #124: OSGi Declarative Services in GlassFish – Accessed from a Java EE client
  • #124: Using CDI + JPA with JAX-RS and JAX-WS
  • #123: f:ajax, Bean Validation for JSF, CDI for JSF and JPA 2.0 Criteria API – all in one Java EE 6 sample application
  • #122: Creating a JPA Persistence Unit using NetBeans 6.8
  • #121: JDBC resource for MySQL and Oracle sample database in GlassFish v3
  • #120: Deployment Descriptor-free Java EE 6 application using JSF 2.0 + EJB 3.1 + Servlets 3.0
  • #119: Telnet to GlassFish v3 with NetBeans 6.8 – "Could not open connection to the host"
  • #118: Managing OSGi bundles in GlassFish v3 – asadmin, filesystem, telnet console, web browser, REST, osgish
  • #117: Invoke a JAX-WS Web service from a Rails app deployed in GlassFish
  • #116: GlassFish v3 Administration using JavaFX front-end – JNLP available
  • #115: GlassFish in Eclipse – Integrated Bundle, Install Stand-alone or Update Existing plugin
  • #114: How to enable Java Console in Mac OS X, Windows, … ?
  • #113: JavaFX front-end for GlassFish v3 Administration – Using REST interface
  • #112: Exposing Oracle database tables as RESTful entities using JAX-RS, GlassFish, and NetBeans
  • #111: Rails Scaffold for a pre-existing table using Oracle and GlassFish
  • #110: JRuby on Rails application using Oracle on GlassFish
  • #109: How to convert a JSF managed bean to JSR 299 bean (Web Beans) ?
  • #108: Java EE 6 web application (JSF 2.0 + JPA 2.0 + EJB 3.1) using Oracle, NetBeans, and GlassFish
  • #107: Connect to Oracle database using NetBeans
  • #106: How to install Oracle Database 10g on Mac OS X (Intel) ?
  • TOTD #105: GlassFish v3 Monitoring – How to monitor a Rails app using asadmin, JavaScript, jConsole, REST ?
  • #104: Popular Ruby-on-Rails applications on GlassFish v3 – Redmine, Typo, Substruct
  • #103: GlassFish v3 with different OSGi runtimes – Felix, Equinox, and Knoplerfish
  • #102: Java EE 6 (Servlet 3.0 and EJB 3.1) wizards in Eclipse
  • #101: Applying Servlet 3.0/Java EE 6 “web-fragment.xml” to Lift – Deploy on GlassFish v3
  • #100: Getting Started with Scala Lift on GlassFish v3
  • #99: Creating a Java EE 6 application using MySQL, JPA 2.0 and Servlet 3.0 with GlassFish Tools Bundle for Eclipse
  • #98: Create a Metro JAX-WS Web service using GlassFish Tools Bundle for Eclipse
  • #97: GlassFish Plugin with Eclipse 3.5
  • #96: GlassFish v3 REST Interface to Monitoring and Management – JSON, XML, and HTML representations
  • #95: EJB 3.1 + Java Server Faces 2.0 + JPA 2.0 web application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
  • #94: A simple Java Server Faces 2.0 + JPA 2.0 application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
  • #93: Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3 – A simple Servlet 3.0 + JPA 2.0 app
  • #92: Session Failover for Rails applications running on GlassFish
  • #91: Applying Java EE 6 "web-fragment.xml" to Apache Wicket – Deploy on GlassFish v3
  • #90: Migrating from Wicket 1.3.x to 1.4 – "Couldn’t load DiskPageStore index from file" error
  • #89: How to add pagination to an Apache Wicket application
  • #88: How add pagination to Rails – will_paginate
  • #87: How to fix the error undefined method `new’ for "Rack::Lock":String caused by Warbler/JRuby-Rack ?
  • #86: Getting Started with Apache Wicket on GlassFish
  • #85: Getting Started with Django Applications on GlassFish v3
  • #84: Using Apache + mod_proxy_balancer to load balance Ruby-on-Rails running on GlassFish
  • #83: Eclipse Tools Bundle for GlassFish 1.0 – Now Available!
  • #82: Getting Started with Servlet 3.0 and EJB 3.1 in Java EE 6 using NetBeans 6.7
  • #81: How to use nginx to load balance a cluster of GlassFish Gem ?
  • #80: Sinatra CRUD application using Haml templates with JRuby and GlassFish Gem
  • #79: Getting Started with Sinatra applications on JRuby and GlassFish Gem
  • #78: GlassFish, EclipseLink, and MySQL efficient pagination using LIMIT
  • #77: Running Seam examples with GlassFish
  • #76: JRuby 1.2, Rails 2.3, GlassFish Gem 0.9.3, ActiveRecord JDBC Adapter 0.9.1 – can they work together ?
  • #75: Getting Started with Grails using GlassFish v3 Embedded
  • #74: JRuby and GlassFish Integration Test #5: JRuby 1.2.0 RC2 + Rails 2.x.x + GlassFish + Redmine
  • #73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler
  • #72: JRuby and GlassFish Integration Test #3: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v3
  • #71: JRuby and GlassFish Integration Test #2: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish v3 Prelude
  • #70: JRuby and GlassFish Integration Test# 1: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish Gem
  • #69: GlassFish High Availability/Clustering using Sun Web Server + Load Balancer Plugin on Windows Vista
  • #68: Installing Zones in Open Solaris 2008/11 on Virtual Box
  • #67: How to front-end a GlassFish Cluster with Apache + mod_jk on Mac OSX Leopard ?
  • #66: GlassFish Eclipse Plugin 1.0.16 – Install v3 Prelude from the IDE
  • #65: Windows 7 Beta 1 Build 7000 on Virtual Box: NetBeans + Rails + GlassFish + MySQL
  • #64: OpenSolaris 2008/11 using Virtual Box
  • #63: jmx4r gem – How to manage/monitor your Rails/Merb applications on JRuby/GlassFish ?
  • #62: How to remotely manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX API ?
  • #61: How to locally manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX ?
  • #60: Configure MySQL 6.0.x-alpha to NetBeans 6.5
  • #59: How to add Twitter feeds to blogs.sun.com ? + Other Twitter Tools
  • #58: Jersey and GlassFish – how to process POST requests ?
  • #57: Jersey Client API – simple and easy to use
  • #56: Simple RESTful Web service using Jersey and Embeddable GlassFish – Text and JSON output
  • #55: How to build GlassFish v3 Gem ?
  • #54: Java Server Faces with Eclipse IDE
  • #53: Scaffold in Merb using JRuby/GlassFish
  • #52: Getting Started with Merb using GlassFish Gem
  • #51: Embedding Google Maps in Java Server Faces using GMaps4JSF
  • #50: Mojarra 2.0 EDR2 is now available – Try them with GlassFish v3 and NetBeans 6.5
  • #49: Converting a JSF 1.2 application to JSF 2.0 – @ManagedBean
  • #48: Converting a JSF 1.2 application to JSF 2.0 – Facelets and Ajax
  • #47: Getting Started with Mojarra 2.0 nightly on GlassFish v2
  • #46: Facelets with Java Server Faces 1.2
  • #45: Ajaxifying Java Server Faces using JSF Extensions
  • #44: JDBC Connection Pooling for Rails on GlassFish v3
  • #43: GlassFish v3 Build Flavors
  • #42: Hello JavaServer Faces World with NetBeans and GlassFish
  • #41: How I created transparent logo of GlassFish using Gimp ?
  • #40: jQuery Autcomplete widget with MySQL, GlassFish, NetBeans
  • #39: Prototype/Script.aculo.us Autcomplete widget with MySQL, GlassFish, NetBeans
  • #38: Creating a MySQL Persistence Unit using NetBeans IDE
  • #37: SQLite3 with Ruby-on-Rails on GlassFish Gem
  • #36: Writing First Test for a Rails Application
  • #35: Rails Database Connection on Solaris
  • #34: Using Felix Shell with GlassFish
  • #33: Building GlassFish v3 Workspace
  • #32: Rails Deployment on GlassFish v3 from NetBeans IDE
  • #31: CRUD Application using Grails – Hosted on GlassFish and MySQL
  • #30: CRUD Application using Grails – Hosted on Jetty and HSQLDB
  • #29: Enabling "Available Plugins" tab in NetBeans IDE
  • #28: Getting Started with Rails 2.0 Scaffold
  • #27: Configurable Multiple Ruby Platforms in NetBeans 6.1 M1
  • #26: Overriding Database Defaults in Rails 2.0.2
  • #25: Rails application with PostgreSQL database using NetBeans
  • #24: Getting Started with Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1RC1
  • #23: JavaFX Client invoking a Metro endpoint
  • #22: Java SE client for a Metro endpoint
  • #21: Metro 1.1 with GlassFish v2 UR1 and NetBeans 6
  • #20: How to create a new jMaki widget ?
  • #19: How to Add Metro Quality-of-Service to Contract-First Endpoint ?
  • #18: How to Build The GlassFish v3 Gem for JRuby ?
  • #17: Backing Up your Blog Posts on Roller
  • #16: Optimizing Metro Stubs by locally packaging the WSDL
  • #15: Delete/Update Row from Database using jMaki Data Table
  • #14: How to generate JRuby-on-Rails Controller on Windows (#9893)
  • #13: Setup Mongrel for JRuby-on-Rails applications on Windows
  • #12: Invoking a Java EE 5 Web service endpoint from JRuby
  • #11: Setup Mongrel cluster for JRuby-on-Rails applications on Unix
  • #10: Consuming JSON and XML representations generated by a Jersey endpoint in a jMaki Table widget
  • #9: Using JDBC connection pool/JNDI name from GlassFish in Rails Application
  • #8: Generating JSON using JAXB annotations in Jersey
  • #7: Switch between JRuby and CRuby interpreter in NetBeans 6
  • #6: Difference between Ruby Gem and Rails Plugin
  • #5: Loading data from beans in jMaki widgets
  • #4: How to convert a Session EJB to a Web service ?
  • #3: Using JavaDB with JRuby on Rails
  • #2: Change the endpoint address on a pre-generated Web services Stub
  • #1: SOAP Messaging Logging in Metro

Just for fun, here is another tag cloud:

You can access all the tips here. And keep those suggestions coming!

Technorati: totd glassfish netbeans jpa jsf jaxws jersey mysql rails osgi eclipse

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 29, 2010

TOTD #148: JPA2 Metamodel Classes in NetBeans 7.0 – Writing type-safe Criteria API

Filed under: glassfish, javaee, netbeans — arungupta @ 10:32 am

NetBeans 7.0 M2 was released recently. There are several Java EE related improvements in this release:

  • Find usages of managed beans (JSF/CDI) and their properties
  • PrimeFaces is now available as an integrated JSF component library
  • Wizard for creating Bean Validation constraint
  • CDI Qualifier creation editor hint
  • Cleaned up Inspect Observer/Producer for CDI events
  • Generation of Bean Validation annotations for Entities

and some others. One of the features that is not much spoken about is the automatic generation of JPA 2 Metamodel classes from Entity classes. This Tip Of The Day (TOTD) will explain how to generate these classes and use them for writing type-safe JPA2 Criteria queries.

The JPA2 Metamodel classes capture the metamodel of the persistent state and relationships of the managed classes of a persistence unit. This abstract persistence schema is then used to author the type-safe queries using Critieria API. The canonical metamodel classes can be generated statically using an annotation processor following the rules defined by the specification. The good thing is that no extra configuration is required to generate these metamodel classes. NetBeans IDE automatically generates the canonical metamodel classes using the EclipseLink Canonical Model Generator. There are two ways these metamodel classes are generated in NetBeans:

  1. Pre-configured when Entity Classes are generated from a Database using the wizards. TOTD #122 provide more details on that. The actual metamodel classes are generated when the project is build using "Clean and Build", "Deploy" or some other related target.
  2. Explicitly configured by right-clicking on the project, "Properties", "Libraries", "Processor", "Add Library…", and select "EclipseLink(JPA 2.0)" and "EclipseLink-ModelGen(JPA 2.0)" libraries and click on "Add Library" as shown below.

This TOTD will use the "Manufacturer" table from the pre-configured "jdbc/sample" JDBC resource in NetBeans and GlassFish. It will create a simple 2-page application where the first page (index.xhtml) accepts a Manufacturer name and the second page (show.xhtml) displays some details about that manufacturer.

  1. Create a NetBeans Web project with the title "CriteriaMetamodel", make sure to enable CDI and Java Server Faces during the creation.
  2. Create "Manufacturer" JPA entity by using the pre-configured "jdbc:derby://localhost:1527/sample" database connection and using the MANUFACTURER table. Notice that the generated manufacturer entity contains the bean validation constraints derived from the database schema, yet another new feature in 7.0 M2. More on this topic in a later blog.
  3. Generate the metamodel classes by right-clicking on the project and selecting "Clean and Build". The generated metamodel class looks like:

    package org.glassfish.samples.entities;
    import javax.annotation.Generated;
    import javax.persistence.metamodel.SingularAttribute;
    import javax.persistence.metamodel.StaticMetamodel;
    @Generated("EclipseLink-2.1.0.v20100614-r7608 @ Mon Oct 25 16:35:03 PDT 2010")
    @StaticMetamodel(Manufacturer.class)
    public class Manufacturer_ {
        public static volatile SingularAttribute addressline2;
        public static volatile SingularAttribute zip;
        public static volatile SingularAttribute phone;
        public static volatile SingularAttribute addressline1;
        public static volatile SingularAttribute fax;
        public static volatile SingularAttribute manufacturerId;
        public static volatile SingularAttribute email;
        public static volatile SingularAttribute name;
        public static volatile SingularAttribute state;
        public static volatile SingularAttribute city;
        public static volatile SingularAttribute rep;
    }
    

    This is shown as "Generated Sources" in NetBeans IDE as shown:

  4. Generate a new Java class "DatabaseBean" and mark it with "@javax.enterprise.inject.Model" annotation. This class will be the "backing bean" for the JSF pages and will have

    1. A field to accept the manufacturer’s name from "index.xhtml"
    2. A field to show information about the searched manufacturer in "show.xhtml"
    3. A business method "searchManufacturer" that searches the database for the given manufacturer’s name. This method will use the generated metamodel class and type-safe Criteria API to query the database.

    The complete source code for the class looks like:

    @PersistenceUnit EntityManagerFactory emf;
    
    String name;
    
    Manufacturer manufacturer;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Manufacturer getManufacturer() {
        return manufacturer;
    }
    
    public void searchManufacturer() {
        EntityManager em = emf.createEntityManager();
    
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery criteria = builder.createQuery(Manufacturer.class);
    
        // FROM clause
        Root root = criteria.from(Manufacturer.class);
    
        // SELECT clause
        criteria.select(root);
    
        // WHERE clause
        Predicate condition = builder.like(root.get(Manufacturer_.name),
                "%" + name + "%");
        criteria.where(condition);
    
        // FIRE query
        TypedQuery query = em.createQuery(criteria);
    
        // PRINT result
        List manufacturers = query.getResultList();
    
        if (manufacturers != null && manufacturers.size() > 0) {
            manufacturer = (Manufacturer)manufacturers.get(0);
        }
    }
    

    The business method returns the first manufacturer whose name contains the text entered in the textbox. No validation is performed in order to keep the business logic simple.

    Notice how "searchManufacturer" method is not using any String-based identifiers for constructing the query graph. This gives the complete type-safety for query construction and allows the errors to be detected much earlier.

  5. Edit the generated "index.xhtml" such that the content within <h:body> looks like:

    <h:form>
     <h:panelGrid columns="3">
     <h:outputText value="Name:" />
     <h:inputText value="#{databaseBean.name}" id="name"/>
     </h:panelGrid>
    
     <h:commandButton
        actionListener="#{databaseBean.searchManufacturer}"
        action="show"
        value="submit"/>
    </h:form>
    
    

    This page shows a text box and a submit button. The "searchManufacturer" method of the DatabaseBean is invoked when the "submit" button is clicked and passes the entered text in the "name"property of the DatabaseBean.

  6. Create a new XHTML page and name it "show.xhtml". Replace the generated boilerplate code with the code given below:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Show Manufacturer's Detail</title>
      </head>
      <body>
        Name: #{databaseBean.manufacturer.name}<br/>
        Phone: #{databaseBean.manufacturer.phone}<br/>
        Address Line1: #{databaseBean.manufacturer.addressline1}<br/>
        Address Line2: #{databaseBean.manufacturer.addressline2}<br/>
     </body>
    </html>
    

Now you can deploy the application to GlassFish by usual means and access "http://localhost:8080/CriteriaMetamodel/faces/index.xhtml" which gets displayed as:

Enter some value as "S" in the text box and click on "Submit" to display the result as:

The complete source code for this sample can be downloaded here.

Now Criteria query is little verbose but it does give you the type-safety and was explicitly asked within the JPA Expert Group. It allows you to manipulate different parts of a query such as SELECT, FROM, and WHERE clauses and that too using the Java type system. This reminds me of Mr Potato Head :-)

This behavior can be achieved in JPQL but is available exclusively using String manipulation and the errors are not detected until runtime.

How are you using Criteria API ?

What use cases would you like to see solved in JPA2.next ?

Technorati: totd jpa2 criteria metamodel netbeans javaee6 glassfish

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 28, 2010

TOTD #147: Java Server Faces 2.0 Composite Components using NetBeans – DRY your code

Filed under: frameworks, glassfish, javaee, javaserverfaces, netbeans — arungupta @ 12:03 pm

The Java Server Faces 2.0 uses Facelets instead of JSP as the view declaration language. This allows "view" part of MVC to be completely written using XHTML and CSS only and all the business logic resides in the backing bean. This enables a cleaner separation of views with model and controller and thus follows the MVC design pattern in a more intuitive way. JSF 2 also defines how resources can be packaged, located, and rendered by JSF runtime within a web application.

Using these two features of Facelets and Resource Handling, JSF2 defines a composite component as a component that consists of one or more JSF components defined in a Facelet markup file that resides inside of a resource library. The composite component is defined in the defining page and used in the using page. The "defining page" defines the metadata (or parameters) using <cc:interface> and implementation using <cc:implementation> where "cc" is the prefix for "http://java.sun.com/jsf/composite" namespace. Future versions of the JSF 2 specification may relax the requirement to specify metadata as it can be derived from the implementation itself.

A composite component can be defined using JSF 1.2 as well but it requires a much deeper understanding of JSF lifecycle and also authoring multiple files. JSF2 really simplifies the authoring of composite components using just an XHTML file.

Code is king! This Tip Of The Day (TOTD) will explain how to convert an existing code fragment into a JSF2 composite component using NetBeans IDE.

Lets say a Facelet (index.xhtml) has the following code fragment:

<h:form>
    <h:panelGrid columns="3">
    <h:outputText value="Name:" />
    <h:inputText value="#{user.name}" id="name"/>
    <h:message for="name" style="color: red" />
    <h:outputText value="Password:" />
    <h:inputText value="#{user.password}" id="password"/>
    <h:message for="password" style="color: red" />
  </h:panelGrid>

  <h:commandButton actionListener="#{userService.register}"
                   id="loginButton" action="status" value="submit"/>
</h:form>

This fragment displays an HTML form with two text input boxes and a "submit" button. The two input boxes are bound to "user" bean and clicking on the button invokes "register" method of the "userService" bean.

Instead of repeating this code in multiple pages, its beneficial to convert this into a composite component and use the resulting tag instead of the complete fragment again. Why ?

  • Follows the DRY principle and allows to keep the code, that can be potentially be repeated at multiple places, in a single file.
  • It allows developers to author new components without any Java code or XML configuration.

How do you convert an existing code fragment to a composite component ? NetBeans makes it really easy.

In NetBeans IDE select the code fragment, right-click, "Refactor", "Convert to Composite Component…" as shown below:

In the next screen, change the filename to "loginPanel" and take every thing else as default as shown below:

and click on "Finish".

This will generate "web/resources/ezcomp/loginPanel.xhtml" and move the component definition to this file, aka "defining page" and looks like:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:h="http://java.sun.com/jsf/html">

 <!-- INTERFACE -->
 <cc:interface>
 </cc:interface>

 <!-- IMPLEMENTATION -->
 <cc:implementation>
   <h:form>
     <h:panelGrid columns="3">
       <h:outputText value="Name:" />
       <h:inputText value="#{user.name}" id="name"/>
       <h:message for="name" style="color: red" />
       <h:outputText value="Password:" />
       <h:inputText value="#{user.password}" id="password"/>
       <h:message for="password" style="color: red" />
     </h:panelGrid>

     <h:commandButton actionListener="#{userService.register}"
                      id="loginButton" action="status" value="submit"/>
   </h:form>
</cc:implementation>
</html>

<cc:interface> defines metadata that describe the characteristics of component, such as supported attributes, facets, and attach points for event listeners. <cc:implementation> contains the markup substituted for the composite component.

<cc:interface> is generated in the page but is empty and may be made optional in a subsequent release of the JSF specification.The "using page" will declare a new namespace as:

xmlns:ez="http://java.sun.com/jsf/composite/ezcomp"

and then replace the code fragment with:

<ez:loginPanel/>

The tag name for the new composite component is the same as the "defining page" file name. This enables "<ez:loginPanel/>" to be used instead of repeating that entire code fragment.

Now lets say that the code fragment need to pass different value expressions (instead of #{user.name}) and invoke a different method (instead of #{userService.register}) when submit button is clicked in different "using page"s. The "defining page" can then look like:

<!-- INTERFACE -->
<cc:interface>
  <cc:attribute name="name"/>
  <cc:attribute name="password"/>
  <cc:attribute name="actionListener"
      method-signature="void action(javax.faces.event.Event)"
      targets="ccForm:loginButton"/>
</cc:interface>

<!-- IMPLEMENTATION -->
<cc:implementation>
  <h:form id="ccForm">
  <h:panelGrid columns="3">
    <h:outputText value="Name:" />
    <h:inputText value="#{cc.attrs.name}" id="name"/>
    <h:message for="name" style="color: red" />
    <h:outputText value="Password:" />
    <h:inputText value="#{cc.attrs.password}" id="password"/>
    <h:message for="password" style="color: red" />
  </h:panelGrid>

  <h:commandButton id="loginButton"
                   action="status"
                   value="submit"/>
  </h:form>
</cc:implementation>

The changes are highlighted in bold and explained below:

  • All the parameters are explicitly specified in <cc:interface> for clarity. The third parameter has a "targets" attribute referrring to "ccForm:loginButton".
  • In <cc:implementation>

    • The <h:form> in has "id" attribute. This is required such that the button within the form can be explicitly referenced.
    • <h:inputText> is now using #{cc.attrs.xxx} instead of #{user.xxx}. #{cc.attrs} is a default EL expression that is available for composite component authors and provide access to attributes of the current composite component. In this case #{cc.attrs} has "name" and "password" defined as attributes.
    • "actionListener" is an attach point for event listener, defined as a "method-signature" and describes the signature of a method pointed to by the expression.
    • <h:commandButton> has "id" attribute so that it can be clearly identified within the <h:form>.

The "user", "password", and "actionListener" are then passed as required attributes in the "using page" as:

<ez:loginPanel
    name="#{user.name}"
    password="#{user.password}"
    actionListener="#{userService.register}"/>

Now the "using page" can pass different "backing beans" and business method to be invoked when "submit" button is invoked.

The complete source code for this TOTD can be downloaded here.

How are you using JSF 2 composite components ?

The entire source code used in this blog can be downloaded here.

JSF 2 implementation is bundled with GlassFish Server Open Source Edition, try it today!

I realized TOTD #135 already explains how to author composite components. Hey, but this TOTD provides new information on how to attach event listeners :-)

Technorati: totd javaee6 glassfish jsf2 composite component facelets

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 20, 2010

Screencast #33: NetBeans 7.0 M2 and GlassFish 3.1 Features

Filed under: frameworks, glassfish, netbeans — arungupta @ 11:28 am

NetBeans 7.0 M2 was released recently and comes pre-bundled with GlassFish 3.1. Vince Kraemer has been blogging about several features that have been introduced in the builds recently:

  • Application Scoped Resources
  • Web Services Nodes
  • Create domains with default ports
  • Support glassfish-resources.xml
  • View Server Log for Remote Instance
  • Restart a Remote Server in Debug Mode
  • Enable/Disable Action for Deployed Apps

Now, you can see all these features live in action in this screencast:

Enjoy!

Thanks Vince!

Technorati: screencast netbeans glassfish

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

August 14, 2010

Java EE 6 & GlassFish workshop delivered at San Francisco JUG – Slides & Demos available

Filed under: glassfish, netbeans — arungupta @ 3:50 am

Roberto and I delivered a 2-part Java EE 6 & GlassFish 3 workshop at the San Francisco JUG last week. Over approximately 7 hours, we gave a preview of Java EE 6, explained the key technologies introduced in the platform, and did lot of coding using NetBeans IDE.

Roberto’s slides are given below:

Overview of Java EE 6 by Roberto Chinnici at SFJUG

And my slides are next:

JavaEE 6 and GlassFish v3 at SFJUG

The completed project from the workshop is now available here. This bundle contains the entire code sample built through out the workshop. The coding sessions are also available in screencast #30, also shown below:

Here is some feedback from the attendees:

The best JUG meeting I’ve ever attended. It was very informative, helpful and relevant. I particularly liked Arun’s follow-along coding demonstrations. I would suggest that the speakers be encouraged to use a mic. If I had not been sitting two rows from the front, then I would have had a hard time hearing Roberto.

It was interesting to hear from the source which parts of EE6 they thought were most valuable, and where new things overlapped with old, and what advantages there were in using standards-based solutions over frameworks like Spring, even when they tend to be behind in usability and power. Also liked the taste of NetBeans, being an Eclipse user.

Very fruitful evening. Thanks so much to the speakers and organizers.

I really enjoyed both nights. There were several JEE 6 (now I now the correct way to say it) technologies that I had heard of but quite didn’t understand … especially in regards to how they integrate. Now I do.…

Thanks aleksandar for organizing this JavaEE6 learning series. It was very helpful and head start on the cool features of EE6. Thanks to arun and Roberto for there well written presentations and hands on workshop. Will look forward for part 2 of this series.

Really enjoyed. Roberto gave a good high level overview of JavaEE (and he was funny) and Arun’s talk was educational and easy to follow along with. Very impressed with how easy it was to get the examples working with NetBeans. Looking forward to Thursday.

Really enjoyed both presentations. The fact that it was hands on added an extra dimension to the learning experience. Looking forward to the next session. Thanks to Roberto and Arun!

This was a fantastic intro to Java EE 6. I really liked Roberto’s down-to-earth overview of Java EE and its role in the enterprise Java world. In his workshop, Arun did an amazing job making Java EE 6 on GlassFish with NetBeans feel really approachable – if not too easy. Great participation from the audience.

The hands-on component worked great for me.

And the ratings are encouraging too …

As always, there is room for improvement and lessons to learn :-)

Check out some pictures:

Thank you Sasa for giving us an opportunity to talk about Java EE 6 & GlassFish 3 at San Francisco JUG!

Technorati: conf sanfrancisco jug sfjug glassfish javaee6

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

July 30, 2010

Dallas Tech Fest 2010 Trip Report

Filed under: glassfish, netbeans — arungupta @ 3:40 pm

Oracle was a gold sponsor of Dallas Tech Fest 2010 – 9 parallel tracks and 5 sessions/track.

I gave a 3-hour hands-on workshop on Java EE 6, GlassFish, and NetBeans. The room was packed during the first part (about 60 or so) and most of the audience retained for second part. The workshop explained several advantages of Java EE 6 such as simplicity, ease-of-use, and richness of the platform. These concepts were demonstrated using multiple coding sessions involving several technologies from the platform. Specifically it showed:

  • Creating simple Java EE 6 application using JSP, Servlets 3.0, Enterprise Java Beans 3.1
  • Facelets-based page creation using Java Server Faces 2
  • Contexts & Dependency Injection with Java Server Faces 2
  • Accessing database table using Java Persistence API 2
  • RESTful Web services using JAX-RS

NetBeans IDE specific features like Deploy-on-Save and Session-Preservation features that boosts your development productivity were also demonstrated using code samples.

The slides are now available:

Java EE 6 Hands-on Workshop at Dallas Tech Fest 2010

Watch Tim Rayburn (one of the conference organizers) talks about where Dallas Tech Fest is today and how they like to evolve it for next year:

One of the attendees mentioned after the workshop that it was like "drinking from the fire hose". The content could be overwhelming for users who are not familiar with NetBeans and new to Java EE 6. As repeated multiple times during the workshop, all the code shown in the workshop is clearly explained in screencast #30 (also in-lined below).

Feel free to re-run the workshop at your own pace and convenience. And you can always post any comment on this blog or GlassFish forum for question and/or clarifications.

Check out some pictures from the event:

On a personal front, met a few avid readers of my blog, connected with some Oracle folks, and barely met Ted Neward. I found Texas very humid and hot early in the morning for me. But still managed to squeeze in a 10K run, at a much slower pace:

One of the advantage of staying at Westin hotels is that they have a running map close to their hotel. And if not then their "Westin Workout" gyms are typically well equipped – even a stepper and exercise ball ;-)

Thanks Erik & Tim for providing me the opportunity to speak, I definitely look forward to participating next year!

And here is the complete photo album below:

Technorati: conf dallastechfest dallas glassfish javaee6 netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

July 28, 2010

FISL 2010 Trip Report

Filed under: glassfish, netbeans — arungupta @ 6:04 am

4998 attendees registered and participated at the 11th edition of FISL – the biggest open source conference in Brazil. This was my second year at FISL. Even though the attendance was slightly down from last year but there was no let down in the energy. With 13 parallel tracks and sessions running from 9am to 11pm, it can be absolutely overwhelming. However most of the sessions were in Portuguese (with no English translation) so I could not attend.

I presented on the Java EE 6 Toolshow to an audience of approx 200. This slides-free session showed how NetBeans 6.9 provides comprehensive tooling around Java EE 6 and GlassFish 3. The session showed:

  • Simplicity and ease-of-use for creating Java EE 6 web applications
  • Boost productivity using Deploy-on-Save and Session-preservation across multiple redeployments
  • JSP, Servlets 3.0, EJB 3.1 in Java EE 6 web apps
  • Database access using Java Persistence API 2.0
  • Using Facelets with Java Server Faces 2.0
  • Contexts & Dependency Injection 1.0 with JSF 2
  • RESTful Web services using JAX-RS

The screencast #30 made the session delivery quite a breeze and you can watch the entire session by watching the multi-part screencasts.

Meeting Bruno Souza and Fabiane Nardon was a good highlight of the trip. They are both fairly well known in the Brazilian community and we shared stories from last year’s presence of Sun Microsystems at FISL. Check out their latest adventure at toolscloud.com where they provide open source tools-based development environment in the cloud.

Personally, I stayed for only couple of days because I had to come back to run a race (more on that in next blog) and attend a wedding over the weekend. A short trip but always good to spend face-to-face time with the local community.

Check out some pictures from the trip:

And the complete album at:

Technorati: conf fisl brazil oracle glassfish javaee6 netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

July 20, 2010

Screencast #30: Java EE 6 & GlassFish 3 using NetBeans 6.9 – 5 screencasts

Filed under: frameworks, glassfish, javaee, netbeans, screencast — arungupta @ 5:43 am

This 5-part screencast shows how NetBeans 6.9 provides comprehensive tooling for Java EE 6 & GlassFish 3. The video tutorial starts with building a simple Java EE 6 application and evolves to add features from several new technologies such as Java Persistence API 2, Java Server Faces 2, Contexts & Dependency Injection, and Java API for RESTful Web services from the platform. Specifically, the different parts show:

  1. How to create a simple Java EE 6 application using JSP, Servlets 3, and EJB 3.1
  2. Reading values from a database table using Java Persistence API 2 POJO entities
  3. Create a template-based website using Facelets with Java Server Faces 2
  4. Use Contexts & Dependency Injection with JSF 2
  5. Publish a RESTful Web service using JAX-RS

Enjoy!

Please give us feedback on GlassFish Forums.

Technorati: screencast javaee6 glassfish tutorial netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Older Posts »

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress