VirtualBox port forwarding via privileged host ports on Linux

Expanding on my previous post, if you’re running a VirtualBox VM and using NAT’ted networking with port forwarding (say <host>:8443 => <guest>:443) and you want to use privileged host ports (to which VirtualBox cannot bind to directly), you can do the following:

[root@host ~]# iptables -t nat -I OUTPUT -o lo -p tcp --dport 443 -j REDIRECT --to-port 8443 Continue reading

SSH to headless VM on VirtualBox

Running a (Linux) VM headless and SSH’ing to it (using NAT’ted networking):

  1. Create a port forwarding rule from, say, host port 2022 to VM port 22 (via Settings => Network => Adapter 1 => Advanced => Port Forwarding)
  2. Startup the VM while holding the shift key, this will run the VM without allocating a window to it
  3. SSH to localhost:2022

The @Configurable Holder pattern

We often use the @Configurable Holder pattern to build richer Java domain objects.

As an example, consider a Linux server entity that is able to patch itself using Ansible, enabling us to place the exact details of the patching process in an Ansible playbook.

package com.example;

public final class LinuxServer {

    public String host;

    // Constructor etc ...

    public void patch() {
    private Ansible ansible() {
        return new AnsibleHolder().ansible;
    private static class AnsibleHolder {
        private Ansible ansible;

A strategy interface for invoking Ansible:

package com.example;

public interface Ansible {
    void patch(LinuxServer server);

This could be implemented to talk to Ansible via SSH or by running it locally.

Now to make this work, simply build your code with the build tool of your choice,
utilizing AspectJ compile-time weaving and the spring-aspects aspect library. We typically use Maven and the aspectj-maven-plugin for this.

Then set up the necessary Spring configuration (here com.example.LocalAnsible implements com.example.Ansible):

<context:spring-configured />
<context:component-scan base-package="com.example" />
<bean class="com.example.LocalAnsible" />

Finally, go ahead and patch a server:

new LinuxServer("").patch();


JSON serialization with Java inner classes and Jackson

Here’s an example of providing a JSON view to (a part of) your data using an inner class and Jackson JSON mapper:

public final class FtpServer {
    public String host;
    public Integer port;
    public String secret;

public final class Environment {
   public String name;
   @JsonIgnore public String username;
   @JsonIgnore public FtpServer ftpServer;
   public FtpInfo ftpInfo() {
     return new FtpInfo();

   @JsonAutoDetect(fieldVisibility = ANY)
   final class FtpInfo {
      String username = Environment.this.username;
      String hostname =;
      Integer port = Environment.this.ftpServer.port;

Which would render something like this:

  "name" : "Example",
  "ftp" : {
    "username" : bob,
    "hostname" : "",
    "port" : 21    

Fairly concise, eh?