diff --git a/src/main/scala/org/vertx/scala/platform/impl/ScalaVerticleFactory.scala b/src/main/scala/org/vertx/scala/platform/impl/ScalaVerticleFactory.scala index 1d7c9de..e405a0a 100644 --- a/src/main/scala/org/vertx/scala/platform/impl/ScalaVerticleFactory.scala +++ b/src/main/scala/org/vertx/scala/platform/impl/ScalaVerticleFactory.scala @@ -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 @@ -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 /** @@ -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() @@ -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) } @@ -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() } } \ No newline at end of file