Blockchain – Big Topic broken down to pieces

bccloud2

Blockchain is certainly not the latest buzzword any longer, it moved well forward on the Gartner Hype Cycle, passed the peak of inflated expectations and I am sure we will find it in the trough of disillusionment in the soon to be updated 2018 version. It is picked up by various industries looking for use-cases and applications. Unfortunately we are looking at Blockchain fatigue already, as there is much hype but little visible implementations outside the cryptocurrency space. I prefer projects that implement blockchain as the right tool for a particular problem over the “let’s see which business case we can throw blockchain at” approach.

In the aviation (airport) space I believe Blockchain has its appliance, but as previously stated I wont attempt to build an AODB with Blockchain as “database” for milestones just for the sake of integrating this technology. For some scenarios you certainly need immutability of data, but we can implement this with the means of other immutable data storage. It is also no point implementing a blockchain into a corporate network infrastructure with few nodes under the control of one entity, this does not fulfill the promise of distributed ledger and trust. A few use-cases that I see, usually involving multiple business parties:

  • Baggage tracking from end-to-end (goes well with IATA 753 effective since June 1st)
  • Service and contract management, billing (eg. groundhandler-airline)
  • Aircraft spare parts management (here the track of provenance have a huge impact)

Trying to understand blockchain can be overwhelming, ranging from Satoshi Nakamoto’s original whitepaper to a endless number of books, talks, websites.
One approach to understand the technology is to break it down into smaller pieces that are implementing proven existing technology or algorithms and understand how they come together eventually forming the much more complex blockchain.

I wont attempt to explain blockchain here, this is redundant, plenty of knowledgeable people have written books and articles you can refer to, but split it into some basic easy digestible portions, some coding included. Before attempting to code against real blockchain implementations, like Ethereum or Hyperledger, I will implement the most basic and simple blockchain first.

1. Hash

The most essential element of blockchain is a hash, a digital signature. A hash is a one way encryption,  once something is hashed there is no way to reverse the process and reveal the original text (decrypt it). Using the SHA (Secure Hash Algorithm), the most popular algorithm with its variants of 256, 384 and 512 bit, defined by NIST, we can convert a text (data) of any length to a 256 bit representation (for SHA256) which is represented by a 64 byte hex string. There is a number of libraries implementing the algorithm, below is the Apache DigestUtils version.

	private void testSHA(){

		String sha256hex = DigestUtils.sha256Hex("Jim Smith");
		System.out.println("\n1 SHA256: " + sha256hex);

		String sha256hex2 = DigestUtils.sha256Hex("jim smith");
		System.out.println("\n2 SHA256: " + sha256hex2);

		String sha256hex3 = DigestUtils.sha256Hex("jim smith and the lazy brown fox");
		System.out.println("\n3 SHA256: " + sha256hex3);

		String sha384hex = DigestUtils.sha384Hex("Jim Smith");
		System.out.println("\n4 SHA384: " + sha384hex);

		String sha512hex = DigestUtils.sha512Hex("Jim Smith");
		System.out.println("\n5 SHA512: " + sha512hex);
	}

resulting in

1 SHA256: 65742910cc03889474f1ee2c8f321a105603d0ae2f91070ffd95b35f8da88261
2 SHA256: bfae13266154ec3c4de5c09cf14358305e44f48d2156953723ebbb184a724499
3 SHA256: e5a4a1b8bd88eb7cf8bff9ee5dd235f87ef996262d4d0213c1387f6141ab9574
4 SHA384: c6e76ad773905c1eedb6a0bd9c0b1602a56928d1ce95d70190cd908797466b948dd342aa69dd0343251afece2e48bfc2
5 SHA512: f813c3d9deb66d4999f6839acc60eb6e2fff6a84266c02e0d4b183f5e56d9674c70b0b136f9e1388673cefbc9278f583e3a4c9803ef0c49f9af28aca60dae5ac

Important to notice:
– Change of one character in the original text produces a complete new hash.
– Independent from the length of the original text the hash has the same length.

2. Chained blocks

As the wording implies, there are blocks of information that are linked together. Sounds like a linked list, where every list entry is pointing to the next information. The chained blocks are linked differently, every block points to the hashed previous block.

For illustration I choose a typical baggage journey (simplified).

blockchain

A bag passes different key touchpoints and changes its custody a few times between the various parties during the handling. Every time there is a new milestone event we record it, eg. bag scanned by groundhandler at the chute at what time, and include the hash of the previous milestone. This way the lifecycle from bag drop at departure to bag delivery at destination is recorded in an immutable way and cannot be changed afterwards.

Pitfall: The bag journey is recorded in an immutable way, but the blockchain cannot verify or confirm the milestone actually happened. This falls into the responsibility of the overall design and service orchestration.

Let’s build a very simple application implementing the above blockchain for baggage handling.

A java class BagTransaction representing the bag attributes inclusive timestamp and the custody transfer.

package blockchaindemo;

import java.time.Instant;

import org.apache.commons.codec.digest.DigestUtils;

public class BagTransaction {

	private String bagTag;
	private String timeStamp;
	private String pnr;
	private String transferFrom;
	private String transferTo;

	private long blockID;
	private String blockHash;
	private String previousBlockHash;

	public BagTransaction(String bagTag, String pnr, String transferFrom, String transferTo, long blockID,
			String previousBlockHash) {
		super();
		this.bagTag = bagTag;
		this.timeStamp = Instant.now().toString();
		this.pnr = pnr;
		this.transferFrom = transferFrom;
		this.transferTo = transferTo;
		this.blockID = blockID;
		this.previousBlockHash = previousBlockHash;

		this.blockHash = createCurrentHash();
	}

	public String getHash() {
		return this.blockHash;
	}

	@Override
	public String toString() {
		return "BagTransaction [bagTag=" + bagTag + ", timeStamp=" + timeStamp + ", pnr=" + pnr + ", transferFrom="
				+ transferFrom + ", transferTo=" + transferTo + ", blockID=" + blockID + ", blockHash=" + blockHash
				+ ", previousBlockHash=" + previousBlockHash + "]";
	}

	private String createCurrentHash() {
		String returnHash = "";

		returnHash = DigestUtils.sha256Hex(
				this.bagTag + this.timeStamp + this.pnr + this.transferFrom + this.transferTo + this.previousBlockHash);

		return returnHash;
	}

}

Take note of the hashing method that includes all fields inclusive of the previous hash.

A java class BagDemoApp using the transaction class.

package blockchaindemo;

import java.util.Random;

public class BagDemoApp {

	public static void main(String[] args) {

		BagDemoApp demoApp = new BagDemoApp();
		demoApp.demo1();

	}

	public void demo1() {

		String myBagTag = randomBagTagID();
		String myPNR = randomPNR();

		String currentBagBlockHash = "";

		// Print Bag Tag (Genesis Block)
		BagTransaction bagTransaction1 = new BagTransaction(myBagTag, myPNR, Entity.NIL.name(), Entity.PAX.name(), 1,
				"0");
		currentBagBlockHash = bagTransaction1.getHash();

		// Bag Drop
		BagTransaction bagTransaction2 = new BagTransaction(myBagTag, myPNR, Entity.PAX.name(), Entity.AIRP.name(), 2,
				currentBagBlockHash);
		currentBagBlockHash = bagTransaction2.getHash();

		// Bag SEC Scan
		BagTransaction bagTransaction3 = new BagTransaction(myBagTag, myPNR, Entity.AIRP.name(), Entity.SEC.name(), 3,
				currentBagBlockHash);
		currentBagBlockHash = bagTransaction3.getHash();

		// Display Transactions
		System.out.println(bagTransaction1);
		System.out.println(bagTransaction2);
		System.out.println(bagTransaction3);

	}

	// HELPER METHODS --------------------------------------------

	public String randomPNR() {
		final String alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		final int N = alphabet.length();

		Random r = new Random();
		StringBuffer tempPNR = new StringBuffer();

		for (int i = 0; i < 6; i++) {
			char nxtChar = alphabet.charAt(r.nextInt(N));
			while ((i == 0) && (Character.isDigit(nxtChar)))
				nxtChar = alphabet.charAt(r.nextInt(N));
			tempPNR.append(nxtChar);
		}
		return tempPNR.toString();
	}

	public String randomBagTagID() {
		String tempBagTag = "";

		long range = 9999999999L;
		Random r = new Random();
		long number = (long) (r.nextDouble() * range);

		tempBagTag = String.format("%010d", number);
		return tempBagTag;
	}

	public enum Entity {

		PAX {
			@Override
			public String toString() {
				return "Passenger";
			}
		},
		GH {
			@Override
			public String toString() {
				return "Groundhandler";
			}
		},
		AIRL {
			@Override
			public String toString() {
				return "Airline";
			}
		},
		AIRP {
			@Override
			public String toString() {
				return "Airport";
			}
		},
		SEC {
			@Override
			public String toString() {
				return "Security";
			}
		},
		NIL {
			@Override
			public String toString() {
				return "nil";
			}
		}

	}

}

Executing the application

BagTransaction [bagTag=1691462171, timeStamp=2018-08-12T08:02:25.745Z, pnr=ICSEAH, transferFrom=NIL, transferTo=PAX, blockID=1, blockHash=3ff736f7158d224db6e2e8ba25f3d50321903cd911646576f442a60f8c5872ed, previousBlockHash=0]
BagTransaction [bagTag=1691462171, timeStamp=2018-08-12T08:02:25.808Z, pnr=ICSEAH, transferFrom=PAX, transferTo=AIRP, blockID=2, blockHash=88dd4a2be3bc90ebce71635bedd6bcb63b326044e4bd49634a859a86458de243, previousBlockHash=3ff736f7158d224db6e2e8ba25f3d50321903cd911646576f442a60f8c5872ed]
BagTransaction [bagTag=1691462171, timeStamp=2018-08-12T08:02:25.808Z, pnr=ICSEAH, transferFrom=AIRP, transferTo=SEC, blockID=3, blockHash=ea7767ddb2dd7c2bfed4d3a038b9249e43df15a0396ff71da717783db9fee3c4, previousBlockHash=88dd4a2be3bc90ebce71635bedd6bcb63b326044e4bd49634a859a86458de243]

Please note, this is the most simple implementation of a blockchain for illustration purpose, it still misses a lot of features to pass to production, eg. mining, proof-of-work, etc.

In a second part might spin this a bit further. Stay tuned.

 

Disclaimer: This discussion, datamodel and sourcecode or application is for study purpose solely. It does not reflect or replicate any existing commercial product.

Dead Glassfish Connections

Running a complex web application with sessions and connections to the DB, MQ and HTTP clients brings you sooner or later into a discussion or problem with connections that have not been properly terminated by Glassfish or the client. Depending on the time-out settings in the OS and in Glassfish you might face CLOSE_WAIT connections. What is causing this is topic by itself, also helping the system to get rid of it. At the end quite likely the problem lies somewhere in the sourcecode.

In my case we found a large number of CLOSE_WAITS to Glassfish with strange IP addresses (closed enterprise app but open to public web).

Here only some helpful scripts (Linux/Ubuntu):

Find the Glassfish PID

jps | grep ASMain | awk '{ print $1 }'

#get it into variable
pid=$(jps | grep ASMain | awk '{ print $1 }')

Show all socket connections

netstat -anp | grep ESTABLISHED
netstat -anp | grep CLOSE_WAIT

Show all connections to Glassfish

netstat -anp | grep CLOSE_WAIT| grep $(jps | grep ASMain | awk '{ print $1 }')
netstat -anp | grep ESTABLISHED| grep $(jps | grep ASMain | awk '{ print $1 }')

Still a bit hard to review

netstat

netstat

Now lets sort by IP and count it (ignoring the ports)

netstat -anp | grep CLOSE_WAIT| grep $(jps | grep ASMain | awk '{ print $1 }')|awk '{print $5}'|awk -F':' '{print $1}'|sort -k1|uniq -c

Creates a list like this (number of connections and the related ip address)

netstat summary

netstat summary

You can pack this int a cron job or even get it combined with a geolocation webservice to find the user.

Getting started with Drools and Netbeans

Rule engines are a rather complex topic with potentially a steep learning curve. I am looking at a few options, Drools being one of them. As usual one can read all the theoretical papers, but rather I have a something to look at and play with fast. Ultimately I need to integrate the rule engine into my application, so I dont want to play with eclipse editors for rules but seeing a rule engine ticking inside my Netbeans project. I found one tutorial from Vishal Akhouri which I updated to the latest version of Drools and some minor fixes. Most of the sample source code is from Vishal.

Pre-Requirements:

Tutorial:

Update DynDNS hosts with Java and HttpComponents

I was looking for a way to update the DynDNS hosts pointing to my EC2 instances that run only on demand (controlled by an web application). Yes, there are apps and tools to let the server itself update his DynDNS hosts, but I want to do it from a central application. The DynDNS API description you find here, unfortunately there are no samples. But if you familiar with REST calls, shouldn’t be a big issue. For those who to get running quick, here the key code. To experiment, just create a simple Java application with Netbeans (or any other IDE), download the HttpComponents from Apache and add the library files to your project.

Libraries

commons-codec-1.4.jar
commons-logging-1.1.1.jar
httpclient-4.1.1.jar
httpclient-cache-4.1.1.jar
httpcore-4.1.jar
httpmime-4.1.1.jar

Sourcecode to update an IP address

void updateDynDNS() {
...
// replace with your own data !
String hostName = "whateveryourdomainnameis.selfip.org";
String hostIP = "1.2.3.5";
String userName = "user";
String userPassword = "password";
int responseCode;

try {

// Encode username and password
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = userName + ":" + userPassword;
String encodedAuthorization = enc.encode(userpassword.getBytes());

// Connect to DynDNS
URL url = new URL("http://members.dyndns.org/nic/update?hostname=" + hostName + "&myip=" + hostIP);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Demo DynDNS Updater");
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);

// Execute GET
responseCode = connection.getResponseCode();
System.out.println(responseCode + ":" + connection.getResponseMessage());

// Print feedback
String line;
InputStreamReader in = new InputStreamReader((InputStream) connection.getContent());
BufferedReader buff = new BufferedReader(in);
do {
line = buff.readLine();
System.out.println(line);
} while (line != null);

connection.disconnect();

} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

}
...

Up to you to embed into your application and do proper exception handling.

In a separate tutorial I will describe the basics of an EC2 controlling application using the AWS SDK for Java.

JavaFX Composer Out !

I was not looking at JavaFX for a few months and the latest rumors on the potential death of the RIA product under the new owner did not make me spend time learning more, even I believe it might be the missing piece for some boring Java (Swing) enterprise applications. Yesterday the plugin that finally supports the visual creation of JavaFX was released. (Go for plugins,reload catalog and select the version 1.0 of JavaFX plugin)

JavaFX Plugin

Once the download is finished you can start creating JavaFX applications

New JavaFX Project

JavaFX Skeleton Project

I cant judge yet the composer or rather non-manual (full control) way to create a fx stage. The composer creates the code (similar to Swing) in the background and you cant touch it without screwing the fx file. I will try on a few samples.

First Remark: The preview pane renders useless now, it does not work with the composer anyway.

More infos on the JavaFX Composer

Netbeans 6.8, Glassfish V3 and JavaEE6 released

The waiting is over, finally we get our hands on a production release of EE6 and Glassfish V3. Last week (Dec 10th) released, you also get it together with Netbeans 6.8. There is lot of attention and coverage on the latest version of the enterprise platform, so I spare you from my personal views,  though I was really looking forward to use it beyond previews and beta’s. I am excited to work with the matured and now “rightsized” framework. Time to get active on writing tutorials and books.

Some useful links with more details:

Jazoon 2009: Day 3

Last day of this years Jazoon.

  • The Android Runtime Environment by Joerg Pleumann from Noser Engineering (which contribute a lot to the Android codebase)
  • Android Application Model by Dominik Gruntz
  • Ed Burns on JavaEE and JavaFX integration. Entertaining and interesting as usual. Thanks Ed !
  • Daniel Seiler on the ZK Ajax framework. Thanks for the confirmation of my choice of framework.

My final resume and feedback:

  • A very good location in terms of conference room quality, since this is a movie theatre with great accoustic and projection.
  • Good mix of topics and tracks. Anyone who is doing something with Java will find his cup of tea (eeer.. Java).
  • Some “famous” folks to get hands on them and discuss face to face.
  • As in all conferences: some speakers cannot present. Sorry to say so. It is boring to read slides with tons of text.
  • Not enough wireless “plugins”. Seems some of the attendees were operating datacentres during the conference and hacking away during the whole time. Thanks for not sharing wireless space.
  • Maybe a bit too expensive, but location, catering and speakers cost money I guess.
  • I will join this event again next year.

Jazoon 2009: Day 2

Skipping the keynote I joined the following sessions:

  • Securing AJAX Applications by Moritz Kuhn and Philipp Färber from AdNovum Informatik AG
  • A complete Tour of JSF 2.0 (again by Ed Burns)
  • SLF4J and logback by Ceki Gülcü from QOS.CH. I will definitely look at this logging framework soon as an powerful alternative to standard java.util.logging (link)
  • Short introduction to JavaFX Rich Internet Applications connecting to GlassFish Java EE 5 services by Ludovic Champenois from Sun Microsystems.
  • Gaming with JavaFX: Developing the Next Generation of Casual Games

New to Jazoon was the Jazoon Rookie Contest, where the 3 choosen finalists presenteded their concepts, ideas or application. This years winners is João Arthur Brunet Monteiro with quite an impressive wizard to verify sourcecode against design conventions (read more at jazoon.com).The day ended with Jazoon Party.

More info on Jazoon 2009 at jazoon.com.

Jazoon 2009: Day 1

The opening session of day 1 of this years Jazoon conference (which seems to be no.2 in size following Javapolis, now called Devoxx) was opened with a keynote by James Gosling (wiki link), the famous brain behind or father of Java. An impressive person and it is an unique experience to hear him talking live, even I am sure he is used to this publicity fuzz, treated like a rockstar (take note of the book Secrets of Rockstar Programmers by Ed Burns that also covers James Gosling).

Further tracks I attended:

  • Dierk Koenig (author of Groovy in Action) on Usage Patterns for Groovy (link). Who is not looking yet at Groovy should be doing so soon.
  • Design Pattern in Dynamic Languages by Neal Ford (blog).
  • Hand Dockter about the build-system Gradle (link). The build system without stable release yet, tries to find its place somewhere in the ANT and MAVEN landscape offering some more dynamic features.
  • Next generation Builds for Enterprise Systems with Maven, Hudson, Nexus and more by Jason van Zyl (bit too complex and too many features for me)
  • Java Rule Engines. ILog versus Drools. A short overview.

Keynote by Neal Ford on how to stay up-to-date with technology and predict upcoming hits and flops. This is my third time I joins his sessions on various topics. He is definetly very experienced and entertaining presenter. Followed by Ivar Jacobson (one of the 3 amigos, link) on agile and smart methodology/strategies.

More info on Jazoon 2009 at jazoon.com.