clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject svn commit: r949836 - in /incubator/clerezza/trunk/scala-scripting: ./ script-engine/ script-engine/src/main/resources/OSGI-INF/ script-engine/src/main/scala/org/apache/clerezza/scala/console/ script-engine/src/main/scala/org/apache/clerezza/scala/scri...
Date Mon, 31 May 2010 19:15:45 GMT
Author: reto
Date: Mon May 31 19:15:45 2010
New Revision: 949836

URL: http://svn.apache.org/viewvc?rev=949836&view=rev
Log:
CLEREZZA-221 Actor based interpreter thread

Added:
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaInterpreter.scala
      - copied, changed from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleContextScalaInterpreter.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleFS.scala
      - copied, changed from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/InterpreterFactory.scala
      - copied, changed from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/InterpreterFactory.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
      - copied, changed from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/ScriptEngineFactory.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/Utils.scala
      - copied, changed from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/Utils.scala
Removed:
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleContextScalaInterpreter.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/InterpreterFactory.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/InterpreterService.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/Launcher.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/ScriptEngineFactory.scala
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/Utils.scala
Modified:
    incubator/clerezza/trunk/scala-scripting/pom.xml
    incubator/clerezza/trunk/scala-scripting/script-engine/pom.xml
    incubator/clerezza/trunk/scala-scripting/script-engine/src/main/resources/OSGI-INF/serviceComponents.xml
    incubator/clerezza/trunk/scala-scripting/tests/src/test/scala/org/apache/clerezza/scala/tests/ScriptEngineFactoryTest.scala

Modified: incubator/clerezza/trunk/scala-scripting/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/pom.xml?rev=949836&r1=949835&r2=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/pom.xml (original)
+++ incubator/clerezza/trunk/scala-scripting/pom.xml Mon May 31 19:15:45 2010
@@ -60,6 +60,12 @@
 						</goals>
 					</execution>
 				</executions>
+				<configuration>
+
+					<args>
+						<arg>-deprecation</arg>
+					</args>
+				</configuration>
 			</plugin>
 		</plugins>
 	</build>

Modified: incubator/clerezza/trunk/scala-scripting/script-engine/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/pom.xml?rev=949836&r1=949835&r2=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/pom.xml (original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/pom.xml Mon May 31 19:15:45 2010
@@ -45,10 +45,10 @@
 				<configuration>
 					<instructions>
 						<Service-Component>OSGI-INF/serviceComponents.xml</Service-Component>
-						<Export-Pacakge>org.apache.clerezza.scala.console</Export-Pacakge>
-						<Private-Package>org.apache.clerezza.scala.console</Private-Package>
+						<Export-Pacakge>org.apache.clerezza.scala.scripting</Export-Pacakge>
+						<Private-Package>org.apache.clerezza.scala.scripting</Private-Package>
 						<DynamicImport-Package>*</DynamicImport-Package>
-						<Bundle-SymbolicName>org.apache.clerezza.scala.console</Bundle-SymbolicName>
+						<Bundle-SymbolicName>org.apache.clerezza.scala.scripting</Bundle-SymbolicName>
 					</instructions>
 				</configuration>
 			</plugin>

Modified: incubator/clerezza/trunk/scala-scripting/script-engine/src/main/resources/OSGI-INF/serviceComponents.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/src/main/resources/OSGI-INF/serviceComponents.xml?rev=949836&r1=949835&r2=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/src/main/resources/OSGI-INF/serviceComponents.xml
(original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/src/main/resources/OSGI-INF/serviceComponents.xml
Mon May 31 19:15:45 2010
@@ -1,29 +1,29 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.0.0">
-    <scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.console.InterpreterService">
-        <implementation class="org.apache.clerezza.scala.console.InterpreterService"/>
-        <property name="service.pid" value="org.apache.clerezza.scala.console.InterpreterService"/>
-    </scr:component>
-	<scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.console.InterpreterFactory">
-        <implementation class="org.apache.clerezza.scala.console.InterpreterFactory"/>
+    <!-- <scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.scripting.InterpreterService">
+        <implementation class="org.apache.clerezza.scala.scripting.InterpreterService"/>
+        <property name="service.pid" value="org.apache.clerezza.scala.scripting.InterpreterService"/>
+    </scr:component> -->
+	<scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.scripting.InterpreterFactory">
+        <implementation class="org.apache.clerezza.scala.scripting.InterpreterFactory"/>
 		<service servicefactory="false">
-            <provide interface="org.apache.clerezza.scala.console.InterpreterFactory"/>
+            <provide interface="org.apache.clerezza.scala.scripting.InterpreterFactory"/>
         </service>
-        <property name="service.pid" value="org.apache.clerezza.scala.console.InterpreterFactory"/>
+        <property name="service.pid" value="org.apache.clerezza.scala.scripting.InterpreterFactory"/>
     </scr:component>
-	<scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.console.ScriptEngineFactory">
-        <implementation class="org.apache.clerezza.scala.console.ScriptEngineFactory"/>
+	<scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.scripting.ScriptEngineFactory">
+        <implementation class="org.apache.clerezza.scala.scripting.ScriptEngineFactory"/>
 		<service servicefactory="false">
             <provide interface="javax.script.ScriptEngineFactory"/>
         </service>
-        <property name="service.pid" value="org.apache.clerezza.scala.console.ScriptEngineFactory"/>
+        <property name="service.pid" value="org.apache.clerezza.scala.scripting.ScriptEngineFactory"/>
 		<reference name="interpreterFactory"
-				interface="org.apache.clerezza.scala.console.InterpreterFactory"
+				interface="org.apache.clerezza.scala.scripting.InterpreterFactory"
 				cardinality="1..1"
 				bind="bindInterpreterFactory" unbind="unbindInterpreterFactory"/>
     </scr:component>
-	<!-- <scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.console.Launcher">
-        <implementation class="org.apache.clerezza.scala.console.Launcher"/>
-        <property name="service.pid" value="org.apache.clerezza.scala.console.Launcher"/>
+	<!-- <scr:component enabled="true" immediate="true" name="org.apache.clerezza.scala.scripting.Launcher">
+        <implementation class="org.apache.clerezza.scala.scripting.Launcher"/>
+        <property name="service.pid" value="org.apache.clerezza.scala.scripting.Launcher"/>
     </scr:component> -->
 </components>

Copied: incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaInterpreter.scala
(from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleContextScalaInterpreter.scala)
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaInterpreter.scala?p2=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaInterpreter.scala&p1=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleContextScalaInterpreter.scala&r1=949595&r2=949836&rev=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleContextScalaInterpreter.scala
(original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaInterpreter.scala
Mon May 31 19:15:45 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.clerezza.scala.console;
+package org.apache.clerezza.scala.scripting;
 
 
 
@@ -48,42 +48,41 @@ class BundleContextScalaInterpreter(bund
 	override lazy val classLoader: AbstractFileClassLoader = {
 		new AbstractFileClassLoader(virtualDirectory, this.getClass.getClassLoader())
 	}
-		override protected def newCompiler(settings: Settings, reporter: Reporter) = {
-			reporter.info(scala.tools.nsc.util.NoPosition, "new Compiler", true)
-			settings.outputDirs setSingleOutput virtualDirectory
-			new Global(settings, reporter) {
-				private lazy val _classPath: ClassPath[AbstractFile] = {
-
-					val classPathOrig: ClassPath[AbstractFile]  = new PathResolver(settings).result
-
-					val classPathAbstractFiles = for (bundle <- bundles;
-							val url = bundle.getResource("/");
-							if url != null) yield {
-						if ("file".equals(url.getProtocol())) {
-							new PlainFile(new File(url.toURI()))
-						}
-						else {
-							BundleFS.create(bundle);
-						}
+	override protected def newCompiler(settings: Settings, reporter: Reporter) = {
+		settings.outputDirs setSingleOutput virtualDirectory
+		new Global(settings, reporter) {
+			private lazy val _classPath: ClassPath[AbstractFile] = {
+
+				val classPathOrig: ClassPath[AbstractFile]  = new PathResolver(settings).result
+
+				val classPathAbstractFiles = for (bundle <- bundles;
+						val url = bundle.getResource("/");
+						if url != null) yield {
+					if ("file".equals(url.getProtocol())) {
+						new PlainFile(new File(url.toURI()))
+					}
+					else {
+						BundleFS.create(bundle);
 					}
-					val classPaths: List[ClassPath[AbstractFile]] = (for (abstractFile <- classPathAbstractFiles)
-						yield {
-							new DirectoryClassPath(abstractFile, classPathOrig.context)
-						}) toList
-
-				   val classPath = new MergedClassPath[AbstractFile](classPathOrig :: classPaths,
-								classPathOrig.context)
-					classPath
-				}
-				override lazy val classPath: ClassPath[_] = {
-					_classPath
 				}
+				val classPaths: List[ClassPath[AbstractFile]] = (for (abstractFile <- classPathAbstractFiles)
+					yield {
+						new DirectoryClassPath(abstractFile, classPathOrig.context)
+					}) toList
+
+			   val classPath = new MergedClassPath[AbstractFile](classPathOrig :: classPaths,
+							classPathOrig.context)
+				classPath
+			}
+			override lazy val classPath: ClassPath[_] = {
+				_classPath
+			}
 
-				override def rootLoader: LazyType = {
-		
-					new loaders.JavaPackageLoader(_classPath)
-				}
+			override def rootLoader: LazyType = {
+
+				new loaders.JavaPackageLoader(_classPath)
 			}
 		}
 	}
+}
 

Copied: incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleFS.scala
(from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala)
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleFS.scala?p2=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleFS.scala&p1=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala&r1=949595&r2=949836&rev=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala
(original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/BundleFS.scala
Mon May 31 19:15:45 2010
@@ -21,9 +21,9 @@ import scala.tools.nsc.io.AbstractFile
 import java.io.{File, InputStream, OutputStream, IOException}
 import java.net.URL
 import org.osgi.framework.Bundle
-import org.apache.clerezza.scala.console.Utils.{valueOrElse, nullOrElse}
+import org.apache.clerezza.scala.scripting.Utils.{valueOrElse, nullOrElse}
 
-package org.apache.clerezza.scala.console {
+package org.apache.clerezza.scala.scripting {
 
 
 

Copied: incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/InterpreterFactory.scala
(from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/InterpreterFactory.scala)
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/InterpreterFactory.scala?p2=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/InterpreterFactory.scala&p1=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/InterpreterFactory.scala&r1=949595&r2=949836&rev=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/InterpreterFactory.scala
(original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/InterpreterFactory.scala
Mon May 31 19:15:45 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.clerezza.scala.console;
+package org.apache.clerezza.scala.scripting;
 
 
 

Copied: incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
(from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/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?p2=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala&p1=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/ScriptEngineFactory.scala&r1=949595&r2=949836&rev=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/ScriptEngineFactory.scala
(original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
Mon May 31 19:15:45 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.clerezza.scala.console;
+package org.apache.clerezza.scala.scripting;
 
 
 
@@ -36,8 +36,9 @@ import scala.tools.nsc.reporters.Reporte
 import scala.tools.util.PathResolver
 import scala.tools.nsc.util.{ClassPath, JavaClassPath}
 import javax.script.ScriptContext
-import javax.script.{ScriptEngineFactory => JavaxEngineFactory, ScriptEngine, AbstractScriptEngine,
Bindings, SimpleBindings}
-
+import javax.script.{ScriptEngineFactory => JavaxEngineFactory, ScriptEngine, AbstractScriptEngine,
Bindings, SimpleBindings, ScriptException}
+import scala.actors.Actor
+import scala.actors.Actor._
 
 class ScriptEngineFactory() extends  JavaxEngineFactory {
 
@@ -98,22 +99,49 @@ class ScriptEngineFactory() extends  Jav
 			}
 			eval(scriptStringWriter.toString, context)
 		}
-		override def eval(script : String, context : ScriptContext) : Object = {
-			//not yet threadsafe
-			val jTypeMap : java.util.Map[String, java.lang.reflect.Type] =
-				new java.util.HashMap[String, java.lang.reflect.Type]()
-			val valueMap = new java.util.HashMap[String, Any]()
-			import _root_.scala.collection.JavaConversions._
-			for (scope <- context.getScopes;
-					if (context.getBindings(scope.intValue) != null);
-					entry <- context.getBindings(scope.intValue)) {
-				interpreter.bind(entry._1,
-								 getAccessibleClass(entry._2.getClass).getName, entry._2)
-			}
 
-			interpreter.eval[Object](script) match   {
-				case Some(x) => x
-				case None => null
+		lazy val interpreterAction = actor {
+				//not using loop { react {, as this method doesn't seem to guarantee
+				//asynchronous execution
+				//also using react with a final invocation of act() different exception from interprter.bind
have been seen
+				while(true) {
+					receive {
+						case (script : String, context : ScriptContext) => {
+							//not yet threadsafe, but the test isn't failing
+							//should pass jobs to actor that guarantees they are executed sequentially
+							//and binding to not inferfere
+							val jTypeMap : java.util.Map[String, java.lang.reflect.Type] =
+								new java.util.HashMap[String, java.lang.reflect.Type]()
+							val valueMap = new java.util.HashMap[String, Any]()
+							import _root_.scala.collection.JavaConversions._
+							for (scope <- context.getScopes;
+								 if (context.getBindings(scope.intValue) != null);
+								 entry <- context.getBindings(scope.intValue)) {
+								interpreter.bind(entry._1,
+												 getAccessibleClass(entry._2.getClass).getName, entry._2)
+							}
+							val result = interpreter.eval[Object](script) match   {
+								case Some(x) => x
+								case None => null
+							}
+							if (interpreter.reporter.hasErrors) {
+								throw new ScriptException("some error","script-file",1)
+							}
+							sender ! result
+						}
+					}
+				}
+		}
+
+
+		override def eval(script : String, context : ScriptContext) : Object = {
+			/*val timeout = 180*1000
+			interpreterAction !? (timeout, (script, context)) match {
+				case Some(x : Object) => x
+				case x => throw new RuntimeException("Timeout executing script")
+			}*/
+			interpreterAction !? ((script, context)) match {
+				case x : Object => x
 			}
 		}
 		override def getFactory() = ScriptEngineFactory.this

Copied: incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/Utils.scala
(from r949595, incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/Utils.scala)
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/Utils.scala?p2=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/Utils.scala&p1=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/Utils.scala&r1=949595&r2=949836&rev=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/console/Utils.scala
(original)
+++ incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/Utils.scala
Mon May 31 19:15:45 2010
@@ -14,12 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.clerezza.scala.console
+package org.apache.clerezza.scala.scripting
 
 /**
  * General purpose utility functions
  */
-object Utils{
+object Utils {
 
   /**
    * Evaluate <code>f</code> on <code>s</code> if <code>s</code>
is not null.
@@ -27,7 +27,7 @@ object Utils{
    * @param f
    * @return <code>f(s)</code> if s is not <code>null</code>, <code>null</code>
otherwise.
    */
-  protected[console] def nullOrElse[S, T](s: S)(f: S => T): T =
+  protected[scripting] def nullOrElse[S, T](s: S)(f: S => T): T =
     if (s == null) null.asInstanceOf[T]
     else f(s)
 
@@ -36,7 +36,7 @@ object Utils{
    * @param default
    * @return <code>t</code> or <code>default</code> if <code>null</code>.
    */
-  protected[console] def valueOrElse[T](t: T)(default: => T) =
+  protected[scripting] def valueOrElse[T](t: T)(default: => T) =
     if (t == null) default
     else t
 
@@ -46,7 +46,7 @@ object Utils{
    * @returns <code>Some(value)</code> if value is not <code>null</code>,
    * <code>None</code> otherwise.
    */
-    protected[console] def option[T](value: T): Option[T] =
+    protected[scripting] def option[T](value: T): Option[T] =
     if (null == value) None else Some(value)
 
 }

Modified: incubator/clerezza/trunk/scala-scripting/tests/src/test/scala/org/apache/clerezza/scala/tests/ScriptEngineFactoryTest.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/tests/src/test/scala/org/apache/clerezza/scala/tests/ScriptEngineFactoryTest.scala?rev=949836&r1=949835&r2=949836&view=diff
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/tests/src/test/scala/org/apache/clerezza/scala/tests/ScriptEngineFactoryTest.scala
(original)
+++ incubator/clerezza/trunk/scala-scripting/tests/src/test/scala/org/apache/clerezza/scala/tests/ScriptEngineFactoryTest.scala
Mon May 31 19:15:45 2010
@@ -32,8 +32,10 @@ import org.junit.Before;
 import org.junit.Test;
 import org.ops4j.pax.exam.Inject;
 import org.osgi.framework.BundleContext;
-import javax.script.ScriptEngineFactory
-import org.osgi.util.tracker.ServiceTracker;
+import javax.script.{ScriptEngineFactory, Bindings, ScriptException}
+import org.osgi.util.tracker.ServiceTracker
+import scala.actors.Actor
+import scala.math.random
 
 
 /**
@@ -50,13 +52,12 @@ class ScriptEngineFactoryTest {
 	
 	private var webServerExist = false;
 
-	private var scriptEngineFactory: ScriptEngineFactory = null;
+	private var factory: ScriptEngineFactory = null;
 
 	@Before
 	def getService() : Unit = {
 		/*webServerExist = waitForWebserver();*/
-		scriptEngineFactory = waitFor(classOf[ScriptEngineFactory], 300000);
-		println("Got: "+scriptEngineFactory)
+		factory = waitFor(classOf[ScriptEngineFactory], 300000);
 	}
 
 	private def waitFor[T](aClass: Class[T], timeout: Long): T = {
@@ -70,9 +71,67 @@ class ScriptEngineFactoryTest {
 
 	@Test
 	def checkEngine(): Unit =  {
-		Assert.assertNotNull(scriptEngineFactory)
+		Assert.assertNotNull(factory)
+		Assert.assertEquals("Scala Scripting Engine for OSGi", factory.getEngineName);
+		val s = "hello"
+		val engine = factory.getScriptEngine
+		Assert.assertEquals(s, engine.eval("\""+s+"\""))
+		val bindings = engine.createBindings
+		bindings.put("s",s)
+		Assert.assertEquals(s, engine.eval("s", bindings))
 	}
 
+	@Test
+	def testConcurrency : Unit = {
+		import scala.actors.Actor._
+		val actorsCount = 5
+		val iterationsCount = 9
+		val testRunner = self
+		for (i <- 1 to actorsCount) {
+			object ValueVerifier extends Actor {
+				def act() {
+					try {
+						for (i <- 1 to iterationsCount) {
+							val s = "r: "+random.toString
+							val engine = factory.getScriptEngine
+							val bindings = engine.createBindings
+							bindings.put("s",s)
+							val script = """
+import scala.math.random
+Thread.sleep((random*10).toInt)
+s"""
+							testRunner ! (s, engine.eval(script, bindings))
+						}
+					} catch {
+						case t => testRunner ! t
+					}
+				}
+			}
+			ValueVerifier.start()
+		}
+		for (i <- 1 to (actorsCount*iterationsCount)) {
+			self.receive {
+				case (expected, got) => {
+						Assert.assertEquals(expected, got)
+				}
+				case t : Throwable => throw t
+			}
+		}
+
+	}
+
+	//This seems hard to realize before https://lampsvn.epfl.ch/trac/scala/ticket/3513 is fixed
+	/*@Test
+	def checkException(): Unit =  {
+		val s = """val s="hello"
+		illegal.do"""
+		val engine = factory.getScriptEngine
+		try {
+			Assert.assertEquals("should have exception",engine.eval(s))
+		} catch {
+			case e : ScriptException => Assert.assertEquals(2, e.getLineNumber)
+		}
+	}*/
 }
 
 object ScriptEngineFactoryTest {



Mime
View raw message