Author: reto
Date: Sat Nov  6 10:06:02 2010
New Revision: 1031988

URL: http://svn.apache.org/viewvc?rev=1031988&view=rev
Log:
CLEREZZA-353: clerezza shell

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/  
 (props changed)
      - copied from r1030117, 
incubator/clerezza/trunk/scala-scripting/script-engine/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ConsoleShell.scala
      - copied, changed from r1030117, 
incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/Shell.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ShellFactory.scala
Removed:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaCompiler.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/scala/scripting/BundleContextScalaInterpreter.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/scala/scripting/BundleFS.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/scala/scripting/InterpreterFactory.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/scala/scripting/Utils.scala
Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/pom.xml
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/resources/OSGI-INF/serviceComponents.xml

Propchange: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sat Nov  6 10:06:02 2010
@@ -0,0 +1,5 @@
+target
+.classpath
+.project
+.externalToolBuilders
+.settings

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/pom.xml?rev=1031988&r1=1030117&r2=1031988&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/pom.xml
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/pom.xml
 Sat Nov  6 10:06:02 2010
@@ -1,15 +1,25 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?><project 
xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
        <modelVersion>4.0.0</modelVersion>
        <parent>
-               <groupId>org.apache.clerezza.scala</groupId>
-               <artifactId>scala</artifactId>
-               <version>0.1-incubating-SNAPSHOT</version>
+               <groupId>org.apache.clerezza</groupId>
+               <artifactId>org.apache.clerezza.parent</artifactId>
+               <version>0.2-incubating-SNAPSHOT</version>
        </parent>
-       <artifactId>script-engine</artifactId>
+       <artifactId>org.apache.clerezza.shell</artifactId>
+       <version>0.1-incubating-SNAPSHOT</version>
        <packaging>bundle</packaging>
-       <name>Scala OSGi Services - Script Engine</name>
+       <name>Clerezza - Shell Service</name>
        <dependencies>
                <dependency>
+                       <groupId>org.apache.clerezza.scala</groupId>
+                       <artifactId>script-engine</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>jline</groupId>
+                       <artifactId>jline</artifactId>
+                       <version>0.9.94</version>
+               </dependency>
+               <dependency>
                        <groupId>org.scala-lang</groupId>
                        <artifactId>scala-compiler</artifactId>
                </dependency>
@@ -20,33 +30,29 @@
                <dependency>
                        <groupId>org.apache.felix</groupId>
                        
<artifactId>org.apache.felix.scr.annotations</artifactId>
-                       <version>1.3.0</version>
                </dependency>
                <dependency>
                        <groupId>org.osgi</groupId>
                        <artifactId>org.osgi.compendium</artifactId>
-                       <version>4.1.0</version>
                </dependency>
                <dependency>
                        <groupId>org.osgi</groupId>
                        <artifactId>org.osgi.core</artifactId>
-                       <version>4.1.0</version>
                </dependency>
        </dependencies>
        <build>
+               <sourceDirectory>src/main/scala</sourceDirectory>
+    <testSourceDirectory>src/test/scala</testSourceDirectory>
                <plugins>
                        <plugin>
                                <groupId>org.apache.felix</groupId>
                                <artifactId>maven-bundle-plugin</artifactId>
-                               <extensions>true</extensions>
-                               <version>2.1.0</version>
                                <configuration>
                                        <instructions>
                                                
<Service-Component>OSGI-INF/serviceComponents.xml</Service-Component>
-                                               
<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.scripting</Bundle-SymbolicName>
+                                               
<Export-Package>org.apache.clerezza.shell</Export-Package>
+                                               
<Private-Package>org.apache.clerezza.shell</Private-Package>
+                                               
<Bundle-SymbolicName>org.apache.clerezza.shell</Bundle-SymbolicName>
                                        </instructions>
                                </configuration>
                        </plugin>
@@ -70,6 +76,11 @@
                                        </classpathContainers>
                                </configuration>
                        </plugin>
+                       <plugin>
+                               <groupId>org.scala-tools</groupId>
+                               <artifactId>maven-scala-plugin</artifactId>
+
+                       </plugin>
                </plugins>
        </build>
 </project>

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/resources/OSGI-INF/serviceComponents.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/resources/OSGI-INF/serviceComponents.xml?rev=1031988&r1=1030117&r2=1031988&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/resources/OSGI-INF/serviceComponents.xml
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/resources/OSGI-INF/serviceComponents.xml
 Sat Nov  6 10:06:02 2010
@@ -1,30 +1,22 @@
 <?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.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.scripting.InterpreterFactory"/>
-        </service>
-        <property name="service.pid" 
value="org.apache.clerezza.scala.scripting.InterpreterFactory"/>
+       <scr:component enabled="true" immediate="true" 
name="org.apache.clerezza.shell.ConsoleShell">
+        <implementation class="org.apache.clerezza.shell.ConsoleShell"/>
+        <property name="service.pid" 
value="org.apache.clerezza.shell.ConsoleShell"/>
+               <reference name="shellFactory"
+                               
interface="org.apache.clerezza.shell.ShellFactory"
+                               cardinality="1..1"
+                               bind="bindShellFactory" 
unbind="unbindShellFactory"/>
     </scr:component>
-       <scr:component enabled="true" immediate="true" 
name="org.apache.clerezza.scala.scripting.ScriptEngineFactory">
-        <implementation 
class="org.apache.clerezza.scala.scripting.ScriptEngineFactory"/>
+       <scr:component enabled="true" immediate="true" 
name="org.apache.clerezza.shell.ShellFactory">
+        <implementation class="org.apache.clerezza.shell.ShellFactory"/>
                <service servicefactory="false">
-            <provide interface="javax.script.ScriptEngineFactory"/>
+            <provide interface="org.apache.clerezza.shell.ShellFactory"/>
         </service>
-        <property name="service.pid" 
value="org.apache.clerezza.scala.scripting.ScriptEngineFactory"/>
-               <property name="javax.script.language" value="scala" />
+        <property name="service.pid" 
value="org.apache.clerezza.shell.ShellFactory"/>
                <reference name="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.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/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ConsoleShell.scala
 (from r1030117, 
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/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ConsoleShell.scala?p2=incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ConsoleShell.scala&p1=incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala&r1=1030117&r2=1031988&rev=1031988&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/ScriptEngineFactory.scala
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ConsoleShell.scala
 Sat Nov  6 10:06:02 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.clerezza.scala.scripting;
+package org.apache.clerezza.shell;
 
 
 
@@ -53,281 +53,48 @@ import scala.tools.nsc.util.{ClassPath, 
 import scala.actors.Actor
 import scala.actors.Actor._
 
-class ScriptEngineFactory() extends  JavaxEngineFactory with BundleListener  {
+class ConsoleShell()  {
 
 
-       def bundleChanged(event: BundleEvent) = {
-               MyScriptEngine.interpreterAction ! 
ScriptEngineFactory.RefreshInterpreter
-       }
-
-       var factory: InterpreterFactory = null
-       var _interpreter : Interpreter = null;
-       private var bundleContext: BundleContext = null
-       def interpreter = {
-               if (_interpreter == null) {
-                       this.synchronized {
-                               if (_interpreter == null) {
-                                       _interpreter = 
factory.createInterpreter(new PrintWriter(System.out))
-                               }
-                       }
-               }
-               _interpreter
-       }
-
 
-       //methods from ScriptEngineFactory
-       override def getEngineName() = "Scala Scripting Engine for OSGi"
-       override def getEngineVersion() = "0.2/scala 2.8.0"
-       override def getExtensions() = 
java.util.Collections.singletonList("scala")
-       override def getMimeTypes() = 
java.util.Collections.singletonList("application/x-scala")
-       override def getNames() = java.util.Collections.singletonList("scala")
-       override def getLanguageName() = "Scala"
-       override def getLanguageVersion ="2.8.0.RC5"
-       override def getParameter(key : String) = {
-               key match {
-                       case ScriptEngine.ENGINE => getEngineName
-                       case ScriptEngine.ENGINE_VERSION => getEngineVersion
-                       case ScriptEngine.NAME => getNames.get(0)
-                       case ScriptEngine.LANGUAGE => getLanguageName
-                       case ScriptEngine.LANGUAGE_VERSION => getLanguageVersion
-                       case _ => null
-               }
-       }
-       override def getMethodCallSyntax(obj : String,
-                                                                        m : 
String,
-                                                                        args : 
String*) = {
-               obj+"."+m+"("+args.mkString(",")+")"
-       }
-       override def getOutputStatement(toDisplay : String) = 
"println(\""+toDisplay+"\")"
-       override def getProgram(statements : String*) = 
statements.mkString("\n")
-       override def getScriptEngine : ScriptEngine = MyScriptEngine
+       var factory: ShellFactory = null
+       var bundleContext: BundleContext = null
+       var stoppedBundle: Option[Bundle] = None
+       var shellOption: Option[Shell] = None
 
        def activate(componentContext: ComponentContext)= {
                bundleContext = componentContext.getBundleContext
-               bundleContext.addBundleListener(this)
+               for (bundle <- bundleContext.getBundles;
+                               if (bundle.getSymbolicName == 
"org.apache.felix.shell.tui");
+                               if (bundle.getState == Bundle.ACTIVE)) {
+                       println("stopping "+bundle);
+                       bundle.stop()
+                       stoppedBundle = Some(bundle)
+               }
+               val shell = factory.createShell
+               shell.start()
+               shellOption = Some(shell)
        }
 
+
        def deactivate(componentContext: ComponentContext) = {
                bundleContext = componentContext.getBundleContext
-               bundleContext.removeBundleListener(this)
+               stoppedBundle match {
+                       case Some(bundle) => bundle.start()
+                       case _ =>
+               }
+               shellOption match {
+                       case Some(shell) => shell.stop()
+                       case _ =>
+               }
        }
 
-       def bindInterpreterFactory(f: InterpreterFactory) = {
+       def bindShellFactory(f: ShellFactory) = {
                factory = f
        }
 
-       def unbindInterpreterFactory(f: InterpreterFactory) = {
+       def unbindShellFactory(f: ShellFactory) = {
                factory = null
-               _interpreter = null
        }
-       /** Inner object as it accesse interpreter
-        */
-       object MyScriptEngine extends AbstractScriptEngine() with Compilable {
-               override def eval(script : Reader, context : ScriptContext) : 
Object = {
-                       val scriptStringWriter = new StringWriter()
-                       var ch = script.read
-                       while (ch != -1) {
-                               scriptStringWriter.write(ch)
-                               ch = script.read
-                       }
-                       eval(scriptStringWriter.toString, context)
-               }
-
-               val interpreterAction = new DaemonActor {
-                       def act() {
-                               //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
-                                                       try {
-                                                               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
-                                                       } catch {
-                                                               case e => 
sender ! ScriptEngineFactory.ActorException(e)
-                                                       }
-                                               }
-                                               case 
ScriptEngineFactory.RefreshInterpreter => {
-                                                               _interpreter = 
null;
-                                               }
-                                       }
-                               }
-                       }
-               }
-               interpreterAction.start()
-
-
-               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 ScriptEngineFactory.ActorException(e) => 
throw e
-                               case x : Object => x
-                       }
-               }
-               override def getFactory() = ScriptEngineFactory.this
-               override def createBindings() : Bindings = new SimpleBindings
-
-               override def compile(script: Reader): CompiledScript = {
-                       val scriptStringWriter = new StringWriter()
-                       var ch = script.read
-                       while (ch != -1) {
-                               scriptStringWriter.write(ch)
-                               ch = script.read
-                       }
-                       compile(scriptStringWriter.toString)
-               }
-
-               
-               var classCounter = 0
-
-               val virtualDirectory = new VirtualDirectory("(memory)", None)
-               var msgWriter = new StringWriter
-
-               val classLoader = new AbstractFileClassLoader(virtualDirectory, 
this.getClass.getClassLoader())
-
-               //var classLoader = createClassLoader
-
-               lazy val compiler = {
-                       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 = {
-                       try {
-                               AccessController.doPrivileged(new 
PrivilegedAction[CompiledScript]() {
-                               override def run() =  {
-
-                                               //inefficient but thread safe
-                                               compiler.synchronized {
-                                                       val objectName = 
"CompiledScript"+classCounter
-                                                       classCounter += 1
-                                                       val classCode = "class 
" + 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) {
-                                                               
compiler.reporter.reset
-                                                               val msg = 
msgWriter.toString
-                                                               msgWriter = new 
StringWriter
-                                                               throw new 
ScriptException(msg, "script", -1);
-                                                       }
-                                                       //val classBytes = 
virtualDirectory.fileNamed(objectName+".class").toCharArray
-                                                       val clazz = 
classLoader.loadClass(objectName)
-                                                       val scriptObject = 
clazz.newInstance()
-
-                                                       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 
runMethod = clazz.getMethod("run", classOf[Map[String, Object]])
-                                                                       try {
-                                                                               
runMethod.invoke(scriptObject, map)
-                                                                       } catch 
{
-                                                                               
case e: InvocationTargetException => {
-                                                                               
        throw e.getCause
-                                                                               
}
-                                                                       }
-                                                               }
-                                                               override def 
getEngine = MyScriptEngine.this
-                                                       }
-                                               }
-                                       }
-                               })
-                       } catch {
-                               case e: PrivilegedActionException => throw 
e.getCause
-                       }
-               }
-
-               
-
-               
-
-               /**
-                * returns an accessible class or interface that is implemented 
by class,
-                * is doesn't look for superinterfaces of implement interfaces
-                */
-               private def getAccessibleClass(clazz : Class[_]) : Class[_] = {
-                       if(isAccessible(clazz)) {
-                               return clazz
-                       } else {
-                               val foo : Class[_] = clazz.getInterfaces()(0)
-                               for (implementedInterface <- 
clazz.getInterfaces()) {
-                                       if (isAccessible(implementedInterface)) 
return implementedInterface
-                               }
-                       }
-                       return getAccessibleSuperClass(clazz)
-               }
-
-               private def getAccessibleSuperClass(clazz : Class[_]) : 
Class[_] = {
-                       val superClass = clazz.getSuperclass
-                       if (superClass == null) {
-                               throw new RuntimeException("No upper class to 
be checked for accessibility for "+clazz)
-                       }
-                       if (isAccessible(superClass)) {
-                               superClass
-                       } else {
-                               getAccessibleSuperClass(superClass)
-                       }
-               }
-
-               private def isAccessible(clazz : Class[_])  = {
-                       try {
-                               Class.forName(clazz.getName)
-                               true
-                       } catch {
-                               case e: Exception => false
-                       }
-               }
-       }
-}
-
-object ScriptEngineFactory {
-       case class ActorException(e: Throwable);
-       case object RefreshInterpreter;
 }
\ No newline at end of file

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/Shell.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/Shell.scala?rev=1031988&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/Shell.scala
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/Shell.scala
 Sat Nov  6 10:06:02 2010
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.shell;
+
+
+
+import org.apache.felix.scr.annotations.Component;
+import org.osgi.framework.BundleContext
+import org.osgi.framework.BundleEvent
+import org.osgi.framework.BundleListener
+import org.osgi.service.component.ComponentContext;
+import org.osgi.framework.Bundle
+import java.io.{File, PrintWriter, Reader, StringWriter, BufferedReader, 
InputStreamReader, InputStream, Writer}
+import java.lang.reflect.InvocationTargetException
+import java.net._
+import java.security.PrivilegedActionException
+import java.security.AccessController
+import java.security.PrivilegedAction
+import java.util.{ArrayList, Arrays};
+import javax.script.ScriptContext
+import javax.script.{ScriptEngineFactory => JavaxEngineFactory, Compilable,
+                                        CompiledScript, ScriptEngine, 
AbstractScriptEngine, Bindings,
+                                        SimpleBindings, ScriptException}
+//import scala.collection.immutable.Map
+import scala.actors.DaemonActor
+import scala.tools.nsc._;
+import scala.tools.nsc.interpreter._;
+import scala.tools.nsc.io.{AbstractFile, PlainFile, VirtualDirectory}
+import scala.tools.nsc.util._
+import scala.tools.nsc.symtab.SymbolLoaders
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.Reporter
+import scala.tools.util.PathResolver
+import scala.tools.nsc.util.{ClassPath, JavaClassPath}
+import scala.actors.Actor
+import scala.actors.Actor._
+import org.apache.clerezza.scala.scripting._
+import java.io.File
+import jline.{ ConsoleReader, ArgumentCompletor, History => JHistory }
+
+class Shell(factory: InterpreterFactory, val inStream: InputStream, out: 
Writer)  {
+
+       //var _interpreter : Interpreter = null;
+       private var bundleContext: BundleContext = null
+       /*def interpreter = {
+               if (_interpreter == null) {
+                       this.synchronized {
+                               if (_interpreter == null) {
+                                       _interpreter = 
factory.createInterpreter(new PrintWriter(System.out, true))
+                               }
+                       }
+               }
+               _interpreter
+       }*/
+
+
+       val interpreterLoop = new InterpreterLoop(new BufferedReader(new 
InputStreamReader(System.in)), new PrintWriter(System.out, true)) {
+               override def createInterpreter() {
+                       interpreter = factory.createInterpreter(out)
+                       interpreter.interpret("import org.apache.clerezza._")
+                       interpreter.interpret("val a = 33")
+                       interpreter.interpret("println(\"enjoy!\")")
+               }
+
+               override val prompt = "zz>"
+
+               override def main(settings: Settings) {
+                       this.settings = settings
+                       createInterpreter()
+
+                       // sets in to some kind of reader depending on 
environmental cues
+                       in = new InteractiveReader() {
+
+                         override lazy val history = 
Some(History(consoleReader))
+                         override lazy val completion = Option(interpreter) 
map (x => new Completion(x))
+
+                         val consoleReader = {
+                               val r = new jline.ConsoleReader(inStream, out)
+                               r setHistory (History().jhistory)
+                               r setBellEnabled false
+                               completion foreach { c =>
+                                 r addCompletor c.jline
+                                 r setAutoprintThreshhold 250
+                               }
+
+                               r
+                         }
+
+                         def readOneLine(prompt: String) = consoleReader 
readLine prompt
+                         val interactive = true
+                       }
+
+                       loadFiles(settings)
+                       try {
+                               // it is broken on startup; go ahead and exit
+                               if (interpreter.reporter.hasErrors) return
+
+                               printWelcome()
+
+                               // this is about the illusion of snappiness.  
We call initialize()
+                               // which spins off a separate thread, then 
print the prompt and try
+                               // our best to look ready.  Ideally the user 
will spend a
+                               // couple seconds saying "wow, it starts so 
fast!" and by the time
+                               // they type a command the compiler is ready to 
roll.
+                               interpreter.initialize()
+                               repl()
+                       }
+                       finally closeInterpreter()
+               }
+       }
+       val console: Actor = actor {
+               //cala.tools.nsc.MainGenericRunner.main(Array[String]());
+               //scalaConsole();
+
+               println("starting console")
+               try {
+                       interpreterLoop.main(Array[String]())
+               } finally {
+                       println("console terminated")
+               }
+       }
+
+       def start() {
+               console.start
+       }
+
+       def stop() {
+               interpreterLoop.command(":q")
+               interpreterLoop.closeInterpreter()
+       }
+
+
+
+}
\ No newline at end of file

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ShellFactory.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ShellFactory.scala?rev=1031988&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ShellFactory.scala
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.shell/src/main/scala/org/apache/clerezza/shell/ShellFactory.scala
 Sat Nov  6 10:06:02 2010
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.clerezza.shell;
+
+
+
+import org.apache.felix.scr.annotations.Component;
+import org.osgi.framework.BundleContext
+import org.osgi.framework.BundleEvent
+import org.osgi.framework.BundleListener
+import org.osgi.service.component.ComponentContext;
+import org.osgi.framework.Bundle
+import java.io.{File, PrintWriter, Reader, StringWriter, FileDescriptor, 
OutputStreamWriter}
+import java.lang.reflect.InvocationTargetException
+import java.net._
+import java.security.PrivilegedActionException
+import java.security.AccessController
+import java.security.PrivilegedAction
+import java.util.{ArrayList, Arrays};
+import javax.script.ScriptContext
+import javax.script.{ScriptEngineFactory => JavaxEngineFactory, Compilable, 
+                                        CompiledScript, ScriptEngine, 
AbstractScriptEngine, Bindings,
+                                        SimpleBindings, ScriptException}
+//import scala.collection.immutable.Map
+import scala.actors.DaemonActor
+import scala.tools.nsc._;
+import scala.tools.nsc.interpreter._;
+import scala.tools.nsc.io.{AbstractFile, PlainFile, VirtualDirectory}
+import scala.tools.nsc.util._
+import scala.tools.nsc.symtab.SymbolLoaders
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.Reporter
+import scala.tools.util.PathResolver
+import scala.tools.nsc.util.{ClassPath, JavaClassPath}
+import scala.actors.Actor
+import scala.actors.Actor._
+import org.apache.clerezza.scala.scripting._
+
+class ShellFactory()  {
+
+
+
+       var factory: InterpreterFactory = null
+       
+       
+       def activate(componentContext: ComponentContext)= {
+               //bundleContext = componentContext.getBundleContext
+       }
+
+       def deactivate(componentContext: ComponentContext) = {
+               //bundleContext = componentContext.getBundleContext
+       }
+
+       def createShell() = {
+               new Shell(factory, System.in, new 
OutputStreamWriter(System.out))
+       }
+
+       def bindInterpreterFactory(f: InterpreterFactory) = {
+               factory = f
+       }
+
+       def unbindInterpreterFactory(f: InterpreterFactory) = {
+               factory = null
+       }
+}
\ No newline at end of file


Reply via email to