You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2010/05/26 21:52:09 UTC
svn commit: r948559 - in /incubator/clerezza/trunk/scala-scripting: ./
console/ console/src/ console/src/main/ console/src/main/resources/
console/src/main/resources/OSGI-INF/ console/src/main/scala/
console/src/main/scala/org/ console/src/main/scala/o...
Author: reto
Date: Wed May 26 19:52:08 2010
New Revision: 948559
URL: http://svn.apache.org/viewvc?rev=948559&view=rev
Log:
CLEREZZA-221: initial experimenting for scala 2.8.0 services
Added:
incubator/clerezza/trunk/scala-scripting/
incubator/clerezza/trunk/scala-scripting/console/
incubator/clerezza/trunk/scala-scripting/console/NOTICE.txt
incubator/clerezza/trunk/scala-scripting/console/pom.xml
incubator/clerezza/trunk/scala-scripting/console/src/
incubator/clerezza/trunk/scala-scripting/console/src/main/
incubator/clerezza/trunk/scala-scripting/console/src/main/resources/
incubator/clerezza/trunk/scala-scripting/console/src/main/resources/OSGI-INF/
incubator/clerezza/trunk/scala-scripting/console/src/main/resources/OSGI-INF/serviceComponents.xml
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/InterpreterService.scala
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Launcher.scala
incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Utils.scala
incubator/clerezza/trunk/scala-scripting/console/src/test/
incubator/clerezza/trunk/scala-scripting/console/src/test/scala/
incubator/clerezza/trunk/scala-scripting/scala-compiler-osgi/
incubator/clerezza/trunk/scala-scripting/scala-compiler-osgi/pom.xml
incubator/clerezza/trunk/scala-scripting/scala-library-osgi/
incubator/clerezza/trunk/scala-scripting/scala-library-osgi/pom.xml
Added: incubator/clerezza/trunk/scala-scripting/console/NOTICE.txt
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/NOTICE.txt?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/NOTICE.txt (added)
+++ incubator/clerezza/trunk/scala-scripting/console/NOTICE.txt Wed May 26 19:52:08 2010
@@ -0,0 +1 @@
+
Added: incubator/clerezza/trunk/scala-scripting/console/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/pom.xml?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/pom.xml (added)
+++ incubator/clerezza/trunk/scala-scripting/console/pom.xml Wed May 26 19:52:08 2010
@@ -0,0 +1,91 @@
+<?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>
+ <groupId>org.apache.clerezza.scala</groupId>
+ <artifactId>console</artifactId>
+ <version>0.1-incubating-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+ <name>Scala Console Launcher</name>
+ <dependencies>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-compiler</artifactId>
+ <version>2.8.0.RC2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-library</artifactId>
+ <version>2.8.0.RC2</version>
+ </dependency>
+ <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.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ <version>2.13.1</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>compile</goal>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <version>2.1.0-SNAPSHOT</version>
+ <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>
+ <DynamicImport-Package>*</DynamicImport-Package>
+ <Bundle-SymbolicName>org.apache.clerezza.scala.console</Bundle-SymbolicName>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-eclipse-plugin</artifactId>
+ <configuration>
+ <downloadSources>true</downloadSources>
+ <downloadJavadocs>true</downloadJavadocs>
+ <buildcommands>
+ <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
+ </buildcommands>
+ <additionalProjectnatures>
+ <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
+ </additionalProjectnatures>
+ <classpathContainers>
+ <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER
+ </classpathContainer>
+ <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER
+ </classpathContainer>
+ </classpathContainers>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
+
+
Added: incubator/clerezza/trunk/scala-scripting/console/src/main/resources/OSGI-INF/serviceComponents.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/src/main/resources/OSGI-INF/serviceComponents.xml?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/src/main/resources/OSGI-INF/serviceComponents.xml (added)
+++ incubator/clerezza/trunk/scala-scripting/console/src/main/resources/OSGI-INF/serviceComponents.xml Wed May 26 19:52:08 2010
@@ -0,0 +1,11 @@
+<?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.Launcher">
+ <implementation class="org.apache.clerezza.scala.console.Launcher"/>
+ <property name="service.pid" value="org.apache.clerezza.scala.console.Launcher"/>
+ </scr:component> -->
+</components>
Added: incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala (added)
+++ incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/BundleFS.scala Wed May 26 19:52:08 2010
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ *
+ * This class was taken from Sling Scala module at
+ * http://code.google.com/p/sling-scala/
+ */
+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}
+
+package org.apache.clerezza.scala.console {
+
+
+
+ /**
+ * Implementation of {@link AbstractFile} on top of a {@link org.osgi.framework.Bundle}
+ */
+ object BundleFS {
+
+ /**
+ * Create a new {@link AbstractFile} instance representing an
+ * {@link org.osgi.framework.Bundle}
+ * @param bundle
+ */
+ def create(bundle: Bundle): AbstractFile = {
+ require(bundle != null, "bundle must not be null")
+
+ /**
+ * The superclass of FileEntry and DirEntry
+ */
+ abstract class BundleEntry(url: URL, parent: DirEntry) extends AbstractFile {
+ require(url != null, "url must not be null")
+ lazy val (path: String, name: String) = getPathAndName(url)
+ lazy val fullName: String = (path::name::Nil).filter(!_.isEmpty).mkString("/")
+
+ /**
+ * @return null
+ */
+ def file: File = null
+
+ //what if this is not a relative file?
+ def absolute = null
+
+ /**
+ * @return last modification time or 0 if not known
+ */
+ def lastModified: Long =
+ try { url.openConnection.getLastModified }
+ catch { case _ => 0 }
+
+ @throws(classOf[IOException])
+ def container: AbstractFile =
+ valueOrElse(parent) {
+ throw new IOException("No container")
+ }
+
+ @throws(classOf[IOException])
+ def input: InputStream = url.openStream()
+
+ /**
+ * Not supported. Always throws an IOException.
+ * @throws IOException
+ */
+ @throws(classOf[IOException])
+ def output = throw new IOException("not supported: output")
+
+ def delete = throw new IOException("not supported: delete")
+ def create = throw new IOException("not supported: create")
+
+ private def getPathAndName(url: URL): (String, String) = {
+ val u = url.getPath
+ var k = u.length
+ while( (k > 0) && (u(k - 1) == '/') )
+ k = k - 1
+
+ var j = k
+ while( (j > 0) && (u(j - 1) != '/') )
+ j = j - 1
+
+ (u.substring(if (j > 0) 1 else 0, if (j > 1) j - 1 else j), u.substring(j, k))
+ }
+
+ override def toString = fullName
+ }
+
+ class DirEntry(url: URL, parent: DirEntry) extends BundleEntry(url, parent) {
+
+ /**
+ * @return true
+ */
+ def isDirectory: Boolean = true
+
+ override def iterator: Iterator[AbstractFile] = {
+ new Iterator[AbstractFile]() {
+ val dirs = bundle.getEntryPaths(fullName)
+ def hasNext = if (dirs != null) { dirs.hasMoreElements} else {false}
+ def next = {
+ val entry = dirs.nextElement.asInstanceOf[String]
+ var entryUrl = bundle.getResource("/" + entry)
+
+ // Bundle.getResource seems to be inconsistent with respect to requiring
+ // a trailing slash
+ if (entryUrl == null)
+ entryUrl = bundle.getResource("/" + removeTralingSlash(entry))
+
+ if (entry.endsWith(".class"))
+ new FileEntry(entryUrl, DirEntry.this)
+ else
+ new DirEntry(entryUrl, DirEntry.this)
+ }
+
+ private def removeTralingSlash(s: String): String = {
+ if (s == null || s.length == 0) {
+ s
+ }else if (s.last == '/') {
+ removeTralingSlash(s.substring(0, s.length - 1))
+ } else {
+ s
+ }
+ }
+ }
+ }
+
+ def lookupName(name: String, directory: Boolean): AbstractFile = {
+ val entry = bundle.getEntry(fullName + "/" + name)
+ nullOrElse(entry) { entry =>
+ if (directory)
+ new DirEntry(entry, DirEntry.this)
+ else
+ new FileEntry(entry, DirEntry.this)
+ }
+ }
+
+ def lookupNameUnchecked(name: String, directory: Boolean): AbstractFile = {
+ val entry = bundle.getEntry(fullName + "/" + name)
+ if (entry == null) {
+ throw new RuntimeException("Not yet implemented: inexistendt file")
+ } else {
+ if (directory)
+ new DirEntry(entry, DirEntry.this)
+ else
+ new FileEntry(entry, DirEntry.this)
+ }
+ }
+
+ }
+
+ class FileEntry(url: URL, parent: DirEntry) extends BundleEntry(url, parent) {
+
+ /**
+ * @return false
+ */
+ def isDirectory: Boolean = false
+ override def sizeOption: Option[Int] = Some(bundle.getEntry(fullName).openConnection().getContentLength())
+ override def iterator: Iterator[AbstractFile] = Iterator.empty
+ def lookupName(name: String, directory: Boolean): AbstractFile = null
+ def lookupNameUnchecked(name: String, directory: Boolean): AbstractFile = null
+ }
+
+ new DirEntry(bundle.getResource("/"), null)
+ }
+
+ }
+
+}
\ No newline at end of file
Added: incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/InterpreterService.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/InterpreterService.scala?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/InterpreterService.scala (added)
+++ incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/InterpreterService.scala Wed May 26 19:52:08 2010
@@ -0,0 +1,209 @@
+/*
+ * 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.scala.console;
+
+
+
+import org.apache.felix.scr.annotations.Component;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.framework.Bundle
+import java.io.{File, PrintWriter}
+import java.util.{ArrayList, Arrays};
+import scala.tools.nsc._;
+import scala.tools.nsc.interpreter._;
+import scala.tools.nsc.io.{AbstractFile, PlainFile}
+import scala.tools.nsc.util._
+import scala.tools.nsc.symtab.SymbolLoaders
+import java.net._
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.Reporter
+import scala.tools.util.PathResolver
+import scala.tools.nsc.util.{ClassPath, JavaClassPath}
+
+
+class InterpreterService() {
+
+ protected val PATH_SEPARATOR = System.getProperty("path.separator")
+
+ var bundles: Array[Bundle] = null;
+ val parentClassLoader: ClassLoader = this.getClass.getClassLoader()
+
+ def activate(componentContext: ComponentContext) {
+ System.out.println("activating");
+
+ //val bundle = componentContext.getBundleContext.getBundle
+ bundles = componentContext.getBundleContext.getBundles
+ val settings = new Settings();/*createSettings(
+ bundles,
+ componentContext)*/
+ val origBootclasspath = settings.bootclasspath.value
+ //settings.bootclasspath.value = (origBootclasspath :: pathList).mkString(java.io.File.separator)
+
+
+ for (i <- 1 to 1000) {
+ val script = """
+ println("hello"""+Math.random+"""");
+ "good bye"
+ """
+ println(new java.util.Date()+"evaluating "+script)
+ try {
+ println("evaluated to "+interpreter.eval[Unit](script))
+ } catch {
+ case e:Exception => e.printStackTrace()
+ }
+ }
+
+
+ //scala.tools.nsc.MainGenericRunner.main(new String[0]);
+ //System.out.println("activated");
+ }
+ private def createSettings(bundles : Array[Bundle], componentContext : ComponentContext) : Settings = {
+ val settings = new Settings()
+ val bootUrls = getBootUrls(bundles(0), componentContext)
+ val bootPath = new StringBuilder()//settings.classpath.v());
+ for (bootUrl <- bootUrls) {
+ // bootUrls are sometimes null, at least when running integration
+ // tests with cargo-maven2-plugin
+ if(bootUrl != null) {
+ bootPath.append(PATH_SEPARATOR).append(bootUrl.getPath())
+ }
+ }
+ settings.classpath.value = bootPath.toString()
+ val dataFile = new File(componentContext.getBundleContext.getDataFile(""),"sclalaclasses")
+ dataFile.mkdirs();
+ settings.outdir.value = dataFile.getAbsolutePath
+ return settings;
+ }
+ private def getBootUrls(bundle : Bundle, componentContext : ComponentContext) : Array[URL] = {
+ val urls = new ArrayList[URL]()
+ val dataFile = componentContext.getBundleContext.getDataFile("")
+ var classLoader = bundle.getClass().getClassLoader()
+ while (classLoader != null) {
+ if (classLoader.isInstanceOf[URLClassLoader]) {
+ //urls.add((componentContext.getBundleContext.getDataFile("")).toURL())
+ urls.addAll(Arrays.asList((classLoader.asInstanceOf[URLClassLoader]).getURLs():_*))
+ }
+ classLoader = classLoader.getParent()
+ }
+ return urls.toArray(new Array[URL](urls.size))
+ }
+
+ lazy val interpreter = new Interpreter(new Settings, new PrintWriter(System.out)) { // new Interpreter(settings){
+ /*override object reporter extends ConsoleReporter(settings, null, new PrintWriter(System.out)) {
+ override def printMessage(msg: String) {
+ println(msg)
+ }
+ }*/
+
+
+ override def classLoader: AbstractFileClassLoader = {
+ println("requested classLoader")
+ new AbstractFileClassLoader(virtualDirectory, parentClassLoader) {
+ override def tryToInitializeClass[T <: AnyRef](path: String): Option[Class[T]] = {
+ println("initializing "+path)
+ super.tryToInitializeClass(path)
+ }
+ }
+ }
+ override protected def newCompiler(settings: Settings, reporter: Reporter) = {
+ println("requested compiler for "+settings)
+ reporter.info(scala.tools.nsc.util.NoPosition /* {
+ override def source = null}*/, "new Compiler", true)
+ //super.newCompiler(settings, reporter)
+ settings.outputDirs setSingleOutput virtualDirectory
+ new Global(settings, reporter) {
+ println("constructing compiler")
+
+ private lazy val _classPath: ClassPath[AbstractFile] = {
+ //println("getting ClassPath!")
+ //throw new RuntimeException
+ //super.classPath
+ //of classpath.packages is a seq of 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
+ }
+
+ override def rootLoader: LazyType = {
+ println("getting rootLoader:"+platform.rootLoader+"("+platform.rootLoader.getClass+")")
+ //platform.rootLoader
+ //new PathResolver(settings).result is a ClassPath[AbstractFile]
+ //of classpath.packages is a seq of 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)*/
+ //new DirectoryClassPath(BundleFS.create(bundle), classPathOrig.context)
+ //class JavaClassPath(containers: List[ClassPath[AbstractFile]], context: JavaContext)
+ class MyPackageLoader(loaderClassPath: ClassPath[AbstractFile])
+ extends loaders.JavaPackageLoader(loaderClassPath) /*{
+ override protected def newClassLoader(bin: AbstractFile) = {
+ println("getting newClassLoader "+bin+"------")
+ super.newClassLoader(bin)
+ }
+
+ override protected def newPackageLoader(pkg: ClassPath[AbstractFile]) = {
+ println("getting newPackageLoader "+pkg+"------*")
+ super.newPackageLoader(pkg)
+ //throw new RuntimeException
+ new MyPackageLoader(pkg)
+ }
+ }*/
+ val result = new MyPackageLoader(_classPath)
+ println("result: "+result)
+ result
+ }
+ }
+ }
+ }
+}
Added: incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Launcher.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Launcher.scala?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Launcher.scala (added)
+++ incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Launcher.scala Wed May 26 19:52:08 2010
@@ -0,0 +1,221 @@
+/*
+ * 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.scala.console;
+
+
+
+import org.apache.felix.scr.annotations.Component;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.framework.Bundle
+import java.io.{File, PrintWriter}
+import java.util.{ArrayList, Arrays};
+import scala.tools.nsc._;
+import scala.tools.nsc.interpreter._;
+import scala.tools.nsc.io.{AbstractFile, PlainFile}
+import scala.tools.nsc.util._
+import scala.tools.nsc.symtab.SymbolLoaders
+import java.net._
+import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.Reporter
+import scala.tools.util.PathResolver
+import scala.tools.nsc.util.{ClassPath, JavaClassPath}
+
+
+class Launcher() {
+
+ protected val PATH_SEPARATOR = System.getProperty("path.separator")
+
+ def activate(componentContext: ComponentContext) {
+ System.out.println("activating");
+ def jarPathOfClass(className: String) = {
+ Class.forName(className).getProtectionDomain.getCodeSource.getLocation
+ }
+ //val bundle = componentContext.getBundleContext.getBundle
+ val bundles = componentContext.getBundleContext.getBundles
+ val libPath = jarPathOfClass("scala.ScalaObject")
+ //this works: val stream = new URL(libPath.toString).openStream()
+ val compilerPath = jarPathOfClass("scala.tools.nsc.Interpreter")
+ println("jar: "+libPath)
+ val settings = new Settings();/*createSettings(
+ bundles,
+ componentContext)*/
+ val origBootclasspath = settings.bootclasspath.value
+ val pathList = List(compilerPath,
+ libPath)
+ //settings.bootclasspath.value = (origBootclasspath :: pathList).mkString(java.io.File.separator)
+
+ val parentClassLoader: ClassLoader = this.getClass.getClassLoader()
+ for (c <- classOf[Interpreter].getConstructors) {
+ println(c);
+
+ }
+
+ val interpreter = new Interpreter(settings, new PrintWriter(System.out)) { // new Interpreter(settings){
+ /*override object reporter extends ConsoleReporter(settings, null, new PrintWriter(System.out)) {
+ override def printMessage(msg: String) {
+ println(msg)
+ }
+ }*/
+
+ override lazy val compilerClasspath: List[URL] = {
+ println("requested path list")
+ pathList
+ }
+ override def classLoader: AbstractFileClassLoader = {
+ println("requested classLoader")
+ new AbstractFileClassLoader(virtualDirectory, parentClassLoader) {
+ override def tryToInitializeClass[T <: AnyRef](path: String): Option[Class[T]] = {
+ println("initializing "+path)
+ super.tryToInitializeClass(path)
+ }
+ }
+ }
+ override protected def newCompiler(settings: Settings, reporter: Reporter) = {
+ println("requested compiler for "+settings)
+ reporter.info(scala.tools.nsc.util.NoPosition /* {
+ override def source = null}*/, "new Compiler", true)
+ //super.newCompiler(settings, reporter)
+ settings.outputDirs setSingleOutput virtualDirectory
+ new Global(settings, reporter) {
+ println("constructing compiler")
+
+ private lazy val _classPath: ClassPath[AbstractFile] = {
+ //println("getting ClassPath!")
+ //throw new RuntimeException
+ //super.classPath
+ //of classpath.packages is a seq of 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
+ }
+
+ override def rootLoader: LazyType = {
+ println("getting rootLoader:"+platform.rootLoader+"("+platform.rootLoader.getClass+")")
+ //platform.rootLoader
+ //new PathResolver(settings).result is a ClassPath[AbstractFile]
+ //of classpath.packages is a seq of 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)*/
+ //new DirectoryClassPath(BundleFS.create(bundle), classPathOrig.context)
+ //class JavaClassPath(containers: List[ClassPath[AbstractFile]], context: JavaContext)
+ class MyPackageLoader(loaderClassPath: ClassPath[AbstractFile])
+ extends loaders.JavaPackageLoader(loaderClassPath) /*{
+ override protected def newClassLoader(bin: AbstractFile) = {
+ println("getting newClassLoader "+bin+"------")
+ super.newClassLoader(bin)
+ }
+
+ override protected def newPackageLoader(pkg: ClassPath[AbstractFile]) = {
+ println("getting newPackageLoader "+pkg+"------*")
+ super.newPackageLoader(pkg)
+ //throw new RuntimeException
+ new MyPackageLoader(pkg)
+ }
+ }*/
+ val result = new MyPackageLoader(_classPath)
+ println("result: "+result)
+ result
+ }
+ }
+ }
+ }
+
+ for (i <- 1 to 1000) {
+ val script = """
+ println("hello"""+Math.random+"""");
+ "good bye"
+ """
+ println(new java.util.Date()+"evaluating "+script)
+ try {
+ println("evaluated to "+interpreter.eval[Unit](script))
+ } catch {
+ case e:Exception => e.printStackTrace()
+ }
+ }
+
+
+ //scala.tools.nsc.MainGenericRunner.main(new String[0]);
+ //System.out.println("activated");
+ }
+ private def createSettings(bundles : Array[Bundle], componentContext : ComponentContext) : Settings = {
+ val settings = new Settings()
+ val bootUrls = getBootUrls(bundles(0), componentContext)
+ val bootPath = new StringBuilder()//settings.classpath.v());
+ for (bootUrl <- bootUrls) {
+ // bootUrls are sometimes null, at least when running integration
+ // tests with cargo-maven2-plugin
+ if(bootUrl != null) {
+ bootPath.append(PATH_SEPARATOR).append(bootUrl.getPath())
+ }
+ }
+ settings.classpath.value = bootPath.toString()
+ val dataFile = new File(componentContext.getBundleContext.getDataFile(""),"sclalaclasses")
+ dataFile.mkdirs();
+ settings.outdir.value = dataFile.getAbsolutePath
+ return settings;
+ }
+ private def getBootUrls(bundle : Bundle, componentContext : ComponentContext) : Array[URL] = {
+ val urls = new ArrayList[URL]()
+ val dataFile = componentContext.getBundleContext.getDataFile("")
+ var classLoader = bundle.getClass().getClassLoader()
+ while (classLoader != null) {
+ if (classLoader.isInstanceOf[URLClassLoader]) {
+ //urls.add((componentContext.getBundleContext.getDataFile("")).toURL())
+ urls.addAll(Arrays.asList((classLoader.asInstanceOf[URLClassLoader]).getURLs():_*))
+ }
+ classLoader = classLoader.getParent()
+ }
+ return urls.toArray(new Array[URL](urls.size))
+ }
+}
Added: incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Utils.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Utils.scala?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Utils.scala (added)
+++ incubator/clerezza/trunk/scala-scripting/console/src/main/scala/org/apache/clerezza/scala/console/Utils.scala Wed May 26 19:52:08 2010
@@ -0,0 +1,52 @@
+/*
+ * 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.scala.console
+
+/**
+ * General purpose utility functions
+ */
+object Utils{
+
+ /**
+ * Evaluate <code>f</code> on <code>s</code> if <code>s</code> is not null.
+ * @param s
+ * @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 =
+ if (s == null) null.asInstanceOf[T]
+ else f(s)
+
+ /**
+ * @param t
+ * @param default
+ * @return <code>t</code> or <code>default</code> if <code>null</code>.
+ */
+ protected[console] def valueOrElse[T](t: T)(default: => T) =
+ if (t == null) default
+ else t
+
+ /**
+ * Converts a value into an Option.
+ * @param value
+ * @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] =
+ if (null == value) None else Some(value)
+
+}
Added: incubator/clerezza/trunk/scala-scripting/scala-compiler-osgi/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/scala-compiler-osgi/pom.xml?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/scala-compiler-osgi/pom.xml (added)
+++ incubator/clerezza/trunk/scala-scripting/scala-compiler-osgi/pom.xml Wed May 26 19:52:08 2010
@@ -0,0 +1,44 @@
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.clerezza.scala</groupId>
+ <artifactId>scala-compiler-osgi</artifactId>
+ <packaging>bundle</packaging>
+ <name>Scala Compiler Bundle</name>
+ <version>0.1-incubating-SNAPSHOT</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-compiler</artifactId>
+ <version>2.8.0.RC2</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <version>2.1.0-SNAPSHOT</version>
+ <configuration>
+ <instructions>
+ <Embed-Dependency>*</Embed-Dependency>
+ <Embed-Transitive>true</Embed-Transitive>
+ <Export-Package>scala.*</Export-Package>
+ <Import-Package>!ch.epfl.lamp.*,!sun.*,
+ !org.apache.tools.ant,!jline</Import-Package>
+ <DynamicImport-Package>*</DynamicImport-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
Added: incubator/clerezza/trunk/scala-scripting/scala-library-osgi/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/scala-scripting/scala-library-osgi/pom.xml?rev=948559&view=auto
==============================================================================
--- incubator/clerezza/trunk/scala-scripting/scala-library-osgi/pom.xml (added)
+++ incubator/clerezza/trunk/scala-scripting/scala-library-osgi/pom.xml Wed May 26 19:52:08 2010
@@ -0,0 +1,42 @@
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.clerezza.scala</groupId>
+ <artifactId>scala-library-osgi</artifactId>
+ <packaging>bundle</packaging>
+ <name>Scala Library Bundle</name>
+ <version>0.1-incubating-SNAPSHOT</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-library</artifactId>
+ <version>2.8.0.RC2</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <version>2.1.0-SNAPSHOT</version>
+ <configuration>
+ <instructions>
+ <Embed-Dependency>*</Embed-Dependency>
+ <Embed-Transitive>true</Embed-Transitive>
+ <Export-Package>scala.*</Export-Package>
+ <DynamicImport-Package>*</DynamicImport-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>