Kohei Nozaki's blog 

Purchased book list in O'Reilly's 60% off sale


Posted on Thursday May 07, 2015 at 05:34PM in Technology


Total: $170.38 USD

  • SQL and Relational Theory, 2nd Edition

  • CSS Secrets

  • Professional Java EE Design Patterns

  • Java Performance: The Definitive Guide

  • Java 8 Lambdas

  • Java Cookbook, 3rd Edition

  • Learning Java, 4th Edition

  • RESTful Java with JAX-RS 2.0, 2nd Edition

  • Continuous Enterprise Development in Java

  • Head First Servlets and JSP, 2nd Edition

  • Head First HTML and CSS, 2nd Edition


Configuring Apache James to use PostgreSQL as the backend


Posted on Sunday Apr 19, 2015 at 09:37PM in Technology


UPDATE: I’m running into a strange problem of using PostgreSQL as backend with Thunderbird. I recommend to use an other database as the backend at the moment. http://www.mail-archive.com/server-user%40james.apache.org/msg14715.html

UPDATE2: I created a patch to solve the issue and it seems fine. for detail and download the patch see: https://issues.apache.org/jira/browse/MAILBOX-228

I have been used Apache James with Embedded Derby, but its CPU/IO consumption is increasing day by day. I’m not sure where is the bottleneck but I decided to move to PostgreSQL as its backend anyway.

  1. Put JDBC driver (I used postgresql-9.3-1100.jdbc41.jar) into $JAMES_HOME/conf/lib

  2. Create james-database.properties from template, and put it into $JAMES_HOME/conf as follows

    database.driverClassName=org.postgresql.Driver
    database.url=jdbc:postgresql://127.0.0.1:5432/james
    database.username=james
    database.password=***
    
    vendorAdapter.database=POSTGRESQL
    
    openjpa.streaming=true
  3. Restart James instance

  4. Create domains, addresses and aliases:

    ./james-cli.sh -h localhost -p 9999 adddomain example.org
    ./james-cli.sh -h localhost -p 9999 adduser kyle@example.org ***
    ./james-cli.sh -p 9999 -h localhost addaddressmapping postmaster example.org kyle@example.org

Note

This guide claims that setting standard_conforming_strings=off is needed, but I guess this issue may address this problem, so I don’t use this setting at the moment. my James installation is shipped with openjpa-2.2.1.jar and the issue was fixed in 2.2.0.


java.util.logging: Programmatic configuration and logging with Java8 lambdas


Posted on Monday Mar 30, 2015 at 06:24PM in Technology


Configuration

We can put configurations in following places (ordered by priority):

  1. java.util.logging.config.class system property. specify FQCN of a class here

  2. java.util.logging.config.file system property. specify the path to a property file here

  3. $JAVA_HOME/lib/logging.properties file. put JRE-wide configuration here

Use of java.util.logging.config.class is most flexible. in that way we can simply put a config class under src/test/java for unit tests. we need to put configuration procedures in the default constructor. an implementation would be something like following:

package testutil;

import java.util.logging.*;

public class LoggingConfig {
    public LoggingConfig() {
        try {
            // Load a properties file from class path that way can't be achieved with java.util.logging.config.file
            /*
            final LogManager logManager = LogManager.getLogManager();
            try (final InputStream is = getClass().getResourceAsStream("/logging.properties")) {
                logManager.readConfiguration(is);
            }
            */

            // Programmatic configuration
            System.setProperty("java.util.logging.SimpleFormatter.format",
                    "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n");

            final ConsoleHandler consoleHandler = new ConsoleHandler();
            consoleHandler.setLevel(Level.FINEST);
            consoleHandler.setFormatter(new SimpleFormatter());

            final Logger app = Logger.getLogger("app");
            app.setLevel(Level.FINEST);
            app.addHandler(consoleHandler);
        } catch (Exception e) {
            // The runtime won't show stack traces if the exception is thrown
            e.printStackTrace();
        }
    }
}

If you prefer configuration in a property file rather than a config class, following will work as the same to config class:

handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n

java.util.logging.ConsoleHandler.level=FINEST
app.level=FINEST

To set system property for every test execution, put following plugin definition to your pom.xml. note that IntelliJ IDEA respects this configuration in its test execution.

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <systemPropertyVariables>
            <java.util.logging.config.class>
                testutil.LoggingConfig
            </java.util.logging.config.class>

            <!-- If you prefer a file this will work too -->
            <!--<java.util.logging.config.file>-->
                <!--src/test/resources/logging.properties-->
            <!--</java.util.logging.config.file>-->
        </systemPropertyVariables>
    </configuration>
</plugin>

Logging examples

Application class:

public class MyClass {
    private static final Logger log = Logger.getLogger(MyClass.class.getName());
    public String hello(String greetings, String name) {
        // entering will be logged as FINER
        log.entering(MyClass.class.getName(), "hello", new Object[]{greetings, name});

        // lambdas
        log.finest(() -> "finest: " + LocalDateTime.now());
        log.finer(() -> "finer: " + LocalDateTime.now());
        log.fine(() -> "fine: " + LocalDateTime.now());
        log.info(() -> "info: " + LocalDateTime.now());
        log.warning(() -> "warning: " + LocalDateTime.now());
        log.severe(() -> "severe: " + LocalDateTime.now());

        // exception logging

        // throwing will be logged as FINER
        log.throwing(MyClass.class.getName(), "hello", new Exception("test"));

        // exception + message logging with lambda
        log.log(Level.FINEST, new Exception("test"), () -> String.format("arg=%s", name));

        // exception + parameter logging with LogRecord
        final LogRecord record = new LogRecord(Level.FINEST, "arg={0}");
        record.setThrown(new Exception("test"));
        record.setLoggerName(log.getName()); // logger name will be null unless this
        record.setParameters(new Object[]{name});
        log.log(record);

        final String rc = greetings + ", " + name;

        // exiting will be logged as FINER
        log.exiting(MyClass.class.getName(), "hello", rc);
        return rc;
    }
}

Test:

public class MyClassTest {
    @org.junit.Test
    public void testHello() throws Exception {
        MyClass sut = new MyClass();
        Assert.assertEquals("Hello, Kyle", sut.hello("Hello", "Kyle"));
    }
}

Output:

2015-03-30 18:01:19.362 FINER   [app.MyClass] (app.MyClass hello) ENTRY Hello Kyle
2015-03-30 18:01:19.425 FINEST  [app.MyClass] (app.MyClass hello) finest: 2015-03-30T18:01:19.425
2015-03-30 18:01:19.427 FINER   [app.MyClass] (app.MyClass hello) finer: 2015-03-30T18:01:19.427
2015-03-30 18:01:19.428 FINE    [app.MyClass] (app.MyClass hello) fine: 2015-03-30T18:01:19.428
2015-03-30 18:01:19.429 INFO    [app.MyClass] (app.MyClass hello) info: 2015-03-30T18:01:19.429
2015-03-30 18:01:19.431 WARNING [app.MyClass] (app.MyClass hello) warning: 2015-03-30T18:01:19.430
2015-03-30 18:01:19.433 SEVERE  [app.MyClass] (app.MyClass hello) severe: 2015-03-30T18:01:19.433
2015-03-30 18:01:19.434 FINER   [app.MyClass] (app.MyClass hello) THROW
java.lang.Exception: test
	at app.MyClass.hello(MyClass.java:27)
	at app.MyClassTest.testHello(MyClassTest.java:10)
...

2015-03-30 18:01:19.439 FINEST  [app.MyClass] (app.MyClass hello) arg=Kyle
java.lang.Exception: test
	at app.MyClass.hello(MyClass.java:30)
	at app.MyClassTest.testHello(MyClassTest.java:10)
...

2015-03-30 18:01:19.442 FINEST  [app.MyClass] (app.MyClass hello) arg=Kyle
java.lang.Exception: test
	at app.MyClass.hello(MyClass.java:34)
	at app.MyClassTest.testHello(MyClassTest.java:10)
...

2015-03-30 18:01:19.447 FINER   [app.MyClass] (app.MyClass hello) RETURN Hello, Kyle


Modifying persistence.xml to execute drop-and-create dynamically


Posted on Monday Mar 23, 2015 at 03:19PM in JPA


I need that for Arquillian testing so I created an utility method for that.


Enabling RequestDumpingHandler of Undertow


Posted on Friday Mar 20, 2015 at 05:01PM in WildFly


Tested with WildFly 8.2.0.Final. Issue following command via jboss-cli and restart the server:

/subsystem=undertow/configuration=filter/custom-filter=request-dumper:add(class-name=io.undertow.server.handlers.RequestDumpingHandler, module=io.undertow.core)
/subsystem=undertow/server=default-server/host=default-host/filter-ref=request-dumper:add

Following log will be dumped to the console:

----------------------------REQUEST---------------------------
               URI=/batcheetest/jbatch/batchee/execution/start/myjob
 characterEncoding=null
     contentLength=95
       contentType=[application/json]
            header=Accept=*/*
            header=Content-Type=application/json
            header=Content-Length=95
            header=User-Agent=curl/7.30.0
            header=Host=localhost:8080
            locale=[]
            method=POST
          protocol=HTTP/1.1
       queryString=
        remoteAddr=/127.0.0.1:57668
        remoteHost=localhost
            scheme=http
              host=localhost:8080
        serverPort=8080
--------------------------RESPONSE--------------------------
     contentLength=-1
       contentType=application/json
            header=Connection=keep-alive
            header=X-Powered-By=Undertow/1
            header=Server=WildFly/8
            header=Transfer-Encoding=chunked
            header=Content-Type=application/json
            header=Date=Fri, 20 Mar 2015 07:58:13 GMT
            status=200
==============================================================

I’m disappointed that there is no dump of request body :(