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/09 19:25:52 UTC

svn commit: r516495 - in /incubator/roller/trunk/sandbox/scripting/jruby: ./ bin/ lib/ src/ src/org/apache/roller/scripting/ src/org/apache/roller/util/

Author: snoopdave
Date: Fri Mar  9 10:25:51 2007
New Revision: 516495

URL: http://svn.apache.org/viewvc?view=rev&rev=516495
Log:
New BSF renderer

Added:
    incubator/roller/trunk/sandbox/scripting/jruby/
      - copied from r516282, incubator/roller/trunk/sandbox/scripting/groovy/
    incubator/roller/trunk/sandbox/scripting/jruby/ant.properties
      - copied, changed from r516280, incubator/roller/trunk/sandbox/scripting/groovy/ant.properties
    incubator/roller/trunk/sandbox/scripting/jruby/lib/
    incubator/roller/trunk/sandbox/scripting/jruby/lib/bsf.jar   (with props)
    incubator/roller/trunk/sandbox/scripting/jruby/src/
      - copied from r516280, incubator/roller/trunk/sandbox/scripting/groovy/src/
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRenderer.java
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRendererFactory.java
Removed:
    incubator/roller/trunk/sandbox/scripting/jruby/bin/
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/GroovyRenderer.java
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/GroovyRendererFactory.java
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/GroovyRollerBinding.java
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/GroovyRunner.java
    incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/util/
Modified:
    incubator/roller/trunk/sandbox/scripting/jruby/README.txt
    incubator/roller/trunk/sandbox/scripting/jruby/build.xml

Modified: incubator/roller/trunk/sandbox/scripting/jruby/README.txt
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/jruby/README.txt?view=diff&rev=516495&r1=516282&r2=516495
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/jruby/README.txt (original)
+++ incubator/roller/trunk/sandbox/scripting/jruby/README.txt Fri Mar  9 10:25:51 2007
@@ -1,22 +1,29 @@
 
-Some experiments with using Groovy
+Some experiments with using BSF to enable scripting in Roller templates.
 
 
-Scripts that run against the Roller API.
+To use it in Roller:
 
-   bin/groovy - Runs Groovy with Roller jars in path. Edit it to set paths.
+1) Put roller-scripting.jar and bsf.jar in WEB-INF/lib
 
-   bin/roller-custom.properties - Database properties for bin/groovy
+2) Add the jars necessary JavaScript, Groovy or JRuby to WEB-INF/lib
 
-   bin/listusers.gy - Lists users and roles in Roller system
+3) In your roller-custom.properies file add this override:
 
-   bin/createuser.gy - Create a user
+   rendering.rollerRendererFactories=\
+      org.apache.roller.ui.rendering.velocity.VelocityRendererFactory,\
+      org.apache.roller.scripting.BSFRendererFactory
 
-   bin/createblog.gy - Create a blog
+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.
 
-A Roller Renderer that allows use of Groovy in Roller templates:
+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}");
 
-   org.apache.roller.scripting.GroovyRenderer
-   org.apache.roller.scripting.GroovyRendererFactory
-   org.apache.roller.scripting.GroovyRollerBinding

Copied: incubator/roller/trunk/sandbox/scripting/jruby/ant.properties (from r516280, incubator/roller/trunk/sandbox/scripting/groovy/ant.properties)
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/jruby/ant.properties?view=diff&rev=516495&p1=incubator/roller/trunk/sandbox/scripting/groovy/ant.properties&r1=516280&p2=incubator/roller/trunk/sandbox/scripting/jruby/ant.properties&r2=516495
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/groovy/ant.properties (original)
+++ incubator/roller/trunk/sandbox/scripting/jruby/ant.properties Fri Mar  9 10:25:51 2007
@@ -1,2 +1,2 @@
 rollersrc.dir=../../..
-groovy.home=/Applications/Java/groovy
\ No newline at end of file
+bsf.home=/Applications/Java/bsf
\ No newline at end of file

Modified: incubator/roller/trunk/sandbox/scripting/jruby/build.xml
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/jruby/build.xml?view=diff&rev=516495&r1=516282&r2=516495
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/jruby/build.xml (original)
+++ incubator/roller/trunk/sandbox/scripting/jruby/build.xml Fri Mar  9 10:25:51 2007
@@ -10,8 +10,8 @@
             <include name="roller-business.jar"/>   
             <include name="commons-logging.jar"/>   
         </fileset>
-        <fileset dir="${groovy.home}/lib">
-            <include name="groovy-1.0.jar"/>
+        <fileset dir="${bsf.home}/lib">
+            <include name="bsf.jar"/>
         </fileset>                      
     </path>
     
@@ -24,7 +24,7 @@
             </classpath>
         </javac>
         <mkdir dir="./dist" />
-        <jar basedir="build/classes" jarfile="./dist/roller-groovy.jar" />
+        <jar basedir="build/classes" jarfile="./dist/roller-scripting.jar" />
     </target>
     
     <target name="clean">

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

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

Added: incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRenderer.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRenderer.java?view=auto&rev=516495
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRenderer.java (added)
+++ incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRenderer.java Fri Mar  9 10:25:51 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/jruby/src/org/apache/roller/scripting/BSFRendererFactory.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRendererFactory.java?view=auto&rev=516495
==============================================================================
--- incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRendererFactory.java (added)
+++ incubator/roller/trunk/sandbox/scripting/jruby/src/org/apache/roller/scripting/BSFRendererFactory.java Fri Mar  9 10:25:51 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;
+    }    
+}