Your SlideShare is downloading. ×
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Global Software Sales
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Global Software Sales

732

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
732
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SUN MICROSYSTEMS Sun Glassfish WebSpace Sever 10 Hands-on Creating an Ajax-Push Web app & portlet Global Software Sales Sun Project : WebSpace Server 10 labs Version Number: D-0.5 Authors: Patrice Goutin Document Date: October 2009 Please send feeback to patrice.goutin@sun.com Sun Confidential: Internal used Only © Sun Microsystems, Inc, 2009
  • 2. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises Table of Contents 1. Abstract............................................................................................................................................................................3 2. Introduction..................................................................................................................................................................... 3 2.1. Important restriction..................................................................................................................................................3 2.1.1. Netbeans ICEfaces Visual Web support limitation...........................................................................................3 2.1.2. Netbeans ICEfaces Visual Web portlet support limitation................................................................................3 2.2. Possible scenario & strategies................................................................................................................................. 3 2.3. Push Server..............................................................................................................................................................5 2.4. ICEFaces Ajax Push API.......................................................................................................................................... 6 3. Hand-on............................................................................................................................................................................7 4. Prerequiste...................................................................................................................................................................... 7 4.1. Lab System Environment......................................................................................................................................... 7 4.2. Lab Software Environment....................................................................................................................................... 7 4.3. Configuration requirements : Software and Hardware............................................................................................. 7 5. Glassfish configiration the activate the Comet Ajax Push server..............................................................................9 6. Creating a Web Session/Application Counter IceFaces with Ajax-Push................................................................ 10 6.1. Overview.................................................................................................................................................................10 6.2. Step-by-step........................................................................................................................................................... 10 6.2.1. Create a Web Application Project with ICEfaces & Facelets frameworks......................................................10 6.2.2. Create the Managed Beans........................................................................................................................... 14 6.2.3. Create the user interface............................................................................................................................... 18 6.2.4. Running the Web Application without AjaxPush effect...................................................................................21 6.2.5. Adding AjaxPush effect.................................................................................................................................. 23 6.2.6. Running the Web Application with AjaxPush effect........................................................................................25 7. Creating a Portlet Session/Application Counter IceFaces with Ajax-Push............................................................ 27 7.1. Overview.................................................................................................................................................................27 7.2. Step-by-step........................................................................................................................................................... 27 7.2.1. Create a portlet project with ICEFaces, Facelet and Portlet support ............................................................ 27 7.2.2. Create The Managed Beans..........................................................................................................................32 7.2.3. Creating the user interface.............................................................................................................................35 7.2.4. Configure the configuration files.................................................................................................................... 35 7.2.5. Running the portlet with AjaxPush effect....................................................................................................... 37 Page 2 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 3. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 1. Abstract The essence of Ajax Push is the ability to trigger updates to the client from events or changes that occur on the server. This makes Ajax Push highly suitable for collaborative applications where one user does something that changes the current state of the application and other users are interested in seeing that change. There are many different ways to take advantage of Ajax Push to add collaborative features to your rich web application or portlet. 2. Introduction This article discusses how you can implement an Ajax Push applications (webapp or portlet) deployed respectively on Sun Glassfish or Sun Glassfish WebSpace Server 10. this tutorial is divided into 2 main parts : • In the 1st part of this tutorial, we will focus on the architecture artifacts & configuration to put in place. For this reason, we will develop a Ajax Push web application. • In the 2nd part, we will transfrom this web application into a JSR 286 portlet. 2.1. Important restriction 2.1.1. Netbeans ICEfaces Visual Web support limitation Source : http://www.icefaces.org/JForum/posts/list/13622.page The Visual Web modules have been removed from the standard distribution, along with the Woodstock components. These modules have been released to the community for support and future development. To get the Visual Web functionality, install the Visual JSF and Visual JSF Runtime modules from the Update Center. The NetBeans IDE still supports web development with JSF and JSPs. In light of this development, and after careful consideration of all the options available to us, we have determined that it is not feasible for us to continue support for the VWP (visual design) editor in future releases of NetBeans, beginning with NetBeans 6.7. We regret any inconvenience caused by this situation, however, it is largely outside of our control. We recommend NetBeans 6.5.1 to any users who would like to continue to use the ICEfaces Visual Web Project integration and will continue to support VWP for NetBeans 6.5.1 with future ICEfaces runtime library maintenance releases for the 1.8.x release track. Note that we do offer a NetBeans 6.7 integration for ICEfaces which fully supports development with the non-visual JSP editor. We look forward to supporting a new visual design capability within NetBeans should one become available at some point in the future, likely based on JSF2.0 and Facelets markup. 2.1.2. Netbeans ICEfaces Visual Web portlet support limitation There's no portlet related support on the ICEfaces plugins so there's some manual config involved. The manual config is provided as-is on the following wiki : https://wikis.sun.com/display/WebSpaceServer10/WebSpace+Server+Portlet+development 2.2. Possible scenario & strategies Source : ICEFace website http://www.icefaces.org Take in account the following scenario : Page 3 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 4. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises • next generation Web 2.0 applications like live forums, chat, auctions, etc. • systems where the server-side data model can change dynamically, and those changes are important to one or more users • collaborative applications where multiple users interact with the same data There is a couple of way to implement these scenario so far as described herebelow. By the way, all these strategies are under the control of the client. A more suitable architecture consists in inversiong the http protocol : from the server to the client. This mechanism is known as Comet, Reverse Ajax, Ajax Push where a Push Server, deployed on the App Server sends to the Client the updated information as soon as available. Page 4 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 5. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 2.3. Push Server The Push Server automatically configures your run time environment for simple and effective deployment of applications and portlets under a single domain name. The Push Server manages a single Ajax Push blocking connection with the client browser and shares that connection between the deployed applications. It also utilizes native Asychronous Request Processing (ARP) capabilities to provide single node scalability to Ajax Push deployments. A basic deployment of the the Push Server is illustrated below, and shows how a single blocking connection is shared between multiple ICEfaces applications, multiple views onto the same application, and portal pages with multiple ICEfaces portlets. Browser Connection Limits Be Gone The primary purpose of the Push Server is to eliminate the browser connection limit associated with a single web domain. Each ICEfaces application, application view, or portlet requires a blocking connection for the long polling mechanism used at the heart of Ajax Push, but if each page in the browser maintained its own blocking connection, the browser connection limit would be rapidly exceeded as new page views were opened. The ICfaces Ajax Bridge performs connection sharing in the client browser, and the Push Server manages that single blocking connection at the server, sharing it between the deployed ICEfaces applications and portlets. Regardless of the number of deployed applications, and the number of page views onto those applications, only two connections are required, so the browser connection limit will never be exceeded. Native Asynchronous Request Processing Because Ajax Push requires a blocking connection per client session, and because threads are consumed for each of these connections during the entire request/response lifecyle, the mechanism suffers from a thread scalability issue under the standard Servlet model. Various application servers overcome this deficiency in the Servlet specification by augmenting the web container with an ARP mechanism capable of freeing up threads for the duration of the blocking period. When configured to do so, the Push Server automatically detects and utilizes the native ARP mechanism in the application server, to provide a thread scalable implementation for Ajax Push in a single node deployment. As far as Glassfish v2 or v3 is used as the target App Server, you get already out of the box a Push Server from Page 5 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 6. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises the Glassfish ARP component. 2.4. ICEFaces Ajax Push API ICEFaces provides a very easy-to-use Ajax-Push API. Internally speaking, the ICEFaces Ajax Push API is based on a Long pool mechanism. PersistentFacesState API • unique to each client • represents the currently active “view” • considered a low-level API Renderable interface API • implemented by JSF managed bean • provides view state and exception callback RenderManager API RenderManager • application singleton • manages GroupAsyncRenderers GroupAsyncRenderer • handles Ajax Push for groups of Renderables • different flavours (OnDemand, Interval, Delay) DisposableBean interface • for cleaning up For an easy reading, • we will use WebSpace Server 10 as an abbreviation for Sun Glassfish WebSpace Server 10 Page 6 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 7. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 3. Hand-on We provide a couple of samples to illustrate how to put in place the Ajax Push mechanism for Web and Portlet applications. In this tutorial, we provide also a step-by-step guide to build a Web Session/Application counter and a portlet Session/Application counter. Session/Applica • Web IceFaces development steps with netbeans 6.5.1 & Portal Pack Step by step guide tion counter 3.0.2 provided in this • Portlet IceFaces development steps with netbeans 6.5.1 & Portal Pack tutorial 3.0.2 Chat • Web IceFaces development steps with netbeans 6.5.1 & Portal Pack Source code 3.0.2 available “as-is” • Web IceFaces development steps with netbeans 6.5.1 & Portal Pack 3.0.2 Source code AuctionManager • Web Icefaces AuctionManager deployment steps available “as-is” • Portlet IceFaces AuctionManager deployment steps 4. Prerequiste 4.1. Lab System Environment You'll need a modern computer with at least 1GB of RAM (2GB is better) and about 2 GB of free disk space to setup the development environment based on Netbeans6.1 and the deployment environment based on Sun Glassfish WebSpace Server 10. This tutorial has been successfully tested on MAC OS X & Windows XP and should be up & running on other platform supported by Netbeans and Sun Glassfish WebSpace Server 10 4.2. Lab Software Environment We assume, your platform is ready to run this lab. As a reminder, the following softwares need to be installed & configured successfully in your platform of choice : • Netbeans 6.5.* with Portal Pack 3.0.2 plugins install the Portal Pack 3.0.2 plugins on NetBeans IDE 6.5, 6.5.x, or 6.7 version. You can download the latest Portal Pack plugin from http://portalpack.netbeans.org/. • ICEFaces 1.8.1 • Glassfish v2, Glassfish v3 • WebSpace Server 10.0-u6 You can download Web Space Server from https://webspace.dev.java.net/. 4.3. Configuration requirements : Software and Hardware Operating Systems OpenSolaris 2008.05 (x86/x64) Solaris 10 or later (SPARC/x86) Modern Linux operating systems (Ubuntu 8, SuSE 10, OpenSuSE 11, Red Hat 5) Page 7 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 8. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises Microsoft Windows 2003 Server, XP Professional, Vista 32–bit Mac OS X 10.4 or later Java Platform Java Runtime Environment 1.6.0_7 or later (1.5 or later on Mac OS X) Java JDK 1.6.0_7 or later (1.5 or later on Mac OS X) Web Container Sun GlassFish Enterprise Server v2.1 Note – Other versions of Sun GlassFish will work with Web Space Server, such as GlassFish v3 Prelude, but are recommended for evaluation or testing purposes only, rather than a production environment. Apache Ant Apache Ant 1.7 or later Note – The version of Ant bundled with Sun GlassFish v2 or later does not work with Web Space Server 10.0. Make sure that Ant 1.7 or later is installed on your system, and that your ANT_HOME environment variable points to this newer version. System Memory (RAM) Solaris, Linux: 1 GB minimum, at least 2 GB recommended Windows: 2 GB minimum, at least 3 GB recommended MacOS X: 1 GB minimum, at least 2 GB recommended Source : Sun GlassFish Web Space Server 10.0 Getting Started Guide available http://docs.sun.com/app/docs/doc/820-7053/ghpbf?a=view Page 8 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 9. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 5. Glassfish configiration the activate the Comet Ajax Push server Note In order to get an Ajax Push server available for you in GlassFish v2 or GlassFish v3 you can either : 1. install the iceFace Ajax Push server. This is a simple webapp provided within the iceFace 1.8.* distribution 2. activate the Comet Ajax Push server (aka Comet) available from GlassFish v2 or v3. This is the way we go. I am using port 14848 Comet Ajax 1. Launch Glassfish admin : http://localhost:14848/ Push server 2. Click Configuration, HTTP Service, http-listener-1 configuration with GlassFish admin Gui 3. In the additional Proprieties panel, click • Name : cometSupport • Value : true 4. Restart your GlassFish instance Comet Ajax 1. Edit your glassFish instance domain.xml to add the following property: For advanced users. Push server configuration in <http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" editing default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" domain.xml port="18080" security-enabled="false" server-name="" xpowered-by="true"> <property name="proxiedProtocols" value="ws/tcp"/> <property name="cometSupport" value="true"/> </http-listener> 2. Restart your GlassFish instance Page 9 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 10. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 6. Creating a Web Session/Application Counter IceFaces with Ajax-Push 6.1. Overview The project we are going to build in this tutorial is a page that contains simple, adjustable counters. The counter values are displayed on the page and buttons are provided that can be used to increment or decrement the counter values. The finished project looks like this: The application consists of two counters. One is an application-scoped counter that can be adjusted and seen by all users. The second counter is session-scoped so changes to the value of this counter are restricted to the individual user. The goal is use the SessionRenderer API so that any modifications to the application-scoped counter result in Ajax Push updates being sent to all application users. Note 6.2. Step-by-step 6.2.1. Create a Web Application Project with ICEfaces & Facelets frameworks Create a Web 1. From the menus, choose File -> New Project... Application 2. On the first screen of the New Project wizard, choose the Java project Web category and the Web Application project. 3. Click Next Page 10 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 11. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises Provide name & Then select an appropriate name and location for the project: location 1. Provide a name (e.g. sessionapplicationCounter) and location for your new NetBeans project. 2. Click Next Server and settings Select the application server and related settings: 1. For this example, we'll choose GlassFish V2 2. Click Next If you'd like to Frameworks design your project For our test application, we want the ICEfaces and Facelets framework visually, you can 1. ICEFaces support: choose Visual Web Page 11 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 12. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 1. From the list of frameworks provided, choose ICEfaces. ICEfaces instead. As we don't really 2. Select the ICEfaces configuration option "Concurrent DOM have a complex View", “Verify Objects” and “validate XML”. visual requirement and we are focusing on Ajax Push, the ICEfaces framework is suitable for this tutorial. 3. Click the ICEfaces Libraries tab and select the ICEFaces Run- Time 1.8.1. Frameworks 1. From the list of frameworks provided, choose “Facelets”. 2. Facelets Page 12 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 13. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 2. Click the ICEfaces Libraries tab and select the Facelets ICEFaces Run-Time 1.8.1. 3. Click Finish Once you have completed the project wizard, you should have an initial project in NetBeans that looks like this: Page 13 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 14. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 6.2.2. Create the Managed Beans Our first task is to create the backing beans that Creating the will be used to hold the Managed Beans First we create a class file for our Counter: state of the counters. First 1. Right-click on Source Packages, select New, then select Java we'll start with a generic Create a New counter: Class... Counter Class 2. Type in a new class name (e.g. Counter). 3. Type in a new package name (e.g sun.com.tutorial). Page 14 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 15. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 4. Click Finish. Now we are going to provide the logic of our Implement the Counter bean. We'll use a Counter Class You can type or copy the following into the editing tab for the Counter.java simple integer member file: variable as the current package com.sun.tutorial; counter value, provide getter and setter methods /** for the value, and also * add a couple of methods * @author patricegoutin for incrementing and */ decrementing the value. The methods will be used import javax.faces.event.ActionEvent; as JSF actionListeners so they need to take an public class Counter { ActionEvent as a parameter. private int count; public Counter() { } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public void increment(ActionEvent event) { count++; } public void decrement(ActionEvent event) { count--; } } Your project should now look something like this: Create a New ApplicationCou Since multiple users can potentially access and modify the value of our nter class application-scoped counter, we need to create a version that guards against concurrent access. So we'll create an ApplicationCounter that extends our basic Counter: 1. Under Source Packages, right-click on the package, select New, then select Java Class... 2. Type in a new class name (e.g. ApplicationCounter). Page 15 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 16. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 3. Type in a new package name (e.g sun.com.tutorial). 4. Click Finish. You can type or copy the following Implement the package org.icefaces.tutorial; into the editing ApplicationCou import javax.faces.event.ActionEvent; tab for the ApplicationCounter. nter class java file: public class ApplicationCounter extends Counter { public ApplicationCounter() { } public synchronized void setCount(int count){ super.setCount(count); } public synchronized void increment(ActionEvent event) { super.increment(event); } public synchronized void decrement(ActionEvent event) { super.decrement(event); } } Your project should now look something like this : Define Managed To make use of our classes as JSF managed beans, we need to declare them in our Beans faces-config.xml file: 1. Under WEB-INF, open the faces-config.xml file for editing. 2. Switch to the XML view (rather than PageFlow). Page 16 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 17. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises To create the session-scoped bean: 1. Right-click in the editor and choose JavaServer Faces -> Add Managed Bean.. 2. Give the bean a unique name (e.g. sessionCounter). 3. Type or browse for the correct class (e.g. org.icefaces.tutorial.Counter). 4. Choose the correct scope. For this bean, 'session'. 5. Add a description (this is optional). 6. Click Add. Tocreate the application-scoped bean: 7. Right-click in the editor and choose JavaServer Faces -> Add Managed Bean.. 8. Give the bean a unique name (e.g. applicationCounter). 9. Type or browse for the correct class (e.g. org.icefaces.tutorial.ApplicationCounter). 10. Choose the correct scope. For this bean, 'application'. 11. Add a description (this is optional). 12. Click Add The faces-config.xml file should now look something like this : Page 17 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 18. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 6.2.3. Create the user interface Creating the Now we need to create our user interface that we can bind our bean values and Pages methods to. When the project was automatically created, the ICEfaces pluging created a couple of JSP files for us (forward.jsp and welcomeJSF.jsp). By adding the Facelets Support plugin, it created a couple of starting facelets pages (template.xhtml and template-client.xhtml). For this tutorial, we're going to use Facelets so we'll be concentrating on the .xhtml resources. With Facelets, the typical approach is to create a template (template.xhtml) that can be used to dynamically include content. Since this isn't designed to be a tutorial on Facelets, we'll simply describe the minium steps required to get our counters up and running. Configure To use Facelets with ICEfaces, there are a couple of configuration settings that we Facelets need to apply. ICEfaces uses it's own copy of the Facelets library which is included Support as part of the ICEfaces integration plugin. To ensure that the library is applied to the project: 1. Right-click on your project and choose Properties. 2. Choose Libraries from the Categories list. In either case, the complete set of libraries should look something like this: Page 18 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 19. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises When using Facelets with ICEfaces, the ICEfaces framework uses a custom JSF ViewHandler implementation. This must be specified in the faces-config.xml file: 3. Under WEB-INF, open the faces-config.xml file for editing. 4. Switch to the XML view (rather than PageFlow). 5. If necessary, add the D2DFaceletViewHandler to the application section of the configuration file by copying or typing the following: <application> <view-handler>com.icesoft.faces.facelets.D2DFaceletViewHandler</view-handler> </application> When you are done, the faces-config.xml file should look something like this. Note that it's possible for more than one ViewHandler to be configured. They are designed to delegate responsibilities appropriately. Edit the Facelet Pages By including the Facelets framework plugin when the project was initially created, two Facelet files are automatically created for you. We'll edit those files now: 1. Open up the template-client.xhtml file for editing. This file provides the viewable content based on the configuration described in the template (template.xhtml). 2. Add the namespace for the ICEfaces components to the html tag. We need to add this so that we can use ICEfaces components (e.g. ) in the page. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:ice="http://www.icesoft.com/icefaces/component"> 3. Change the title definition from: <ui:define name="title"> Facelets </ui:define> to <ui:define name="title"> Ajax Push Counter </ui:define> Page 19 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 20. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 4. Change the body definition from: <ui:define name="body"> Hello from the Facelets client template! </ui:define> to <ui:define name="body"> <ice:form> <ice:panelGrid columns="3"> <ice:outputText value="Application counter:" /> <ice:outputText value="#{applicationCounter.count}" /> <ice:panelGroup> <ice:commandButton value="-" actionListener="#{applicationCounter.decrement}"/> <ice:commandButton value="+" actionListener="#{applicationCounter.increment}"/> </ice:panelGroup> <ice:outputText value="Session counter:" /> <ice:outputText value="#{sessionCounter.count}" /> <ice:panelGroup> <ice:commandButton value="-" actionListener="#{sessionCounter.decrement}"/> <ice:commandButton value="+" actionListener="#{sessionCounter.increment}"/> </ice:panelGroup> </ice:panelGrid> </ice:form> </ui:define> The body content now provides a number of JSF components including a form, a panelGrid (for layout), outputText for showing the values of our counters, and commandButtons for modifying the values of the counters. The values and methods use the JSF expression language to bind to the backing beans we described in our faces-config.xml. Page 20 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 21. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 6.2.4. Running the Web Application without AjaxPush effect Preparation to Before running the application, you should ensure that the default URL that run NetBeans attempts to use for launching your browser is correct. By default, the ICEfaces project attempts to use the JSP page via the ICEfaces extension (welcomeJSF.iface). You should change this to use the default facelets page with the proper extension (template-client.iface). 1. Right-click on the project name (e.g. ajaxpush) and choose Properties. 2. Select the Run category. 3. Ensure that the Relative URL value points to your Facelets page and uses the virtual ICEfaces extension (e.g. template-client.iface). I am using port 18080 Running the If the tutorial is your main (or only) NetBeans project, then you should be able to Run Application Main Project, using either the green arrow button in the toolbar, the F6 key, or choosing Run -> Run Main Project from the menu. Your browser should automatically launch using the following URL: http://localhost:18080/ajaxpush/template-client.iface And the application should look something like this: Clicking the buttons allows you to increment and decrement the counter values. Opening a second browser shows the the application counter is shared, but the session counters are not. There are two very important things to note at this point: Page 21 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 22. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 1. You can't just open another window or tab of the same browser instance to do this because browser windows or tabs from the same browser will share sessions. So use two separate browsers (e.g. IE and Firefox) or use browsers on separate machines. 2. Since we haven't added Ajax Push features yet, the application-scoped counter doesn't update automatically. It only updates when you interact with the page. In the following section, we'll use the SessionRenderer API to add Ajax Push so that page updates are automatic. 6.2.5. Adding AjaxPush effect Adding Ajax There are basically two parts to using the SessionRenderer API. Push with SessionRendere • adding sessions to the group or groups we wish to render r • calling a render when something of interest changes for that group The SessionRenderer API supports this in a simple and straight-forward manner. public static void addCurrentSession(final String groupName) public static void removeCurrentSession(final String groupName) public static void render(final String groupName) Since we want everyone to be able to access the application-scoped counter, we're going to keep it simple and just add everyone to a single, global group. The easiest way to do this is to have the group membership logic in the constructor of the session-scoped bean. That way, when our session-scoped bean is created, it's added to a group. Since we only want to do this for our session-scoped counter, and not our application-scoped counter, we'll create another subclass of Counter that does this for us. Create a new 1. Under Source Packages, right-click on the package, select New, then SessionCounter select Java Class... class 2. Type in a new class name (e.g. SessionCounter). 3. Type in a new package name (e.g sun.com.tutorial). 4. Click Finish. Implement the The only thing we want to do differently in this counter is add the current session to SessionCounter our group. You can type or copy the following into the editing tab for the Counter.java class file: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.sun.tutorial; import com.icesoft.faces.async.render.SessionRenderer; /** * * @author patricegoutin */ public class SessionCounter extends Counter { public SessionCounter() { SessionRenderer.addCurrentSession("all"); } } For ICEfaces 1.7.1 Page 22 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 23. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises Your project should now look something like this: and 1.7.2, the SessionRenderer API was considered experimental so the package for it reflects this (org.icefaces.x.core.pu sh.SessionRenderer). For ICEfaces 1.8, the API is no longer considered experimental and resides with the rest of the Ajax Push APIs under com.icesoft.faces.asyn c.render. Adjust the Now that we have a new bean implementation for our session-scoped counter, we Managed Bean need to update the JSF descriptor to make use of it. Description Under WEB-INF, open the faces-config.xml file for editing. Switch to the XML view (rather than PageFlow). Change the class of the session-scoped bean from Counter to SessionCounter: <managed-bean> <description>A session-scoped counter.</description> <managed-bean-name>sessionCounter</managed-bean-name> <managed-bean-class>org.icefaces.tutorial.Counter</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> becomes <managed-bean> <description>A session-scoped counter.</description> <managed-bean-name>sessionCounter</managed-bean-name> <managed-bean-class>org.icefaces.tutorial.SessionCounter</managed-bean- class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> Your faces-config.xml file should now read like this: Using Our final step in adding Ajax Push features to our application counter requires Page 23 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 24. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises SessionRender modifications to the ApplicationCounter class. What we want is for updates to be for Ajax Push pushed out whenever there is a change. To do this, you simply need to call the render method of the SessionRenderer when a notable change occurs. public static void render(final String groupName) Type or copy the following changes to the ApplicationCounter class: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.sun.tutorial; /** * * @author patricegoutin */ import com.icesoft.faces.async.render.SessionRenderer; import javax.faces.event.ActionEvent; public class ApplicationCounter extends Counter { public ApplicationCounter() { } public synchronized void setCount(int count){ super.setCount(count); SessionRenderer.render("all"); } public synchronized void increment(ActionEvent event) { super.increment(event); SessionRenderer.render("all"); } public synchronized void decrement(ActionEvent event) { super.decrement(event); SessionRenderer.render("all"); } } So whenever a change is made to the application-scoped counter, we issue a request for a render to the group "all" which, in our case, means every active session. 6.2.6. Running the Web Application with AjaxPush effect I am using port 18080 Running with Now if you run the main project (using either the green arrow button in the toolbar, Ajax Push the F6 key, or choosing Run -> Run Main Project from the menu) and point two Running the browsers at the application: application Page 24 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 25. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises http://localhost:18080/ajaxpush/template-client.iface Clicking the buttons for the application-scoped counter should provide a richer experience. Updates from interaction in one browser that result in a change to the application counter are now pushed out to other active users. And there you have it. Using the SessionRenderer API is an easy and straightforward way to add collaborative features to your ICEfaces applications. You are done with the session/application web application development and test. Page 25 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 26. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 7. Creating a Portlet Session/Application Counter IceFaces with Ajax-Push 7.1. Overview The project we are going to build in this tutorial is a portlet that contains simple, adjustable counters. The counter values are displayed on the portlet and buttons are provided that can be used to increment or decrement the counter values. The portlet is deployed in Sun Glassfish WebSpace Server 10. The finished portlet looks like this: Note We assume you have already implemented successfully the web Session/Application Counter sample without and with AjaxPush effect. Through this tutorial, we will focus mainly on the portlet specific configuration steps to implement directly a portlet with AjaxPush effect. 7.2. Step-by-step 7.2.1. Create a portlet project with ICEFaces, Facelet and Portlet support Create a portlet 1. From the menus, choose File -> New Project... application 2. On the first screen of the New Project wizard, choose the Java project Web category and the Web Application project. Page 26 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 27. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 3. Click Next Provide name & Then select an appropriate name and location for the project: location 1. Provide a name (e.g. counterPortlet) and location for your new NetBeans project. 2. Click Next Server & Select the application server and related settings: settings 1. For this example, we'll choose “WebSpace 10.x (Project WebSynergy)” 2. Click Next Frameworks For our portlet application, we want the ICEfaces, Facelets and Portlet 2.0 1. ICEFaces framework support: 1. From the list of frameworks provided, choose “ICEfaces”. 2. Select the ICEfaces configuration option "Concurrent DOM View", “Verify Objects” & “Validate XML”. Page 27 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 28. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 3. Click the ICEfaces Libraries tab and select the ICEFaces Run- Time 1.8.1. 4. From the list of frameworks provided, choose “Facelets”. Frameworks 2. Facelets Page 28 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 29. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 5. Click the ICEfaces Libraries tab and select the Facelets ICEFaces Run-Time 1.8.1. Frameworks 3. Portlet 1. From the list of frameworks provided, choose “Portlet Support”. 2. Select the Portlet Version 2.0. Do not create any Portlet. 3. Click Finish Configure To use Facelets with ICEfaces, there are a couple of configuration settings that we Facelets need to apply. ICEfaces uses it's own copy of the Facelets library which is included Support as part of the ICEfaces integration plugin. To ensure that the library is applied to the project: 1. Right-click on your project and choose Properties. 2. Choose Libraries from the Categories list. In either case, the complete set of libraries should look something like this: Page 29 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 30. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 7.2.2. Create The Managed Beans Create the These steps have been described in the previous chapter. We just recall Managed Beans the source code of each Java classes to be developed and available in the com.sun.tutorial source package : As a short cut, you can simply copy the package “com.su..tutorial” from the sessionapplicationCounter developed in the previous chapter. Counter.java You can type or copy the following into the editing tab for the Counter.java file: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.sun.tutorial; /** * * @author patricegoutin */ import javax.faces.event.ActionEvent; public class Counter { private int count; Page 30 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 31. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises public Counter() { } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public void increment(ActionEvent event) { count++; } public void decrement(ActionEvent event) { count--; } } ApplicationCou You can type or copy the following into the editing tab for the Counter.java nter.java file: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.sun.tutorial; /** * * @author patricegoutin */ import com.icesoft.faces.async.render.SessionRenderer; import javax.faces.event.ActionEvent; public class ApplicationCounter extends Counter { public ApplicationCounter() { } public synchronized void setCount(int count){ super.setCount(count); SessionRenderer.render("all"); } public synchronized void increment(ActionEvent event) { super.increment(event); SessionRenderer.render("all"); } public synchronized void decrement(ActionEvent event) { super.decrement(event); SessionRenderer.render("all"); } } SessionCounter .java You can type or copy the following into the editing tab for the Counter.java file: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ Page 31 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 32. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises package com.sun.tutorial; import com.icesoft.faces.async.render.SessionRenderer; /** * * @author patricegoutin */ public class SessionCounter extends Counter { public SessionCounter() { SessionRenderer.addCurrentSession("all"); } } Define the To make use of our classes as JSF managed beans, we need to declare them in our Managed Beans faces-config.xml file: 1. Under WEB-INF, open the faces-config.xml file for editing. 2. Switch to the XML view (rather than PageFlow). To create the session-scoped bean: 3. Right-click in the editor and choose JavaServer Faces -> Add Managed Bean.. 4. Give the bean a unique name (e.g. sessionCounter). 5. Type or browse for the correct class (e.g. org.icefaces.tutorial.Counter). 6. Choose the correct scope. For this bean, 'session'. 7. Add a description (this is optional). 8. Click Add. To create the application-scoped bean: 9. Right-click in the editor and choose JavaServer Faces -> Add Managed Bean.. 10. Give the bean a unique name (e.g. applicationCounter). 11. Type or browse for the correct class (e.g. org.icefaces.tutorial.ApplicationCounter). 12. Choose the correct scope. For this bean, 'application'. 13. Add a description (this is optional). Page 32 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 33. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 14. Click Add The faces-config.xml file should now lock something kike this : 7.2.3. Creating the user interface Creating the Pages We will reuse the user interface from the previous chapter. The template-client.xtml looks like the following <?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:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:ice="http://www.icesoft.com/icefaces/component"> <body> This text above will not be displayed. <ui:composition template="/template.xhtml"> This text will not be displayed. <ui:define name="title"> Ajax Push Counter </ui:define> This text will also not be displayed. <ui:define name="body"> <ice:form> <ice:panelGrid columns="3"> <ice:outputText value="Application counter:" /> <ice:outputText value="#{applicationCounter.count}" /> <ice:panelGroup> <ice:commandButton value="-" actionListener="#{applicationCounter.decrement}"/> <ice:commandButton value="+" actionListener="#{applicationCounter.increment}"/> </ice:panelGroup> <ice:outputText value="Session counter:" /> <ice:outputText value="#{sessionCounter.count}" /> <ice:panelGroup> <ice:commandButton value="-" actionListener="#{sessionCounter.decrement}"/> <ice:commandButton value="+" actionListener="#{sessionCounter.increment}"/> </ice:panelGroup> Page 33 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 34. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises </ice:panelGrid> </ice:form> </ui:define> This text will not be displayed. </ui:composition> This text below will also not be displayed. </body> </html> 7.2.4. Configure the configuration files Configuration Files Now, it is required to review and adjust a couples of XML files mainly defined under “ Configuration Files”. This Facelet View Handler is not needed faces- config.xml Expand the project node “counterPortlet”, expand the Web pages node and because ICEfaces actually delegates to the then expand the WEB-INF node. standard FaceletsViewHandler. Double-click the file faces-config.xml for editing. Make sure the following tag from the <application> section ha been removed : <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> The portlet will be available in WebSpace liferay- display.xml Expand the project node “counterPortlet”, expand the Web pages node and Server under the category “User_Portlets” then expand the WEB-INF node. Double-click the fileliferay-display.xml for editing to be <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.2.0//EN" "http://www.liferay.com/dtd/liferay-display_5_2_0.dtd"> <display> <category name="User_Portlets"> <portlet id="counterPortlet">counterPortlet</portlet> </category> </display> liferay- Expand the project node “counterPortlet”, expand the Web pages node and portlet.xml then expand the WEB-INF node. Double-click the fileliferay-portlet.xml for editing and add the following portlet definition section : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 5.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_5_2_0.dtd"> <liferay-portlet-app> <portlet> <portlet-name>counterPortlet</portlet-name> <instanceable>true</instanceable> <render-weight>1</render-weight> <ajaxable>true</ajaxable> </portlet> <role-mapper> <role-name>administrator</role-name> <role-link>Administrator</role-link> </role-mapper> <role-mapper> Page 34 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 35. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises <role-name>guest</role-name> <role-link>Guest</role-link> </role-mapper> <role-mapper> <role-name>power-user</role-name> <role-link>Power User</role-link> </role-mapper> <role-mapper> <role-name>user</role-name> <role-link>User</role-link> </role-mapper> </liferay-portlet-app> portlet.xml Expand the project node “counterPortlet”, expand the Web pages node and then expand the WEB-INF node. Double-click the portlet.xml for editing and add the following portlet definition section : <?xml version='1.0' encoding='UTF-8' ?> <portlet-app xmlns='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd' version='2.0'> <portlet> <description>counterPortlet</description> <portlet-name>counterPortlet</portlet-name> <display-name>counterPortlet</display-name> <portlet-class>com.icesoft.faces.webapp.http.portlet.MainPortlet</portlet- class> <init-param> <name>com.icesoft.faces.VIEW</name> <value>/template-client.iface</value> </init-param> <expiration-cache>-1</expiration-cache> <supports> <mime-type>text/html</mime-type> <portlet-mode>VIEW</portlet-mode> <portlet-mode>EDIT</portlet-mode> <portlet-mode>HELP</portlet-mode> </supports> <portlet-info> <title>counterPortlet</title> <short-title>counterPortlet</short-title> </portlet-info> </portlet> </portlet-app> web.xml Expand the project node “counterPortlet”, expand the Web pages node and then expand the WEB-INF node. Double-click the web.xml for editing and add the following context-param definition tag : <context-param> <param-name>com.icesoft.faces.compressResources</param-name> <param-value>false</param-value> </context-param> You can also remove the following section : <welcome-file-list> Page 35 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 36. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises <welcome-file>index.html</welcome-file> </welcome-file-list> Define the Right-click on the portlet project “counterPortlet” and select Properties relative URL to be used Click the “Run” node. Set the Relative URL to be “/template-client.iface” Clean and Build Right-click on the portlet project “counterPortlet” and select Clean and Build Deploy Right-click on the portlet project “counterPortlet” and select Deploy 7.2.5. Running the portlet with AjaxPush effect Of course, you can install the counterPortlet in your Running the own portal page. portlet 1. Launch a browser with the following URL : http://localhost:8080 Page 36 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 37. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises 2. Click on “Express login as admin” for the “Base Sample Users” 3. Select a page where you want to add the counterPortlet (e.g enterprisespace/privates pages). 4. Right click on “Welcome Admin User!” and select “Add Application' and then expand “User_Portlets” 5. Drag on drop the counterPortlet on you page. 6. Launch a second browser with the following URL : http://localhost:8080 DO NOT USE THE SAME BROWSER 7. Click on “Express login as admin” for the “Base Sample Users” and open the same page 8. Clicking the buttons for the application-scoped counter should provide a richer experience. Updates from interaction in one browser that result in a change to the application counter are now pushed out to other active users. And there you have it. Using the SessionRenderer API is an easy and straightforward way to add collaborative features to your ICEfaces applications. You are done with the counterPortlet application development and test. Page 37 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009
  • 38. WebSpace Server 10 labs Java Marcher Global Software Sales Sun Glasfish WebSpace 10 Server Hands On Exercises Page 38 of 38 Ref: PG/GTE-2009 Copyright © Sun Microsystems, Inc, 2009 Issue Date : October 2009

×