commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly maven.xml
Date Tue, 08 Oct 2002 11:26:00 GMT
jstrachan    2002/10/08 04:26:00

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/werkz
                        WerkzTagSupport.java ProjectTag.java
                        AttainGoalTag.java
               jelly/src/java/org/apache/commons/jelly/impl TagScript.java
               jelly/src/java/org/apache/commons/jelly/tags/util
                        UtilTagLibrary.java
               jelly/src/java/org/apache/commons/jelly/tags/core
                        ThreadTag.java
               jelly    maven.xml
  Added:       jelly/src/java/org/apache/commons/jelly/tags/util
                        SleepTag.java
  Log:
  Patches to fix Vincent's problem when trying to invoke goals asynchronously in other threads.
This seems to work fine now.
  
  Added helper tag, <util:sleep millis="1000"/> to be able to get one thread to sleep
for a certain time. Adding some kind of <waitFor> would be much more appropriate instead.
  
  Also there's a sample of this in action via
  
    maven demo:async
  
  Revision  Changes    Path
  1.6       +6 -2      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/WerkzTagSupport.java
  
  Index: WerkzTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/WerkzTagSupport.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- WerkzTagSupport.java	14 Jun 2002 11:22:33 -0000	1.5
  +++ WerkzTagSupport.java	8 Oct 2002 11:26:00 -0000	1.6
  @@ -107,11 +107,15 @@
        * @return the goal manager instance 
        */
       protected Project getProject() {
  +        Project answer = null;
           ProjectTag tag = (ProjectTag) findAncestorWithClass(ProjectTag.class);
           if ( tag != null) {
  -            return tag.getProject();
  +            answer = tag.getProject();
           }
  -        return (Project) context.findVariable( "org.apache.commons.jelly.werkz.Project"
);
  +        if (answer == null) {
  +            answer = (Project) context.findVariable( "org.apache.commons.jelly.werkz.Project"
);
  +        }
  +        return answer;
       }
       
       
  
  
  
  1.13      +1 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/ProjectTag.java
  
  Index: ProjectTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/ProjectTag.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ProjectTag.java	26 Jun 2002 09:24:34 -0000	1.12
  +++ ProjectTag.java	8 Oct 2002 11:26:00 -0000	1.13
  @@ -91,7 +91,7 @@
        * @return the project instance 
        */
       public Project getProject() {
  -        if ( project == null ) {
  +        if ( project == null && context != null ) {
   	        // we may be invoked inside a child script, so lets try find the parent project
   	        project = (Project) context.findVariable( "org.apache.commons.jelly.werkz.Project"
);
   	        if ( project == null ) {
  
  
  
  1.6       +19 -23    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/AttainGoalTag.java
  
  Index: AttainGoalTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/AttainGoalTag.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- AttainGoalTag.java	15 Jul 2002 16:18:15 -0000	1.5
  +++ AttainGoalTag.java	8 Oct 2002 11:26:00 -0000	1.6
  @@ -94,44 +94,40 @@
        * then run all the current targets
        */
       public void doTag(final XMLOutput output) throws Exception {
  -        
  -        AttainTag attainTag = (AttainTag) findAncestorWithClass( AttainTag.class );
  +        AttainTag attainTag =
  +            (AttainTag) findAncestorWithClass(AttainTag.class);
           Session session = null;
   
  -        if ( attainTag == null ) {
  -            session = new JellySession( output );
  -        } else {
  +        if (attainTag == null) {
  +            session = new JellySession(output);
  +        }
  +        else {
               session = attainTag.getSession();
           }
   
  -        ProjectTag projectTag = (ProjectTag) findAncestorWithClass( ProjectTag.class );
  -
  -        Project project = projectTag.getProject();
  +        Project project = getProject();
  +        
  +        if (project == null) {
  +            throw new JellyException("No Project available");
  +        }
   
           invokeBody(output);
   
  -        try
  -        {
  -            project.attainGoal( getName(),
  -                                session );
  +        try {
  +            project.attainGoal(getName(), session);
           }
  -        catch (UnattainableGoalException e)
  -        {
  +        catch (UnattainableGoalException e) {
               Throwable root = e.getRootCause();
   
  -            if ( root != null )
  -            {
  -                if ( root instanceof JellyException )
  -                {
  +            if (root != null) {
  +                if (root instanceof JellyException) {
                       throw (JellyException) root;
                   }
  -                if ( root instanceof UnattainableGoalException )
  -                {
  +                if (root instanceof UnattainableGoalException) {
                       throw e;
                   }
               }
  -            else
  -            {
  +            else {
                   e.fillInStackTrace();
                   throw e;
               }
  
  
  
  1.21      +33 -31    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TagScript.java
  
  Index: TagScript.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/TagScript.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- TagScript.java	3 Oct 2002 18:14:43 -0000	1.20
  +++ TagScript.java	8 Oct 2002 11:26:00 -0000	1.21
  @@ -487,32 +487,6 @@
       }
       
       /**
  -     * A helper method to handle this non-Jelly exception.
  -     * This method will rethrow the exception, wrapped in a JellyException
  -     * while adding line number information etc.
  -     */
  -    protected void handleException(Exception e) throws Exception {
  -        log.error( "Caught exception: " + e, e );
  -
  -        if ( e instanceof JellyException )
  -        {
  -            e.fillInStackTrace();
  -            throw e;
  -        }
  -
  -        if ( e instanceof InvocationTargetException)
  -        {
  -            throw new JellyException( ((InvocationTargetException)e).getTargetException(),
  -                                      fileName,
  -                                      elementName,
  -                                      columnNumber,
  -                                      lineNumber );
  -        }
  -
  -        throw new JellyException(e, fileName, elementName, columnNumber, lineNumber); 
          
  -    }
  -    
  -    /**
        * Creates a new Jelly exception, adorning it with location information
        */
       protected JellyException createJellyException(String reason) {
  @@ -549,6 +523,9 @@
        * such as adding line number information etc.
        */
       protected void handleException(JellyException e) throws Exception {
  +        //e.printStackTrace();
  +        log.error( "Caught exception: " + e, e );
  +
           if (e.getLineNumber() == -1) {
               e.setColumnNumber(columnNumber);
               e.setLineNumber(lineNumber);
  @@ -560,6 +537,31 @@
               e.setElementName( elementName );
           }
           throw e;
  +    }
  +    
  +    /**
  +     * A helper method to handle this non-Jelly exception.
  +     * This method will rethrow the exception, wrapped in a JellyException
  +     * while adding line number information etc.
  +     */
  +    protected void handleException(Exception e) throws Exception {
  +        //e.printStackTrace();
  +        log.error( "Caught exception: " + e, e );
  +
  +        if ( e instanceof JellyException ) {
  +            e.fillInStackTrace();
  +            throw e;
  +        }
  +
  +        if ( e instanceof InvocationTargetException) {
  +            throw new JellyException( ((InvocationTargetException)e).getTargetException(),
  +                                      fileName,
  +                                      elementName,
  +                                      columnNumber,
  +                                      lineNumber );
  +        }
  +
  +        throw new JellyException(e, fileName, elementName, columnNumber, lineNumber); 
          
       }
       
   }
  
  
  
  1.4       +8 -7      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java
  
  Index: UtilTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- UtilTagLibrary.java	2 Oct 2002 16:07:27 -0000	1.3
  +++ UtilTagLibrary.java	8 Oct 2002 11:26:00 -0000	1.4
  @@ -68,11 +68,12 @@
    *  @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
    *  @version $Revision$
    */
  -public class UtilTagLibrary extends TagLibrary
  -{
  +public class UtilTagLibrary extends TagLibrary {
  +    
       public UtilTagLibrary() {
           registerTag("loadText", LoadTextTag.class);
           registerTag("properties", PropertiesTag.class);
           registerTag("tokenize", TokenizeTag.class);
  +        registerTag("sleep", SleepTag.class);
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/SleepTag.java
  
  Index: SleepTag.java
  ===================================================================
  /*
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 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", "Tomcat", 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
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.jelly.tags.util;
  
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  /**
   * A tag which sleeps for a given amount of time.
   * 
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.6 $
   */
  public class SleepTag extends TagSupport {
      private long millis;
  
      public SleepTag() {
      }
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(final XMLOutput output) throws Exception {
          if (millis > 0) {
              Thread.currentThread().sleep(millis);
          }
      }
  
      // Properties
      //------------------------------------------------------------------------- 
  
      /**
       * Sets the amount of time that this thread should sleep for in milliseconds.
       */
      public void setMillis(long millis) {
          this.millis = millis;
      }
  
  }
  
  
  
  1.4       +11 -3     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ThreadTag.java
  
  Index: ThreadTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ThreadTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ThreadTag.java	8 Oct 2002 09:20:10 -0000	1.3
  +++ ThreadTag.java	8 Oct 2002 11:26:00 -0000	1.4
  @@ -74,7 +74,8 @@
       private String name = null;
       /** the destination of output */
       private XMLOutput xmlOutput;
  -    
  +    /** Should we close the underlying output */
  +    private boolean closeOutput;
   
       public ThreadTag() {
       }
  @@ -88,14 +89,19 @@
           }
   
           // lets create a child context
  -        final JellyContext newContext = new JellyContext(context);
  +        final JellyContext newContext = context.newJellyContext();
                   
           Thread thread = new Thread(
               new Runnable() {
                   public void run() {
                       try {
                           getBody().run(newContext, xmlOutput);
  -                        xmlOutput.close();
  +                        if (closeOutput) {
  +                            xmlOutput.close();
  +                        }
  +                        else {
  +                            xmlOutput.flush();
  +                        }
                       }
                       catch (Exception e) {
                           e.printStackTrace();
  @@ -121,6 +127,7 @@
        * Sets the destination of output
        */
       public void setXmlOutput(XMLOutput xmlOutput) {
  +        this.closeOutput = false;
           this.xmlOutput = xmlOutput;
       }
       
  @@ -129,6 +136,7 @@
        * @param name The output file name
        */
       public void setFile(String name) throws IOException {
  +        this.closeOutput = true;
           setXmlOutput( XMLOutput.createXMLOutput(new FileOutputStream(name)) );
       }
   }
  
  
  
  1.38      +20 -1     jakarta-commons-sandbox/jelly/maven.xml
  
  Index: maven.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/maven.xml,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- maven.xml	4 Oct 2002 11:28:20 -0000	1.37
  +++ maven.xml	8 Oct 2002 11:26:00 -0000	1.38
  @@ -1,4 +1,4 @@
  -<project default="java:jar" xmlns:j="jelly:core" xmlns:maven="jelly:maven">
  +<project default="java:jar" xmlns:j="jelly:core" xmlns:maven="jelly:maven" xmlns:util="jelly:util">
   
     <!-- define jelly task -->	
     <goal name="jelly-task" prereqs="java:compile, test:compile">
  @@ -498,4 +498,23 @@
         <arg value="-Dtestsysprop=valueOfTestSystemProp"/> 
       </java>
     </goal>
  +
  +
  +  <goal name="demo:async" 
  +  	description="An example of using asynchronous invocation of goals">
  +
  +    <j:thread>
  +      <attainGoal name="demo:asyncOtherThread"/>
  +    </j:thread>
  +
  +    <echo>XXXX</echo>
  +
  +		<util:sleep millis="1000"/>
  +  </goal>
  +
  +  <goal name="demo:asyncOtherThread"
  +  	description="A private target invoked by the async demo">
  +    <echo>YYYY</echo>
  +  </goal>
  +
   </project>
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message