Skip to content

Commit 6e91dff

Browse files
committed
Issue-#30 Find all jars under lib/ directory automatically
1 parent 3684c77 commit 6e91dff

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

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

+54-8
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ import org.vertx.java.platform.{Container => JContainer}
2828
import org.vertx.java.platform.{Verticle => JVerticle}
2929
import org.vertx.java.platform.VerticleFactory
3030
import org.vertx.scala.platform.Verticle
31+
import java.io.{FilenameFilter, File}
32+
import scala.util.matching.Regex
3133

3234
/**
3335
* @author swilliams
34-
*
36+
* @author Ranie Jade Ramiso
37+
* @author Galder Zamarreño
3538
*/
3639
class ScalaVerticleFactory extends VerticleFactory {
3740

41+
import ScalaVerticleFactory._
42+
3843
protected val SUFFIX: String = ".scala"
3944

4045
private val settings = new Settings()
@@ -56,8 +61,6 @@ class ScalaVerticleFactory extends VerticleFactory {
5661
this.jcontainer = jcontainer
5762
this.loader = aloader
5863

59-
println("HELLO WORLD")
60-
6164
initializeScalaInterpreter()
6265
}
6366

@@ -93,12 +96,14 @@ class ScalaVerticleFactory extends VerticleFactory {
9396
}
9497

9598
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+
for {
100+
jar <- findAll(classLoader, "lib", JarFileRegex)
101+
} yield {
102+
println(s"Found $jar, add to compiler bootstrap")
103+
settings.bootclasspath.append(jar.getAbsolutePath)
104+
}
99105

100-
settings.bootclasspath.append(scalaLibrary.replaceFirst("file:", ""))
101-
settings.bootclasspath.append(scalaReflectLibrary.replaceFirst("file:", ""))
106+
val modLangScala = classLoader.getResource("./").toExternalForm
102107
settings.bootclasspath.append(modLangScala.replaceFirst("file:", ""))
103108

104109
settings.usejavacp.value = true
@@ -108,4 +113,45 @@ class ScalaVerticleFactory extends VerticleFactory {
108113
interpreter.setContextClassLoader()
109114
}
110115

116+
}
117+
118+
object ScalaVerticleFactory {
119+
120+
val JarFileRegex = "^(.*\\.jar)$".r
121+
122+
/**
123+
* Find all files matching the given regular expression in the directory.
124+
* Note that the search is not recursive.
125+
*
126+
* @param directory File denoting directory to search files in
127+
* @param regex regular expression to match
128+
* @return an [[scala.Array[File]] representing the collection of files matching
129+
* the regular expression
130+
*/
131+
def findAll(directory: File, regex: Regex): Array[File] = {
132+
println(s"Find $regex pattern in $directory")
133+
// Protect against null return from listing files
134+
val files: Array[File] = directory.listFiles(new FilenameFilter {
135+
def accept(dir: File, name: String): Boolean = {
136+
regex.findFirstIn(name).isDefined
137+
}
138+
})
139+
Option(files).getOrElse(Array[File]())
140+
}
141+
142+
/**
143+
* Find all files matching the given regular expression in a path within a
144+
* classloader. Note that the search is not recursive.
145+
*
146+
* @param classLoader class repository where to look for files
147+
* @param path String representing the path within the classloader where to look for files
148+
* @param regex regular expression to match
149+
* @return an [[scala.Array[File]] representing the collection of files matching
150+
* the regular expression
151+
*/
152+
def findAll(classLoader: ClassLoader, path: String, regex: Regex): Array[File] = {
153+
println(s"Find $regex pattern in $classLoader")
154+
findAll(new File(classLoader.getResources(path).nextElement().toURI), regex)
155+
}
156+
111157
}

0 commit comments

Comments
 (0)