lenya-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Hartmann <andr...@apache.org>
Subject Extracting Avalon service handling
Date Fri, 04 Apr 2008 11:03:28 GMT
Hi Lenya users and devs,

I'm sure I'm not the first one who found this little trick, but maybe it 
will be of help for some of you.

With Avalon services, one the most annoying issues is the typical 
lookup/release block which has to be repeated for each usage of the 
service. This is especially verbose if the service is fetched from a 
selector or needs additional initialization. Here's a simple example:


   public Document getDocument(InputStream stream) {
       Parser parser = null;
       try {
           parser = (Parser) this.manager.lookup(Parser.ROLE);
           parser.setValidating(true);
           parser.set(..., ...)
           return parser.parse(stream);
       }
       catch (ParseException e) {
           throw new SAXException(e);
       }
       finally {
           if (parser != null) {
                this.manager.release(parser);
           }
       }
   }


Now imagine you also want to parse String objects. The trivial approach 
would be to duplicate the method and replace parser.parse(stream) with 
parser.parse(string). But this leads to a lot of code duplication. 
Instead, you can declare a little interface that you can use to 
"infiltrate" the code, allowing you to access the parser:


    protected interface ParserAccessor() {
        Document access(Parser parser);
    }


Instead of duplicating the code, you provide a method which uses the 
ParserAccessor to invoke the parser:


   public Document getDocument(ParserAccessor accessor) {
       Parser parser = null;
       try {
           parser = (Parser) this.manager.lookup(Parser.ROLE);
           parser.setValidating(true);
           parser.set(..., ...)
           return accessor.access(parser);
       }
       catch (ParseException e) {
           throw new SAXException(e);
       }
       finally {
           if (parser != null) {
                this.manager.release(parser);
           }
       }
   }


Now you can use different ParserAccessor implementations, for each kind 
of parsing you want to provide:


   public Document getDocument(final InputStream stream) {
       return getDocument(new ParserAccessor() {
           public Document access(Parser parser) {
               return parser.parse(stream);
           }
       });
   }

   public Document getDocument(final String string) {
       return getDocument(new ParserAccessor() {
           public Document access(Parser parser) {
               return parser.parse(string);
           }
       });
   }

   ...


I hope this will make your lives a bit easier.

-- Andreas


-- 
Andreas Hartmann, CTO
BeCompany GmbH
http://www.becompany.ch
Tel.: +41 (0) 43 818 57 01


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@lenya.apache.org
For additional commands, e-mail: user-help@lenya.apache.org


Mime
View raw message