You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by ds...@apache.org on 2012/01/18 18:21:56 UTC

svn commit: r1232960 - in /incubator/clerezza/issues/CLEREZZA-617: platform.typerendering.scalaserverpages/ platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ scala-scripting/ scala-script...

Author: dspicar
Date: Wed Jan 18 17:21:56 2012
New Revision: 1232960

URL: http://svn.apache.org/viewvc?rev=1232960&view=rev
Log:
CLEREZZA-617: updated POM versions, changed interface to provide clasloader and preserve backwards compatibility

Modified:
    incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/pom.xml
    incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java
    incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java
    incubator/clerezza/issues/CLEREZZA-617/scala-scripting/pom.xml
    incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/pom.xml
    incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/CompilerService.scala
    incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/TrackingCompiler.scala

Modified: incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/pom.xml?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/pom.xml (original)
+++ incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/pom.xml Wed Jan 18 17:21:56 2012
@@ -30,7 +30,7 @@
 	<groupId>org.apache.clerezza</groupId>
 	<artifactId>platform.typerendering.scalaserverpages</artifactId>
 	<packaging>bundle</packaging>
-	<version>0.3-incubating-SNAPSHOT</version>
+	<version>0.4-incubating-SNAPSHOT</version>
 	<name>Clerezza - Platform ScalaServerPages Renderlet</name>
 	<description>Renderlet implementation wrapping org.apache.clerezza.scala</description>
 	<dependencies>

Modified: incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java (original)
+++ incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java Wed Jan 18 17:21:56 2012
@@ -57,7 +57,7 @@ public class ScalaServerPagesService {
 	}
 
 	/**
-	 * Creates a Renderle-Service registered using the BundleContext of this
+	 * Creates a Renderlet-Service registered using the BundleContext of this
 	 * service
 	 *
 	 * @param location
@@ -66,7 +66,7 @@ public class ScalaServerPagesService {
 	 * @param mediaType
 	 * @return
 	 */
-	public ServiceRegistration registerScalaServerPage(URL location,  UriRef rdfType,
+	public ServiceRegistration registerScalaServerPage(URL location, UriRef rdfType,
 			String modePattern, MediaType mediaType) {
 		return registerScalaServerPage(location, rdfType, modePattern, mediaType, bundleContext);
 	}
@@ -85,11 +85,23 @@ public class ScalaServerPagesService {
 	 */
 	public ServiceRegistration registerScalaServerPage(URL location,  UriRef rdfType,
 			String modePattern, MediaType mediaType, BundleContext callerBundleContext) {
+		return registerScalaServerPage(location, rdfType, modePattern, mediaType, 
+				callerBundleContext, null);
+	}
+	
+	/**
+	 * Creates a renderlet service registered using the given bundle context. Using
+	 * This method ensures that the passed BundleContext is used to determine the priority
+	 * of the renderlets.
+	 * 
+	 * The passed class loader will be used to load the compiled SSP. 
+	 */
+	public ServiceRegistration registerScalaServerPage(URL location,  UriRef rdfType,
+			String modePattern, MediaType mediaType, BundleContext callerBundleContext,
+			ClassLoader classLoader) {
 		TypeRenderlet sspTypeRenderlet = new SspTypeRenderlet(location, rdfType,
-				modePattern, mediaType, scalaCompilerService, callerBundleContext);
+				modePattern, mediaType, scalaCompilerService, classLoader);
 		return callerBundleContext.registerService(TypeRenderlet.class.getName(),
 				sspTypeRenderlet, null);
 	}
-
-
 }

Modified: incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java (original)
+++ incubator/clerezza/issues/CLEREZZA-617/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java Wed Jan 18 17:21:56 2012
@@ -35,7 +35,6 @@ import java.security.PrivilegedActionExc
 import java.security.PrivilegedExceptionAction;
 import java.util.Arrays;
 import java.util.Map;
-import org.osgi.framework.BundleContext;
 
 /**
  * A TypeRenderlet delegating the actual renderlet to a compiled ScalaServerPage. On every request the
@@ -60,27 +59,25 @@ public class SspTypeRenderlet implements
 	private char[] lastCompiledChars;
 	private TypeRenderlet lastCompiledSsp = null;
 	private CompilerService scalaCompilerService;
-	private BundleContext registartorContext = null;
+	private final ClassLoader registratorClassLoader;
 
-	SspTypeRenderlet(URL sspLocation, UriRef rdfType, String modePattern, MediaType mediaType,
-					 CompilerService scalaCompilerService, BundleContext registratorContext) {
+	SspTypeRenderlet(URL sspLocation, UriRef rdfType, String modePattern, 
+			MediaType mediaType, CompilerService scalaCompilerService, 
+			ClassLoader registratorClassLoader) {
 		this.sspLocation = sspLocation;
 		this.rdfType = rdfType;
 		this.modePattern = modePattern;
 		this.mediaType = mediaType;
 		this.scalaCompilerService = scalaCompilerService;
-		this.registartorContext = registratorContext;
+		this.registratorClassLoader = registratorClassLoader;
 	}
 	
-	SspTypeRenderlet(URL sspLocation, UriRef rdfType, String modePattern, MediaType mediaType,
-					 CompilerService scalaCompilerService) {
-		this.sspLocation = sspLocation;
-		this.rdfType = rdfType;
-		this.modePattern = modePattern;
-		this.mediaType = mediaType;
-		this.scalaCompilerService = scalaCompilerService;
+	SspTypeRenderlet(URL sspLocation, UriRef rdfType, String modePattern, 
+			MediaType mediaType, CompilerService scalaCompilerService) {
+		this(sspLocation, rdfType, modePattern, mediaType, scalaCompilerService, 
+				null);
 	}
-
+	
 	@Override
 	public UriRef getRdfType() {
 		return rdfType;
@@ -100,7 +97,7 @@ public class SspTypeRenderlet implements
 	public void render(GraphNode node, GraphNode context,
 					   Map<String, Object> sharedRenderingValues, CallbackRenderer callbackRenderer,
 					   RequestProperties requestProperties, OutputStream os) throws IOException {
-		TypeRenderlet compiledSsp = getCompiledSsp(this.registartorContext);
+		TypeRenderlet compiledSsp = getCompiledSsp();
 		compiledSsp.render(node, context, sharedRenderingValues, callbackRenderer, requestProperties, os);
 	}
 
@@ -116,49 +113,11 @@ public class SspTypeRenderlet implements
 			renderletClass = AccessController.doPrivileged(new PrivilegedExceptionAction<Class>() {
 				@Override
 				public Class run() {
-					return scalaCompilerService.compile(scripts)[0];
-				}
-			});
-
-		} catch (PrivilegedActionException e) {
-			Throwable cause = e.getCause();
-			if (cause instanceof RuntimeException) {
-				throw (RuntimeException) cause;
-			}
-			/*if (cause instanceof CompileErrorsException) {
-				throw (CompileErrorsException) cause;
-			}*/
-			throw new RuntimeException(e);
-		}
-		TypeRenderlet compiledSsp;
-		try {
-			compiledSsp = (TypeRenderlet) renderletClass.newInstance();
-		} catch (InstantiationException ex) {
-			throw new RuntimeException(ex);
-		} catch (IllegalAccessException ex) {
-			throw new RuntimeException(ex);
-		}
-		lastCompiledSsp = compiledSsp;
-		lastCompiledChars = scriptChars;
-		return compiledSsp;
-	}
-	
-	private synchronized TypeRenderlet getCompiledSsp(final BundleContext registratorContext)  {
-		if(registratorContext == null) {
-			return getCompiledSsp();
-		}
-		char[] scriptChars = getScriptChars(sspLocation);
-		if (Arrays.equals(scriptChars, lastCompiledChars)) {
-			return lastCompiledSsp;
-		}
-		final char[][] scripts = new char[][]{scriptChars};
-		Class renderletClass;
-		try {
-			//doing as priviledged so that no CompilePermission is needed
-			renderletClass = AccessController.doPrivileged(new PrivilegedExceptionAction<Class>() {
-				@Override
-				public Class run() {
-					return scalaCompilerService.compile(scripts, registratorContext)[0];
+					if (registratorClassLoader == null) {
+						return scalaCompilerService.compile(scripts)[0];
+					} else {
+						return scalaCompilerService.compile(scripts, registratorClassLoader)[0];
+					}
 				}
 			});
 

Modified: incubator/clerezza/issues/CLEREZZA-617/scala-scripting/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/scala-scripting/pom.xml?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/scala-scripting/pom.xml (original)
+++ incubator/clerezza/issues/CLEREZZA-617/scala-scripting/pom.xml Wed Jan 18 17:21:56 2012
@@ -24,7 +24,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>org.apache.clerezza.scala</groupId>
 	<artifactId>scala</artifactId>
-	<version>0.1-incubating-SNAPSHOT</version>
+	<version>0.2-incubating-SNAPSHOT</version>
 	<packaging>pom</packaging>
 	<name>Scala OSGi Services</name>
 	<description>Provides bundles and services to allow compiling and executing scala code

Modified: incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/pom.xml
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/pom.xml?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/pom.xml (original)
+++ incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/pom.xml Wed Jan 18 17:21:56 2012
@@ -25,7 +25,7 @@
 	<parent>
 		<groupId>org.apache.clerezza.scala</groupId>
 		<artifactId>scala</artifactId>
-		<version>0.1-incubating-SNAPSHOT</version>
+		<version>0.2-incubating-SNAPSHOT</version>
 	</parent>
 	<artifactId>script-engine</artifactId>
 	<packaging>bundle</packaging>

Modified: incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/CompilerService.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/CompilerService.scala?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/CompilerService.scala (original)
+++ incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/CompilerService.scala Wed Jan 18 17:21:56 2012
@@ -95,23 +95,34 @@ class CompilerService() extends BundleLi
 		TrackingCompiler(bundleContext, out, Some(outputSirectory))
 	}
 
+	/**
+	 * Compiles an array of class sources returning an array of compiled classes.
+	 * Uses the optionally supplied class loader to load compiled classes.
+	 * If no class loader is supplied, the class loader of this bundle is used.
+	 */
 	def compile(sources: Array[Array[Char]]): Array[Class[_]] = {
-		compile(sources.toList).toArray
+	  compile(sources, null)
 	}
-
+	
 	/**
 	 * Compiles an array of class sources returning an array of compiled classes.
-	 * Uses the ClassLoader of the supplied BundleContext's bundle.'
+	 * Uses the optionally supplied class loader to load compiled classes.
+	 * If no class loader is supplied, the class loader of this bundle is used.
 	 */
-	def compile(sources: Array[Array[Char]], registratorContext: BundleContext): Array[Class[_]] = {
-		compile(sources.toList, registratorContext).toArray
+	def compile(sources: Array[Array[Char]], classLoader: ClassLoader): Array[Class[_]] = {
+		var classLoaderOption: Option[ClassLoader] = None
+		if(classLoader != null) {
+		  classLoaderOption = Some(classLoader);
+		}
+		compile(sources.toList, classLoaderOption).toArray
 	}
 	
 	/**
 	 * Compiles a list of class sources returning a list of compiled classes.
-	 * Uses the ClassLoader of the supplied BundleContext's bundle.'
+	 * Uses the optionally supplied class loader to load compiled classes.
+	 * If no class loader is supplied, the class loader of this bundle is used.
 	 */
-	def compile(sources: List[Array[Char]], registratorContext: BundleContext = null): List[Class[_]] = {
+	def compile(sources: List[Array[Char]], classLoaderOption: Option[ClassLoader] = None): List[Class[_]] = {
 		AccessController.checkPermission(new CompilePermission)
 		sharedCompiler.synchronized {
 			try {
@@ -121,11 +132,7 @@ class CompilerService() extends BundleLi
 							val baos = new ByteArrayOutputStream
 							currentSharedCompilerOutputStream = baos
 							try {
-								var context: Option[BundleContext] = None;
-								if(registratorContext != null) {
-								  context = Some(registratorContext)
-								}
-								sharedCompiler.compile(sources, context)
+								sharedCompiler.compile(sources, classLoaderOption)
 							} catch {
 								case c: CompileErrorsException => throw new CompileErrorsException(
 										new String(baos.toByteArray, "utf-8"))

Modified: incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/TrackingCompiler.scala
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/TrackingCompiler.scala?rev=1232960&r1=1232959&r2=1232960&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/TrackingCompiler.scala (original)
+++ incubator/clerezza/issues/CLEREZZA-617/scala-scripting/script-engine/src/main/scala/org/apache/clerezza/scala/scripting/TrackingCompiler.scala Wed Jan 18 17:21:56 2012
@@ -66,13 +66,14 @@ class TrackingCompiler private (bundleCo
 
 	/**
 	 * Compiles a list of class sources returning a list of compiled classes.
-	 * Uses the ClassLoader of the supplied BundleContext's bundle, if supplied.
+	 * Uses the optionally supplied class loader to load compiled classes.
+	 * If no class loader is supplied, the class loader of this class is used.
 	 */
 	@throws(classOf[CompileErrorsException])
-	def compile(sources: List[Array[Char]], registratorContext: Option[BundleContext] = None): List[Class[_]] = {
+	def compile(sources: List[Array[Char]], classLoaderOption: Option[ClassLoader] = None): List[Class[_]] = {
 		val classFiles = compileToDir(sources)
-		val classLoader = registratorContext match {
-		  case Some(x) => classLoaderBuilder(getBundleClassLoader(x.getBundle))
+		val classLoader = classLoaderOption match {
+		  case Some(x) => classLoaderBuilder(x)
 		  case None => classLoaderBuilder(this.getClass.getClassLoader)
 		}
 		//println("ClassLoader: " + classLoader + " Parent: " + classLoader.getParent)
@@ -91,27 +92,27 @@ class TrackingCompiler private (bundleCo
 	 *
 	 * Hack to get the classloader from a bundle reference, is felix dependend
 	 */
-    def getBundleClassLoader(bundle: Bundle): ClassLoader = {
-	  val modulesField: Field = bundle.getClass.getClassLoader.loadClass("org.apache.felix.framework.BundleImpl").getDeclaredField("m_modules")
-	  modulesField.setAccessible(true);
-	  val classLoaderField: Field = bundle.getClass.getClassLoader.loadClass("org.apache.felix.framework.ModuleImpl").getDeclaredField("m_classLoader")
-	  classLoaderField.setAccessible(true);
-	  var module: Object = None
-	  try {
-		val modules: Array[Object] = modulesField.get(bundle).asInstanceOf[Array[Object]]
-		module = modules(modules.length - 1)
-	  } catch {
-		case e: ClassCastException => {
-		  val modules: java.util.List[Object] = modulesField.get(bundle).asInstanceOf[java.util.List[Object]]
-		  module = modules.get(modules.size - 1)
-		}
-	  }
-	  
-	  bundle.loadClass("java.lang.Object")
-	  val classLoader: ClassLoader = classLoaderField.get(module).asInstanceOf[ClassLoader]
-	
-	  classLoader
-	}
+//    def getBundleClassLoader(bundle: Bundle): ClassLoader = {
+//	  val modulesField: Field = bundle.getClass.getClassLoader.loadClass("org.apache.felix.framework.BundleImpl").getDeclaredField("m_modules")
+//	  modulesField.setAccessible(true);
+//	  val classLoaderField: Field = bundle.getClass.getClassLoader.loadClass("org.apache.felix.framework.ModuleImpl").getDeclaredField("m_classLoader")
+//	  classLoaderField.setAccessible(true);
+//	  var module: Object = None
+//	  try {
+//		val modules: Array[Object] = modulesField.get(bundle).asInstanceOf[Array[Object]]
+//		module = modules(modules.length - 1)
+//	  } catch {
+//		case e: ClassCastException => {
+//		  val modules: java.util.List[Object] = modulesField.get(bundle).asInstanceOf[java.util.List[Object]]
+//		  module = modules.get(modules.size - 1)
+//		}
+//	  }
+//	  
+//	  bundle.loadClass("java.lang.Object")
+//	  val classLoader: ClassLoader = classLoaderField.get(module).asInstanceOf[ClassLoader]
+//	
+//	  classLoader
+//	}
 	
 }