Scala Nuggets: Finding All Classes in a Package


Recently, while doing a project for one of our clients in the US, we had the opportunity to build a plugin based framework for the product. While, the details of the plugin framework are reserved as a topic of another post, in this one we would see how easy it is to find all the classes in a package using a utility provided by Clapper. The name of the utility is ClassUtil and you can get more details from their site.

Our scenario was that we should be able to discover any new plugins which are added for the product at the time when the product is initialized. For this the above utility came in pretty handy.

The simple code as you would expect in Scala is

def init() {
    val classpath = List(".").map(new File(_))
    val finder = ClassFinder(classpath)
    val classes = finder.getClasses
    val classMap = ClassFinder.classInfoMap(classes)
    val plugins = ClassFinder.concreteSubclasses("com.inphina.plugin.ProdPlugin", classMap)

    plugins.foreach {
      pluginString =>
        val plugin = Class.forName(pluginString.name).newInstance().asInstanceOf[ProdPlugin]
        pluginMap += (plugin.name -> pluginString.name)
    }
  }

As you would notice, we start from the current location and scan for all the files in that location which extend our trait called com.inphina.plugin.ProdPlugin
This, returns back a list of all classes which are extending the trait. Next, we instantiate the plugin and put it in a map for our product to consume it. There are several other ways in which you can filter your classes and you would get more details here.

If you are using SBT 0.10x then you can add the dependency in build.sbt with the following

name := "InphinaProductManager"
version := "1.0"
organization := "com.inphina.prod"
scalaVersion := "2.9.0-1"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "se.scalablesolutions.akka" % "akka-actor" % "1.1.3",
  "se.scalablesolutions.akka" % "akka-typed-actor" % "1.1.3",
  "se.scalablesolutions.akka" % "akka-amqp" % "1.1.3",
  "se.scalablesolutions.akka" % "akka-testkit" % "1.1.3"
)

libraryDependencies += "org.scalatest" % "scalatest_2.9.0" % "1.6.1"

libraryDependencies += "org.clapper" % "classutil_2.9.0" % "0.3.6"

A word of caution, finding classes in such a way is an involved process and might not be performant for your scenario. For our case, we have to do it only once at the start of the product and the number of plugins would not exceed a score.

Advertisements

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. Knoldus does niche Reactive and Big Data product development on Scala, Spark and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Scala and tagged , , . Bookmark the permalink.

3 Responses to Scala Nuggets: Finding All Classes in a Package

  1. Sonny says:

    Hey Vikas,

    Neat! How I wish I was using Scala 🙂

    Looking forward to your post on the design of the plugin framework. I have been thinking about introducing a plugin based design in my current project. Need some fodder for those grey cells.

    Is it implemented in Scala? Did you find any usecases where it would have been an order of magnitude more difficult to do it in Java?

    Cheers,
    Sonny

    • Hey Sonny, yeah would be posting the design soon. Yes, its is implemented in Scala but it would be quite simple in Java as well. We are using Scala in the current project for web scalability but we get concise and better looking code as a bonus 🙂

  2. Pingback: Building a Plugin Based Architecture in Scala « Inphina Thoughts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s