Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 89786 invoked from network); 6 Mar 2003 00:18:26 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 6 Mar 2003 00:18:26 -0000 Received: (qmail 5483 invoked by uid 97); 6 Mar 2003 00:20:10 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 5476 invoked from network); 6 Mar 2003 00:20:10 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 6 Mar 2003 00:20:10 -0000 Received: (qmail 89593 invoked by uid 500); 6 Mar 2003 00:18:24 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 86445 invoked from network); 5 Mar 2003 00:47:01 -0000 To: commons-dev@jakarta.apache.org Cc: james_strachan@yahoo.co.uk From: "Pete Kazmier" Subject: [Jelly][PATCH] BSF Script Patch Date: Tue, 04 Mar 2003 19:47:05 -0500 Message-ID: User-Agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.2 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Here is a patch for the Jelly BSF Taglib. In summary, this gets the BSF script tag working properly for Jython. In Maven-land, we need this for a few plugins we are working on. A brief run down of the changes: - The existing ScriptTag calls the BSF eval() method, which can only be used to evaluate single expressions. To evaluate a script, the exec() method must be used. This patch fixes that issue. - Updated the BSF Taglib to use the latest and greatest BSF thats been moved into the jakarta namespace. Also provides more support for addtional languages. - The ScriptTag was not populating the BSF object registry so the jelly context was not available to scripts. I used the same hack that was used for the expressions, could be cleaned up, just wanted to get this in so we had something working. - Added a JythonTagLibrary in addition to the JPythonTagLibrary. Also updated jelly.properties to register this tag library under "jython". Outstanding issues (basically things I couldn't figure out myself yet as I am taglib deficient): - Couldn't figure out how to use the tag that expects the 'language' attribute, for that matter, didn't even know which one supported that attribute. So I was not able to get this to work: ... With all that said, here is how I use the jython script tag in my maven build file: print "This is a jython script" print "My Maven POM's name is", pom.getName() print "If the POM has a name, then the context passing is working" And don't forget to include the deps needed for this in your POM (for you mavenites): commons-jelly commons-jelly-tags-bsf 1.0-SNAPSHOT bsf 2.3-rc1 jython 2.1 And finally, if you decide to try this out before the latest version of Jelly has been updated in Maven, you'll have to explicity set the class for the xmlns line. For example, the above would look like: Thanks, Pete --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=jelly.patch Content-Description: Jelly BSF Taglib Patch Index: jelly-tags/bsf/project.xml =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/project.xml,v retrieving revision 1.4 diff -u -r1.4 project.xml --- jelly-tags/bsf/project.xml 27 Jan 2003 05:24:17 -0000 1.4 +++ jelly-tags/bsf/project.xml 5 Mar 2003 00:25:08 -0000 @@ -30,7 +30,7 @@ bsf - 2.2 + 2.3-rc1 http://jakarta.apache.org/bsf Index: jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpression.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpression.java,v retrieving revision 1.1 diff -u -r1.1 BSFExpression.java --- jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpression.java 8 Jan 2003 05:02:48 -0000 1.1 +++ jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpression.java 5 Mar 2003 00:25:08 -0000 @@ -61,8 +61,8 @@ */ package org.apache.commons.jelly.tags.bsf; -import com.ibm.bsf.BSFEngine; -import com.ibm.bsf.BSFManager; +import org.apache.bsf.BSFEngine; +import org.apache.bsf.BSFManager; import java.util.Iterator; Index: jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpressionFactory.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpressionFactory.java,v retrieving revision 1.2 diff -u -r1.2 BSFExpressionFactory.java --- jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpressionFactory.java 24 Jan 2003 07:40:58 -0000 1.2 +++ jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFExpressionFactory.java 5 Mar 2003 00:25:08 -0000 @@ -67,9 +67,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.ibm.bsf.BSFEngine; -import com.ibm.bsf.BSFException; -import com.ibm.bsf.BSFManager; +import org.apache.bsf.BSFEngine; +import org.apache.bsf.BSFException; +import org.apache.bsf.BSFManager; /** Represents a factory of BSF expressions * Index: jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFTagLibrary.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFTagLibrary.java,v retrieving revision 1.2 diff -u -r1.2 BSFTagLibrary.java --- jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFTagLibrary.java 20 Feb 2003 18:56:16 -0000 1.2 +++ jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/BSFTagLibrary.java 5 Mar 2003 00:25:09 -0000 @@ -68,8 +68,8 @@ import org.apache.commons.jelly.tags.core.CoreTagLibrary; import org.xml.sax.Attributes; -import com.ibm.bsf.BSFEngine; -import com.ibm.bsf.BSFException; +import org.apache.bsf.BSFEngine; +import org.apache.bsf.BSFException; /** Describes the Taglib. This class could be generated by XDoclet @@ -83,14 +83,14 @@ public BSFTagLibrary() { registerTagFactory( - "script", - new TagFactory() { + "script", + new TagFactory() { public Tag createTag(String name, Attributes attributes) throws JellyException { return createScriptTag(name, attributes); } - } - ); + } + ); } public void setLanguage(String language) { @@ -115,7 +115,8 @@ */ protected Tag createScriptTag(String name, Attributes attributes) throws JellyException { try { - return new ScriptTag( expressionFactory.getBSFEngine() ); + return new ScriptTag( expressionFactory.getBSFEngine(), + expressionFactory.getBSFManager()); } catch (BSFException e) { throw new JellyException("Failed to create BSFEngine: " + e, e); Index: jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/JellyContextRegistry.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/JellyContextRegistry.java,v retrieving revision 1.1 diff -u -r1.1 JellyContextRegistry.java --- jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/JellyContextRegistry.java 8 Jan 2003 05:02:48 -0000 1.1 +++ jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/JellyContextRegistry.java 5 Mar 2003 00:25:09 -0000 @@ -61,7 +61,7 @@ */ package org.apache.commons.jelly.tags.bsf; -import com.ibm.bsf.util.ObjectRegistry; +import org.apache.bsf.util.ObjectRegistry; import org.apache.commons.jelly.JellyContext; import org.apache.commons.logging.Log; Index: jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/PNutsTagLibrary.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/PNutsTagLibrary.java,v retrieving revision 1.1 diff -u -r1.1 PNutsTagLibrary.java --- jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/PNutsTagLibrary.java 8 Jan 2003 05:02:47 -0000 1.1 +++ jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/PNutsTagLibrary.java 5 Mar 2003 00:25:09 -0000 @@ -61,7 +61,7 @@ */ package org.apache.commons.jelly.tags.bsf; -import com.ibm.bsf.BSFManager; +import org.apache.bsf.BSFManager; /** Describes the Taglib. This class could be generated by XDoclet * Index: jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/ScriptTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/ScriptTag.java,v retrieving revision 1.1 diff -u -r1.1 ScriptTag.java --- jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/ScriptTag.java 20 Feb 2003 18:56:16 -0000 1.1 +++ jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/ScriptTag.java 5 Mar 2003 00:25:09 -0000 @@ -61,6 +61,8 @@ */ package org.apache.commons.jelly.tags.bsf; +import java.util.Iterator; + import org.apache.commons.jelly.JellyTagException; import org.apache.commons.jelly.LocationAware; import org.apache.commons.jelly.MissingAttributeException; @@ -69,8 +71,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.ibm.bsf.BSFEngine; -import com.ibm.bsf.BSFException; +import org.apache.bsf.BSFEngine; +import org.apache.bsf.BSFManager; +import org.apache.bsf.BSFException; /** * A tag which evaluates its body using the current scripting language @@ -84,13 +87,15 @@ private static final Log log = LogFactory.getLog(ScriptTag.class.getName() + ".evaluating"); private BSFEngine engine; + private BSFManager manager; private String elementName; private String fileName; private int columnNumber; private int lineNumber; - public ScriptTag(BSFEngine engine) { + public ScriptTag(BSFEngine engine, BSFManager manager) { this.engine = engine; + this.manager = manager; } // Tag interface @@ -98,15 +103,28 @@ public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { String text = getBodyText(); - log.debug(text); - - try { - engine.eval(fileName, lineNumber, columnNumber, text); - } - catch (BSFException e) { - throw new JellyTagException("Error occurred with script: " + e, e); + log.debug(text); + + // XXXX: unfortunately we must sychronize evaluations + // so that we can swizzle in the context. + // maybe we could create an expression from a context + // (and so create a BSFManager for a context) + synchronized (getRegistry()) { + getRegistry().setJellyContext(context); + + try { + // XXXX: hack - there must be a better way!!! + for ( Iterator iter = context.getVariableNames(); iter.hasNext(); ) { + String name = (String) iter.next(); + Object value = context.getVariable( name ); + manager.declareBean( name, value, value.getClass() ); + } + engine.exec(fileName, lineNumber, columnNumber, text); + } + catch (BSFException e) { + throw new JellyTagException("Error occurred with script: " + e, e); + } } - } // Properties @@ -186,4 +204,8 @@ this.lineNumber = lineNumber; } + private JellyContextRegistry getRegistry() + { + return (JellyContextRegistry) this.manager.getObjectRegistry(); + } } Index: src/java/org/apache/commons/jelly/jelly.properties =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/jelly.properties,v retrieving revision 1.36 diff -u -r1.36 jelly.properties --- src/java/org/apache/commons/jelly/jelly.properties 26 Feb 2003 09:41:37 -0000 1.36 +++ src/java/org/apache/commons/jelly/jelly.properties 5 Mar 2003 00:25:14 -0000 @@ -20,6 +20,7 @@ jetty = org.apache.commons.jelly.tags.jetty.JettyTagLibrary jms = org.apache.commons.jelly.tags.jms.JMSTagLibrary jpython = org.apache.commons.jelly.tags.bsf.JPythonTagLibrary +jython = org.apache.commons.jelly.tags.bsf.JythonTagLibrary jsl = org.apache.commons.jelly.tags.jsl.JSLTagLibrary junit = org.apache.commons.jelly.tags.junit.JUnitTagLibrary log = org.apache.commons.jelly.tags.log.LogTagLibrary /* * $Header: /home/cvs/jakarta-commons/jelly/jelly-tags/bsf/src/java/org/apache/commons/jelly/tags/bsf/JPythonTagLibrary.java,v 1.1 2003/01/08 05:02:48 dion Exp $ * $Revision: 1.1 $ * $Date: 2003/01/08 05:02:48 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * * $Id: JPythonTagLibrary.java,v 1.1 2003/01/08 05:02:48 dion Exp $ */ package org.apache.commons.jelly.tags.bsf; /** Describes the Taglib. This class could be generated by XDoclet * * @author James Strachan * @version $Revision: 1.1 $ */ public class JythonTagLibrary extends BSFTagLibrary { public JythonTagLibrary() { setLanguage( "jython" ); } } --=-=-= Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org --=-=-=--