June 27, 2017

Postback Lifecycle in JSF 2.0 (EE 6)

"The request-response lifecycle handles two kinds of requests: initial requests and postbacks. An initial request occurs when a user makes a request for a page for the first time. A postback request occurs when a user submits the form contained on a page that was previously loaded into the browser as a result of executing an initial request."

Reference: Oracle Java EE 6 Tutorial The Lifecycle of a JavaServer Faces Application

Getting Started with JSF 2.0 (EE 6)

Introduction

JSF 2.0 is the standard Web Framework that ships with Java EE 6. Here I will build a simple JSF web app to get you started with the build stones.

Maven

With starting with Java EE 6, there is a ONE dependency for all Java EE, namely javax:javaee-api:[6.0|7.0]. A basic pom.xml file for Java EE 6 is.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>se.magnuskkarlsson.examples</groupId>
    <artifactId>example-jsf20</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
    </build>
</project>

Deployment Descriptors

Starting with EE 6 the web.xml is no longer compulsory, so here we will skip it.

A new feature in JSF 2.0, is that you do not need to declare navigation in the faces-config.xml, but you need to declare. That is the same thing with CDI. To enable CDI you need to have beans.xml. So we have to deployment descriptors.

  • WEB-INF/faces-config.xml
  • WEB-INF/beans.xml

Another feature in JSF 2.0, is that you can use ordinary CDI annotation instead of JSF specific. This makes one thing less to remember.

So instead of @javax.faces.bean.ManagedBean we can use @javax.inject.Named.

And instead of @javax.faces.bean.RequestScoped we can use @javax.enterprise.context.RequestScoped.

So a simple request scope baking bean for JSF is.

package se.magnuskkarlsson.example.jsf;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
// Shorthand for @Named and @RequestScoped is @javax.enterprise.inject.Model
public class UserBean {

    private String name;

    public String getMessage() {
        return (name != null) ? "Hello " + name : null;
    }

    public String getName() {
        return name;
    }

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

}

And finally our simple hello JSF page. Notice the commandButton and action, it points to JSF page hello, i.e. to the page itself.

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>Example JSF 2.0 Hello</title>
</h:head>
<h:body>
    <h:form>
        <h:outputLabel value="What is your name?" for="name" />
        <h:inputText id="name" value="${userBean.name}" />
        <h:commandButton value="Submit" action="hello" />
    </h:form>
    <p>
        <h:outputText value="${userBean.message}"
            rendered="${not empty userBean.message}" />
    </p>
</h:body>
</html>

Now build and deploy it to e.g. JBoss EAP 6, the app is accessible from either

http://localhost:8080/example-jsf20-1.0.0-SNAPSHOT/hello.jsf

http://localhost:8080/example-jsf20-1.0.0-SNAPSHOT/faces/hello.xhtml

June 17, 2017

JSF 2.0 Facelets Tag Libraries

Tag Library

"JSP technology is considered to be a deprecated presentation technology for JavaServer Faces. Facelets is a part of the JavaServer Faces specification and also the preferred presentation technology for building JavaServer Faces technology-based applications."

Tag Library URI Prefix Example Contents
JavaServer Faces HTML Tag Library http://java.sun.com/jsf/html h: h:head
h:body
h:outputText
h:inputText
JavaServer Faces component tags for all UIComponent objects
JavaServer Faces Core Tag Library http://java.sun.com/jsf/core f: f:actionListener
f:attribute
Tags for JavaServer Faces custom actions that are independent of any particular render kit
JavaServer Faces Facelets Tag Library http://java.sun.com/jsf/facelets ui: ui:component
ui:insert
Tags for templating
JSTL Core Tag Library http://java.sun.com/jsp/jstl/core c: c:forEach
c:catch
JSTL 1.2 Core Tags
JSTL Functions Tag Library http://java.sun.com/jsp/jstl/functions fn: fn:toUpperCase
fn:toLowerCase
JSTL 1.2 Functions Tags

Reference: Oracle Java EE 6 Tutorial: What Is Facelets?

Facelet Template

<?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:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:c="http://java.sun.com/jsp/jstl/core"
 xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <h:outputStylesheet library="css" name="default.css" />
 <title>Facelets Template</title>
</h:head>
<h:body>

 <h1>Hello</h1>

</h:body>
</html>

May 28, 2017

Roadmap Apache ActiveMQ, HornetQ and Apache ActiveMQ Artemis

HornetQ was the built in Message Broker in JBoss EAP 6.

Red Hat bought FuseSource in 2012, the company behind ActiveMQ commercial support, and rebranded the product JBoss A-MQ server. This left Red Hat with two competing Message Brokers.

In 2014 Red Hat and the HornetQ community decided to join efforts with the ActiveMQ community and the Apache ActiveMQ Artemis was created.

The current Red Hat JBoss A-MQ releases, at the time of 7.0.0, was still based on the original ActiveMQ project. It is expected a future release will be based on the Artemis project. This future release will feature the external appearance of the current A-MQ product (administration interfaces based on Fabric, protocol supported, and native client libraries) but with many of the internals from HornetQ (asynchronous I/O engine and high performance journaled disk storage).

The JBoss EAP 7 is shipped with the Apache ActiveMQ Artemis.

May 18, 2017

NamingException in RPM Installed Tomcat 7 in RHEL 7 and CentoIOS 7

If you are using datasource in rpm installed tomcat 7 on RHEL 7 and CentOS and do not specify factory, you will get a NamingException.

For details see https://bugzilla.redhat.com/show_bug.cgi?id=819087.

<Context>

    ...
    <Resource name="jdbc/ReviewDb" auth="Container" type="javax.sql.DataSource"
              factory="org.apache.commons.dbcp.BasicDataSourceFactory"
              driverClassName="org.postgresql.Driver"
              url="jdbc:postgresql://dbhost:5432/reviewdb"
              username="gerrit2" password="******" maxActive="20"
              maxIdle="10" maxWait="-1"/>
</Context>
javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourc
eFactory]
        at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:119)
        at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:842)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
        at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:117)
        at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:71)
        at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:34)
        at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:138)
        at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:110)
        at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:82)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:347)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:724)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
Caused by: java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:115)
        ... 22 more

How to Generate Elliptic Curve Keys in Java 8

// Supported Providers in Java 8
for (Provider provider : Security.getProviders()) {
    System.out.println("Provider : " + provider);
}

// Supported Algorithm in Java: "DiffieHellman", "DSA", "RSA" and "EC"
// https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "SunEC");
// SecureRandom.getInstanceStrong() (Java 8) is the Recommended way to use initialize random,
// it chooses automatically the strongest algorithm for your OS
keyGen.initialize(256, SecureRandom.getInstanceStrong());
KeyPair kp = keyGen.genKeyPair();
PrivateKey privateKey = kp.getPrivate();
PublicKey publicKey = kp.getPublic();
System.out.println("PrivateKey : " + privateKey);
System.out.println("PublicKey : " + publicKey);
// the key in its primary encoding format
byte[] publicKeyInfo = publicKey.getEncoded();
c