Java EE Application Client on top of the NetBeans Platform Tutorial

Draft

This is document currently has draft status

This tutorial will show you how easy is to create an application client on top of the NetBeans Platform. It will be demonstrated on the example of Database Reader.

Table of Contents



Requirements

  • Java(TM) SE Development Kit 5.0
  • NetBeans IDE 5.5.1
  • NetBeans Platform 5.5.1
  • GlassFish v2 build 33

Installation And Configuration

Install all of the required products (installation guides are available on the product's websites). When it'll be done we have to set up a few things. First of all please start NetBeans IDE 5.5.1 and register GlassFish v2. Right click on the Servers node in the Runtime tab and select Add server (choose Sun Java Application Server).

addserver.png

Now we need to register NetBeans Platform into IDE. It's in fact almost same as to add a new server. In menu Tools -> NetBeans Platform Manager click on a Add Platform button and pass through the wizard (as a new platform select downloaded NetBeans Platform 5.5.1).

addplatform.png

Projects Creation

It's time to create all projects. We need NetBeans Module Suite project, NetBeans Module (added into your NetBeans Module Suite) project and Enterprise Application project with Application Client and EJB module included. Let's do it. First of all we create NetBeans Module Suite project. Call it dbreader. As used platform choose the new one what you registered before.

createsuite1.png

createsuite2.png

Then create NetBeans Module Project. Call it customers. And check that you want to add it into your dbreader suite. All other options leave as default.

createmodule.png

Actually we have had NetBeans Modules created and now we have to create Java EE part. So let's create an Enterprise Application with Application Client and EJB module. Call it dbreader-ear. Include Application Client and EJB module. Exclude Web module. Also select Java EE 5 version and choose Sun Java Application Server as development server.

createear1.png

createear2.png

Great ! You have successfully created all required projects. Now you should see something like this in Projects tab.

projects.png

Enterprise Application Development

Build Script Modifying

We need to modify dbreader-ear build.xml script because the dbreader suite jnlp distro has to be packed into dbreader ear. Due to add these lines into dbreader-ear build.xml.

    <property name="dbreader.home" value="../"/>
    <target name="build-dbreader-jnlp">
        <java classname="org.apache.tools.ant.Main" dir="${dbreader.home}" failonerror="true" fork="true">
            <jvmarg value="-Dant.home=${ant.home}"/>
            <arg value="build-jnlp"/>
            <classpath path="${java.class.path}"/>
        </java>
    </target>
    <target name="pre-dist" depends="build-dbreader-jnlp">
        <!-- dbreader.home must point to DatabaseReader Application home directory -->
        <mkdir dir="${build.dir}/lib"/>
        <copy todir="${build.dir}/lib">
            <fileset dir="${dbreader.home}/build/jnlp/app" includes="*.jar" />
            <fileset dir="${dbreader.home}/build/jnlp/branding" includes="*.jar" />
            <fileset dir="${dbreader.home}/build/jnlp/netbeans" includes="*.jar" />
        </copy>
    </target>

You are able to access build.xml file in Files view.

editearbuild1.png

After editing you should see something like this.

editearbuild2.png

Generating Entity Classes From Database

We have dbreader-ear project infrastructure prepared. Now we have to generate entity classes from sample database. Right click on dbreader-ear-ejb project in Project tab and select New -> Entity Classes From Database. In wizard chose as datasource jdbc/sample datasource and select CUSTOMER table.

generateentity1.png

On the next wizard panel type package for entity classes. Type db. Then Click on create persistence unit. Persistence unit dialog will appear. Click on Create. Now finish the wizard by clicking on the Finish button.

generateentity2.png

Now we have generated entity classes from jdbc/sample database. Under dbreader-ear-ejb project you can see generated classes.

generateentity3.png

Create Session Bean

We need to create stateless session bean with remote interface to communicate with persistence unit. Create one and call it DataBean.

createsession1.png

When you have session bean created add business method called getData. You are able to do it by right clicking on the editor pane (in DataBean.java file opened) and select EJB Methods -> Add Business Method. Pass through the wizard and create getData method which returns java.util.List.

createsession2.png

Now use entity manager. Once again do a right click on the editor pane and select Persistence -> Use Entity Manager. Entity manager code is generated. Now implement getData method.

    public List getData() {
        //TODO implement getData
        return em.createQuery("SELECT c FROM Customer c").getResultList();
    }

After that you should see in editor (in DataBean.java file) something like this.

createsession3.png

Modify Application Client

We prepared EJB module and now we have to implement functionality into dbreader-ear-app-client Application Client module. Open Main.java file in dbreader-ear-app-client project.

modifyappclient1.png

Now call your session bean DataBean. Right click on editor pane and select Enterprise Resources -> Call Enterprise Bean. In the dialog select your DataBean and click OK.

modifyappclient2.png

Now we need to implement main method and create getCustomers method. Before that add <dbreader_project_home>/build/jnlp/netbeans/boot.jar file on classpath. Do it by right clicking on dbreader-ear-app-client project and select Properties. There select Libraries and then click on Add JAR/Folder and in open file dialog select boot.jar file. Don't forget to uncheck the checkbox. We do not want to package this file with dbreader-ear-app-client module. Actually you have to run build-jnlp target on dbreader suite. Before that please perform step Set Up Suite. Then you can right click on dbreader project and select Build JNLP Application.

modifyappclient3.png

Implement main method by this code.

    public static void main(String[] args) {
        try {
            String userDir = System.getProperty("user.home") + File.separator + ".dbreader";
            org.netbeans.Main.main(new String[] {"--branding", "dbreader", "--userdir", userDir});
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

Now create getCustomers static method.

    public static List getCustomers() {
        return dataBean.getData();
    }

After doing this you should see something like this in editor pane.

modifyappclient4.png

Great ! We have finished development of the dbreader-ear Enterprise Application. Let's go to develop NetBeans Modules.

NetBeans Modules Development

Set Up Suite

Now we set up the dbreader NetBeans module suite. We have to set it as standalone application and also we are able to change splash screen. Right click on dbreader project and select Properties. There select Application and then click on the Create Standalone Application.

setupsuite1.png

Also you are able to set up your own splash screen. Do it by same way and under the Application node in project Properties click on Splash Screen.

setupsuite2.png

Set Up Module

Now we set up the customers NetBeans Module. We have to add dbreader-ear-ejb.jar, dbreader-ear-app-client.jar and javaee.jar on compile classpath. First of all set sources level of the module to 1.5. Right click on customers project and on the first panel select 1.5 for sources level.

setupmodule1.png

Open project.properties file from project tab.

setupmodule2.png

Add this code into project.properties file. Of course use your own path to dbreader and glassfish.

cp.extra=\
/home/marigan/temp/dbreader/dbreader-ear/dbreader-ear-ejb/dist/dbreader-ear-ejb.jar:\
/home/marigan/temp/dbreader/dbreader-ear/dbreader-ear-app-client/dist/dbreader-ear-app-client.jar:\
/home/marigan/apps/glassfish/lib/javaee.jar

After that you should see something like this in editor pane.

setupmodule3.png

Create Window Component

Now we create a new window component which will serve as viewer for database data. Right click on customers project and select New -> Window Component. On the first wizard panel choose editor as Window Position and select Open on Application Start.

createwindow1.png

On the second panel specify component Class Name Prefix (use Customers) and finish the wizard.

createwindow2.png

After that you should see this in Project tab.

createwindow3.png

Write Customers Top Component Logic

We have to write application logic for customers top component. Open CustomersTopComponent.java file in design mode and drag and drop a jTable component from palette into it.

writelogic1.png

Now switch into source view and modify constructor and add initData method.

    private CustomersTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(CustomersTopComponent.class, "CTL_CustomersTopComponent"));
        setToolTipText(NbBundle.getMessage(CustomersTopComponent.class, "HINT_CustomersTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        initData();
    }
    private void initData() {
        List<Customer> data = Main.getCustomers();
        Object[][] rows = new Object[data.size()][3];
        int i = 0;
        for (Customer c : data) {
            rows[i][0] = c.getName();
            rows[i][1] = c.getEmail();
            rows[i++][2] = c.getPhone();
        }
        Object[] colums = new Object[] {"Name", "E-mail", "Phone"};
        jTable1.setModel(new DefaultTableModel(rows, colums));
    }

After that you should see something like this.

writelogic2.png

Run Application

Great job !! Everything is done. Now you can run your application. Right click on dbreader-ear project and select Run Project. Wait a minute do build and glassfish to start. Enjoy your application :o)

runapp.png

Attachments

addplatform.png Info on addplatform.png 10653 bytes
addserver.png Info on addserver.png 19325 bytes
createear1.png Info on createear1.png 25977 bytes
createear2.png Info on createear2.png 21489 bytes
createmodule.png Info on createmodule.png 20651 bytes
createsession1.png Info on createsession1.png 18103 bytes
createsession2.png Info on createsession2.png 6203 bytes
createsession3.png Info on createsession3.png 7189 bytes
createsuite1.png Info on createsuite1.png 19423 bytes
createsuite2.png Info on createsuite2.png 18646 bytes
createwindow1.png Info on createwindow1.png 15472 bytes
createwindow2.png Info on createwindow2.png 19600 bytes
createwindow3.png Info on createwindow3.png 10229 bytes
editearbuild1.png Info on editearbuild1.png 11116 bytes
editearbuild2.png Info on editearbuild2.png 14580 bytes
generateentity1.png Info on generateentity1.png 20163 bytes
generateentity2.png Info on generateentity2.png 20561 bytes
generateentity3.png Info on generateentity3.png 12047 bytes
modifyappclient1.png Info on modifyappclient1.png 9677 bytes
modifyappclient2.png Info on modifyappclient2.png 5540 bytes
modifyappclient3.png Info on modifyappclient3.png 14283 bytes
modifyappclient4.png Info on modifyappclient4.png 9593 bytes
projects.png Info on projects.png 3696 bytes
runapp.png Info on runapp.png 20157 bytes
setupmodule1.png Info on setupmodule1.png 7398 bytes
setupmodule2.png Info on setupmodule2.png 8793 bytes
setupmodule3.png Info on setupmodule3.png 8406 bytes
setupsuite1.png Info on setupsuite1.png 11674 bytes
setupsuite2.png Info on setupsuite2.png 88817 bytes
writelogic1.png Info on writelogic1.png 16460 bytes
writelogic2.png Info on writelogic2.png 15182 bytes

NetBeans


Referenced by: