/ collar

Collarjava preview : make high quality java code with collarjava

collarjava demo

I have created and been using collar.js for a while, and I always want to port the idea to other languages. Now here comes the first one: collarjava!

I try to make the API of collarjava as the same as the one of collar.js, unless it is limited by Java language itself. If you already know collar.js API, it is easy for you to start using collarjava. For example:

To create a namespace:

Javascript

var ns = collar.ns("com.collartechs.collarjs.example");

Java

Namespace ns = Collar.ns("collar.collartechs.collarjava.example");

To create a data flow

Javascript

ns.input("input endpoint")
  .when("'auth' event")
  .do("input validation check, or throw error")
  .do("get user from db, or throw error")
  .do("verify credential, or throw error")
  .map("generate token, or throw error")
  .to(output)

Java

ns.input("input endpoint")
  .when("'auth' event")
  .action("input validation check, or throw error")
  .action("get user from db, or throw error")
  .action("verify credential, or throw error")
  .map("generate token, or throw error")
  .to(output)

do is a reserved word in Java, so it is not possible to name actuator operator as do, so In collarjava, short name for actuator operator is action.

Implement an operator

Javascript

ns.input("input endpoint")
  .when("'auth' event", signal => {
    return signal.get("event") === "auth";
  })
  .do("input validation check, or throw error", signal => {
    var username = signal.get("username");
    var password = signal.get("password");
    // throw error, if no username or password found in signal
    if (!username || !password) throw new Error("Bad request");
    // otherwise pass to next node
  })
  .do("get user from db, or throw error")
  .do("verify credential, or throw error")
  .map("generate token, or throw error", signal => {
    var user = signal.getResult();
    var token = jwt.sign(user, jwtSecret, {
      expiresIn: "24h" // expires in 24 hours
    });
    return signal.new({
      token : token
    });
  })
  .to(output)

Java

ns.input("input endpoint")
  .when("'auth' event", signal -> {
    return "auth".equals(signal.getString("event"));
  })
  .action("input validation check, or throw error", signal -> {
    String username = signal.getString("username");
    String password = signal.getString("password");
    if (username == null || password == null) 
      throw new CollarRuntimeException("Bad Request");
    return null;
  })
  .action("get user from db, or throw error")
  .action("verify credential, or throw error")
  .map("generate token, or throw error", signal -> {
    User user = signal.getResult(User.class);
    String token = null;
    try {
      token = getTokenFromUser(user);
    } catch (JoseException e) {
      throw new CollarRuntimeException(e);
    }
    return signal.newInstance("token", token);
  })
  .to(output)

Turn collar flow to a direct function call

Javascript

var func = collar.toNode(input, output)
func(signal, (error, result) => {
  // handle result here
});

Java

// async version
CollarFlowAsyncCaller asyncCaller = Collar.toJavaAsync(input, output);
asyncCaller.call(signal, (exception, resultSignal) -> {
  // handle result here
});

// sync version
CollarFlowSyncCaller syncCaller = Collar.toJavaSync(input, output);
Signal result = syncCaller.call(signal);

Enable dev tool

Javascript

collar.enableDevtool();

Java

Collar.setCollarDevClient(new DefaultCollarDevClient());
Collar.enableDevtool();

Once you enabled dev tool in your java code, you can use the same collar-dev-server to visualize your program.

For example, with collarjava, I reimplement the authentication server of my last post :

Build an authentication server with collar.js + express + JWT

And test it in a Java application:

public class Main {
    public static void main(String[] args) {
        Collar.setCollarDevClient(new DefaultCollarDevClient());
        Collar.enableDevtool();

        Auth authModule = Auth.getInstance();

        String token = null;
        try {
            token = authModule.generateToken("test@collarjava.com", "collarjava123");
        } catch (CollarRuntimeException e) {
            e.printStackTrace();
        }
        System.out.println(token);
    }
}

If I start recording on collar-dev-server, and run this Java app, you can see the following flow:

collarjava demo

What's next?

Now collarjava covers 80% of the features of collar.js v0.4.4. I am working on the rest 20% and the documentation. The first alpha version will be released in mid october.