You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by sn...@apache.org on 2007/03/12 02:56:48 UTC

svn commit: r517070 - in /incubator/roller/trunk/sandbox/scripting/bsf: ./ build/ build/classes/ build/classes/org/ build/classes/org/apache/ build/classes/org/apache/roller/ build/classes/org/apache/roller/scripting/ build/classes/org/apache/roller/ut...

Author: snoopdave
Date: Sun Mar 11 18:56:46 2007
New Revision: 517070

URL: http://svn.apache.org/viewvc?view=rev&rev=517070
Log:
BSF renderer, tested with Groovy and JavaScript

Added:
    incubator/roller/trunk/sandbox/scripting/bsf/
    incubator/roller/trunk/sandbox/scripting/bsf/README.txt
    incubator/roller/trunk/sandbox/scripting/bsf/ant.properties
    incubator/roller/trunk/sandbox/scripting/bsf/build/
    incubator/roller/trunk/sandbox/scripting/bsf/build.xml
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRenderer.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRendererFactory.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRenderer.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRendererFactory.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRollerBinding.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRunner.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/util/
    incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/util/GroovyRunner.class   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/dist/
    incubator/roller/trunk/sandbox/scripting/bsf/dist/roller-scripting.jar   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/lib/
    incubator/roller/trunk/sandbox/scripting/bsf/lib/bsf.jar   (with props)
    incubator/roller/trunk/sandbox/scripting/bsf/src/
    incubator/roller/trunk/sandbox/scripting/bsf/src/org/
    incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/
    incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/
    incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/
    incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRenderer.java
    incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRendererFactory.java

Added: incubator/roller/trunk/sandbox/scripting/bsf/README.txt
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/README.txt?view=auto&rev=517070
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/bsf/README.txt (added)
+++ incubator/roller/trunk/sandbox/scripting/bsf/README.txt Sun Mar 11 18:56:46 2007
@@ -0,0 +1,29 @@
+
+Some experiments with using BSF to enable scripting in Roller templates.
+
+
+To use it in Roller:
+
+1) Put roller-scripting.jar and bsf.jar in WEB-INF/lib
+
+2) Add the jars necessary JavaScript, Groovy or JRuby to WEB-INF/lib
+
+3) In your roller-custom.properies file add this override:
+
+   rendering.rollerRendererFactories=\
+      org.apache.roller.ui.rendering.velocity.VelocityRendererFactory,\
+      org.apache.roller.scripting.BSFRendererFactory
+
+4) Restart Roller
+
+5) Create a page template and set Template Language to either "groovy", "jruby"
+   or "javascript" -- right now you'll have to do that via SQL.
+
+6) In your page template you'll have access to all normal Roller models
+   plus the name "out" will be bound to a Writer that you can use for output.
+   For example, Here's Groovy for a simple Hello World template:
+
+        import java.io.PrintWriter;
+        pw = new PrintWriter(out);
+        pw.println("Hello World, my blog is named ${model.weblog.name}");
+

Added: incubator/roller/trunk/sandbox/scripting/bsf/ant.properties
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/ant.properties?view=auto&rev=517070
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/bsf/ant.properties (added)
+++ incubator/roller/trunk/sandbox/scripting/bsf/ant.properties Sun Mar 11 18:56:46 2007
@@ -0,0 +1,9 @@
+rollersrc.dir=../../..
+
+# Need this to build Roller/BSF plugins
+bsf.home=/Applications/Java/bsf
+
+# Need these to run Roller/BSF plugins
+jruby.home=/Applications/Java/jruby
+rhino.home=/Applications/Java/rhino
+groovy.home=/Applications/Java/groovy

Added: incubator/roller/trunk/sandbox/scripting/bsf/build.xml
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build.xml?view=auto&rev=517070
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/bsf/build.xml (added)
+++ incubator/roller/trunk/sandbox/scripting/bsf/build.xml Sun Mar 11 18:56:46 2007
@@ -0,0 +1,62 @@
+
+<project name="roller-scripting" default="build" basedir=".">
+    <description>Build Roller-Groovy integration classes</description>   
+
+    <property file="./ant.properties" />
+        
+    <path id="scripting.path">
+        <fileset dir="${rollersrc.dir}/build/webapp/WEB-INF/lib">   
+            <include name="roller-web.jar"/>   
+            <include name="roller-business.jar"/>   
+            <include name="commons-logging.jar"/>   
+        </fileset>
+        <fileset dir="${bsf.home}/lib">
+            <include name="bsf.jar"/>
+        </fileset>                      
+    </path>
+    
+    <target name="build">        
+        <mkdir dir="./build/classes" />
+        <javac debug="on" destdir="./build/classes">
+            <src path="./src" />
+            <classpath>
+                <path refid="scripting.path"/>
+            </classpath>
+        </javac>
+        <mkdir dir="./dist" />
+        <jar basedir="build/classes" jarfile="./dist/roller-scripting.jar" />
+    </target>
+
+    <!--
+    Copy BSF, JRuby, Groovy and Rhino jars. 
+    Versions tested:
+       - Groovy 1.0
+       - BSF 2.4.0
+       - JRuby 0.9.8
+       - Rhino 1.6 R5
+    -->
+    <target name="copy-to-roller" >
+        <copy todir="../../../build/webapp/WEB-INF/lib">
+            <fileset dir="./dist">
+               <include name="roller-scripting.jar" />
+            </fileset>
+            <fileset dir="./lib">
+               <include name="bsf.jar" />
+            </fileset>
+            <fileset dir="${groovy.home}/embeddable">
+               <include name="groovy-all-1.0.jar" />
+            </fileset>
+            <fileset dir="${jruby.home}/lib">
+               <include name="jruby.jar" />
+            </fileset>
+            <fileset dir="${rhino.home}">
+               <include name="js.jar" />
+            </fileset>
+        </copy>
+    </target>
+
+    <target name="clean">
+        <delete dir="./build" />
+    </target>
+    
+</project>

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRenderer.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRenderer.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRenderer.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRendererFactory.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRendererFactory.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/BSFRendererFactory.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRenderer.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRenderer.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRenderer.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRendererFactory.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRendererFactory.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRendererFactory.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRollerBinding.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRollerBinding.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRollerBinding.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRunner.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRunner.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/scripting/GroovyRunner.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/util/GroovyRunner.class
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/util/GroovyRunner.class?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/build/classes/org/apache/roller/util/GroovyRunner.class
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/dist/roller-scripting.jar
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/dist/roller-scripting.jar?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/dist/roller-scripting.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/lib/bsf.jar
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/lib/bsf.jar?view=auto&rev=517070
==============================================================================
Binary file - no diff available.

Propchange: incubator/roller/trunk/sandbox/scripting/bsf/lib/bsf.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRenderer.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRenderer.java?view=auto&rev=517070
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRenderer.java (added)
+++ incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRenderer.java Sun Mar 11 18:56:46 2007
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.scripting;
+
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.bsf.BSFManager;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.pojos.Template;
+import org.apache.roller.ui.rendering.Renderer;
+import org.apache.roller.ui.rendering.RenderingException;
+
+/**
+ * Renderer that evaluates template as BSF script.
+ * Supports groovy, javascript, jruby and others (via BSF).
+ */
+public class BSFRenderer implements Renderer {
+    private static Log log = LogFactory.getLog(BSFRenderer.class);
+    private Template template = null;    
+    static {
+        // Javascript, other BSF langs are registered by default
+        // and Groovy self-registers, so we need only to register JRuby 
+        BSFManager.registerScriptingEngine("jruby", 
+            "org.jruby.javasupport.bsf.JRubyEngine", new String[] {"rb"});        
+    }
+    
+    public BSFRenderer(Template template) {
+        this.template = template;
+    }    
+    public void render(Map model, Writer writer) throws RenderingException {
+        try {
+            long startTime = System.currentTimeMillis();
+            
+            BSFManager manager = new BSFManager();            
+            for (Iterator it = model.keySet().iterator(); it.hasNext();) {
+                String key = (String)it.next();
+                manager.declareBean(key, model.get(key), model.get(key).getClass());
+            }
+            manager.declareBean("out", writer, Writer.class);
+            manager.exec(template.getTemplateLanguage(), 
+                    "(java)", 1, 1, template.getContents());
+
+            long endTime = System.currentTimeMillis();
+            long renderTime = (endTime - startTime)/1000;
+            log.debug("Rendered ["+template.getId()+"] with language ["
+                    +template.getTemplateLanguage()+"] in "+renderTime+" secs"); 
+            
+        } catch (Exception ex) {
+            throw new RenderingException("Error during rendering", ex);
+        }
+    }
+}
+
+    

Added: incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRendererFactory.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRendererFactory.java?view=auto&rev=517070
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRendererFactory.java (added)
+++ incubator/roller/trunk/sandbox/scripting/bsf/src/org/apache/roller/scripting/BSFRendererFactory.java Sun Mar 11 18:56:46 2007
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.scripting;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.roller.config.RollerConfig;
+import org.apache.roller.pojos.Template;
+import org.apache.roller.ui.rendering.Renderer;
+import org.apache.roller.ui.rendering.RendererFactory;
+import org.apache.roller.util.Utilities;
+
+/**
+ * RendererFactory that calls BSF scripting language.
+ */
+public class BSFRendererFactory implements RendererFactory {   
+    private static String[] names = {"groovy", "jruby", "javascript"};
+    private static final List bsfLanguages; 
+    static {
+        String value = RollerConfig.getProperty("bsfrenderer.languageNames");
+        if (value != null) {
+            names = Utilities.stringToStringArray(value,",");            
+        } 
+        bsfLanguages = Arrays.asList(names);
+    }        
+    public Renderer getRenderer(Template template) {        
+        Renderer renderer = null;
+        if(template.getTemplateLanguage() == null || template.getId() == null) {
+            return null;
+        }        
+        if(bsfLanguages.contains(template.getTemplateLanguage())) {             
+            try {
+               renderer = new BSFRenderer(template);
+            } catch(Exception ex) {
+                return null;
+            }                        
+        }      
+        return renderer;
+    }    
+}