keiron      02/02/22 01:18:47

  Modified:    src/org/apache/fop/fo FOUserAgent.java
               src/org/apache/fop/image ImageFactory.java ImageLoader.java
               src/org/apache/fop/image/analyser SVGReader.java
  Log:
  improved cache
  gets base dir from user agent
  
  Revision  Changes    Path
  1.6       +10 -1     xml-fop/src/org/apache/fop/fo/FOUserAgent.java
  
  Index: FOUserAgent.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/fo/FOUserAgent.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- FOUserAgent.java  21 Feb 2002 09:54:26 -0000      1.5
  +++ FOUserAgent.java  22 Feb 2002 09:18:47 -0000      1.6
  @@ -1,5 +1,5 @@
   /*
  - * $Id: FOUserAgent.java,v 1.5 2002/02/21 09:54:26 keiron Exp $
  + * $Id: FOUserAgent.java,v 1.6 2002/02/22 09:18:47 keiron Exp $
    * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
    * For details on use and redistribution please refer to the
    * LICENSE file included with these sources.
  @@ -34,6 +34,7 @@
       HashMap defaults = new HashMap();
       HashMap handlers = new HashMap();
       Logger log;
  +    String base;
   
       public void setLogger(Logger logger) {
           log = logger;
  @@ -41,6 +42,14 @@
   
       public Logger getLogger() {
           return log;
  +    }
  +
  +    public void setBaseDirectory(String b) {
  +        base = b;
  +    }
  +
  +    public String getBaseDirectory() {
  +        return base;
       }
   
       /**
  
  
  
  1.2       +69 -18    xml-fop/src/org/apache/fop/image/ImageFactory.java
  
  Index: ImageFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/ImageFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ImageFactory.java 21 Feb 2002 09:54:27 -0000      1.1
  +++ ImageFactory.java 22 Feb 2002 09:18:47 -0000      1.2
  @@ -1,5 +1,5 @@
   /*
  - * $Id: ImageFactory.java,v 1.1 2002/02/21 09:54:27 keiron Exp $
  + * $Id: ImageFactory.java,v 1.2 2002/02/22 09:18:47 keiron Exp $
    * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
    * For details on use and redistribution please refer to the
    * LICENSE file included with these sources.
  @@ -20,6 +20,7 @@
   import java.util.HashSet;
   import java.util.Set;
   import java.util.Collections;
  +import java.util.Iterator;
   
   // FOP
   import org.apache.fop.image.analyser.ImageReaderFactory;
  @@ -39,7 +40,7 @@
    */
   public class ImageFactory {
       private static ImageFactory factory = new ImageFactory();
  -    ImageCache cache = new ContextImageCache();
  +    ImageCache cache = new ContextImageCache(true);
   
       private ImageFactory() {}
   
  @@ -277,26 +278,58 @@
    * weak hashmap so they may be garbage collected.
    */
   class ContextImageCache implements ImageCache {
  -    Set invalid = Collections.synchronizedSet(new HashSet());
  +    // if this cache is collective then images can be shared
  +    // among contexts, this implies that the base directory
  +    // is either the same or does not effect the images being
  +    // loaded
  +    boolean collective;
       Map contextStore = Collections.synchronizedMap(new HashMap());
  -    Map weakStore = Collections.synchronizedMap(new WeakHashMap());
  +    Set invalid = null;
  +    Map weakStore = null;
  +
  +    public ContextImageCache(boolean col) {
  +        collective = col;
  +        if(collective) {
  +            weakStore = Collections.synchronizedMap(new WeakHashMap());
  +            invalid = Collections.synchronizedSet(new HashSet());
  +        }
  +    }
   
       // sync around lookups and puts
       // another sync around load for a particular image
       public FopImage getImage(String url, FOUserAgent context) {
  -        ImageLoader im;
  +        ImageLoader im = null;
           // this protects the finding or creating of a new
           // ImageLoader for multi threads
           synchronized (this) {
  -            if (invalid.contains(url)) {
  +            if (collective && invalid.contains(url)) {
                   return null;
               }
               Context con = (Context) contextStore.get(context);
               if (con == null) {
  -                con = new Context(context);
  +                con = new Context(context, collective);
                   contextStore.put(context, con);
  +            } else {
  +                if(con.invalid(url)) {
  +                    return null;
  +                }
  +                im = con.getImage(url);
               }
  -            im = (ImageLoader) weakStore.get(url);
  +            if(im == null && collective) {
  +                for(Iterator iter = contextStore.values().iterator(); 
iter.hasNext(); ) {
  +                    Context c = (Context)iter.next();
  +                    if(c != con) {
  +                        im = c.getImage(url);
  +                        if(im != null) {
  +                            break;
  +                        }
  +                    }
  +                }
  +                if(im == null) {
  +                    im = (ImageLoader) weakStore.get(url);
  +                }
  +            }
  +
               if (im != null) {
                   con.putImage(url, im);
               } else {
  @@ -315,39 +348,49 @@
       public void releaseImage(String url, FOUserAgent context) {
           Context con = (Context) contextStore.get(context);
           if (con != null) {
  -            ImageLoader im = con.getImage(url);
  -            weakStore.put(url, im);
  +            if(collective) {
  +                ImageLoader im = con.getImage(url);
  +                weakStore.put(url, im);
  +            }
               con.releaseImage(url);
           }
       }
   
       public void invalidateImage(String url, FOUserAgent context) {
  -        // cap size of invalid list
  -        if (invalid.size() > 100) {
  -            invalid.clear();
  +        if(collective) {
  +            // cap size of invalid list
  +            if (invalid.size() > 100) {
  +                invalid.clear();
  +            }
  +            invalid.add(url);
           }
  -        invalid.add(url);
           Context con = (Context) contextStore.get(context);
           if (con != null) {
  -            con.releaseImage(url);
  +            con.invalidateImage(url);
           }
       }
   
       public void removeContext(FOUserAgent context) {
           Context con = (Context) contextStore.get(context);
           if (con != null) {
  -            Map images = con.getImages();
  -            weakStore.putAll(images);
  +            if(collective) {
  +                Map images = con.getImages();
  +                weakStore.putAll(images);
  +            }
               contextStore.remove(context);
           }
       }
   
       class Context {
           Map images = Collections.synchronizedMap(new HashMap());
  +        Set invalid = null;
           FOUserAgent userAgent;
   
  -        public Context(FOUserAgent ua) {
  +        public Context(FOUserAgent ua, boolean inv) {
               userAgent = ua;
  +            if(inv) {
  +                invalid = Collections.synchronizedSet(new HashSet());
  +            }
           }
   
           public ImageLoader getImage(String url, ImageCache c) {
  @@ -373,6 +416,14 @@
   
           public Map getImages() {
               return images;
  +        }
  +
  +        public void invalidateImage(String url) {
  +            invalid.add(url);
  +        }
  +
  +        public boolean invalid(String url) {
  +            return invalid.contains(url);
           }
   
       }
  
  
  
  1.2       +2 -2      xml-fop/src/org/apache/fop/image/ImageLoader.java
  
  Index: ImageLoader.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/ImageLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ImageLoader.java  21 Feb 2002 09:54:27 -0000      1.1
  +++ ImageLoader.java  22 Feb 2002 09:18:47 -0000      1.2
  @@ -1,5 +1,5 @@
   /*
  - * $Id: ImageLoader.java,v 1.1 2002/02/21 09:54:27 keiron Exp $
  + * $Id: ImageLoader.java,v 1.2 2002/02/22 09:18:47 keiron Exp $
    * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
    * For details on use and redistribution please refer to the
    * LICENSE file included with these sources.
  @@ -30,7 +30,7 @@
           if (!valid || image != null) {
               return image;
           }
  -        String base = Configuration.getStringValue("baseDir");
  +        String base = userAgent.getBaseDirectory();
           image = ImageFactory.loadImage(url, base, userAgent);
           if (image == null) {
               cache.invalidateImage(url, userAgent);
  
  
  
  1.19      +13 -7     xml-fop/src/org/apache/fop/image/analyser/SVGReader.java
  
  Index: SVGReader.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/image/analyser/SVGReader.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- SVGReader.java    22 Feb 2002 09:09:35 -0000      1.18
  +++ SVGReader.java    22 Feb 2002 09:18:47 -0000      1.19
  @@ -1,5 +1,5 @@
   /*
  - * $Id: SVGReader.java,v 1.18 2002/02/22 09:09:35 keiron Exp $
  + * $Id: SVGReader.java,v 1.19 2002/02/22 09:18:47 keiron Exp $
    * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
    * For details on use and redistribution please refer to the
    * LICENSE file included with these sources.
  @@ -56,6 +56,7 @@
       public static final String SVG_MIME_TYPE = "image/svg+xml";
       FOUserAgent userAgent;
       Document doc;
  +    boolean batik = true;
   
       public SVGReader() {
       }
  @@ -80,13 +81,17 @@
        * Possibly need a slightly different design for the image stuff.
        */
       protected boolean loadImage(String uri) {
  -        try {
  -            Loader loader = new Loader();
  -            return loader.getImage(uri);
  -        } catch (NoClassDefFoundError e) {
  -            //userAgent.getLogger().error("Batik not in class path", e);
  -            return false;
  +        if(batik) {
  +            try {
  +                Loader loader = new Loader();
  +                return loader.getImage(uri);
  +            } catch (NoClassDefFoundError e) {
  +                batik = false;
  +                //userAgent.getLogger().error("Batik not in class path", e);
  +                return false;
  +            }
           }
  +        return false;
       }
   
       /**
  @@ -134,6 +139,7 @@
   
                   return true;
               } catch (NoClassDefFoundError ncdfe) {
  +                batik = false;
                   //userAgent.getLogger().error("Batik not in class path", ncdfe);
                   return false;
               }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to