cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vadim Gritsenko <va...@reverycodes.com>
Subject ThreadSafe Transformers
Date Wed, 22 Sep 2004 14:31:01 GMT
Y'all,

Some time ago I remember discussions about making transformers 
threadsafe, major changes, about Cocoon 3, etc. Yesterday it occured to 
me that Transformers can easily be made ThreadSafe with only additions 
to current Cocoon architecture without any major rewrites. It can be 
done in this way:

Add new interface marking threadsafe transformer:
     public interface TransformerFactory {
         Transformer getTransformer();
     }

Override select method in ComponentSelector:
     public Component select(Object hint) throws ComponentException {
         Component component = super.select(hint);
         if (this.roleId == TRANSFORMER) {
             if (component instanceof TransformerFactory) {
                 return ((TransformerFactory)component).getTransformer();
             }
         }

         return component;
     }
(it is in org.apache.cocoon.components.treeprocessor.sitemap package)

Modify existing transformer so that transformer class implements 
TransformerFactory, ThreadSafe, and nested class TransformerImpl 
implements Transformer, CacheableProcessingComponent (and it can extend 
AbstractXMLPipe). On the example of EncodeURLTransformer:
     public class EncodeURLTransformer
         implements TransformerFactory, Configurable, ThreadSafe {

         public void configure(Configuration config) {...}

         public Transformer getTransformer() {
             return new TransformerImpl();
         }

         private class TransformerImpl
             extends AbstractTransformer
             implements CacheableProcessingComponent {

             public void setup(SourceResolver resolver,
                               Map objectModel,
                               String source,
                               Parameters parameters) {...}

             public Serializable getKey() {...}

             public SourceValidity getValidity() {...}

             public void startElement(String uri,
                                      String name,
                                      String raw,
                                      Attributes attributes) {...}
             ...
         }
     }


Thus ThreadSafe transformer is a factory creating instances of SAX pipes 
performing actual transformation. Pluses of this approach, as I see it, are:
  * Transformer is <init>ed / contextualize()d / service()ed /
    configure()d / etc just once. Saves CPU cycles.
  * Transformer global configuration / context / components
    stored in single instance of ThreadSafe transformer instead
    of beeing copied in each instance of usual pooled
    transformer. Saves memory.
  * Users of ComponentSelector still receive instance of
    Transformer as they used to expect. Backward compatibility.


Now question; is it a good idea to check in this code into 2.2? Any 
drawbacks of this approach?

Thanks,
Vadim


Mime
View raw message