Skip to content

Commit

Permalink
Issue-vert-x#30 Find all jars under lib/ directory automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
galderz committed Aug 27, 2013
1 parent 59b4b3b commit 0ae95fc
Showing 1 changed file with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ import org.vertx.java.platform.{Container => JContainer}
import org.vertx.java.platform.{Verticle => JVerticle}
import org.vertx.java.platform.VerticleFactory
import org.vertx.scala.platform.Verticle
import java.io.{FilenameFilter, File}
import scala.util.matching.Regex

/**
* @author swilliams
*
* @author Ranie Jade

This comment has been minimized.

Copy link
@raniejade

raniejade Aug 28, 2013

Minor issue, can you change this to Ranie Jade Ramiso. :D

This comment has been minimized.

Copy link
@galderz

galderz Aug 29, 2013

Author Owner

Sure

* @author Galder Zamarreño
*/
class ScalaVerticleFactory extends VerticleFactory {

import ScalaVerticleFactory._

protected val SUFFIX: String = ".scala"

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

println("HELLO WORLD")

initializeScalaInterpreter()
}

Expand Down Expand Up @@ -93,12 +96,14 @@ class ScalaVerticleFactory extends VerticleFactory {
}

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

settings.bootclasspath.append(scalaLibrary.replaceFirst("file:", ""))
settings.bootclasspath.append(scalaReflectLibrary.replaceFirst("file:", ""))
val modLangScala = classLoader.getResource("./").toExternalForm
settings.bootclasspath.append(modLangScala.replaceFirst("file:", ""))

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

}

object ScalaVerticleFactory {

val JarFileRegex = "(.*.jar)".r

/**
* Find all files matching the given regular expression in the directory.
* Note that the search is not recursive.
*
* @param directory File denoting directory to search files in
* @param regex regular expression to match
* @return an [[scala.Array[File]] representing the collection of files matching
* the regular expression
*/
def findAll(directory: File, regex: Regex): Array[File] = {
println(s"Find $regex pattern in $directory")
// Protect against null return from listing files
val files: Array[File] = directory.listFiles(new FilenameFilter {
def accept(dir: File, name: String): Boolean = {
regex.findFirstIn(name).isDefined
}
})
Option(files).getOrElse(Array[File]())
}

/**
* Find all files matching the given regular expression in a path within a
* classloader. Note that the search is not recursive.
*
* @param classLoader class repository where to look for files
* @param path String representing the path within the classloader where to look for files
* @param regex regular expression to match
* @return an [[scala.Array[File]] representing the collection of files matching
* the regular expression
*/
def findAll(classLoader: ClassLoader, path: String, regex: Regex): Array[File] = {
println(s"Find $regex pattern in $classLoader")
findAll(new File(classLoader.getResources(path).nextElement().toURI), regex)
}

}

0 comments on commit 0ae95fc

Please sign in to comment.