Author: reto
Date: Tue Jun 15 21:01:09 2010
New Revision: 955052

URL: http://svn.apache.org/viewvc?rev=955052&view=rev
Log:
CLEREZZA-221: doing the compiling as priviledged

Modified:
    
incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala

Modified: 
incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala?rev=955052&r1=955051&r2=955052&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
 (original)
+++ 
incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
 Tue Jun 15 21:01:09 2010
@@ -27,6 +27,8 @@ import org.osgi.framework.BundleListener
 import org.osgi.service.component.ComponentContext;
 import org.osgi.framework.Bundle
 import java.io.{File, PrintWriter, Reader, StringWriter}
+import java.security.AccessController
+import java.security.PrivilegedAction
 import java.util.{ArrayList, Arrays};
 //import scala.collection.immutable.Map
 import scala.tools.nsc._;
@@ -199,49 +201,57 @@ class ScriptEngineFactory() extends  Jav
                val virtualDirectory = new VirtualDirectory("(memory)", None)
                val msgWriter = new StringWriter
                lazy val compiler = {
-                       val settings = new Settings     
-                       settings.outputDirs setSingleOutput virtualDirectory
-                       val out = new PrintWriter(System.out)
-                       new BundleContextScalaCompiler(bundleContext, settings, 
                                                                           
-                       new ConsoleReporter(settings, null, out) {
-                               override def printMessage(msg: String) {
-                                       msgWriter write msg
-                                       //out.flush()
+                       AccessController.doPrivileged(new 
PrivilegedAction[BundleContextScalaCompiler]() {
+                               override def run() =  {
+                                       val settings = new Settings     
+                                       settings.outputDirs setSingleOutput 
virtualDirectory
+                                       val out = new PrintWriter(System.out)
+                                       new 
BundleContextScalaCompiler(bundleContext, settings,                             
                                               
+                                               new ConsoleReporter(settings, 
null, out) {
+                                                       override def 
printMessage(msg: String) {
+                                                               msgWriter write 
msg
+                                                               //out.flush()
+                                                       }
+                                               })
                                }
                        })
                }
                
                override def compile(script: String): CompiledScript = {
-                       //inefficient but thread safe
-                       compiler.synchronized {
-                               val objectName = "CompiledScript"+classCounter
-                               classCounter += 1
-                               val classCode = "object " + objectName + """ {
-                                       |       def run($: Map[String, Object]) 
= {
-                                       |""".stripMargin + script +"""
-                                       |       }
-                                       |}""".stripMargin
-                               val sources: List[SourceFile] = List(new 
BatchSourceFile("<script>", classCode))
-                               (new compiler.Run).compileSources(sources)
-                               if (compiler.reporter.hasErrors) {
-                                       throw new 
ScriptException(msgWriter.toString, "script", -1);
-                               }
-                               new CompiledScript() {
-                                       override def eval(context: 
ScriptContext) = {
-                                               var map = Map[String, Object]()
-                                               import 
_root_.scala.collection.JavaConversions._
-                                               for (   scope <- 
context.getScopes;
-                                                               if 
(context.getBindings(scope.intValue) != null);
-                                                               entry <- 
context.getBindings(scope.intValue)) {
-                                                       map = map + (entry._1 
-> entry._2)
+                       AccessController.doPrivileged(new 
PrivilegedAction[CompiledScript]() {
+                               override def run() =  {
+                                       //inefficient but thread safe
+                                       compiler.synchronized {
+                                               val objectName = 
"CompiledScript"+classCounter
+                                               classCounter += 1
+                                               val classCode = "object " + 
objectName + """ {
+                                                       |       def run($: 
Map[String, Object]) = {
+                                                       |""".stripMargin + 
script +"""
+                                                       |       }
+                                                       |}""".stripMargin
+                                               val sources: List[SourceFile] = 
List(new BatchSourceFile("<script>", classCode))
+                                               (new 
compiler.Run).compileSources(sources)
+                                               if 
(compiler.reporter.hasErrors) {
+                                                       throw new 
ScriptException(msgWriter.toString, "script", -1);
+                                               }
+                                               new CompiledScript() {
+                                                       override def 
eval(context: ScriptContext) = {
+                                                               var map = 
Map[String, Object]()
+                                                               import 
_root_.scala.collection.JavaConversions._
+                                                               for (   scope 
<- context.getScopes;
+                                                                               
if (context.getBindings(scope.intValue) != null);
+                                                                               
entry <- context.getBindings(scope.intValue)) {
+                                                                       map = 
map + (entry._1 -> entry._2)
+                                                               }
+                                                               val classLoader 
= new AbstractFileClassLoader(virtualDirectory, this.getClass.getClassLoader())
+                                                               val runMethod = 
classLoader.findClass(objectName).getMethod("run", classOf[Map[String, Object]])
+                                                               
runMethod.invoke(null, map)
+                                                       }
+                                                       override def getEngine 
= MyScriptEngine.this
                                                }
-                                               val classLoader = new 
AbstractFileClassLoader(virtualDirectory, this.getClass.getClassLoader())
-                                               val runMethod = 
classLoader.findClass(objectName).getMethod("run", classOf[Map[String, Object]])
-                                               runMethod.invoke(null, map)
                                        }
-                                       override def getEngine = 
MyScriptEngine.this
                                }
-                       }
+                       })
                }
 
                


Reply via email to