December 22, 2014

SyntaxHighlighter

Supported styling.

https://github.com/rwz/SyntaxHighlighter/blob/master/demos/autoloader.html

Java EE 7 Maven Dependency

In my previous blog about Java EE 6 Maven Dependency a wrote about the crippled javaee-api maven dependency in maven central.

That is finally fixed in EE 7.

<dependency>
    <groupid>javax</groupid>
    <artifactid>javaee-api</artifactid>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

See also Essential Maven POM For JavaEE 7

December 21, 2014

Java EE 6 Maven Dependency

The EE 6 classes are available in maven central.

<dependency>
    <groupid>javax</groupid>
    <artifactid>javaee-api</artifactid>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

But when running unit test against them you receive the below error. Thats because they do not contain implementation classes, only api class.

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/LockModeType
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)

So in practice, the dependecy is in real life unusable. Instead you must use api classes from specific vendors like JBoss.

<dependency>
    <groupid>org.jboss.spec</groupid>
    <artifactid>jboss-javaee-6.0</artifactid>
    <version>1.0.0.Final</version>
    <type>pom</type>
    <scope>provided</scope>
</dependency>

Java EE 7 Deployment Descriptors

Deployment descriptors are like configuration files.

The below sites summaries all xml schemas defintions for all EE 7 deployment descriptors.

http://antoniogoncalves.org/2013/06/04/java-ee-7-deployment-descriptors/

Java EE 6 Interceptors 1.0 (JSR318)

Introduction

Interceptor was in EE 5 introduced in EJB 3.0, see EJB3 Interceptors javax.ejb.AroundInvoke. In EE 6 that was taken out and made generic into Interceptors (JSR318), package javax.interceptor.*.

You can call Interceptor in EE 6 in two way:

  • By annotating method or class with @javax.interceptor.Interceptors(MyInterceptor.class)
  • Create custom Annotation with Annotation @javax.interceptor.InterceptorBinding and annotate with that you method or class.

Example

Your custom Interceptor.

package se.magnuskkarlsson.example.interceptor;

import java.io.Serializable;

import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import org.apache.log4j.Logger;

@Audit
@Interceptor
public class AuditInterceptor implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final Logger log = Logger.getLogger(AuditInterceptor.class);

    @AroundInvoke
    public Object intercept(InvocationContext ctx) throws Exception {
        log.info("### Entering method: " + ctx.getMethod().getName()
                + " in class " + ctx.getMethod().getDeclaringClass().getName());

        return ctx.proceed();
    }
}

Interception in a POJO.

package se.magnuskkarlsson.example.interceptor;

import javax.interceptor.Interceptors;

public class DemoPOJO {

    @Interceptors(AuditInterceptor.class)
    public String sayHello() {
        return "Hello POJO";
    }
}

Interception in a Stateless Session Bean.

package se.magnuskkarlsson.example.interceptor;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

@Stateless
public class DemoSLSB {

    @Interceptors(AuditInterceptor.class)
    public String sayHello() {
        return "Hello SLSB";
    }
}

beans.xml located for war in WEB-INF/.

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">

</beans>

To use the Interceptor you must let weld create the intercepted class, otherwise will weld never knew about the interceptor and hence will the interceptor never work.

Example usage from a Servlet.

package se.magnuskkarlsson.example.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/audit")
public class DemoServlet extends javax.servlet.http.HttpServlet {

    private static final long serialVersionUID = 1L;

    @Inject
    private DemoPOJO pojo;

    @EJB
    private DemoSLSB slsb;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            out.println("<html><body>");
            out.println("<h1>" + pojo.sayHello() + "</h1>");
            out.println("<h1>" + slsb.sayHello() + "</h1>");
            out.println("</html></html>");
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}

Limitation

You cannot use Interceptor directly in servlets, see https://java.net/projects/servlet-spec/lists/jsr340-experts/archive/2012-02/message/0.

Taking Snapshot in Ubuntu

When taking snapshot in Ubuntu you could always use the predefined keyboard shortcuts, see https://help.ubuntu.com/community/KeyboardShortcuts#Desktop_shortcuts

A better tool is to use the gnome-screenshot tool.

You can install via `sudo apt-get install gnome-screenshot`


How to Manage Properties Files in JBoss Module

From JBoss AS 7 and EAP 6 forth, classloading have changed and all jar dependencies are packaged and loaded as jboss modules.

Besides dependency jar in a module you also might need to be able to configure it for different server environment. The best way and maybe the natural way, is to make use of a java properties file.

This is a good practice, now you can RPM package your custom module. This is how Red Hat do with JBoss EAP 6. With RPM packing you have now the capability of versioning your module and to centrally install it from, e.g. Red Hat Satellite. And finally centrally configure the property file from, e.g. Satellite 6 via Puppet.

But there is classloading gotchas. You need to add <resource-root path="."/> to you module.xml.

See https://developer.jboss.org/wiki/HowToPutAnExternalFileInTheClasspath for longer description.

List JNDI Tree Entries with JBoss CLI

To list all JNDI entries with JBoss CLI.

First start JBoss CLI.

$ ./jboss-cli.sh --connect

Then execute.

[standalone@localhost:9999 /] /subsystem=naming:jndi-view

How to Deploy Sources and Javadoc Jar

When deploying a snapshot release or if you by some other reason want to generate sources or javadoc jar, you could always configure maven-source-plugin and maven-javadoc-plugin and hook them in maven life cycle.

But that is not necessary in maven uber pom is that already defined for release. You can reuse that by adding -DperformRelease=true

mvn -DperformRelease=true [goals]

Different example of usage:

Local build with sources and javadoc jar generation

mvn -DperformRelease=true clean install

Deploy target. Use only with snapshot version. Then deploy means snapshot publishing to defined snapshot repository.

mvn -DperformRelease=true clean install deploy

Overview EE and JBoss EE Compliance

Overview of EE evolution

The capabilities and what is supported out of the box in EE, is increasing from next version to next version. The drive behind the new feature are:

  • Common task are finally included in standard EE.
  • Better maven dependency management.
  • Better test support (FINALLY!), but in this area, more can be done.

[http://en.wikipedia.org/wiki/Java_EE_version_history]

Summary of JBoss AS, EAP and new Wildfly EE Compliance

EE 4 - JBoss AS 4.X, JBoss EAP 4 *

EE 5 - JBoss AS 5.1, JBoss EAP 5 *

EE 6 - JBoss AS 7.1, JBoss EAP 6 *

EE 7 - Wildfly 8.x, JBoss EAP 7 * (release date Q2-Q3? 2015)

*) The EAP is forked from AS/Wildfly version left to it, but with much more quality (tested, patched, security compliance testing, security patched), documentation and with support.

Reference