Skip to content

Commit 695061f

Browse files
committed
Fixes issue scalatron#39: Server: fails to initialize correctly when run from a directory containing spaces
1 parent ba3621a commit 695061f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Scalatron/src/scalatron/scalatron/impl/CompileActor.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ case class CompileActor(verbose: Boolean) extends Actor {
3333
* @return a collection of class paths to append to the classPath property of the Scala compiler
3434
*/
3535
private def detectScalaClassPaths : Iterable[String] = {
36-
val scalatronJarFilePath = classOf[CompileActor].getProtectionDomain.getCodeSource.getLocation.getPath
37-
val scalaCompilerClassPath = classOf[Global].getProtectionDomain.getCodeSource.getLocation.getPath
38-
val scalaLibraryClassPath = classOf[List[_]].getProtectionDomain.getCodeSource.getLocation.getPath
36+
val scalatronJarFilePath = ScalatronImpl.getClassPath(classOf[CompileActor])
37+
val scalaCompilerClassPath = ScalatronImpl.getClassPath(classOf[Global])
38+
val scalaLibraryClassPath = ScalatronImpl.getClassPath(classOf[List[_]])
3939
if( verbose ) {
4040
println(" detected class path for Scalatron: " + scalatronJarFilePath)
4141
println(" detected class path for scala-compiler: " + scalaCompilerClassPath)

Scalatron/src/scalatron/scalatron/impl/ScalatronImpl.scala

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.util.concurrent.{ThreadPoolExecutor, ThreadFactory, LinkedBlockingQu
1616
import akka.dispatch.ExecutionContext
1717
import java.security.Permission
1818
import java.io.FilePermission
19+
import java.net.URLDecoder
1920

2021

2122
object ScalatronImpl
@@ -63,7 +64,7 @@ object ScalatronImpl
6364

6465

6566
val sandboxUntrustedCode = argMap.get("-sandboxed").getOrElse("no") == "yes"
66-
val scalatronJarFilePath = classOf[ScalatronImpl].getProtectionDomain.getCodeSource.getLocation.getPath
67+
val scalatronJarFilePath = getClassPath(classOf[ScalatronImpl])
6768
implicit val executionContextForUntrustedCode = createExecutionContextForUntrustedCode(
6869
scalatronJarFilePath,
6970
scalatronInstallationDirectoryPath + "/out/",
@@ -223,6 +224,24 @@ object ScalatronImpl
223224
sandboxedExecutionContext
224225
}
225226

227+
228+
/** Given a class, this method determines the file system path of the .jar file the class definition was
229+
* loaded from. It handles the URLDecoding that is necessary to e.g. deal with paths that contain spaces.
230+
* Example usage: <code>val scalatronJarFilePathAsURL = getClass(classOf[ScalatronImpl])</code>
231+
* @param theClass the class whose .jar file path is sought
232+
* @tparam T the type of the class (not used)
233+
* @return a string representing the file system path to the .jar file
234+
*/
235+
def getClassPath[T](theClass: Class[T]) : String = {
236+
// The path returned here is URL encoded, resulting in components like "Documents%20and%20Settings"
237+
val jarFilePathAsURL = theClass.getProtectionDomain.getCodeSource.getLocation.getPath
238+
239+
// so we URL-decode the path before returning it:
240+
val characterEncoding = "UTF-8" // Eight-bit UCS Transformation Format
241+
URLDecoder.decode(jarFilePathAsURL, characterEncoding)
242+
}
243+
244+
226245
// try to locate a base directory for the installation, e.g. '/Scalatron'
227246
def detectInstallationDirectory(verbose: Boolean) = {
228247
// Strategy A: use the Java user directory
@@ -231,7 +250,8 @@ object ScalatronImpl
231250
// val scalatronInstallationDirectoryPath = userDirectoryPath + "/.."
232251

233252
// Strategy B: use the path to the Scalatron.jar file
234-
val scalatronJarFilePath = classOf[ScalatronImpl].getProtectionDomain.getCodeSource.getLocation.getPath
253+
val scalatronJarFilePath = getClassPath(classOf[ScalatronImpl])
254+
235255
if (verbose) println("Detected Scalatron class path to be: " + scalatronJarFilePath)
236256

237257
val scalatronInstallationDirectoryPath =

0 commit comments

Comments
 (0)