From: <aa...@sv...> - 2007-10-29 10:32:35
|
Author: aaime Date: 2007-10-29 03:32:32 -0700 (Mon, 29 Oct 2007) New Revision: 27653 Modified: geotools/branches/2.3.x/module/render/src/org/geotools/renderer/style/SLDStyleFactory.java Log: GEOT-1551 SLDStyleFactory image and font loading is not thread safe Modified: geotools/branches/2.3.x/module/render/src/org/geotools/renderer/style/SLDStyleFactory.java =================================================================== --- geotools/branches/2.3.x/module/render/src/org/geotools/renderer/style/SLDStyleFactory.java 2007-10-29 10:18:28 UTC (rev 27652) +++ geotools/branches/2.3.x/module/render/src/org/geotools/renderer/style/SLDStyleFactory.java 2007-10-29 10:32:32 UTC (rev 27653) @@ -731,162 +731,166 @@ * @return The first of the specified fonts found on this machine or null if none found */ private java.awt.Font getFont(Feature feature, Font[] fonts) { - if (fontFamilies == null) { - GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); - fontFamilies = new HashSet(); + // we need to synchronize font loading or multithreaded access may + // result in us returning the wrong font + synchronized (loadedFonts) { + if (fontFamilies == null) { + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + fontFamilies = new HashSet(); - List f = Arrays.asList(ge.getAvailableFontFamilyNames()); - fontFamilies.addAll(f); + List f = Arrays.asList(ge.getAvailableFontFamilyNames()); + fontFamilies.addAll(f); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("there are " + fontFamilies.size() + " fonts available"); - } - } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("there are " + fontFamilies.size() + " fonts available"); + } + } - java.awt.Font javaFont = null; + java.awt.Font javaFont = null; - int styleCode = 0; - int size = 6; - String requestedFont = ""; + int styleCode = 0; + int size = 6; + String requestedFont = ""; - for (int k = 0; k < fonts.length; k++) { - requestedFont = fonts[k].getFontFamily().getValue(feature).toString(); + for (int k = 0; k < fonts.length; k++) { + requestedFont = fonts[k].getFontFamily().getValue(feature).toString(); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("trying to load " + requestedFont); - } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("trying to load " + requestedFont); + } - if (loadedFonts.containsKey(requestedFont)) { - javaFont = (java.awt.Font) loadedFonts.get(requestedFont); + if (loadedFonts.containsKey(requestedFont)) { + javaFont = (java.awt.Font) loadedFonts.get(requestedFont); - String reqStyle = (String) fonts[k].getFontStyle().getValue(feature); + String reqStyle = (String) fonts[k].getFontStyle().getValue(feature); - if (fontStyleLookup.containsKey(reqStyle)) { - styleCode = ((Integer) fontStyleLookup.get(reqStyle)).intValue(); - } else { - styleCode = java.awt.Font.PLAIN; - } + if (fontStyleLookup.containsKey(reqStyle)) { + styleCode = ((Integer) fontStyleLookup.get(reqStyle)).intValue(); + } else { + styleCode = java.awt.Font.PLAIN; + } - String reqWeight = (String) fonts[k].getFontWeight().getValue(feature); + String reqWeight = (String) fonts[k].getFontWeight().getValue(feature); - if (reqWeight.equalsIgnoreCase("Bold")) { - styleCode = styleCode | java.awt.Font.BOLD; - } + if (reqWeight.equalsIgnoreCase("Bold")) { + styleCode = styleCode | java.awt.Font.BOLD; + } - size = ((Number) fonts[k].getFontSize().getValue(feature)).intValue(); + size = ((Number) fonts[k].getFontSize().getValue(feature)).intValue(); - return javaFont.deriveFont(styleCode, size); - } + return javaFont.deriveFont(styleCode, size); + } - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("not already loaded"); - } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("not already loaded"); + } - if (fontFamilies.contains(requestedFont)) { - String reqStyle = (String) fonts[k].getFontStyle().getValue(feature); + if (fontFamilies.contains(requestedFont)) { + String reqStyle = (String) fonts[k].getFontStyle().getValue(feature); - if (fontStyleLookup.containsKey(reqStyle)) { - styleCode = ((Integer) fontStyleLookup.get(reqStyle)).intValue(); - } else { - styleCode = java.awt.Font.PLAIN; - } + if (fontStyleLookup.containsKey(reqStyle)) { + styleCode = ((Integer) fontStyleLookup.get(reqStyle)).intValue(); + } else { + styleCode = java.awt.Font.PLAIN; + } - String reqWeight = (String) fonts[k].getFontWeight().getValue(feature); + String reqWeight = (String) fonts[k].getFontWeight().getValue(feature); - if (reqWeight.equalsIgnoreCase("Bold")) { - styleCode = styleCode | java.awt.Font.BOLD; - } + if (reqWeight.equalsIgnoreCase("Bold")) { + styleCode = styleCode | java.awt.Font.BOLD; + } - size = ((Number) fonts[k].getFontSize().getValue(feature)).intValue(); + size = ((Number) fonts[k].getFontSize().getValue(feature)).intValue(); - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("requesting " + requestedFont + " " + styleCode + " " + size); - } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("requesting " + requestedFont + " " + styleCode + " " + size); + } - javaFont = new java.awt.Font(requestedFont, styleCode, size); - loadedFonts.put(requestedFont, javaFont); + javaFont = new java.awt.Font(requestedFont, styleCode, size); + loadedFonts.put(requestedFont, javaFont); - return javaFont; - } + return javaFont; + } - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("not a system font"); - } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("not a system font"); + } - // may be its a file or url - InputStream is = null; + // may be its a file or url + InputStream is = null; - if (requestedFont.startsWith("http") || requestedFont.startsWith("file:")) { - try { - URL url = new URL(requestedFont); - is = url.openStream(); - } catch (MalformedURLException mue) { - // this may be ok - but we should mention it - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("Bad url in SLDStyleFactory " + requestedFont + "\n" + mue); - } - } catch (IOException ioe) { - // we'll ignore this for the moment - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("IO error in SLDStyleFactory " + requestedFont + "\n" + ioe); - } - } - } else { - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("not a URL"); - } + if (requestedFont.startsWith("http") || requestedFont.startsWith("file:")) { + try { + URL url = new URL(requestedFont); + is = url.openStream(); + } catch (MalformedURLException mue) { + // this may be ok - but we should mention it + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("Bad url in SLDStyleFactory " + requestedFont + "\n" + mue); + } + } catch (IOException ioe) { + // we'll ignore this for the moment + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("IO error in SLDStyleFactory " + requestedFont + "\n" + ioe); + } + } + } else { + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("not a URL"); + } - File file = new File(requestedFont); + File file = new File(requestedFont); - //if(file.canRead()){ - try { - is = new FileInputStream(file); - } catch (FileNotFoundException fne) { - // this may be ok - but we should mention it - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("Bad file name in SLDStyleFactory" + requestedFont + "\n" + fne); - } - } - } + //if(file.canRead()){ + try { + is = new FileInputStream(file); + } catch (FileNotFoundException fne) { + // this may be ok - but we should mention it + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("Bad file name in SLDStyleFactory" + requestedFont + "\n" + fne); + } + } + } - if (LOGGER.isLoggable(Level.FINEST)) { - LOGGER.finest("about to load"); - } + if (LOGGER.isLoggable(Level.FINEST)) { + LOGGER.finest("about to load"); + } - if (is == null) { - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("null input stream"); - } + if (is == null) { + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("null input stream"); + } - continue; - } + continue; + } - try { - javaFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is); - } catch (FontFormatException ffe) { - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("Font format error in SLDStyleFactory " + requestedFont + "\n" - + ffe); - } + try { + javaFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is); + } catch (FontFormatException ffe) { + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("Font format error in SLDStyleFactory " + requestedFont + "\n" + + ffe); + } - continue; - } catch (IOException ioe) { - // we'll ignore this for the moment - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info("IO error in SLDStyleFactory " + requestedFont + "\n" + ioe); - } + continue; + } catch (IOException ioe) { + // we'll ignore this for the moment + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("IO error in SLDStyleFactory " + requestedFont + "\n" + ioe); + } - continue; - } + continue; + } - loadedFonts.put(requestedFont, javaFont); + loadedFonts.put(requestedFont, javaFont); - return javaFont; + return javaFont; + } + + // if everything else fails fall back on a default font distributed + // along with the jdk + return new java.awt.Font("Serif",java.awt.Font.PLAIN,12); } - - // if everything else fails fall back on a default font distributed - // along with the jdk - return new java.awt.Font("Serif",java.awt.Font.PLAIN,12); } void setScaleRange(Style style, Range scaleRange) { @@ -1224,7 +1228,10 @@ } try { - img = imageLoader.get(eg.getLocation(), false); + // imageLoader is not thread safe + synchronized (imageLoader) { + img = imageLoader.get(eg.getLocation(), false); + } if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("Image return = " + img); |