Skip to content

Commit 3684c77

Browse files
raniejadegalderz
authored andcommitted
Issue-#30 Initial commit for compiling scala verticles.
1 parent 47be12f commit 3684c77

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/main/scala/org/vertx/scala/platform/impl/ScalaVerticleFactory.scala

+38-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.vertx.scala.platform.impl
1818

19+
import scala.collection.mutable
1920
import scala.reflect.internal.util.BatchSourceFile
2021
import scala.reflect.io.Path.string2path
2122
import scala.reflect.io.PlainFile
@@ -26,7 +27,6 @@ import org.vertx.java.core.{Vertx => JVertx}
2627
import org.vertx.java.platform.{Container => JContainer}
2728
import org.vertx.java.platform.{Verticle => JVerticle}
2829
import org.vertx.java.platform.VerticleFactory
29-
import org.vertx.scala.core.Vertx
3030
import org.vertx.scala.platform.Verticle
3131

3232
/**
@@ -35,7 +35,7 @@ import org.vertx.scala.platform.Verticle
3535
*/
3636
class ScalaVerticleFactory extends VerticleFactory {
3737

38-
protected val PREFIX: String = "scala:"
38+
protected val SUFFIX: String = ".scala"
3939

4040
private val settings = new Settings()
4141

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

4848
private var interpreter: IMain = null
4949

50+
private val classLoader = classOf[ScalaVerticleFactory].getClassLoader
51+
52+
private val classCache = mutable.Map[String, java.lang.Class[_]]()
53+
5054
override def init(jvertx: JVertx, jcontainer: JContainer, aloader: ClassLoader): Unit = {
5155
this.jvertx = jvertx
5256
this.jcontainer = jcontainer
5357
this.loader = aloader
54-
settings.embeddedDefaults(aloader)
55-
settings.usejavacp.value = true
56-
// settings.verbose.value = true
57-
interpreter = new IMain(settings)
58-
interpreter.setContextClassLoader()
58+
59+
println("HELLO WORLD")
60+
61+
initializeScalaInterpreter()
5962
}
6063

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

7879
@throws(classOf[Exception])
7980
private def loadScript(main: String): Class[_] = {
80-
val resolved = loader.getResource(main).toExternalForm()
81-
interpreter.compileSources(new BatchSourceFile(PlainFile.fromPath(resolved.replaceFirst("file:", ""))))
81+
val resolved = loader.getResource(main).toExternalForm
8282
val className = main.replaceFirst(".scala$", "").replaceAll("/", ".")
83-
interpreter.classLoader.loadClass(className)
83+
var cls = classCache.get(className).getOrElse(null)
84+
85+
if (cls == null) {
86+
interpreter.compileSources(new BatchSourceFile(PlainFile.fromPath(resolved.replaceFirst("file:", ""))))
87+
cls = interpreter.classLoader.loadClass(className)
88+
}
89+
90+
classCache += className -> cls
91+
92+
cls
93+
}
94+
95+
private def initializeScalaInterpreter(): Unit = {
96+
val scalaLibrary = classLoader.getResource("./lib/scala-library-2.10.2.jar").toExternalForm
97+
val scalaReflectLibrary = classLoader.getResource("./lib/scala-reflect-2.10.2.jar").toExternalForm
98+
val modLangScala = classLoader.getResource("./").toExternalForm
99+
100+
settings.bootclasspath.append(scalaLibrary.replaceFirst("file:", ""))
101+
settings.bootclasspath.append(scalaReflectLibrary.replaceFirst("file:", ""))
102+
settings.bootclasspath.append(modLangScala.replaceFirst("file:", ""))
103+
104+
settings.usejavacp.value = true
105+
settings.verbose.value = true
106+
interpreter = new IMain(settings)
107+
interpreter.classLoader
108+
interpreter.setContextClassLoader()
84109
}
85110

86111
}

0 commit comments

Comments
 (0)