14 November 2013

Streaming takes over internet

The Sandvine report on the first half of 2013 reports 40% of the fixed access downstream traffic entertainment streaming. YouTube accounts for half of this traffic.

In the US, entertainment already takes up 70% of fixed downstream traffic. Here, half of the entertainment downstream traffic is NetFlix, while YouTube accounts for another 25% of entertainment traffic.

12 November 2013

StartWebLogic: \xxx was unexpected at this time

Just installed weblogic 12c with an initial domain on windows.
When running startweblogic in the domain i get an error
\xxx was unexpected at this time 

WebLogic seems to stumble over some stuff in my CLASSPATH. Mine 
has jars for the Belgium identity Card in it (others had a problem with QuickTime: QTJava.zip).

Easiest solution is the cleean my classpath, but then other applications will have problems.

After the SETLOCAL line in the startWebLogic.cmd and stopWeblogic.cmd in my domain, I added a line

Now it starts fine. Updating all these scripts is not the ideal solution however.

6 November 2013

Java 7 NIO.2 file handling (updated)

Java 7 contains a newer set of IO API's. This is an extension of the NIO (New Input/Output) API introduced in Java 1.4. The extenson is is called NIO.2 and specified in JSR 203. NIO.2 contains
  • a new API for handling filesystems and files
  • an API for asynchronous  I/O on both sockets and files
  • additional features on sockets ( binding, multicast datagrams...)


In this article we will discuss the file handling API. All the classes for this functionality are in the new java.nio.file package.

The Path class

This is the new class  representing a file location (replacing corresponding methodsd in java.io.File).
The Paths (plural!) class contains static utility methods that return a Path object:
import java.nio.file.Path;
import java.nio.file.Paths; 

// p1: relative Path to a file
Path p1 = Paths.get(“subdir/in.txt"); 
// p2: Absolute path to c:\java\local\jan\Hello.java
// Paths.get() can take a variable number of arguments
// each extra argument indicates an extra directly level
Path p2 = Paths.get("c:\\java\\local”,”rijkswatch”,”Hello.java"); 
In the above example, remark that when using backslashes as a directory separator (Microsoft style), they need to be doubled to escape their special meaning in java String.

FileSystem support

These methods will give you information on the different filesystems on the machine, their mount points, type, size, free space...

The picture shows the output of the code in DOS.

// once more the plural class (FileSystems) contains static utility methods
// that produce objects of the singular (FileSystem) class
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths; 

FileSystem fs = FileSystems.getDefault(); 
for (Path rootPath : fs.getRootDirectories()) { 
  try { 
    FileStore store = Files.getFileStore(rootPath);
    System.out.println(rootPath + ": " + store.type()); 
  } catch (IOException e) { 
    System.out.println(rootPath + ": " + 
      "<error getting store details>"); 

java.nio.file.Files methods


Static utilities in the Files class operate on Path (directory of file) objects.

searching in a directory

for (Path file : Files.newDirectoryStream(Path base, String pattern)){
The pattern can contain wildcards like:

* any string
** any string, going down in directories
? any single character
[a,0-9] a or a number
[!a-z] NOT a lower case character
{one,two} String one or two

To recurse into subdirectories use Files.walkFileTree.

Some more static Files methods

public static long size(Path path) // Returns size of the file
public static boolean isReadable(Path path)
public static boolean isSymbolicLink(Path path) // path a soft link? 
  // LinkOption below specifies how symbolic links should be handled 
public static Path createLink(Path link,Path existing) // create hard link
public static Object getAttribute(Path path,String attribute,LinkOption... options)  
  // attribute "unix:nlink" gives # hard links in unix attribute space<
public static boolean exists(Path path, LinkOption... options)
public static boolean isDirectory(Path path, LinkOption... options)
public static Path move(Path source, Path target, CopyOption... options)
public static List<String> readAllLines(Path path, Charset cs) 

Switching between the new nio Path and the old io File

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

Path p1 = Paths.get(“subdir/in.txt");
File f1 = p1.getFile();
Path p2 = f2.getPath();

4 November 2013

JPA : choose your enum ordinals (Updated)

Ordinal enums are brittle with JPA.
If you change the order of the enum constants,
the ordinal number that is saved to the database changes.

You can solve that by assigning your own code in the enum (10,20,30 in the example).
In the entity, make the enum transient.
Before saving the entity put your code in an int attribute, which will be stored in the database.
After loading convert the int back to the enum.

 Update: In JPA 2.1 you can also do this with @Converter

public enum Status{
  OPEN(10), CLOSED(20), CANCELLED(30);
  private int code;  
  private Status(int code) {  
    this.code = code;  
  public int getCode() {  
    return code;  
  public static  Status getStatus(int code){
    for (Status stat : Status.values()) {
      if (stat.getCode()==code) return stat;
    return null; // not found: invalid code
public class Auction{
  private int status;
  @Transient private Status statEnum;

  public int getStatus(){
    return status;

   public void setStatus(int status){
    this.status = status;

  @PostLoad private void int2enum(){
    statenum = Status.getStatus(status);

  @PrePersist private void enum2int(){
    status = statEnum.getCode();

NB: kudos to Guy for moving conversion to callbacks.

A JUnit 4 Test class

package infotool;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class InfoModelTest {

    private InfoModel instance;

    public void setUp() {
        instance = new InfoModel();

     * Test of getMessage method, of class InfoModel.
    public void testGetMessage() {
        assertEquals("welcome to mvc", instance.getMessage());

     * Test of getWeather method, of class InfoModel.
    public void testGetWeather() {
        assertEquals("Sunny", instance.getWeather());

     * Test of setMessage method, of class InfoModel.
    public void testSetMessage() {
        String wthString = "JUnit @Rules";
        assertEquals(wthString, instance.getMessage());

     * Test of setWeather method, of class InfoModel.
    public void testSetWeather() {
        String wthString = "It's going to rain";
        assertEquals(wthString, instance.getWeather());

     * Test of addModelChangeListener method, of class InfoModel.
    public void testAddModelChangeListener() {
        InfoModel instance = new InfoModel();
        InfoView view = new InfoViewTestSupport(instance);
        instance.setMessage("From testAddModelChangeListener");
        assertTrue(((InfoViewTestSupport) view).getNotification());

1 November 2013

Java programming: extra labs

 Lesson 3

Extra Practice 3-2, Task 3

Step e. Add a method addEmployees that adds multiple employees

Lesson 4

  Extra Practice 3-2: Equals method

  1.  Add an equals method to the employee class
    1. Use employee id to test equality
      1. Make  sure that it takes into account null values and references to other classes being passed in
      2. You can use ALT+INSERT in NetBeans to insert a sample equals method
    2. Test and correct until the code succeeds
  2. Modify the equality test to use the social security number
    1. Test and correct until the code runs correctly

Lesson 5

  Extra Practice: Deck

  1. Start from the Deck lab from Java Funcamentals
  2. Replace the CardNames and CardValues arrays with enums Suit and Face
  3. Replace the decksize (52) with a number calculated from the available enums 
    1. hint: using the values() method on your enum, returns an array of all values
  4. Test and correct the code until the program runs correctly
  5. Extend the Suit enums with the corresponding unicode characters
  6. Extend the Face enum with numbers/names
     ACE("Ace"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"),SEVEN("7"), EIGHT("8"), NINE("9"), TEN("10"), JACK("Jack"), QUEEN("Queen"), KING("King");
  7. Modify the code to display the cards using the new data
  8. Test and correct the code until the program runs correctly

Lesson 7

  Extra Practice: Couple

  1. Write a generic class Couple<S,T> that stores two values each with its own generic type
  2. Write a constructor without parameters and a constructor with two parameters
  3. Write getters and setters for each of the members
  4. Write a test, creating a Couple <Integer,String> 
    1. Try to call getters and setters respecting the given types
    2. Try to call getters and setters that do not respect the given types

Extra practice: EmployeeDAOMapImpl

  1. Write an EmployeeDAOMapImpl for practice 6-2 using a Map as internal storage. Start from a copy of the EmployeeDAOMemoryImpl 
  2. Have the factory return the  EmployeeDAOMapImpl
  3. Test and correct until the program runs well

Lesson JUnit

practice DAO

  1. Add a reset() method to the EmployeeDAO from practice 6-2 that clears the employee storage
  2. Create  a JUnit 4 testcase for the EmployeeDAO
    1. Right click on the class and select tools > create Tests
      1. The testcase is created in a dedicated test directory
      2. in which package does the testcase reside?
  3. Complete the generated testcase
    1. In the @BeforeClass method create the DAO object and store it in a static attribute
    2. In the @Before method, initialise the employee table with 3 records
      1. Hint: to make a Date object use Calendar cal = Calendar.getInstance();  
        // you can reuse the same cal object for multiple dates // set the date to januari 6th 2013
        cal.set(2013, 0, 6);
        Date d = cal.getTime();
    3. Write tests for all operations
      1. You may add a setter method to Employee for testing the update operation
  4. Right click the project and select Test (or press ALT+F6)
    1. Test until all tests succeed

Lesson 8

 Extra practice: CSV changer

Write a unit test that replaces all blanks in a file by colons. Multiple blanks should be replaced by only one colon.