1 17 18 package org.apache.jasper.runtime; 19 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.tagext.Tag ; 23 24 import org.apache.AnnotationProcessor; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.jasper.Constants; 28 29 34 public class TagHandlerPool { 35 36 private Tag [] handlers; 37 38 public static String OPTION_TAGPOOL="tagpoolClassName"; 39 public static String OPTION_MAXSIZE="tagpoolMaxSize"; 40 41 private Log log = LogFactory.getLog(TagHandlerPool.class); 42 43 private int current; 45 protected AnnotationProcessor annotationProcessor = null; 46 47 public static TagHandlerPool getTagHandlerPool( ServletConfig config) { 48 TagHandlerPool result=null; 49 50 String tpClassName=getOption( config, OPTION_TAGPOOL, null); 51 if( tpClassName != null ) { 52 try { 53 Class c=Class.forName( tpClassName ); 54 result=(TagHandlerPool)c.newInstance(); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 result=null; 58 } 59 } 60 if( result==null ) result=new TagHandlerPool(); 61 result.init(config); 62 63 return result; 64 } 65 66 protected void init( ServletConfig config ) { 67 int maxSize=-1; 68 String maxSizeS=getOption(config, OPTION_MAXSIZE, null); 69 if( maxSizeS != null ) { 70 try { 71 maxSize=Integer.parseInt(maxSizeS); 72 } catch( Exception ex) { 73 maxSize=-1; 74 } 75 } 76 if( maxSize <0 ) { 77 maxSize=Constants.MAX_POOL_SIZE; 78 } 79 this.handlers = new Tag [maxSize]; 80 this.current = -1; 81 this.annotationProcessor = 82 (AnnotationProcessor) config.getServletContext().getAttribute(AnnotationProcessor.class.getName()); 83 } 84 85 88 public TagHandlerPool() { 89 } 92 93 99 public TagHandlerPool(int capacity) { 100 this.handlers = new Tag [capacity]; 101 this.current = -1; 102 } 103 104 114 public Tag get(Class handlerClass) throws JspException { 115 Tag handler = null; 116 synchronized( this ) { 117 if (current >= 0) { 118 handler = handlers[current--]; 119 return handler; 120 } 121 } 122 123 try { 126 Tag instance = (Tag ) handlerClass.newInstance(); 127 AnnotationHelper.postConstruct(annotationProcessor, instance); 128 return instance; 129 } catch (Exception e) { 130 throw new JspException (e.getMessage(), e); 131 } 132 } 133 134 141 public void reuse(Tag handler) { 142 synchronized( this ) { 143 if (current < (handlers.length - 1)) { 144 handlers[++current] = handler; 145 return; 146 } 147 } 148 handler.release(); 150 if (annotationProcessor != null) { 151 try { 152 AnnotationHelper.preDestroy(annotationProcessor, handler); 153 } catch (Exception e) { 154 log.warn("Error processing preDestroy on tag instance of " 155 + handler.getClass().getName(), e); 156 } 157 } 158 } 159 160 164 public synchronized void release() { 165 for (int i = current; i >= 0; i--) { 166 handlers[i].release(); 167 if (annotationProcessor != null) { 168 try { 169 AnnotationHelper.preDestroy(annotationProcessor, handlers[i]); 170 } catch (Exception e) { 171 log.warn("Error processing preDestroy on tag instance of " 172 + handlers[i].getClass().getName(), e); 173 } 174 } 175 } 176 } 177 178 protected static String getOption( ServletConfig config, String name, String defaultV) { 179 if( config == null ) return defaultV; 180 181 String value=config.getInitParameter(name); 182 if( value != null ) return value; 183 if( config.getServletContext() ==null ) 184 return defaultV; 185 value=config.getServletContext().getInitParameter(name); 186 if( value!=null ) return value; 187 return defaultV; 188 } 189 190 } 191 192 | Popular Tags |