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/src/java/org/apache/commons/jelly JellyContext.java
Date Tue, 22 Oct 2002 13:44:04 GMT
jstrachan    2002/10/22 06:44:04

  Modified:    jelly/src/java/org/apache/commons/jelly/parser
                        XMLParser.java
               jelly/src/java/org/apache/commons/jelly JellyContext.java
  Log:
  Optimization patch to reuse XMLParser instances per thread as a cheap object pool to void
unnecessary startup costs of XML parsers and to avoid unnecessary Properties file loading
in the XMLParser
  
  Revision  Changes    Path
  1.36      +50 -32    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java
  
  Index: XMLParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/parser/XMLParser.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- XMLParser.java	17 Oct 2002 18:23:48 -0000	1.35
  +++ XMLParser.java	22 Oct 2002 13:44:03 -0000	1.36
  @@ -125,6 +125,12 @@
    */
   public class XMLParser extends DefaultHandler {
   
  +    /**
  +     * Share the Jelly properties across parsers
  +     */
  +    private static Properties jellyProperties;
  +    
  +    
       /** JellyContext which is used to locate tag libraries*/
       private JellyContext context = new JellyContext();
   
  @@ -972,39 +978,51 @@
        */
       protected void configure() {
           // load the properties file of libraries available
  -        InputStream in = null;
  -        URL url =
  -            getClassLoader().getResource("org/apache/commons/jelly/jelly.properties");
  -        if (url != null) {
  -            log.debug("Loading Jelly default tag libraries from: " + url);
  -            Properties properties = new Properties();
  -            try {
  -                in = url.openStream();
  -                properties.load(in);
  -                for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();)
{
  -                    Map.Entry entry = (Map.Entry) iter.next();
  -                    String uri = (String) entry.getKey();
  -                    String className = (String) entry.getValue();                    
  -                    String libraryURI = "jelly:" + uri;
  -                    
  -                    // don't overload any Mock Tags already
  -                    if ( ! context.isTagLibraryRegistered(libraryURI) ) {
  -                        context.registerTagLibrary(libraryURI, className);
  -                    }
  -                }
  -            }
  -            catch (IOException e) {
  -                log.error("Could not load jelly properties from: " + url + ". Reason: "
+ e, e);
  +        Properties properties = getJellyProperties();
  +        for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
  +            Map.Entry entry = (Map.Entry) iter.next();
  +            String uri = (String) entry.getKey();
  +            String className = (String) entry.getValue();                    
  +            String libraryURI = "jelly:" + uri;
  +            
  +            // don't overload any Mock Tags already
  +            if ( ! context.isTagLibraryRegistered(libraryURI) ) {
  +                context.registerTagLibrary(libraryURI, className);
               }
  -            finally {
  +        }
  +    }
  +
  +
  +    /**
  +     * A helper method which loads the static Jelly properties once on startup
  +     */
  +    protected synchronized Properties getJellyProperties() {
  +        if (jellyProperties == null) {
  +            jellyProperties = new Properties();
  +            
  +            InputStream in = null;
  +            URL url =
  +                getClassLoader().getResource("org/apache/commons/jelly/jelly.properties");
  +            if (url != null) {
  +                log.debug("Loading Jelly default tag libraries from: " + url);
                   try {
  -                    in.close();
  +                    in = url.openStream();
  +                    jellyProperties .load(in);
  +                }
  +                catch (IOException e) {
  +                    log.error("Could not load jelly properties from: " + url + ". Reason:
" + e, e);
                   }
  -                catch (Exception e) {
  -                    // ignore
  +                finally {
  +                    try {
  +                        in.close();
  +                    }
  +                    catch (Exception e) {
  +                        // ignore
  +                    }
                   }
               }
           }
  +        return jellyProperties;
       }
   
       /**
  
  
  
  1.31      +17 -2     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java
  
  Index: JellyContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- JellyContext.java	10 Oct 2002 14:50:21 -0000	1.30
  +++ JellyContext.java	22 Oct 2002 13:44:04 -0000	1.31
  @@ -109,7 +109,9 @@
           
       /** Should we cache Tag instances, per thread, to reduce object contruction overhead?
*/
       private boolean cacheTags = false;
  -    
  +
  +    /** a Thread local cache of XMLParsers to avoid startup overhead of an XMLParser */
  +    private ThreadLocal parserPool = new ThreadLocal();
       
       /**
        * The class loader to use for instantiating application objects.
  @@ -455,10 +457,23 @@
        * {@link #getResource} method then returns the compiled script.
        */
       public Script compileScript(URL url) throws Exception {
  -        XMLParser parser = new XMLParser();
  +        XMLParser parser = getXMLParser();
           parser.setContext(this);
           Script script = parser.parse(url.toString());
           return script.compile();
  +    }
  +
  +    /**
  +     * @return a thread pooled XMLParser to avoid the startup overhead
  +     * of the XMLParser
  +     */
  +    protected XMLParser getXMLParser() {
  +        XMLParser parser = (XMLParser) parserPool.get();
  +        if (parser == null) {
  +            parser = new XMLParser();
  +            parserPool.set(parser);
  +        }
  +        return parser;
       }
   
       /** 
  
  
  

--
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