sebastiandaschner news


saturday, april 01, 2017

Welcome to my third newsletter!

In this week I attended the JavaLand conference in Germany — for my third time. It’s held in a theme park — no, really — and one of my favorite Java conferences in the year. I gave two sessions on the next version of JAX-RS and the topic of CQRS with Java EE, spoke on a Java EE panel together with other EE evangelists and moderated the NightHacking interview section on stage. Pretty busy days but always enjoyable to see enthusiasm for the Java Platform everywhere!

Now back home in Munich for client work the next days and planning some further content on Java EE and Cloud Native.

 

What’s new

 

Access Git from Java with SSH key

For some use-cases you might access a git repository from a Java application. JGit offers a helpful integration with builder pattern APIs.

To open a Git repository call the cloneRepository() command.

File workingDir = Files.createTempDirectory("workspace").toFile();

TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();

git = Git.cloneRepository()
        .setDirectory(workingDir)
        .setTransportConfigCallback(transportConfigCallback)
        .setURI("ssh://example.com/repo.git")
        .call();

Our own implementation of the transport config callback configures the SSH communication for accessing the repository. We want to tell JGit to use SSH communication and to ignore the host key checking.

private static class SshTransportConfigCallback implements TransportConfigCallback {

    private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }
    };

    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }

}

Per default this will take the id_rsa key file, available under the ~/.ssh. If you want to specify another file location or configure a key secured by a passphrase, change the creation of the Java Secure Channel as well.

private static class SshTransportConfigCallback implements TransportConfigCallback {

    private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }

        @Override
        protected JSch createDefaultJSch(FS fs) throws JSchException {
            JSch jSch = super.createDefaultJSch(fs);
            jSch.addIdentity("/path/to/key", "super-secret-passphrase".getBytes());
            return jSch;
        }
    };

    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }

}

Now you can issue commands on your Git handle.

For a full example on how to access Git repositories, see my AsciiBlog application.

 

Intercepting EJB timer methods

Interceptors provide a helpful functionality to include cross-cutting concerns to Java EE components without tight coupling. @AroundInvoke in the annotation to use there. However, it also possible to intercept EJB timer methods by the @AroundTimeout annotation that can be added to an interceptor or a business method in the same bean.

See our Hello bean:

@Startup
@Singleton
@Interceptors(LoggingInterceptor.class)
public class Hello {

    @Schedule(second = "*/5", minute = "*", hour = "*", persistent = false)
    public void startProcess() {
        System.out.println("hello!");
    }

}

The LoggingInterceptor will log any invocation and retrieve more information about the timer by accessing InvocationContext#getTimer():

@Interceptor
public class LoggingInterceptor {

    @AroundTimeout
    public Object aroundTimeout(InvocationContext context) throws Exception {
        Date nextTimeout = ((Timer) context.getTimer()).getNextTimeout();

        System.out.println("firing method " + context.getMethod().getName() +
            ", next invocation will be at " + nextTimeout);

        return context.proceed();
    }

}

 

Debug Maven builds

If you’re building Maven plugins you might need the possibility to debug the build process. You can do so by calling mvnDebug that is included in $M2_HOME/bin:

mvnDebug clean package -X

The process with then wait and listen on a specific port for a debugger to connect. You can do so for instance by debugging from IntelliJ and being able to set breakpoints in your plugin’s code.

 

Detect direction of text

Today’s find of the JDK: java.text.Bidi for bidirectional information of text. It implements the Unicode Bidirectional Algorithm and shows information whether the provided text is read from left-to-right or right-to-left like in Arabic or Hebrew.

Bidi bidi = new Bidi("Hello World", Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
assertTrue(bidi.isLeftToRight());

bidi = new Bidi("حياة", Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
assertTrue(bidi.isRightToLeft());

Always interesting to discover new finding of the extensive Java platform!

 

Thanks a lot for reading and see you next time!

 

Did you like the content? You can subscribe to the newsletter for free: