diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/MainGenericRunner.scala | 68 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/ObjectRunner.scala | 44 |
2 files changed, 81 insertions, 31 deletions
diff --git a/src/compiler/scala/tools/nsc/MainGenericRunner.scala b/src/compiler/scala/tools/nsc/MainGenericRunner.scala index 55a214e492..e554ccfa72 100644 --- a/src/compiler/scala/tools/nsc/MainGenericRunner.scala +++ b/src/compiler/scala/tools/nsc/MainGenericRunner.scala @@ -10,6 +10,7 @@ package scala.tools.nsc import java.io.File import java.lang.{ClassNotFoundException, NoSuchMethodException} import java.lang.reflect.InvocationTargetException +import java.net.{MalformedURLException, URL} /** An object that runs Scala code. It has three possible * sources for the code to run: pre-compiled code, a script file, @@ -76,16 +77,62 @@ object MainGenericRunner { return } - def paths(str: String) = str.split(File.pathSeparator).toList - def listJars(dirs: String): List[String] = + def paths0(str: String): List[String] = + str.split(File.pathSeparator).toList + + def fileToURL(f: File): Option[URL] = + try { + Some(f.toURL) + } + catch { + case e: MalformedURLException => + Console.println(e) + None + } + + def paths(str: String): List[URL] = for ( - val libdir <- (paths(dirs) map { s => new File(s) }); libdir.exists; !libdir.isFile; - val jar <- libdir.listFiles; jar.isFile; jar.getName.endsWith(".jar") - ) yield jar.toString - val classpath: List[String] = + file <- paths0(str) map (new File(_)) if file.exists; + val url = fileToURL(file); if !url.isEmpty + ) yield url.get + + def jars(dirs: String): List[URL] = + for ( + libdir <- paths0(dirs) map (new File(_)) if libdir.isDirectory; + jarfile <- libdir.listFiles if jarfile.isFile && jarfile.getName.endsWith(".jar"); + val url = fileToURL(jarfile); if !url.isEmpty + ) yield url.get + + def specToURL(spec: String): Option[URL] = + try { + Some(new URL(spec)) + } + catch { + case e: MalformedURLException => + Console.println(e) + None + } + + def urls(specs: String): List[URL] = { + val urls = for ( + spec <- specs.split(" ").toList; + val url = specToURL(spec); if !url.isEmpty + ) yield url.get + if (!urls.isEmpty && (System.getSecurityManager == null)) { + // Here we require a security manager to be present ! + // Security permissions are defined in a user-defined + // file to be specified in the environment variable + // JAVA_OPTS="-Djava.security.policy=scala.policy" + System.setSecurityManager(new SecurityManager()) + } + urls + } + + val classpath: List[URL] = paths(settings.bootclasspath.value) ::: paths(settings.classpath.value) ::: - listJars(settings.extdirs.value) + jars(settings.extdirs.value) ::: + urls(settings.Xcodebase.value) command.thingToRun match { case None => @@ -113,7 +160,12 @@ object MainGenericRunner { e.getCause.printStackTrace } } else { - ScriptRunner.runScript(settings, thingToRun, command.arguments) + try { + ScriptRunner.runScript(settings, thingToRun, command.arguments) + } catch { + case e: SecurityException => + Console.println(e) + } } } } diff --git a/src/compiler/scala/tools/nsc/ObjectRunner.scala b/src/compiler/scala/tools/nsc/ObjectRunner.scala index 5bafbd7cc7..b674e18755 100644 --- a/src/compiler/scala/tools/nsc/ObjectRunner.scala +++ b/src/compiler/scala/tools/nsc/ObjectRunner.scala @@ -7,10 +7,9 @@ package scala.tools.nsc -import java.io.File import java.lang.{Class, ClassNotFoundException, NoSuchMethodException} -import java.lang.reflect.{Method,Modifier} -import java.net.URLClassLoader +import java.lang.reflect.{Method, Modifier} +import java.net.{URL, URLClassLoader} /** An object that runs another object specified by name. * @@ -25,15 +24,18 @@ object ObjectRunner { * @param objectName ... * @return ... */ - def findClass(classpath: List[String], objectName: String) + private def findClass(classpath: List[URL], objectName: String) : Option[Class] = { - val classpathURLs = classpath.map(s => new File(s).toURL).toArray - val mainLoader = new URLClassLoader(classpathURLs, null) try { + val mainLoader = new URLClassLoader(classpath.toArray, null) Some(Class.forName(objectName, true, mainLoader)) } catch { - case _:ClassNotFoundException => None + case e: SecurityException => + Console.println(e.getMessage) + None + case _: ClassNotFoundException => + None } } @@ -42,10 +44,10 @@ object ObjectRunner { * * @param classpath ... * @param objectName ... - * @return ... + * @return <code>true</code> iff ... */ - def classExists(classpath: List[String], objectName: String) = - !(findClass(classpath, objectName).isEmpty) + def classExists(classpath: List[URL], objectName: String): Boolean = + !findClass(classpath, objectName).isEmpty /** Run a given object, specified by name, using a * specified classpath and argument list. @@ -58,20 +60,16 @@ object ObjectRunner { * @throws NoSuchMethodError ... * @throws InvocationTargetException ... */ - def run( - classpath: List[String], - objectName: String, - arguments: Seq[String]): Unit = - { - val clsToRun = findClass(classpath, objectName) match { - case Some(cls) => cls - case None => throw new ClassNotFoundException(objectName) - } + def run(classpath: List[URL], objectName: String, arguments: Seq[String]) { + val clsToRun = findClass(classpath, objectName) match { + case Some(cls) => cls + case None => throw new ClassNotFoundException(objectName) + } - val method = clsToRun.getMethod("main", List(classOf[Array[String]]).toArray) - if ((method.getModifiers & Modifier.STATIC) == 0) - throw new NoSuchMethodException(objectName + ".main is not static") + val method = clsToRun.getMethod("main", List(classOf[Array[String]]).toArray) + if ((method.getModifiers & Modifier.STATIC) == 0) + throw new NoSuchMethodException(objectName + ".main is not static") - method.invoke(null, List(arguments.toArray).toArray) + method.invoke(null, List(arguments.toArray).toArray) } } |