March 17, 2017

Subscription Management in RHEL 7

Register first, then manually add a subscription.

# subscription-manager register

List all available subscription.

# subscription-manager list --available --all

Attach a specific subscription.

# subscription-manager attach --pool=XXXXXXXXXXXXXX

Verify that you now have access to subscribed rpm repos.

# yum repolist

Debugging HTTP Request in JBoss EAP 7

<subsystem xmlns="urn:jboss:domain:undertow:3.1">
    ...
    <server name="default-server">
        ...
        <host name="default-host" alias="localhost">
            ...
            <filter-ref name="request-dumper"/>
        </host>
    </server>
    ...
    <filters>
        ...
        <filter name="request-dumper" class-name="io.undertow.server.handlers.RequestDumpingHandler" module="io.undertow.core"/>
    </filters>
</subsystem>

How to Set Hostname on RHEL 7

To change permanent:

# hostnamectl set-hostname openam.example.com

Reboot and verify.

# hostnamectl status

January 23, 2017

Elliptic Curve Keys with OpenSSL

List pre-defined curves in OpenSSL:

Generate private key:

Generate public key:

Reference: https://wiki.openssl.org/index.php/Command_Line_Elliptic_Curve_Operations

January 12, 2017

Jackson Standard JSON library for Java

Introduction

Jackson is build up by the following modules:

  • Streaming: "jackson-core"
  • Annotations: "jackson-annotations"
  • Databind: "jackson-databind" implements data-binding (and object serialization)

Maven

Jackson JSON Parser and Generator

To do JSON parsing and generation you only need one class com.fasterxml.jackson.databind.ObjectMapper.

To parse a json file.

And to generate json.

Different ObjectMapper#readValue(...) and ObjectMapper#writeValue(...)

Different parse methods:

  • com.fasterxml.jackson.databind.ObjectMapper.readValue(File, Class)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(URL, Class)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(String, Class)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(Reader, Class)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(InputStream, Class)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(byte[], Class)
  • com.fasterxml.jackson.databind.ObjectMapper.readValue(DataInput, Class)

Different generation methods:

  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(File, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(OutputStream, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(DataOutput, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValue(Writer, Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(Object)
  • com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(Object)

Configure ObjectMapper

January 6, 2017

BDD Testing with Cucumber, Java and JUnit

Introduction

BDD (Behaviour-Driven Development) is about designing code by defining your application in terms of behavior.

You start by gathering Tester, Developer and Product Owner (The Three Amigos) and define User Stories. These User Stories are given a Feature name and each Feature is broken down into a Scenario with Steps defines with the keywords: Given, When and Then

Cucumber

Now in Cucumber you write your User Stories (=Feature) in a language called Gherkin. Example:


Feature: Cash withdrawal 

Scenario: Withdrawal from an account in credit 
    Given I have deposited $100.00 in my account 
    When I withdraw $20 
    Then $20 should be dispensed 

Maven Dependency

Cucumber Code Structure

All code (production code, test code and gherkin code) must be placed in the same catalog structure.

  • Production Code - src/main/java/se/magnuskkarlsson/examples/cucumber/Account.java
  • Test Code - src/test/java/se/magnuskkarlsson/examples/cucumber/AccountTest.java
  • Test Code - src/test/java/se/magnuskkarlsson/examples/cucumber/AccountSteps.java
  • Gherkin Code - src/test/resources/se/magnuskkarlsson/examples/cucumber/cash_withdrawal.feature

Cucumber Test Code

We need two classes: one JUnit wrapper class and one Cucumber class that implements the steps in your Gherkin Code.

Verify

You can now either run you JUnit test class AccountTest inside your IDE or run complete test from command line as usual in maven: mvn clean install.

January 5, 2017

JDK Logger/Java Logging API

Background

Since JDK 1.4 there is a default Logging API in Java. The entire Logging API is contained in the package java.util.logging.

Reference: https://docs.oracle.com/javase/8/docs/technotes/guides/logging/overview.html

Usage

Default Logging Configuration

Java comes with a default logging configuration file, that only contains a ConsoleHandler and writes to standard error.

$JAVA_HOME/lib/logging.properties

Configuration

To change default configuration, create a new configuration file and add a system property java.util.logging.config.file to you java process, which point to yours configuration file. Example:

The available handlers/appenders and their configuration are:

Log Levels

Java Logging API has the following levels: OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, ALL

You define a global log level with .level, then if you need specific configuration you add: <package>[.class].level=<level>

NOTE: ConsoleHandler is special, for that you need to set ConsoleHandler.level for others you don't!

Log Formatter

There exists only two formatter and you typically want to use the SimpleFormatter.

NOTE: Most handlers uses the SimpleFormatter as default, but some do not. For them you need to set the formatter configuration.

Configure java.util.logging.SimpleFormatter

The default format is quite odd, so you want to change that.

The configuration is done with java.util.logging.SimpleFormatter.format. And the value is for the java call in java.util.logging.SimpleFormatter.format(LogRecord)

To understand the java.lang.String.format(String, Object...) syntax, read https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax.

Configuration in Java EE 6

Background

Dependency Injection has come strong in Java EE 6, which is widely influenced from Spring Framework.

You can use the same pattern to handle configuration.

Code

And to use it

And finally you need to add and empty WEB-INF/beans.xml to your achieve to make CDI work.

January 4, 2017

Java 7 - Implement equals() and hashCode() with java.util.Objects

When using JPA you should implement equals() and hashCode() if you

  • Intend to put instances of persistent classes in a Set.
  • Intend to use reattachment of detached instances.

For JDK 7 and above, you can use the new java.util.Objects class to generate the equals and hash code values.

Test code

January 3, 2017

MySQL – Backup and Restore

Backup

Restore

Internationalization (i18n) with ResourceBundle and Properties

To externalize local messages, Java have the java.util.ResourceBundle class.

Create a properties file for the default locale. Then create a new properties for each locale with the suffix _<language>_<COUNTRY>, in the same way you initialize java.util.Locale.Locale(String, String).

Example

To load the localized text, use java.util.ResourceBundle.getString(String).

Example

messages.properties

messages_sv_SE.properties

And the Java code to load them.

Output

Java 8 - Base64

Java 8 now supports java.util.Base64.

  • Base64.getEncoder()/Base64.getDecoder() - Output is mapped to a set of characters lying in A-Za-z0-9+/.
  • Base64.getUrlEncoder()/Base64.getUrlDecoder() - Output is mapped to set of characters lying in A-Za-z0-9+_. Output is URL and filename safe.
  • Base64.getMimeEncoder()/Base64.getMimeDecoder() - Simple format, but represented in lines of 76 characters and seperated with '\r\n'.

Example

Output

Java 8 - Streams

What is Stream?

A sequence of elements supporting sequential and parallel aggregate operations.

How to create?

The java.util.Collection interface has two methods:

  • stream()
  • parallelStream() - supports parallel computing.

How to use?

The java.util.stream.Stream interface has several methods, here are the most common used.

filter

map

sorted

forEach

collect

java.util.stream.Collectors

Collectors are used of collecting result from Stream operations, but there are several useful methods in the java.util.stream.Collectors interface. Here some of them.

toList()/toSet()

groupingBy

January 2, 2017

Java 8 - Lambda Expressions

Syntax

  • Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
  • Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
  • Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
  • Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

Examples

Old inner class

With lambda expression

Old inner class

With lambda expression