Tuesday, January 5, 2016

Simple class versioning in Scala

This post describes a very simple library versioning scheme using a class loader and jar file identified by its URL.
Versioning is a common challenge in commercial software development. The most common technique to support multiple versions of a library, executable or framework, in Java or Scala relies on the class loader.
A library can be easily versioned by creating multiple jar files, one for each version.

Simple implementation
Let's select a simple case for each a library is self-contained in a jar file with two versions
  • ArrayIndex1.jar
  • ArrayIndex2.jar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.net.{URL, URLClassLoader}
import java.io.File
import java.lang.reflect.Method

val cl1 = new URLClassLoader( 
  Array[URL](new File("ArrayIndex1.jar").toURL),   
  Thread.currentThread().getContextClassLoader()
)
val cl2 = new URLClassLoader( 
  Array[URL](new File("ArrayIndex2.jar").toURL),  
  Thread.currentThread().getContextClassLoader()
)
  
val compatibleClass: Class[_] = 
  if( version > 0.9) 
    cl1.loadClass("ArrayIndex") 
  else 
    cl2.loadClass("ArrayIndex")

val obj = compatibleClass.newInstance
val methods = compatibleClass.getMethods
println(s"${methods.map( _.getName).mkString(",")}")

The first step consists of loading the two versions of the library through their URL by converting each jar file names to a URL. This feat is accomplished by instantiating the class loaders: In this particular case, the class loader is undefined by its URL with the type URLClassLoader (line 5 & 9). The jar files are loaded within the current thread (line 7 & 11): A more efficient approach would consists of creating a future to load the classes asynchronously.
The class to be used in this case depends on the variable version (line 15). Once the appropriate class is loaded, its instance and method are ready available to the client be invoked.

3 comments:

  1. Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities.Java Course in Chennai | Best JAVA Training in Chennai

    ReplyDelete
  2. To learn shorter programs when compared to Java then it is the right time to choose python training. There is increasing market demand for professionals who have completed python training.
    python training in chennai | python training institutes in chennai

    ReplyDelete