Java and Scala Dynamic Load of a Class

I was in the need to dynamically create a Object from the Database and execute a method to run a task. This object can be different based on the requirements from user input.

/**
  * Handle Execution returned from database
  *
  * @param ex
  * @param text
  * @param sender
  */
def handleExecution(ex: Execution, text: String, sender: Sender): Unit = {

  if (ex.execution.split(":").size == 3 ) {
    val cname = ex.execution.split(":")(1)
    val fname = ex.execution.split(":")(2)

    try {
      val theclass = Class.forName(cname).newInstance().asInstanceOf[TExecutionService]

      if (theclass.isInstanceOf[TExecutionService]) {
        val output = theclass.run(text)
        Logger.debug("Output from " + cname + ".run " + text + " -- " + output)
        handleOutput(output, sender)
      }
    } catch {
      case exception: Throwable => {
        Logger.debug("Error in HandleExecution " + ex + " with exception " + exception)
        throw exception
      }
    }
  }

 

Although Scala is a Typed Language it was not difficult to assure that I would have a run method for the returned class and execute this code dynamically.

 

Anúncios

Scala Playframework connect to mysql

I’m writting this because I had issues following the documentation that comes with playframework
the docs are wrong or at least for me it gave me an error.

The docs states that to connect to a mysql database server you have to do the following:

If you need to connect to another JDBC-compliant database, first add the corresponding driver library to your application dependencies in build.sbt e.g.:

libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.18"

This above works ok, I had no problems.

Then add the corresponding JDBC configuration to the conf/application.conf file:

db.url=jdbc:mysql:database_name
db.driver=org.mysql.Driver
db.user=root
db.pass=secret

This above now has problems namely the db.driver section it returned me an error
java.lang.ClassNotFoundException: org.mysql.Driver

Searching the internet I found this answer

http://solvedstack.com/questions/steps-needed-to-use-mysql-database-with-play-framework-2-0

So the correct working version is:

db.default.driver=com.mysql.jdbc.Driver
db.default.user=root
db.default.pass=secret

db.default.url=”jdbc:mysql://localhost/database_name?characterEncoding=UTF-8″

Maybe for others this is easy to spot but for me it wasn’t.

Java, Scala

Reading about scala I could only be curious about it, went to read details about the language and how it works. I get the idea that this is happening:

PHP is moving in the Java style direction… (Not trying to compare anything.)

Java is moving into a simpler coding Groovy, Scala ( in some cases harder) has Gosling him self professed. Both languages are being used in the financial markets( scala 1, scala 2, scala 3, Groovy ) for trading algorithms and social network Twitter , LinkedIn, it is said that Actors in Scala are a great way for Async communication Akka.Actors is said to be even better. Would like to have seen this two years ago, could have used this instead of Perl for a daemon I wrote. Both Scala and Groovy can use imported Java code.

James Gosling:
http://www.theserverside.com/news/2240022782/James-Gosling-Interview-from-Basementcoderscom

“I like Scala, but my big problem is that the type theory has gotten really complicated. The language lawyers are driving the bus. It’s turning into something that journeyman developers and even people like me… I started getting into the Scala stuff and my head starts to spin and I go, “what?””

Some Scala comments.

http://www.schauderhaft.de/2010/12/05/is-it-bad-to-use-scala-as-java-with-a-more-concise-syntax/

http://www.infoq.com/news/2009/07/scala-replace-java

Notice:
Gosling is listed as an advisor at the Scala company Typesafe Inc.[13] and Strategic Advisor for Eucalyptus.[14]

Clearly I’m more curious about Scala http://twitter.github.io/scala_school/ although Groovy has company’s supporting it but at the moment I’m just looking and playing not using. Scala Puzzlers