From e0b37784e5db7eff07679a7c699b234591df7161 Mon Sep 17 00:00:00 2001 From: Niels Charlier Date: Wed, 7 Oct 2015 14:35:18 +0200 Subject: [PATCH] Resources: port wms --- .../geoserver/wms/WMSLifecycleHandler.java | 27 +++++----- .../wms/capabilities/LegendSampleImpl.java | 46 ++++++++--------- .../wms/decoration/MapDecorationLayout.java | 6 +-- .../org/geoserver/wms/kvp/PALFileLoader.java | 49 ++++++++----------- .../org/geoserver/wms/kvp/PaletteManager.java | 24 ++++----- .../RasterLayerLegendHelper.java | 2 - .../wms/map/RenderedImageMapOutputFormat.java | 5 +- 7 files changed, 68 insertions(+), 91 deletions(-) diff --git a/src/wms/src/main/java/org/geoserver/wms/WMSLifecycleHandler.java b/src/wms/src/main/java/org/geoserver/wms/WMSLifecycleHandler.java index 314716d4c61..b240146e53a 100644 --- a/src/wms/src/main/java/org/geoserver/wms/WMSLifecycleHandler.java +++ b/src/wms/src/main/java/org/geoserver/wms/WMSLifecycleHandler.java @@ -21,6 +21,8 @@ import org.apache.commons.io.FileUtils; import org.geoserver.config.GeoServerDataDirectory; import org.geoserver.config.impl.GeoServerLifecycleHandler; +import org.geoserver.platform.resource.Resource; +import org.geoserver.platform.resource.Resources; import org.geotools.renderer.style.FontCache; import org.geotools.renderer.style.ImageGraphicFactory; import org.geotools.renderer.style.SVGGraphicFactory; @@ -109,22 +111,17 @@ void reloadFontCache() { List loadFontsFromDataDirectory() { List result = new ArrayList(); - try { - Collection files = FileUtils.listFiles(data.findStyleDir(), new String[] { "ttf", - "TTF" }, true); - for (File file : files) { - try { - final Font font = Font.createFont(Font.TRUETYPE_FONT, file); - result.add(font); - LOGGER.log(Level.INFO, - "Loaded font file " + file + ", loaded font '" + font.getName() - + "' in family '" + font.getFamily() + "'"); - } catch (Exception e) { - LOGGER.log(Level.WARNING, "Failed to load font file " + file, e); - } + for (Resource file : Resources.list(data.getStyles(), new Resources.ExtensionFilter("TTF"), + true)) { + try { + final Font font = Font.createFont(Font.TRUETYPE_FONT, file.file()); + result.add(font); + LOGGER.log(Level.INFO, + "Loaded font file " + file + ", loaded font '" + font.getName() + + "' in family '" + font.getFamily() + "'"); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Failed to load font file " + file, e); } - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Failed to scan style directory for fonts", e); } return result; diff --git a/src/wms/src/main/java/org/geoserver/wms/capabilities/LegendSampleImpl.java b/src/wms/src/main/java/org/geoserver/wms/capabilities/LegendSampleImpl.java index d1ee556e485..03d748a7eec 100644 --- a/src/wms/src/main/java/org/geoserver/wms/capabilities/LegendSampleImpl.java +++ b/src/wms/src/main/java/org/geoserver/wms/capabilities/LegendSampleImpl.java @@ -9,9 +9,8 @@ import java.awt.Dimension; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -31,6 +30,7 @@ import org.geoserver.platform.GeoServerResourceLoader; import org.geoserver.platform.resource.Paths; import org.geoserver.platform.resource.Resource; +import org.geoserver.platform.resource.Resource.Type; import org.geoserver.wms.GetLegendGraphicOutputFormat; import org.geoserver.wms.GetLegendGraphicRequest; import org.geoserver.wms.legendgraphic.BufferedImageLegendGraphic; @@ -65,13 +65,13 @@ public class LegendSampleImpl implements CatalogListener, LegendSample, private Set invalidated = new HashSet(); - File baseDir; + Resource baseDir; public LegendSampleImpl(Catalog catalog, GeoServerResourceLoader loader) { super(); this.catalog = catalog; this.loader = loader; - this.baseDir = loader.getBaseDirectory(); + this.baseDir = loader.get(Paths.BASE); this.clean(); this.catalog.addListener(this); } @@ -83,7 +83,7 @@ private void clean() { for (StyleInfo style : catalog.getStyles()) { synchronized (style) { Resource styleResource = getStyleResource(style); - File sampleFile; + Resource sampleFile; try { // remove old samples sampleFile = getSampleFile(style); @@ -106,10 +106,10 @@ private void clean() { * @return */ private boolean isStyleNewerThanSample(Resource styleResource, - File sampleFile) { + Resource sampleFile) { return isSampleExisting(sampleFile) && styleResource.getType() == Resource.Type.RESOURCE - && styleResource.lastmodified() > sampleFile.lastModified(); + && styleResource.lastmodified() > sampleFile.lastmodified(); } /** @@ -120,7 +120,7 @@ private boolean isStyleNewerThanSample(Resource styleResource, * @return * @throws IOException */ - private File getSampleFile(StyleInfo style) throws IOException { + private Resource getSampleFile(StyleInfo style) throws IOException { String fileName = getSampleFileName(style); return getSampleFile(fileName); } @@ -131,8 +131,8 @@ private File getSampleFile(StyleInfo style) throws IOException { * @param fileName * @return */ - private File getSampleFile(String fileName) { - return new File(getSamplesFolder(), fileName); + private Resource getSampleFile(String fileName) { + return getSamplesFolder().get(fileName); } /** @@ -179,7 +179,7 @@ public Dimension getLegendURLSize(StyleInfo style) throws Exception { synchronized (style) { GetLegendGraphicOutputFormat pngOutputFormat = new PNGLegendOutputFormat(); - File sampleLegend = getSampleFile(style); + Resource sampleLegend = getSampleFile(style); if (isSampleExisting(sampleLegend) && !isStyleSampleInvalid(style)) { // using existing sample if sld has not been updated from @@ -193,8 +193,8 @@ public Dimension getLegendURLSize(StyleInfo style) throws Exception { } } - private boolean isSampleExisting(File sampleLegend) { - return sampleLegend != null && sampleLegend.exists(); + private boolean isSampleExisting(Resource sampleLegend) { + return sampleLegend != null && sampleLegend.getType() == Type.RESOURCE; } /** @@ -210,7 +210,7 @@ private boolean isSampleExisting(File sampleLegend) { private Dimension createNewSample(StyleInfo style, GetLegendGraphicOutputFormat pngOutputFormat) throws Exception { GetLegendGraphicRequest legendGraphicRequest = new GetLegendGraphicRequest(); - File sampleLegendFolder = getSamplesFolder(); + Resource sampleLegendFolder = getSamplesFolder(); legendGraphicRequest.setStrict(false); legendGraphicRequest.setLayer((FeatureType) null); @@ -223,14 +223,10 @@ private Dimension createNewSample(StyleInfo style, .getLegend(); PNGWriter writer = new PNGWriter(); - FileOutputStream outStream = null; + OutputStream outStream = null; try { - File sampleFile = new File(sampleLegendFolder.getAbsolutePath() + File.separator + - getSampleFileName(style)); - if(!sampleFile.getParentFile().exists()) { - sampleFile.getParentFile().mkdirs(); - } - outStream = new FileOutputStream(sampleFile); + Resource sampleFile = sampleLegendFolder.get(getSampleFileName(style)); + outStream = sampleFile.out(); writer.writePNG(image, outStream, 0.0f, FilterType.FILTER_NONE); removeStyleSampleInvalidation(style); return new Dimension(image.getWidth(), image.getHeight()); @@ -245,8 +241,8 @@ private Dimension createNewSample(StyleInfo style, return null; } - private File getSamplesFolder() { - return new File(baseDir + File.separator + LEGEND_SAMPLES_FOLDER); + private Resource getSamplesFolder() { + return baseDir.get(LEGEND_SAMPLES_FOLDER); } /** @@ -254,12 +250,12 @@ private File getSamplesFolder() { * @param sampleLegendFile * @return */ - private Dimension getSizeFromSample(File sampleLegendFile) { + private Dimension getSizeFromSample(Resource sampleLegendFile) { PngReader pngReader = null; try { // reads size using PNGJ reader, that can read metadata without reading // the full image - pngReader = new PngReader(sampleLegendFile); + pngReader = new PngReader(sampleLegendFile.file()); return new Dimension(pngReader.imgInfo.cols, pngReader.imgInfo.rows); } finally { if (pngReader != null) { diff --git a/src/wms/src/main/java/org/geoserver/wms/decoration/MapDecorationLayout.java b/src/wms/src/main/java/org/geoserver/wms/decoration/MapDecorationLayout.java index e69e1714205..1586daa4ca5 100644 --- a/src/wms/src/main/java/org/geoserver/wms/decoration/MapDecorationLayout.java +++ b/src/wms/src/main/java/org/geoserver/wms/decoration/MapDecorationLayout.java @@ -13,7 +13,6 @@ import java.awt.Point; import java.awt.Rectangle; import java.awt.Shape; -import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -23,6 +22,7 @@ import org.geoserver.platform.GeoServerExtensions; import org.geoserver.platform.ServiceException; +import org.geoserver.platform.resource.Resource; import org.geoserver.wms.WMSMapContent; import org.jdom.Document; import org.jdom.Element; @@ -245,12 +245,12 @@ public MapDecorationLayout() { * @return a new MapDecorationLayout containing the MapDecorations specified * @throws Exception if the configuration is invalid or other errors occur while parsing */ - public static MapDecorationLayout fromFile(File f, boolean tiled) throws Exception { + public static MapDecorationLayout fromFile(Resource f, boolean tiled) throws Exception { MapDecorationLayout dl = tiled ? new MetatiledMapDecorationLayout() : new MapDecorationLayout(); - Document confFile = new SAXBuilder().build(f); + Document confFile = new SAXBuilder().build(f.file()); for (Element e : (List)confFile.getRootElement().getChildren("decoration")){ Map m = new HashMap(); diff --git a/src/wms/src/main/java/org/geoserver/wms/kvp/PALFileLoader.java b/src/wms/src/main/java/org/geoserver/wms/kvp/PALFileLoader.java index e7f257343b1..4ade803934d 100644 --- a/src/wms/src/main/java/org/geoserver/wms/kvp/PALFileLoader.java +++ b/src/wms/src/main/java/org/geoserver/wms/kvp/PALFileLoader.java @@ -8,14 +8,16 @@ import java.awt.Color; import java.awt.image.IndexColorModel; import java.io.BufferedReader; -import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; +import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; +import org.geoserver.platform.resource.Resource; +import org.geoserver.platform.resource.Resource.Type; + /** * Loads a JASC Pal files into an {@link IndexColorModel}. * @@ -58,35 +60,26 @@ class PALFileLoader { protected IndexColorModel indexColorModel; /** - * {@link PALFileLoader} constructor that accept a file path as a string. - * - *

- * Note that the transparentIndex pixel should not exceed the last - * zero-based index available for the colormap we area going to create. If - * this happens we might get very bad behaviour. Note also that if we set - * this parameter to -1 we'll get an opaque {@link IndexColorModel}. - * - * @param filePath - * to the aplette file. - * @param transparentIndex - * transparent pixel index (zero-based). - */ - public PALFileLoader(final String filePath) { - this(new File(filePath)); - } - - /** - * {@link PALFileLoader} constructor that accept a file. - * - * @see #PALFileLoader(String, int) - */ - public PALFileLoader(File file) { - if (!file.exists() | !file.canRead()) + * {@link PALFileLoader} constructor that accept a resource. + + *

+ * Note that the transparentIndex pixel should not exceed the last + * zero-based index available for the colormap we area going to create. If + * this happens we might get very bad behaviour. Note also that if we set + * this parameter to -1 we'll get an opaque {@link IndexColorModel}. + * + * @param filePath + * to the aplette file. + * @param transparentIndex + * transparent pixel index (zero-based). + */ + public PALFileLoader(Resource file) { + if (file.getType() != Type.RESOURCE) throw new IllegalArgumentException( - "The provided file cannot be read."); + "The provided file does not exist."); BufferedReader reader = null; try { - reader = new BufferedReader(new FileReader(file)); + reader = new BufferedReader(new InputStreamReader(file.in())); // header boolean loadNext = false; String temp = reader.readLine().trim(); diff --git a/src/wms/src/main/java/org/geoserver/wms/kvp/PaletteManager.java b/src/wms/src/main/java/org/geoserver/wms/kvp/PaletteManager.java index 4ee08da5c42..bad8a339be3 100644 --- a/src/wms/src/main/java/org/geoserver/wms/kvp/PaletteManager.java +++ b/src/wms/src/main/java/org/geoserver/wms/kvp/PaletteManager.java @@ -8,8 +8,6 @@ import java.awt.image.ColorModel; import java.awt.image.DataBuffer; import java.awt.image.IndexColorModel; -import java.io.File; -import java.io.FilenameFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -26,6 +24,7 @@ import org.geoserver.platform.GeoServerExtensions; import org.geoserver.platform.GeoServerResourceLoader; import org.geoserver.platform.resource.Resource; +import org.geoserver.platform.resource.Resources; import org.geotools.image.palette.InverseColorMapOp; import org.geotools.util.SoftValueHashMap; @@ -93,8 +92,6 @@ public static IndexColorModel getPalette(String name) throws Exception { // users // adds the paletteInverter dir with a running Geoserver, we won't find it // anymore... - // final File root = GeoserverDataDirectory.getGeoserverDataDirectory(); - // final File paletteDir = GeoserverDataDirectory.findConfigDir(root,"palettes"); GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class); Resource palettes = loader.get("palettes"); @@ -113,17 +110,16 @@ public static IndexColorModel getPalette(String name) throws Exception { // scan the files found (we may have multiple files with different // extensions and return the first paletteInverter you find for (Resource resource : paletteFiles) { - final File file = resource.file(); - final String fileName = file.getName(); + final String fileName = resource.name(); if (fileName.endsWith("pal")) { - final IndexColorModel icm = new PALFileLoader(file).getIndexColorModel(); + final IndexColorModel icm = new PALFileLoader(resource).getIndexColorModel(); if (icm != null) { - paletteCache.put(name, new PaletteCacheEntry(file, icm)); + paletteCache.put(name, new PaletteCacheEntry(resource, icm)); return icm; } } else { - ImageInputStream iis = ImageIO.createImageInputStream(file); + ImageInputStream iis = ImageIO.createImageInputStream(resource.in()); final Iterator it = ImageIO.getImageReaders(iis); if (it.hasNext()) { final ImageReader reader = (ImageReader) it.next(); @@ -132,7 +128,7 @@ public static IndexColorModel getPalette(String name) throws Exception { .getColorModel(); if (cm instanceof IndexColorModel) { final IndexColorModel icm = (IndexColorModel) cm; - paletteCache.put(name, new PaletteCacheEntry(file, icm)); + paletteCache.put(name, new PaletteCacheEntry(resource, icm)); return icm; } } @@ -200,17 +196,17 @@ static IndexColorModel buildDefaultPalette() { * too */ private static class PaletteCacheEntry { - File file; + Resource file; long lastModified; IndexColorModel icm; - public PaletteCacheEntry(File file, + public PaletteCacheEntry(Resource file, IndexColorModel icm) { this.file = file; this.icm = icm; - this.lastModified = file.lastModified(); + this.lastModified = file.lastmodified(); } /** @@ -220,7 +216,7 @@ public PaletteCacheEntry(File file, * @return */ public boolean isStale() { - return !file.exists() || (file.lastModified() != lastModified); + return !Resources.exists(file) || (file.lastmodified() != lastModified); } } } diff --git a/src/wms/src/main/java/org/geoserver/wms/legendgraphic/RasterLayerLegendHelper.java b/src/wms/src/main/java/org/geoserver/wms/legendgraphic/RasterLayerLegendHelper.java index bbb7c19631c..2563e20a40d 100644 --- a/src/wms/src/main/java/org/geoserver/wms/legendgraphic/RasterLayerLegendHelper.java +++ b/src/wms/src/main/java/org/geoserver/wms/legendgraphic/RasterLayerLegendHelper.java @@ -51,8 +51,6 @@ public class RasterLayerLegendHelper { try { GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class); Resource rasterLegend = loader.get( Paths.path("styles","rasterLegend.png")); -// final File stylesDirectory = GeoserverDataDirectory.findCreateConfigDir("styles"); -// final File rasterLegend = new File(stylesDirectory, "rasterLegend.png"); if (rasterLegend.getType() == Type.RESOURCE ){ imgShape = ImageIO.read(rasterLegend.file()); } diff --git a/src/wms/src/main/java/org/geoserver/wms/map/RenderedImageMapOutputFormat.java b/src/wms/src/main/java/org/geoserver/wms/map/RenderedImageMapOutputFormat.java index 02965891c73..5708ea61714 100644 --- a/src/wms/src/main/java/org/geoserver/wms/map/RenderedImageMapOutputFormat.java +++ b/src/wms/src/main/java/org/geoserver/wms/map/RenderedImageMapOutputFormat.java @@ -21,7 +21,6 @@ import java.awt.image.DataBuffer; import java.awt.image.IndexColorModel; import java.awt.image.RenderedImage; -import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -676,11 +675,9 @@ protected MapDecorationLayout findDecorationLayout(GetMapRequest request, final Resource layouts = loader.get("layouts"); if (layouts.getType() == Type.DIRECTORY ) { Resource layoutConfig = layouts.get(layoutName+".xml"); - //File layoutConfig = new File(layoutDir, layoutName + ".xml"); if( layoutConfig.getType() == Type.RESOURCE ){ - File layoutConfigFile = layoutConfig.file(); - layout = MapDecorationLayout.fromFile(layoutConfigFile, tiled); + layout = MapDecorationLayout.fromFile(layoutConfig, tiled); } else { LOGGER.log(Level.WARNING, "Unknown layout requested: " + layoutName); }