clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject svn commit: r1083114 - in /incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main: java/org/apache/clerezza/platform/typerendering/scalaserverpages/ resources/org/apache/clerezza/platform/typerendering/scalaserverpages/
Date Sat, 19 Mar 2011 00:32:15 GMT
Author: reto
Date: Sat Mar 19 00:32:15 2011
New Revision: 1083114

URL: http://svn.apache.org/viewvc?rev=1083114&view=rev
Log:
CLEREZZA-460: added new ScalaServerPage to be used to register SSPs instead of via RenderletManager

Added:
    incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java
    incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java
    incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/resources/org/apache/clerezza/platform/typerendering/scalaserverpages/typerenderlet-header.txt
Modified:
    incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesRenderlet.java

Modified: incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesRenderlet.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesRenderlet.java?rev=1083114&r1=1083113&r2=1083114&view=diff
==============================================================================
--- incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesRenderlet.java
(original)
+++ incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesRenderlet.java
Sat Mar 19 00:32:15 2011
@@ -18,39 +18,29 @@
  */
 package org.apache.clerezza.platform.typerendering.scalaserverpages;
 
-import java.io.CharArrayReader;
-import java.io.CharArrayWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.LineNumberReader;
-import java.io.OutputStream;
-import java.io.Reader;
+import org.apache.clerezza.platform.typerendering.*;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.scala.scripting.CompileErrorsException;
+import org.apache.clerezza.scala.scripting.CompilerService;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import java.io.*;
+import java.lang.reflect.Type;
 import java.net.MalformedURLException;
 import java.net.URI;
-import java.util.Map;
-import java.lang.reflect.Type;
 import java.nio.charset.Charset;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.Collections;
 import java.util.HashMap;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.clerezza.platform.typerendering.CallbackRenderer;
-import org.apache.clerezza.platform.typerendering.RenderingException;
-import org.apache.clerezza.platform.typerendering.Renderlet;
-import org.apache.clerezza.rdf.utils.GraphNode;
-import org.apache.clerezza.platform.typerendering.RenderingspecificationException;
-import org.apache.clerezza.platform.typerendering.TypeRenderingException;
-import org.apache.clerezza.scala.scripting.CompileErrorsException;
-import org.apache.clerezza.scala.scripting.CompilerService;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.Service;
-import scala.collection.Seq;
+import java.util.Map;
 
 /**
  * 
@@ -62,6 +52,7 @@ import scala.collection.Seq;
  */
 @Component
 @Service(Renderlet.class)
+@Deprecated
 public class ScalaServerPagesRenderlet implements Renderlet {
 
 	final Charset UTF8 = Charset.forName("UTF-8");

Added: incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java?rev=1083114&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java
(added)
+++ incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/ScalaServerPagesService.java
Sat Mar 19 00:32:15 2011
@@ -0,0 +1,68 @@
+/*
+ * 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.platform.typerendering.scalaserverpages;
+
+import org.apache.clerezza.platform.typerendering.TypeRenderlet;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.scala.scripting.CompilerService;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+
+import javax.ws.rs.core.MediaType;
+import java.net.URL;
+
+/**
+ * 
+ * A service to create and register TypeRenderlets from ScalaServerPages
+ * 
+ * @author reto
+ *  
+ * 
+ */
+@Component
+@Service(ScalaServerPagesService.class)
+public class ScalaServerPagesService {
+
+
+	@Reference
+	private CompilerService scalaCompilerService;
+	private BundleContext bundleContext;
+
+	protected void activate(ComponentContext componentContext) {
+		bundleContext = componentContext.getBundleContext();
+	}
+
+	protected void deactivate(ComponentContext componentContext) {
+		bundleContext = null;
+	}
+
+	public ServiceRegistration registerScalaServerPage(URL location,  UriRef rdfType,
+			String modePattern, MediaType mediaType) {
+		TypeRenderlet sspTypeRenderlet = new SspTypeRenderlet(location, rdfType,
+				modePattern, mediaType, scalaCompilerService);
+		return bundleContext.registerService(TypeRenderlet.class.getName(),
+				sspTypeRenderlet, null);
+	}
+
+
+}

Added: incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java?rev=1083114&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java
(added)
+++ incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/java/org/apache/clerezza/platform/typerendering/scalaserverpages/SspTypeRenderlet.java
Sat Mar 19 00:32:15 2011
@@ -0,0 +1,165 @@
+/*
+ * 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.platform.typerendering.scalaserverpages;
+
+import java.util.logging.Level;
+import org.apache.clerezza.platform.typerendering.CallbackRenderer;
+import org.apache.clerezza.platform.typerendering.TypeRenderlet;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.scala.scripting.CompilerService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.core.MediaType;
+import java.io.*;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.Arrays;
+import java.util.Map;
+
+/**
+ * A TypeRenderlet delegating the actual renderlet to a compiled ScalaServerPage. On every
request the
+ * ScalaServerPage is checked for changes and recompiled if needed.
+ */
+public class SspTypeRenderlet implements TypeRenderlet {
+
+
+	private static final Logger logger = LoggerFactory.getLogger(ScalaServerPagesRenderlet.class);
+	private static final Charset UTF8 = Charset.forName("UTF-8");
+	private static final char[] headerChars
+			= getChars(SspTypeRenderlet.class.getResource("typerenderlet-header.txt"));
+	private static final String lineSeparator = System.getProperty("line.separator");
+	private static final char[] footerChars = (";}" + lineSeparator + "}"
+			+ lineSeparator + "}" + lineSeparator + "}" + lineSeparator).toCharArray();
+	
+
+	private UriRef rdfType;
+	private String modePattern;
+	private MediaType mediaType;
+	private URL sspLocation;
+	private char[] lastCompiledChars;
+	private TypeRenderlet lastCompiledSsp = null;
+	private CompilerService scalaCompilerService;
+
+	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;
+	}
+
+	@Override
+	public UriRef getRdfType() {
+		return rdfType;
+	}
+
+	@Override
+	public String getModePattern() {
+		return modePattern;
+	}
+
+	@Override
+	public MediaType getMediaType() {
+		return mediaType;
+	}
+
+	@Override
+	public void render(GraphNode node, GraphNode context,
+					   Map<String, Object> sharedRenderingValues, CallbackRenderer callbackRenderer,
+					   RequestProperties requestProperties, OutputStream os) throws IOException {
+		TypeRenderlet compiledSsp = getCompiledSsp();
+		compiledSsp.render(node, context, sharedRenderingValues, callbackRenderer, requestProperties,
os);
+	}
+
+	private synchronized TypeRenderlet 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)[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 static char[] getScriptChars(URL location) {
+		try {
+			final CharArrayWriter caos = new CharArrayWriter();
+			//Add the scriptHeader to the beginning of the script
+			caos.write(headerChars);
+			//add the content
+			caos.write(getChars(location));
+			caos.write(footerChars);
+			return caos.toCharArray();
+		} catch (IOException ex) {
+			throw new RuntimeException(ex);
+		}
+	}
+
+	private static char[] getChars(URL location) {
+		try {
+			final Reader in = new InputStreamReader(location.openStream(), UTF8);
+			final CharArrayWriter caos = new CharArrayWriter();
+			
+			final char[] buffer = new char[1024];
+			int charsRead;
+			while ((charsRead = in.read(buffer, 0, 1024)) != -1) {
+				caos.write(buffer, 0, charsRead);
+			}
+			logger.debug("getting bytes for: {}", location);
+			return caos.toCharArray();
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+}

Added: incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/resources/org/apache/clerezza/platform/typerendering/scalaserverpages/typerenderlet-header.txt
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/resources/org/apache/clerezza/platform/typerendering/scalaserverpages/typerenderlet-header.txt?rev=1083114&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/resources/org/apache/clerezza/platform/typerendering/scalaserverpages/typerenderlet-header.txt
(added)
+++ incubator/clerezza/trunk/parent/platform.typerendering.scalaserverpages/src/main/resources/org/apache/clerezza/platform/typerendering/scalaserverpages/typerenderlet-header.txt
Sat Mar 19 00:32:15 2011
@@ -0,0 +1,16 @@
+import org.apache.clerezza.rdf.ontologies._
+import org.apache.clerezza.rdf.core._
+import org.apache.clerezza.rdf.utils._
+import org.apache.clerezza.rdf.scala.utils._
+import org.apache.clerezza.rdf.scala.utils.Preamble._
+import org.apache.clerezza.platform.typerendering.scala._
+import javax.ws.rs.core.UriInfo
+
+ class CompiledScalaServerPage extends SRenderlet {
+
+	def getMediaType = null
+	def getModePattern = null
+	def getRdfType = null
+	override def renderedPage(arguments: XmlResult.Arguments): XmlResult = {
+ 		new XmlResult(arguments) {
+			override def content = {



Mime
View raw message