tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kin...@apache.org
Subject cvs commit: jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler Generator.java JspUtil.java Node.java
Date Sat, 08 Jun 2002 00:14:35 GMT
kinman      2002/06/07 17:14:35

  Modified:    jasper2/src/share/org/apache/jasper/compiler Generator.java
                        JspUtil.java Node.java
  Log:
  Applied the patch supplied by Jan Luehe, to implement tag pooling/reuse
  based on a scheme Jan and I discussed.  Jan's note:
  
  Tag pooling/reuse is implemented as follows:
  
  - Every custom tag is assigned a tag handler pool, based on its full tag name
  and attribute set.
  
  - Tag handler pools are declared as servlet instance variables, so
  that they can be shared across page invocations.
  
  - Tag handler pools are instantiated with a
  yet-to-be-made-configurable capacity at servlet construction time
  (current default capacity is 5).
  
  - When a tag handler is required, it is retrieved from the appropriate
  tag handler pool. If the pool is empty, a new tag handler is instantiated and
  returned.
  
  - When a tag handler is no longer needed, it is returned to the appropriate
  pool. It is added to the pool, unless the pool has already reached its
  capacity, in which case the tag handler's release() method is called.
  
  - The servlet's destroy() method enumerates the servlet's tag handler
  pools and calls the release() method of every available tag handler.
  
  Outstanding issues:
  - Make tag handler pool capacity configurable.
  - Optimize tag reuse in the body of iteration tags.
  
  Revision  Changes    Path
  1.18      +154 -44   jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java
  
  Index: Generator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- Generator.java	5 Jun 2002 22:01:33 -0000	1.17
  +++ Generator.java	8 Jun 2002 00:14:35 -0000	1.18
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v
1.17 2002/06/05 22:01:33 kinman Exp $
  - * $Revision: 1.17 $
  - * $Date: 2002/06/05 22:01:33 $
  + * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v
1.18 2002/06/08 00:14:35 kinman Exp $
  + * $Revision: 1.18 $
  + * $Date: 2002/06/08 00:14:35 $
    *
    * ====================================================================
    * 
  @@ -96,6 +96,7 @@
       private boolean breakAtLF;
       private PageInfo pageInfo;
       private int maxTagNesting;
  +    private Vector tagHandlerPoolNames;
   
       /**
        * @param s the input string
  @@ -158,6 +159,99 @@
       }
   
       /**
  +     * Compiles list of tag handler pool names.
  +     */
  +    private void compileTagHandlerPoolList(Node.Nodes page)
  +	    throws JasperException {
  +
  +	class TagHandlerPoolVisitor extends Node.Visitor {
  +
  +	    private Vector names;
  +
  +	    /*
  +	     * Constructor
  +	     *
  +	     * @param v Vector of tag handler pool names to populate
  +	     */
  +	    TagHandlerPoolVisitor(Vector v) {
  +		names = v;
  +	    }
  +
  +	    /*
  +	     * Gets the name of the tag handler pool for the given custom tag
  +	     * and adds it to the list of tag handler pool names unless it is
  +	     * already contained in it.
  +	     */
  +	    public void visit(Node.CustomTag n) throws JasperException {
  +		
  +		String name = createTagHandlerPoolName(n.getPrefix(),
  +							n.getShortName(),
  +							n.getAttributes());
  +		n.setTagHandlerPoolName(name);
  +		if (!names.contains(name)) {
  +		    names.add(name);
  +		}
  +		visitBody(n);
  +	    }
  +
  +	    /*
  +	     * Creates the name of the tag handler pool whose tag handlers may
  +	     * be (re)used to service this action.
  +	     *
  +	     * @return The name of the tag handler pool
  +	     */
  +	    private String createTagHandlerPoolName(String prefix,
  +						    String shortName,
  +						    Attributes attrs) {
  +		String poolName = null;
  +
  +		if (prefix.indexOf('-') >= 0)
  +		    prefix = JspUtil.replace(prefix, '-', "$1");
  +		if (prefix.indexOf('.') >= 0)
  +		    prefix = JspUtil.replace(prefix, '.', "$2");
  +
  +		if (shortName.indexOf('-') >= 0)
  +		    shortName = JspUtil.replace(shortName, '-', "$1");
  +		if (shortName.indexOf('.') >= 0)
  +		    shortName = JspUtil.replace(shortName, '.', "$2");
  +		if (shortName.indexOf(':') >= 0)
  +		    shortName = JspUtil.replace(shortName, ':', "$3");
  +
  +		poolName = "_jspx_tagPool_" + prefix + "_" + shortName;
  +		if (attrs != null) {
  +		    String[] attrNames = new String[attrs.getLength()];
  +		    for (int i=0; i<attrNames.length; i++) {
  +			attrNames[i] = attrs.getQName(i);
  +		    }
  +		    Arrays.sort(attrNames, Collections.reverseOrder());
  +		    for (int i=0; i<attrNames.length; i++) {
  +			poolName = poolName + "_" + attrNames[i];
  +		    }
  +		}
  +		return poolName;
  +	    }
  +	}
  +
  +	page.visit(new TagHandlerPoolVisitor(tagHandlerPoolNames));
  +    }
  +
  +    /**
  +     * Generates the destroy() method which is responsible for calling the
  +     * release() method on every tag handler in any of the tag handler pools.
  +     */
  +    private void generateDestroy() {
  +	out.printil("public void jspDestroy() {");
  +	out.pushIndent();
  +	for (int i=0; i<tagHandlerPoolNames.size(); i++) {
  +	    out.printin((String) tagHandlerPoolNames.elementAt(i));
  +	    out.println(".release();");
  +	}
  +	out.popIndent();	
  +	out.printil("}");
  +	out.println();
  +    }
  +
  +    /**
        * Generates the beginning of the static portion of the servelet.
        */
       private void generatePreamble(Node.Nodes page) throws JasperException {
  @@ -204,15 +298,13 @@
   
           // Static data for getIncludes()
           out.printil("private static java.util.Vector _jspx_includes;");
  -        out.println();  
           out.println();
           List includes = pageInfo.getIncludes();
           iter = includes.iterator();
           if( !includes.isEmpty() ) {
               out.printil("static {");
               out.pushIndent();
  -            out.printin(
  -                "_jspx_includes = new java.util.Vector(");
  +            out.printin("_jspx_includes = new java.util.Vector(");
               out.print(""+includes.size());
               out.println(");");
               while (iter.hasNext()) {
  @@ -222,14 +314,25 @@
               }
               out.popIndent();                     
               out.printil("}");
  -            out.println();  
               out.println();
           }
   
  - 	// Class fields declarations
  + 	// Class variable declarations
        	
  -	// Constructor (empty so far) here
  -
  +	// Declare tag pools, one per (tag with specific attribute list).
  +	if (!tagHandlerPoolNames.isEmpty()) {
  +	    for (int i=0; i<tagHandlerPoolNames.size(); i++) {
  +		out.printil("private org.apache.jasper.runtime.TagHandlerPool "
  +			    + tagHandlerPoolNames.elementAt(i) + ";");
  +	    }
  +            out.println();
  +	}
  + 
  +	// Constructor
  +	if (!tagHandlerPoolNames.isEmpty()) {
  +	    generateServletConstructor(servletClassName);
  +	}
  + 
   	// Methods here
   
   	// Method used to get compile time include file dependencies
  @@ -239,8 +342,11 @@
           out.popIndent();
           out.printil("}");
           out.println();
  -        out.println();
   
  +	if (!tagHandlerPoolNames.isEmpty()) {
  +	    generateDestroy();
  +	}
  + 
   	// Now the service method
   	out.printin("public void ");
   	out.print  (serviceMethodName);
  @@ -298,6 +404,21 @@
   	out.println();
       }
   
  +    /*
  +     * Generates the servlet constructor.
  +     */
  +    private void generateServletConstructor(String servletClassName) {
  +	out.printil("public " + servletClassName + "() {");
  +	out.pushIndent();
  +	for (int i=0; i<tagHandlerPoolNames.size(); i++) {
  +	    out.printin((String) tagHandlerPoolNames.elementAt(i));
  +	    out.println(" = new org.apache.jasper.runtime.TagHandlerPool();");
  +	}
  +	out.popIndent();
  +	out.printil("}");
  +	out.println();
  +    }
  +
       /**
        * Generate codes defining the classes used in the servlet.
        * 1. Servlet state object, used to pass servlet info round methods.
  @@ -1081,9 +1202,14 @@
   	    out.printin(tagHandlerClass.getName());
   	    out.print(" ");
   	    out.print(tagHandlerVar);
  -	    out.print(" = new ");
  +	    out.print(" = (");
  +	    out.print(tagHandlerClass.getName());
  +	    out.print(") ");
  +	    out.print(n.getTagHandlerPoolName());
  +	    out.print(".get(");
   	    out.print(tagHandlerClass.getName());
  -	    out.println("();");
  +	    out.println(".class);");
  +
   	    generateSetters(n, tagHandlerVar, handlerInfo);
   	    
               if (implementsTryCatchFinally) {
  @@ -1206,13 +1332,17 @@
                   out.pushIndent();
   		out.printin(tagHandlerVar);
   		out.println(".doFinally();");
  -                out.printin(tagHandlerVar);
  -                out.println(".release();");
  +                out.printin(n.getTagHandlerPoolName());
  +                out.print(".reuse(");
  +		out.print(tagHandlerVar);
  +		out.println(");");
                   out.popIndent();
                   out.printil("}");
               } else {
  -                out.printin(tagHandlerVar);
  -                out.println(".release();");
  +                out.printin(n.getTagHandlerPoolName());
  +                out.println(".reuse(");
  +		out.printin(tagHandlerVar);
  +		out.println(");");
   	    }
   
   	    // Declare and synchronize AT_END variables
  @@ -1266,28 +1396,6 @@
   	    }
   	}
   
  -	private String replace(String name, char replace, String with) {
  -	    StringBuffer buf = new StringBuffer();
  -	    int begin = 0;
  -	    int end;
  -	    int last = name.length();
  -
  -	    while (true) {
  -		end = name.indexOf(replace, begin);
  -		if (end < 0) {
  -		    end = last;
  -		}
  -		buf.append(name.substring(begin, end));
  -		if (end == last) {
  -		    break;
  -		}
  -		buf.append(with);
  -		begin = end + 1;
  -	    }
  -
  -	    return buf.toString();
  -	}
  -	
   	/*
   	 * Creates a tag variable name by concatenating the given prefix and
   	 * shortName and replacing '-' with "$1", '.' with "$2", and ':' with
  @@ -1296,16 +1404,16 @@
   	private String createTagVarName(String fullName, String prefix,
   					String shortName) {
   	    if (prefix.indexOf('-') >= 0)
  -		prefix = replace(prefix, '-', "$1");
  +		prefix = JspUtil.replace(prefix, '-', "$1");
   	    if (prefix.indexOf('.') >= 0)
  -		prefix = replace(prefix, '.', "$2");
  +		prefix = JspUtil.replace(prefix, '.', "$2");
   
   	    if (shortName.indexOf('-') >= 0)
  -		shortName = replace(shortName, '-', "$1");
  +		shortName = JspUtil.replace(shortName, '-', "$1");
   	    if (shortName.indexOf('.') >= 0)
  -		shortName = replace(shortName, '.', "$2");
  +		shortName = JspUtil.replace(shortName, '.', "$2");
   	    if (shortName.indexOf(':') >= 0)
  -		shortName = replace(shortName, ':', "$3");
  +		shortName = JspUtil.replace(shortName, ':', "$3");
   
   	    synchronized (tagVarNumbers) {
   		String varName = prefix + "_" + shortName + "_";
  @@ -1493,6 +1601,7 @@
   	pageInfo = compiler.getPageInfo();
   	beanInfo = pageInfo.getBeanRepository();
   	breakAtLF = ctxt.getOptions().getMappedFile();
  +	tagHandlerPoolNames = new Vector();
       }
   
       /**
  @@ -1505,6 +1614,7 @@
   				Node.Nodes page) throws JasperException {
   	Generator gen = new Generator(out, compiler);
   
  +	gen.compileTagHandlerPoolList(page);
   	gen.generatePreamble(page);
   	page.visit(gen.new GenerateVisitor(out, gen.methodsBuffer));
   	gen.generatePostamble(page);
  
  
  
  1.4       +31 -5     jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/JspUtil.java
  
  Index: JspUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/JspUtil.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JspUtil.java	20 May 2002 18:02:56 -0000	1.3
  +++ JspUtil.java	8 Jun 2002 00:14:35 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/JspUtil.java,v
1.3 2002/05/20 18:02:56 kinman Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/05/20 18:02:56 $
  + * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/JspUtil.java,v
1.4 2002/06/08 00:14:35 kinman Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/06/08 00:14:35 $
    *
    * ====================================================================
    * 
  @@ -356,7 +356,7 @@
   	}
   	return escString;
       }
  -    
  + 
       /**
        *  Escape the 5 entities defined by XML.
        */
  @@ -381,7 +381,33 @@
           }
           return sb.toString();
       }
  -    
  +
  +    /**
  +     * Replaces any occurrence of <tt>replace</tt> with <tt>with</tt>
in the
  +     * given string.
  +     */
  +    public static String replace(String name, char replace, String with) {
  +	StringBuffer buf = new StringBuffer();
  +	int begin = 0;
  +	int end;
  +	int last = name.length();
  +
  +	while (true) {
  +	    end = name.indexOf(replace, begin);
  +	    if (end < 0) {
  +		end = last;
  +	    }
  +	    buf.append(name.substring(begin, end));
  +	    if (end == last) {
  +		break;
  +	    }
  +	    buf.append(with);
  +	    begin = end + 1;
  +	}
  +	
  +	return buf.toString();
  +    }
  +
       public static class ValidAttribute {
      	String name;
   	boolean mandatory;
  
  
  
  1.12      +13 -3     jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java
  
  Index: Node.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Node.java	5 Jun 2002 23:29:53 -0000	1.11
  +++ Node.java	8 Jun 2002 00:14:35 -0000	1.12
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v
1.11 2002/06/05 23:29:53 kinman Exp $
  - * $Revision: 1.11 $
  - * $Date: 2002/06/05 23:29:53 $
  + * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v
1.12 2002/06/08 00:14:35 kinman Exp $
  + * $Revision: 1.12 $
  + * $Date: 2002/06/08 00:14:35 $
    *
    * ====================================================================
    * 
  @@ -669,6 +669,7 @@
   	private boolean hasUsebean;
   	private boolean hasIncludeAction;
   	private boolean hasSetProperty;
  +	private String tagHandlerPoolName;
   
   	public CustomTag(Attributes attrs, Mark start, String name,
   			 String prefix, String shortName, Node parent) {
  @@ -750,6 +751,15 @@
   	public boolean isHasSetProperty() {
   	    return hasSetProperty;
   	}
  +
  +	public String getTagHandlerPoolName() {
  +	    return tagHandlerPoolName;
  +	}
  +
  +	public void setTagHandlerPoolName(String s) {
  +	    tagHandlerPoolName = s;
  +	}
  +
       }
   
       /**
  
  
  

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


Mime
View raw message