Skip to content

Commit

Permalink
Issue-vert-x#30 Initial commit for compiling scala verticles.
Browse files Browse the repository at this point in the history
  • Loading branch information
raniejade authored and galderz committed Aug 28, 2013
1 parent 47be12f commit cf04a29
Showing 1 changed file with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.vertx.scala.platform.impl

import scala.collection.mutable
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.Path.string2path
import scala.reflect.io.PlainFile
Expand All @@ -26,7 +27,6 @@ import org.vertx.java.core.{Vertx => JVertx}
import org.vertx.java.platform.{Container => JContainer}
import org.vertx.java.platform.{Verticle => JVerticle}
import org.vertx.java.platform.VerticleFactory
import org.vertx.scala.core.Vertx
import org.vertx.scala.platform.Verticle

/**
Expand All @@ -35,7 +35,7 @@ import org.vertx.scala.platform.Verticle
*/
class ScalaVerticleFactory extends VerticleFactory {

protected val PREFIX: String = "scala:"
protected val SUFFIX: String = ".scala"

private val settings = new Settings()

Expand All @@ -47,22 +47,23 @@ class ScalaVerticleFactory extends VerticleFactory {

private var interpreter: IMain = null

private val classLoader = classOf[ScalaVerticleFactory].getClassLoader

private val classCache = mutable.Map[String, java.lang.Class[_]]()

override def init(jvertx: JVertx, jcontainer: JContainer, aloader: ClassLoader): Unit = {
this.jvertx = jvertx
this.jcontainer = jcontainer
this.loader = aloader
settings.embeddedDefaults(aloader)
settings.usejavacp.value = true
// settings.verbose.value = true
interpreter = new IMain(settings)
interpreter.setContextClassLoader()

println("HELLO WORLD")

initializeScalaInterpreter()
}

@throws(classOf[Exception])
override def createVerticle(main: String): JVerticle = {
// TODO: main without language Id is passed in.
//val rawClass = if (main.startsWith(PREFIX)) loader.loadClass(main.replaceFirst(PREFIX, "")) else loadScript(main)
val rawClass = loader.loadClass(main.replaceFirst(PREFIX, ""))
val rawClass = if (!main.endsWith(SUFFIX)) loader.loadClass(main) else loadScript(main)
val delegate = rawClass.newInstance().asInstanceOf[Verticle]
ScalaVerticle.newVerticle(delegate, jvertx, jcontainer)
}
Expand All @@ -77,10 +78,34 @@ class ScalaVerticleFactory extends VerticleFactory {

@throws(classOf[Exception])
private def loadScript(main: String): Class[_] = {
val resolved = loader.getResource(main).toExternalForm()
interpreter.compileSources(new BatchSourceFile(PlainFile.fromPath(resolved.replaceFirst("file:", ""))))
val resolved = loader.getResource(main).toExternalForm
val className = main.replaceFirst(".scala$", "").replaceAll("/", ".")
interpreter.classLoader.loadClass(className)
var cls = classCache.get(className).getOrElse(null)

if (cls == null) {
interpreter.compileSources(new BatchSourceFile(PlainFile.fromPath(resolved.replaceFirst("file:", ""))))
cls = interpreter.classLoader.loadClass(className)
}

classCache += className -> cls

cls
}

private def initializeScalaInterpreter(): Unit = {
val scalaLibrary = classLoader.getResource("./lib/scala-library-2.10.2.jar").toExternalForm
val scalaReflectLibrary = classLoader.getResource("./lib/scala-reflect-2.10.2.jar").toExternalForm
val modLangScala = classLoader.getResource("./").toExternalForm

settings.bootclasspath.append(scalaLibrary.replaceFirst("file:", ""))
settings.bootclasspath.append(scalaReflectLibrary.replaceFirst("file:", ""))
settings.bootclasspath.append(modLangScala.replaceFirst("file:", ""))

settings.usejavacp.value = true
settings.verbose.value = true
interpreter = new IMain(settings)
interpreter.classLoader
interpreter.setContextClassLoader()
}

}

0 comments on commit cf04a29

Please sign in to comment.