costin 2003/01/22 11:54:25 Modified: jasper2/src/share/org/apache/jasper/runtime TagHandlerPool.java Log: Few small optimizations ( take expensive operations out of sync ). Added a factory method and a mechanism to configure individual jsps at _runtime_. Some JSPs may have more tags or be more frequently used, one size won't fit all. It uses servlet and context init params. Revision Changes Path 1.4 +73 -22 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/runtime/TagHandlerPool.java Index: TagHandlerPool.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/runtime/TagHandlerPool.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TagHandlerPool.java 16 Nov 2002 04:20:10 -0000 1.3 +++ TagHandlerPool.java 22 Jan 2003 19:54:24 -0000 1.4 @@ -63,6 +63,7 @@ import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.Tag; +import javax.servlet.Servlet; import org.apache.jasper.Constants; /** @@ -74,20 +75,55 @@ private Tag[] handlers; + public static String OPTION_TAGPOOL="jasper.tagpoolClassName"; + public static String OPTION_MAXSIZE="jasper.tagpoolMaxSize"; + // index of next available tag handler private int current; + public static TagHandlerPool getTagHandlerPool( Servlet jspServlet) { + TagHandlerPool result=null; + + String tpClassName=getOption( jspServlet, OPTION_TAGPOOL, null); + if( tpClassName != null ) { + try { + Class c=Class.forName( tpClassName ); + result=(TagHandlerPool)c.newInstance(); + } catch (Exception e) { + e.printStackTrace(); + result=null; + } + } + if( result==null ) result=new TagHandlerPool(); + result.init(jspServlet); + + return result; + } + + protected void init( Servlet servlet ) { + int maxSize=-1; + String maxSizeS=getOption(servlet, OPTION_MAXSIZE, null); + maxSize=Integer.parseInt(maxSizeS); + if( maxSize <0 ) { + maxSize=Constants.MAX_POOL_SIZE; + } + this.handlers = new Tag[maxSize]; + this.current = -1; + } + /** * Constructs a tag handler pool with the default capacity. */ public TagHandlerPool() { - this(Constants.MAX_POOL_SIZE); + // Nothing - jasper generated servlets call the other constructor, + // this should be used in future + init . } /** * Constructs a tag handler pool with the given capacity. * * @param capacity Tag handler pool capacity + * @deprecated Use static getTagHandlerPool */ public TagHandlerPool(int capacity) { this.handlers = new Tag[capacity]; @@ -104,20 +140,22 @@ * * @throws JspException if a tag handler cannot be instantiated */ - public synchronized Tag get(Class handlerClass) throws JspException { + public Tag get(Class handlerClass) throws JspException { Tag handler = null; - - if (current >= 0) { - handler = handlers[current--]; - } else { - try { - return (Tag) handlerClass.newInstance(); - } catch (Exception e) { - throw new JspException(e.getMessage(), e); - } - } - - return handler; + synchronized( this ) { + if (current >= 0) { + handler = handlers[current--]; + return handler; + } + } + + // Out of sync block - there is no need for other threads to + // wait for us to construct a tag for this thread. + try { + return (Tag) handlerClass.newInstance(); + } catch (Exception e) { + throw new JspException(e.getMessage(), e); + } } /** @@ -127,11 +165,15 @@ * * @param handler Tag handler to add to this tag handler pool */ - public synchronized void reuse(Tag handler) { - if (current < (handlers.length - 1)) - handlers[++current] = handler; - else - handler.release(); + public void reuse(Tag handler) { + synchronized( this ) { + if (current < (handlers.length - 1)) { + handlers[++current] = handler; + return; + } + } + // There is no need for other threads to wait for us to release + handler.release(); } /** @@ -143,5 +185,14 @@ handlers[i].release(); } } + + protected static String getOption( Servlet servlet, String name, String defaultV) { + String value=servlet.getServletConfig().getInitParameter(name); + if( value != null ) return value; + value=servlet.getServletConfig().getServletContext().getInitParameter(name); + if( value!=null ) return value; + return defaultV; + } + }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>