Saturday, December 5, 2015

Returning first successful result from CompletableFuture

Recently I wanted to get first non empty result from bunch of CompletableFutures. For my case, allOf - is bad because it runs all futures till end and returns nothing. AnyOf - is bad because it returns first result, even if it is empty. So there is no such function in Java API, but it is still very easy to implement, and maybe it is even more logical, because actually there are 2 cases: found or not found; and it can be easily solved with two CompletableFutures. There is example:

import java.util.concurrent.CompletableFuture

success = new CompletableFuture()

def futures = (1..10).collect { CompletableFuture.supplyAsync {
                                    if (val == 6) {
                                    } else {

println CompletableFuture.allOf(futures as CompletableFuture[])
        .thenApply({ throw new RuntimeException("not found") })
        .applyToEither(success, {it}).get()

This way it will return result if there is any, or throw exception if all done and there is no result, and it can be easily adjusted to return default value, for example.

Saturday, November 28, 2015

Groovy framework for AWS lambda

I was playing with AWS lambda recently and I find it pretty exciting. It is cheap, does not require any maintenance and it is simple. The biggest problem I found so far, is lack of tooling. AWS has web UI which does what it supposed to do, but is far from pleasant. AWS command line tools and API are much better, but are AWS centric. So, I have created tool to work with AWS lambda. It uses some convention now, but can be adjusted to whatever feels right for specific situation, idea is that it is just one script with few lines of code. Basically, it does 2 things: uploads application and local testing. I have created small example that illustrates how and what it does.

Example is trivial application that increments number by clicking on button. It is SPA, with static client in S3 and two API functions add and get.

All lambda code is just single Groovy script. It consists of router function, dependencies and logic. Router function is what will be mapped to lambda. It is function that accepts map and returns map. Input map can be plain request JSON, or some additional parameters can be injected at API gateway. Even for this trivial case it was a good idea and I injected configuration parameters - AWS access and secret keys and region for DynamoDB access, so they are hidden at admin level and are environment specific. Simplest way to do routing is just by using some designated parameter, in this example, it is called "function". Doing it with real URI will need AWS configuration, and it is not very convenient and flexible.

In this simple example, router script does logic, but for more complex cases it is better to be just router and logic would be in dependencies.

During packaging Groovy script is compiled and packaged together with all dependencies into uberjar which is required by AWS. After that, it is uploaded via AWS API.

Development can be done and tested locally by using mock server. Mock server mimics AWS, parses input into JSON like lambda, injects parameters like API gateway, includes Access-Control-Allow-Origin and separates configuration via config.js, supports debug and reloading.

Saturday, October 17, 2015

Trim is not removing all whitespaces in Java

Java trim is removing only ASCII whitespace characters, but ignores unicode whitespaces. This is backward compatibility thing, and there is big and detailed explanation of this problem. It can be easily fixed by using regular expression that will remove all official unicode whitespaces:
Pattern TRIM_PATTERN = Pattern.compile("^\\s*(.*?)\\s*$", Pattern.UNICODE_CHARACTER_CLASS);
Matcher matcher = TRIM_PATTERN.matcher(input);
if (matcher.matches() && matcher.groupCount() > 0) {
return input;

But for more extreme cases you may want to use also this pattern

besides whitespaces it will also remove other invisible symbols.

Wednesday, September 16, 2015

Using IDE scripting console in Intellij IDEA

Some time ago, Intellij added option to write simple scripts to automate it's functions, extract information and write simple plugins. It is located under Tools / IDE scripting console. It is very basic now, just scripting file that runs inside of Intellij JVM and has access to Intellij API.

 There is simple Groovy example that automatically adds non-suspending breakpoint with log expression:

import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.ProjectManager
import com.intellij.xdebugger.breakpoints.SuspendPolicy

def file = LocalFileSystem.getInstance().findFileByPath("/users/test/project/src/")
def doc = FileDocumentManager.getInstance().getDocument(file)
def bp = ProjectManager.getInstance().getOpenProjects()[0].getComponent("DebuggerManager").getBreakpointManager().addLineBreakpoint(doc, 11)

Of course, this basic task can be done through XML configuration, but IDE scripting has access to much more and can do trickier tasks. There is very cool demo. Generally, from what I have seen, API is pretty impressive, it is intuitive, powerful and easy to start with.

My biggest complains are that it is pretty verbose (as Java itself), it would be great if in future versions it would be solved at least at tooling levels - with auto imports for packages, autocompletion, and more convenience functions, like attaching to actions, getting projects or files quickly. Second problem is total lack of documentation. Basically, what I have found is only GitHub repo of Intellij community edition. It has multiple modules with API scattered across them, most generic classes are under Core API.

Friday, August 14, 2015

Adding custom hostname to docker-machine VirtualBox image

When boot2docker was changed to docker-machine, it added dubious feature of being read only container, so no changes to VirtualBox will be preserved after restart. Saving changes is still perfectly fine for Linux Docker users, but not docker-machine users anymore for some reason.

So, to add new hostname before, you could just go to VirtualBox, change /etc/hosts file, and this is it - this will not work with docker-machine, as hosts file will be erased after restart. You are luckier, if you are not using net:host in docker-compose, there is special property for that too. But, if you are using net:host, the only working option is to change VirtualBox image and add this configuration:

VBoxManage modifyvm "default" --natdnshostresolver1 on

In this case it will use host machine to resolve all dns requests, even those from /etc/hosts.

Thursday, July 23, 2015

How to add configuration parameters to AWS lambda with API gateway

Unfortunately, there is no easy way to add configuration parameters to AWS lambdas, there is almost nothing in context that you can configure; there is description, but besides being ambiguous, it is not accessible from function itself. Fortunately, it is possible to set additional values from API gateway, even it is not straightforward.

When you have created API gateway mapping, go to the method screen in gateway console, there is screen with multiple boxes, one of which is called Integration Request, click it and there will be section called Mapping Templates. Add new mapping template for type application/x-www-form-urlencoded, save it, edit value and change from default Input passthrough to Mapping template, then you can transform your original request, by adding additional parameters, it uses velocity for transformation, but there is basic example:

  "my-new-configuration" : "my value",
  "body" : "$input.path('$')",
  "params" : "$input.params()",
  "ip" : "$context.identity.sourceIp",
  "user-agent" : "$context.identity.userAgent"

Besides possibility to add anything to lambda function it also has access to interesting request data like headers, ip, client type, user agent, country, etc. - all will be available in your map or pojo as parameter for lambda function. And do not forget to click Deploy API after making changes to make them live.

Thursday, June 25, 2015

How to create executable JAR from single Groovy script

Groovy scripts are nice - they allow to use dependencies and write logic in small single file, but Groovy is mostly available on developers machine, but not on servers, so deploying them is a little more tricky. You can package them in uberjar with Gradle, but it requires additional build file and compatible structure, so it is more like project than script already.

To keep things simple, I have created Groovy script that converts other Groovy scripts into executable JARs. It packages all dependencies from Grab and compiles original script into Java main class with same name as script and sets it in manifest. So it is perfectly usable from empty JVM. JAR structure is also compatible with AWS lambda, so it can be launched from there too, just put logic in specific method. To convert your script, just download it from here and run like:

./scriptjar.groovy input.groovy output.jar

Script tries to find groovy libs via GROOVY_HOME, so set it or just hack it for your location, it is just one small Groovy script.

Friday, May 15, 2015

Erlang connection from boot2docker

If you need to connect from outside of Docker into boot2docker instance on MacOS, but nothing happens: First, make sure that all ports are mapped in VirtualBox instance - at minimum it requires 4369 and 20000-20999. Then, for hidden nodes, you may need to ping them explicitly, like:

Thursday, April 9, 2015

Using Clojure dependencies without specifying version number

Clojure has mandatory version number for dependecies, so until you are a real fan of the library, you may need to look it up somewhere. Fortunately, it is possible to specify LATEST instead of version, like:
(leiningen.exec/deps '[[http-kit "LATEST"]])

Clojure command line script with dependencies

If you want to use Clojure for command line scripts, the easiest way is by using lein-exec plugin. It compiles clj files on the fly and downloads all necessary dependencies that are declared in same file, similarly to Groovy @Grab annotation.

For example, if you have file named download.clj with
(leiningen.exec/deps '[[http-kit "2.1.16"]])
(require '[org.httpkit.client :as http])

(prn (:body @(http/get "")))

it is possible to run it by:
lein exec download.clj

Sunday, April 5, 2015

My recipe for IntelliJ IDEA distraction free mode

One of the top features in IntelliJ IDEA 14.1 is distraction free mode. I am totally in love with it and not looking back, working 100% of time in it. Biggest concern about it is: loosing tabs. I was happy with tabs before, but it looks like there is a bunch of better options:

  • Recent Files (Cmd + E) - basically dropdown with tabs, but you can also do text search in it.
  • Switcher (Ctrl + Tab) - same as Recent Files, but opens file once shortcut is released. A little faster, but you are loosing search. This is the closest to original tabs.
  • Search Everywhere (Shift + Shift) - acts as Recent Files, but if you type something - also does search for Classes, Files, Symbols, Actions, Preferences. So together with tabs it basically replaces also Cmd + E, Cmd + O, Shift + Cmd + O, Alt + Cmd + O, Shift + Cmd + A.

I chose Search Everywhere as it is most feature rich and is really awesome. I also remapped it to Ctrl + E as it includes Recent Files, but as shortcut Ctrl + E works faster and is easier to reach.

Tabs had another aspect - tabs pinning, I didn't used it a lot, but for occasional use, bookmarks replace them nicely and provide more features as can pin specific line together with file. So use F3 to bookmark and Cmd + F3 to list bookmarks.

Everything else: like splitting, navigation bar, file path, tool windows, etc. is easy accessible via shortcuts or Find Action... (Shift + Cmd + A) if you forgot shortcut.

Wednesday, March 4, 2015

Changing SSH port for dynamic inventory script in Ansible

If you have to write dynamic inventory script in Ansible, but need to use SSH port which is not 22, there is example output of the script output:


Saturday, February 21, 2015

Ansible freezes during start

If Ansible stops for several minutes or so during start without any messages, check if it calls your inventory script, because it might query for additional options using --host parameter and if there are a lot of hosts, it may take some time.

It can be easily fixed by adding this to JSON root:

    "_meta" : {
       "hostvars" : {

More about it here.

Wednesday, February 11, 2015

My Everyday Carry

This will be rare post not about IT. So, apart from programming, my other obsession is EDC, if not familiar: very good intro is here.

Current setup: phone + keys

For long time my EDC kit was phone and keys. I had special phone case, that holds cards and keys hold other essentials like CashStash for bills, Leatherman Squirt PS4 as multitool, TrueUtility pen,
even Utili-key as second multitool, as it is easier to get off keyring. I am still looking for good USB cable (I tried InCharge, but it is another story)

I like this setup very much despite drawbacks. One problem was that it is very tight and there is no place for occasional spare items: checks, change, etc. Other problem is that case holds only 2 cards and e-ticket, but ideally I need more. Final and biggest problem is that my phone gets old
and I want to change it, and wallet cases are not so popular as they used to, and there is no any decent for the new phone I want to buy. So I have to shuffle my items to fit caseless phone.

Saddleback + phone + keys

Saddleback is nice slim wallet, one of the best according to LifeHacker. Holds all cards and bills, and occasional checks. A little fat itself, even without cards, but not too fat. Phone with this wallet is almost as fat as phone with case. My biggest problem is that it is 3 pieces setup. 2 pieces - one item per pocket. With 3 pieces, 2 items should go into single pocket and there is problem that when I take out one item, another can fall out of pocket accidentally.

Crabby wallet + phone

Crabby is best for cards, super slim, holds cards firmly but still convenient to take card even from the middle. But it is not suitable for my keys as I have too many of them. I even got rid of Leatherman and CashStash (not super important as CashStash is not really needed anymore, as there is place for bills now; and Utili-key has most important tools of Leatherman).
First problem: strap is too weak and narrow. It can hold 1 or max 2 flat keys. Second: single strap locks both cards and keys, so if you need keys - cards can fall out, and if you need cards, keys are not locked and it is disturbing. Third: strapping so many keys is always annoying and difficult, and with single strap you have to do it two times more often.
Generally, Crabby is fine, but only if you have small number of keys. They warn about it in their ad, but I still decided to try. Unfortunately, they were right. Another disadvantage is that cards pocket is not zipped, so no coins or occasional small items.

Local wallet with crabby claw phone

Very good setup, wallet has 2 pockets - one puffy for keys and other - larger but tighter - for cards and bills. Both pockets are zipped separately, so everything is safe and independent. Wallet is bigger, but still comfortable for pocket.
Downsides: keys pocket is not intended for keys, so there is no any special ring or strap. I adapted slider to hold crabby claw, but it is not ideal. Also size of the keys pocket is pretty tight, and it is hard to put keys there. Taking keys out of wallet is fine and crabby claw gives some freedom, but putting them back is a little tricky. Other complain is that it is real, soft leather, which gives it great texture, but it is also a little bulkier and heavier.

Chums marsupial with crabby claw + phone

Best setup. Size is almost as small as crabby, but has special pocket for keys, which even has strap (unfortunately it is not retractable, this is my only complain). Cards pocket is zipped, so it can hold occasional change, and size is just right for cards, easily holds about 10 cards, and there is some space left. Special keys pocket is very good. It is open from the short side, It is made from some artificial fabric and is really thin and light, feels like cheap plastic though. I should see if it is durable, hopefully - it is, as I'd like to keep it.

Friday, January 23, 2015

VirtualBox DHCP gives same IP address to different machines

Usually happens after cloning VMs and problem is because there is the same MAC address. Check virtual machine network settings and change as needed.

Friday, January 2, 2015

CanJS and ClojureScript

I don't really like compiled JavaScript languages, but if I had to choose - I like ClojureScript the most. It is big friends with Om MVC framework, which is based on React.js. React is another component framework like CanJS, but is too verbose to my tastes and has less features out of the box, so I like CanJS more. Only problem is that it does not have a lot of integration with ClojureScript. But it is pretty easy to solve, because ClojureScipt and CanJS are both amazing and flexible. There is example of simple component, there is ClojureScript:

(ns myexample
  (:require [domina :refer [by-id set-html!]]
            [domina.css :refer [sel]]))

(.extend can/Component
  #js {:tag "my-component"
       :scope #js {:value 3
                   :increment #(.attr % "value" (inc (.attr % "value")))}})

(set-html! (by-id "my-root") (can/view "my-template" {}))


  <div id="my-root"></div>
  <script type="text/mustache" id="my-template">
      Value: {{value}} <input type="button" can-click="increment" value="Increment"/>
  <script src=""></script>
  <script src=""></script>
  <script src="base.js" type="text/javascript"></script>
  <script src="myexample.js" type="text/javascript"></script>
  <script type="text/javascript">goog.require("myexample");</script>