Review #1 about the “JavaScript Mobile Application Development” Book

Attached below the review of Zubin Wadia (the CEO & Co-Founder of CiviGuard, Inc, CEO & Co-Founder of RedRock IT Solutions, and Chief Technology Officer at ImageWork Technologies) about the “JavaScript Mobile Application Development” book:

JavaScript Mobile Application Development Book

JavaScript Mobile Application Development Book

A fine guide for web and native developers alike!
“Fabulous coverage of a powerful and mature cross-platform library. The book goes through the basics over the first five chapters, making sure native app developers aren’t alienated by the diaspora of HTML5/CSS3 web standards out there. A web developer will find that the book truly gets into its stride via the 5th, 6th, 7th and 8th chapters – which cover advanced Cordova API calls, platform specific code for iOS/Android/WinPhone, unit testing (critical) and finally a “Mega App” that puts your knowledge through some practical pacing.”

Reference:

http://www.amazon.com/review/R33GUDCXH5787J

The book in Amazon:

http://www.amazon.com/JavaScript-Mobile-Application-Development-Hazem/dp/1783554177/

JSF 2.3 Part2, Using @inject for FacesContext

One of the wonderful features of the upcoming JSF 2.3 is the ability to inject many JSF objects such as UIViewRoot, ViewMap, ApplicationMap, ExternalContext and FacesContext. In this post, I will show you how to simply inject FacesContext without having to use the old way FacesContext.getCurrentInstance().

Continuing working on the sample that was introduced in the previous post, let’s modify User managed bean to get FacesContext in order to display an information message to the user once the operation is done. The following code snippet shows the modified User managed bean.

package beans;

import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class User implements Serializable {
    private static final long serialVersionUID = 4629817047379532658L;
    private static final String INFO_MESSAGE = "Operation is done ...";
        
    private String name;
    
    @Inject
    FacesContext facesContext;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String greet() {
        facesContext.addMessage("form1", new FacesMessage(FacesMessage.SEVERITY_INFO, INFO_MESSAGE, INFO_MESSAGE));
        
        return null;
    }
}

As shown, all what you need to inject FacesContext in your managed bean is to annotate its declared instance with @inject annotation.

Running Application on GlassFish 4.1

In order to run our JSF application this time on GlassFish, we need to build Mojarra jars for GlassFish and then configure them on the server as follows:

  1. First of all, you need to make sure that you have both SVN and Apache Ant installed and configured in your operating system.
  2. Check out Mojarra 2.3 source code using the following SVN command:
    svn checkout https://svn.java.net/svn/mojarra~svn/trunk

    In order to build the JSF Mojarra jars for GlassFish then create a new file called build.properties under trunk. Then copy build.properties.glassfish file (which is located directly under trunk) content into it. Do not forget to set jsf.build.home property to the directory in which the sources were checked out into as follows for example.

    # --------------------------------------------------
    # Set this to the directory in which the sources
    # were checked out into
    # --------------------------------------------------
    jsf.build.home=/Users/hazems/projects/mojarra23/trunk
  3. CD to the jsf.build.home path, and then execute the following ant command:
    ant clean main
  4. After the successful execution of the previous command, you can get Mojarra jars from:
    1. jsf-api-intermediate.jar from trunk/jsf-api/build/lib directory.
    2. javax.faces.jar from trunk/jsf-ri/build/lib directory.
  5. Finally, overwrite the default javax.faces.jar file under glassfish-4.1/glassfish/modules with the new built javax.faces.jar file, and deploy our JSF application to GlassFish. After running our JSF application, you can see the information message after clicking "Greet" button as shown below.
    Screen Shot 2014-12-21 at 4.03.07 AM

Getting the JSF sample source code:

The sample source code is available in GitHub:
https://github.com/hazems/jsf2.3-work/tree/master/sampleJSF2

JSF 2.3 Part1, Getting Started

It is really not so early to start working with the upcoming JSF 2.3 and testing it. In this post, I will show you how to get started in JSF 2.3 by building JSF (Mojarra) jars from its source then use them for your JSF 2.3 application which will run in Tomcat 7.

Building JSF Jars from SVN:

  1. First of all, you need to make sure that you have both SVN and Apache Ant installed and configured in your operating system.
  2. Check out the source code using the following SVN command:
    svn checkout https://svn.java.net/svn/mojarra~svn/trunk
    
  3. In order to build the JSF Mojarra jars for Apache Tomcat then create a new file called build.properties under trunk. Then copy build.properties.tomcat file (which is located directly under trunk) content into it. Do not forget to set jsf.build.home property to the directory in which the sources were checked out into as follows for example.
    # --------------------------------------------------
    # Set this to the directory in which the sources
    # were checked out into
    # --------------------------------------------------
    jsf.build.home=/Users/hazems/projects/mojarra23/trunk
    
  4. CD to the jsf.build.home path, and then execute the following ant command:
    ant clean main
    
  5. After the successful execution of the previous command, you can get Mojarra jars from:
    1. jsf-api-intermediate.jar from trunk/jsf-api/build/lib directory.
    2. javax.faces.jar from trunk/jsf-ri/build/lib directory.

Configuring your first JSF 2.3 application:

Simply all what you need to work with JSF 2.3 in Tomcat 7 is to do the following:

  • Place the two Mojarra jars in step 4 in your web application WEB-INF/lib folder.
  • Set the JSF version in the faces-config.xml to 2.3 as follows.
    <?xml version='1.0' encoding='UTF-8'?>
    <faces-config version="2.3"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_3.xsd"> 
    </faces-config>
    
  • Then now, you can write your JSF application and it will be powered by the upcoming JSF 2.3. I made a very basic JSF 2.3 sample which you can reach its code in the following GitHub URL:
    https://github.com/hazems/jsf2.3-work/tree/master/sampleJSF1
  • Screen Shot 2014-12-20 at 1.07.36 AM

    In my next JSF 2.3 posts, I will show you some of the JSF 2.3 implemented features “so far” in order to allow you to start working with them so stay tuned.

    Tip #4: Storing Media Files Properly in Cordova iOS apps

    In order to avoid being surprised by the loss of your recorded audio file in iOS, you should be aware of how to properly record and store a media file in Cordova iOS.
    Generally, in order to record an audio file in Apache Cordova, you can use the Cordova Media object as follows:

    var recordingMedia = new Media(mediaFilePath, recordingSuccess, recordingError);
    
    // Start Recording Audio
    recordingMedia.startRecord();
    
    // Stop Recording Audio
    recordingMedia.stopRecord();
    
    // Release Media resource
    recordingMedia.release();
    

    As shown in the code above, in order to record an audio file in Apache Cordova, you need to do the following:

    1. Create a Media object (recordingMedia) and specify mediaFilePath (the path of the audio file), recordingSuccess (the success callback which will be called if the media operation succeeds), recordingError (the error callback which will be called if the media operation fails).
    2. Start recording the audio by calling recordingMedia.startRecord().
    3. After completing the audio recording, call recordingMedia.stopRecord() and then release the used Media object by calling recordingMedia.release()

    In iOS, if you set mediaFilePath to the audio file name only without specifying any path (e.g "test.wav"), you may be surprised to find your audio file stored under the iOS app’s tmp directory (which is located under the iOS app’s sandbox directory). It is very important to be aware that the iOS app’s tmp directory content can be deleted automatically by iOS at anytime by iOS.

    In order to avoid losing your app’s recorded files, just place the "documents://" prefix before the audio file name as follows.

    var recordingMedia = new Media("documents://test.wav", recordingSuccess, recordingError);
    

    Doing this will make the recorded audio file stored under the iOS app’s Documents directory. The iOS app’s Documents directory is located under the app’s sandbox directory and is suitable for storing the app files.

    Reference:

    “JavaScript Mobile Application Development” Book:

    Tip #3: Implementing the Back button behavior for Android and Windows Phone 8 Apps

    If you use Apache Cordova for developing your Android and Windows Phone 8 mobile apps, you may need to exit the app when the user clicks on the back button on the app’s home page, and to navigate back when the user clicks the back button and the current page is not the home page.

    Note that back button usually exists in Android and Windows Phone 8 devices.

    In order to do implement this behavior, you can implement the “backbutton” event handler after the “deviceready” event is triggered. This is an example to implement this in jQuery mobile.

        var homePage = "appHome";
       
        //Handle back buttons decently for Android and Windows Phone 8 ...
        function onDeviceReady() {
            document.addEventListener("backbutton", function(e){
                if ($.mobile.activePage.is('#' + homePage)){
                    e.preventDefault();
                    navigator.app.exitApp();
                } else {
                    history.back();
                }
            }, false);
        }
    
        $(document).ready(function() {
            document.addEventListener("deviceready", onDeviceReady, false); 
        });
    

    Using navigator.app.exitApp(), you can exit your mobile app when the app user is in the home page. And using history.back(), you can navigate back to the previous page when the app user is in other pages.

    Reference:

    “JavaScript Mobile Application Development” Book: