Thursday, July 14, 2016

Java 9 Process API

Java 9 will come with interesting features. Here is a quick review of the new Java 9 Process API.

Use Cases for the Java Process API

These are probably some ways how we are using the Java Process API:

  • To Install, configure or to communicate to other programs from Java: For example, I implemented a Java Installer to install the WebSphere Application Server into the system, and then I configure it by running a script coded in Jython executed the wsadmin tool.
  • To install specific Operating System stuff. I didn’t find any other simpler way to install Windows start menu items from Java other than by doing it in a VBScript program.
  • In my opinion, the Java Process API is in general used to integrate Java with other programs or processes. For example. I made a game in Blender and I implemented a launcher in Java. So, from Java I am calling the blender.exe passing the game.blender file as argument.

Known Problems and Workarounds

  • There are common issues in Windows when invoking commands which have spaces or stream redirections. The solution is to use instead the “cmd /C” command. This oracle blog talks about this. There is also an Apache commons-exec library apparently solving the same issues, but its kind of abandoned project.
  • Another issue when running processes is how to read from standard output and error streams properly. The solution is basically creating separate threads for reading the streams so the process don’t get stucked. There is an elegant solution in this blog.
  • On the other “side” (literally), if we want to kill a Java Process from the Operating System we may use the Java jps command to get the PID. Then, for instance, we can use kill -9 to kill it.
  • Finally. Sometimes when calling a Process it ends up hanging. Hopefully the Java 9 implementation will be more stable about this issue.

Java 9 Specification

Taking a look at the new spec I have to say the 4 new features look really nice! Having that features available, we will be able to implement a pure Java Windows “Task Manager” like; because now we will be able to get rid of some process calls to get process data.

Getting started with Java 9

  • Download the Java 9 version from
  • Install java 9 on folder /usr/lib/jvm/java-9-oracle
  • Check installation by running /usr/lib/jvm/java-9-oracle/bin/java -version

Code examples

So after reading about the new spec I got hands on. I compared the Javadoc for the 8 version and the new Java 9 and I found interesting classes like java.lang.ProcessHandle & java.lang.Process.Info. Lets see what we can do with them! (the full source code for the examples is located here):

Get PID of current and created processes

System.out.println("Running java process with PID " + ProcessHandle.current().getPid()+ ". Parent: " + ProcessHandle.current().parent().get().getPid());
Process pr = Runtime.getRuntime().exec(cmdArray);
System.out.println("Create Process with PID "+pr.getPid());

Enumerate Process List

ProcessHandle.allProcesses().forEach(e -> System.out.println(e.getPid() + " " +;

Deal with Process Trees

In this case, we want to deploy a process tree in order to kill everything afterwards. For this, we can implement a Java Process that creates the same Java Process recursively, finally we kill it.
Process ptree = RunHelper.exec(java, output, "-cp", cp, RecursiveJavaProcess.class.getName(), "3");

Compile & execute!

$ cd src/test/java
$ /usr/lib/jvm/java-9-oracle/bin/javac org/andresoviedo/tests/java9/process/
$ /usr/lib/jvm/java-9-oracle/bin/javac org/andresoviedo/tests/java9/process/
$ /usr/lib/jvm/java-9-oracle/bin/java org.andresoviedo.tests.java9.process.NewJava9ProcessAPI


From the main 4 features the spec mentions, I have tested the first 3. So where is the 4th? About dealing with hundred of sub-processes? I didn’t find in the current version any new option for doing that. Probably they are still implementing it. I don't know. However, if we want, we can now implement very easily a pure Java Task Manager GUI application for managing the system processes :)

Although I would like to know more real complex use cases to further evaluate the API, so far the promised features seems to work. Anyway, I am wondering what else would be nice to have for the future in the API. Probably a way to manage the process priority? I don’t know. For me, the API looks quite complete for now. I hope you like the new features.

Feel free to leave your comments. Thank you for reading me :)

Wednesday, April 6, 2016

Android 3D Model Viewer Open Source

So finally I decided to publish my 3D model Viewer android app on the Google Play Market :)

This app is about drawing 3D objects in android using OpenGL ES 2.0; and its Open Source as my other projects, so people can either contribute, start a similar project based on that or just learn how to do it by looking at the source code.

Although there is already similar apps on the market and lots of 3D frameworks out there, neither of the published apps are Open Source.

So I wanted to learn how 3D works and I think the best way to understand how it works is by looking at how matrices transforms the objects in space and how pixels are drawn to the screen. Thats why this app does not use any third party framework.

So far the app serves as a model viewer, but I have some ideas of what can be done with the code; or as I prefer to think of it, mini-3D-framework.  So for example, I am thinking in creating my very first and simple mini-game and I want to do it in 3D as it is a matter I really like.

If you want to see whats this all about, please download the app to your android device and give it a try:

Source code:

Thank you for reading me.

Saturday, March 5, 2016

Introduction to Software Design Patterns

This is a presentation I did for the company I work Digital Origin. Its an introduction about architecting software solutions using Design Patterns.  Hope you like it.

What is a Design Pattern

"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice"

- Cristopher Alexander -

"Is a general resuable solution to common ocurring problems. It is a description of template for how to solve a problem in different situations"

- Wikipedia -

Prerequisites & Basic Principles

These are the basic principles one should be concerned before applying patterns.
  • Use Object Oriented language
  • Use of Abstraction
  • Use of Encapsulation
  • Use High Cohesion
  • Use Loose Coupling
  • Use Open - Closed priniple
  • Use Separation Of Concerns
  • Use Dependency Inversion
  • Separate volatile from Stable components
  • Use Interfaces simple, clear and course grained
  • Use Composite reuse instead of inheritance

Most Famous Patterns

  • Layers Pattern
  • Tiers Pattern
  • Model-View-Controller Pattern
  • P.A.C. Architectural Pattern

The Layers Pattern

The Layers pattern structures applications so they can be decomposed into groups of subtasks, each group represents a particular level of abstraction.

The Tiers Pattern

Tiers describe how a distribute application is decomposed into modules to reduce the coupling and imprive the system flexibility. This permits each tier to be modified or enhanced without affecting other tiers. Example for the JAVA world.

The MVC Pattern

The MVC Pattern separates concerns in data presentation, processing and storage. This pattern is useful for emphasizing separation of concerns.

PAC Architectural Pattern

The PAC pattern is a further refinement of the Model View Controller pattern. For complex UIs, it creates a hierarchy of interacting, cooperating agents. Each agen is its own "mini" MVC component. It differs from MVC in that within each agent, the presentation and abstraction are completely separated. This pattern enforces decoupling and separation of concerns.

Pattern Catalog

Here is a list where you can find more design patterns.
  • Hardware Configuration Patterns
  • Software Design Patterns
    • GOF Patterns (Gang Of Four Patterns)
    • Java EE Patterns
  • Architecture Patterns
    • Buschmann, et al. Patterns
    • Enterprise Architecture Integration Patterns
    • Enterprise Application Architecture Patterns
  • Antipatterns: to avoid common design traps
    • Analysis Paralysis
    • Cash Cow
    • Silos
    • God Object

GOF Catalog Of Design Patterns

24 Design Patterns that will help you solve the majority of your problems.
  • Abstract Factory
  • Adapter
  • Bridge
  • Builder
  • Chain Of Responsability
  • Command
  • Composite
  • Decorator
  • Facade
  • Factory Method
  • Flyweight
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • Prototype
  • Proxy
  • Singleton
  • State
  • Strategy
  • Template
  • Visitor
  • Method

Pattern Classification

The patterns can be classified according to the scope and purpose.

JAVA Patterns

The JAVA language defines another set of design patterns.
  • DAO (Data Access Object)
  • Service Locator
  • Facade
  • Transfer Object
  • Application Service
  • Business Delegate
  • Value List Handler
  • Filter
  • Front Controller
  • Application Controller
  • Context Object
  • View Helper

Using Patterns Across a Network

When working in a network, the Command Pattern is an example of design pattern to send messages from the client to the server. 

Pattern Integration

Several patterns can be combined to architect your solution as show in the picture.


  • Oracle JEE Student Guide for JEE Architects
  • Design Patterns: Elements Of Reusable Object-Oriented Software
    • Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides

Tuesday, August 4, 2015

How to build a replicated ehcache through firewall and not to die trying

It's assumed that:

  • You already have a java application and you have an ehcache instance running in your program with 1 cache configured to replicate.
  • You want to connect 1 instance to another instance you have in a remote machine
  • The remote machine you are connecting to is located behind a firewall.
  • You have been given access to only 1 port to make an ssh connection to the remote (firewalled) machine.
Ehcache uses RMI to connect the remote instances. When the cache in host 1 tries to connect to cache in host 2, RMI opens a new server socket in host 1 to listen for incoming connections from the host 2.
The problem here is that these random ports are not accesible to remote hosts because the hosts are behind a firewall.

So to fix it, we need to configure ehcache to only use 2 ports. The "remoteObjectPort" does the magic. So we have this ehcache.xml configuration:
  • 1 server port for the ehcache server to listen to other ehcache servers.
  • 1 server port for the ehcache cache to listen to incoming connections (this cache is the actual map in memory that is being replicated).
And the related properties are these. Inject it with System.setProperties(ehcacheProperties) before loading the ehcache instance in your program.
Open the 4 tunnels to the remote machine:

  • 1 local port for the ehcache server to listen to incoming connections from remote cache server
  • 1 local port for the ehcache cache for incoming connections from the remote cache
  • 1 remote port for remote ehcache server to listen incoming connections from our local host
  • 1 remote port for remote ehcache cache to listen to incoming connections from our local host

Run java programs on both machines (local and remote) with the "java.rmi.server.hostname" so the RMI server reports to the other RMI (the remote one) that it's address is "" rather than any other IP it may has; so the tunnels can work. If you have any comment or suggestion please contact me.

Saturday, July 25, 2015

How to package a jar-with-dependencies with maven and not to die trying!

How to package a jar-with-dependencies with maven and how to manage resource collisions (if we have multiple log4j.xml for example).

Sometimes it's easier to distribute our application in a single fat jar (myapp-1.0-jar-with-dependencies.jar) instead of distributing our jar + libs.jar. To achieve this, we can use maven-assembly-plugin and the jar-with-dependencies descriptor.

However, the jar-with-dependencies works but until we have resource collisions. Imagine that you have your own /src/main/resources/log4.xml but one of your dependencies (i.e. dependency.jar) has a /log4j.xml as well.

To handle this situation, we can redefine the jar-with-dependencies assembly descriptor so we start packing the fat jar first with our own project files and then include the rest of the dependencies. That will work because maven will not overwrite files that already exists.

Another thing to keep in mind, is that it works fine for any resource, except for the MANIFEST.MF file that is handled by the maven archiver-plugin. In that case, we must redefine the manifest configuration in the maven-assembly-plugin.

Here is the configuration you need to package your own myapp-1.0.0-jar-with-dependencies.jar with your own log4j and MANIFEST and not die trying:
And here is the src/main/assembly/jar-with-dependencies:xml:

Sunday, November 10, 2013

Google Drive FTP Adapter

Google Drive FTP Adapter

This open source application lets you connect your FTP applications to your Google Drive files through the FTP protocol rather than using the Official's Google Drive client.

This custom google drive client was created because official's one can't be reinstalled in a new PC without having to redownload all your drive files again, from the cloud to your local PC.  So if you have hundred of Gigas and a regular ADSL it would take weeks to complete.
Also, because the official client does not support FAT32 partitions and I used to have all my files in one of this partitions.

So this application basically starts a FTP server in your local machine emulating that it is hosting your google drive files, acting as a gateway. Once this setup is done, you can connect any FTP client to connect to your google drive files. I use it in conjunction with Beyond compare to compare my local files (stored anywhere in my hard drive) and compare them to ones I have in the google drive cloud.
You can download and see the instructions in