diff options
-rw-r--r-- | src/org/apache/fop/fo/FOUserAgent.java | 9 | ||||
-rw-r--r-- | src/org/apache/fop/image/ImageFactory.java | 85 | ||||
-rw-r--r-- | src/org/apache/fop/image/ImageLoader.java | 2 | ||||
-rw-r--r-- | src/org/apache/fop/image/analyser/SVGReader.java | 18 |
4 files changed, 90 insertions, 24 deletions
diff --git a/src/org/apache/fop/fo/FOUserAgent.java b/src/org/apache/fop/fo/FOUserAgent.java index c47bd3d2a..2dbf98c1a 100644 --- a/src/org/apache/fop/fo/FOUserAgent.java +++ b/src/org/apache/fop/fo/FOUserAgent.java @@ -34,6 +34,7 @@ public class FOUserAgent { HashMap defaults = new HashMap(); HashMap handlers = new HashMap(); Logger log; + String base; public void setLogger(Logger logger) { log = logger; @@ -43,6 +44,14 @@ public class FOUserAgent { return log; } + public void setBaseDirectory(String b) { + base = b; + } + + public String getBaseDirectory() { + return base; + } + /** * Set the default xml handler for the given mime type. */ diff --git a/src/org/apache/fop/image/ImageFactory.java b/src/org/apache/fop/image/ImageFactory.java index 6b5fb3738..837a84f7c 100644 --- a/src/org/apache/fop/image/ImageFactory.java +++ b/src/org/apache/fop/image/ImageFactory.java @@ -20,6 +20,7 @@ import java.util.Map; 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 @@ handle context: base dir, logger, caching */ public class ImageFactory { private static ImageFactory factory = new ImageFactory(); - ImageCache cache = new ContextImageCache(); + ImageCache cache = new ContextImageCache(true); private ImageFactory() {} @@ -277,26 +278,58 @@ class BasicImageCache implements ImageCache { * 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); + } + 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); + } } - im = (ImageLoader) weakStore.get(url); + if (im != null) { con.putImage(url, im); } else { @@ -315,39 +348,49 @@ class ContextImageCache implements ImageCache { 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) { @@ -375,6 +418,14 @@ class ContextImageCache implements ImageCache { return images; } + public void invalidateImage(String url) { + invalid.add(url); + } + + public boolean invalid(String url) { + return invalid.contains(url); + } + } } diff --git a/src/org/apache/fop/image/ImageLoader.java b/src/org/apache/fop/image/ImageLoader.java index 6c95f1da7..679cf2325 100644 --- a/src/org/apache/fop/image/ImageLoader.java +++ b/src/org/apache/fop/image/ImageLoader.java @@ -30,7 +30,7 @@ class ImageLoader { 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); diff --git a/src/org/apache/fop/image/analyser/SVGReader.java b/src/org/apache/fop/image/analyser/SVGReader.java index ae84ac584..34e7406a6 100644 --- a/src/org/apache/fop/image/analyser/SVGReader.java +++ b/src/org/apache/fop/image/analyser/SVGReader.java @@ -56,6 +56,7 @@ public class SVGReader extends AbstractImageReader { public static final String SVG_MIME_TYPE = "image/svg+xml"; FOUserAgent userAgent; Document doc; + boolean batik = true; public SVGReader() { } @@ -80,13 +81,17 @@ public class SVGReader extends AbstractImageReader { * 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 @@ public class SVGReader extends AbstractImageReader { return true; } catch (NoClassDefFoundError ncdfe) { + batik = false; //userAgent.getLogger().error("Batik not in class path", ncdfe); return false; } |