Skip to content

Commit

Permalink
Resources: port wms
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Nov 20, 2015
1 parent a94aabe commit e0b3778
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 91 deletions.
27 changes: 12 additions & 15 deletions src/wms/src/main/java/org/geoserver/wms/WMSLifecycleHandler.java
Expand Up @@ -21,6 +21,8 @@
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.geoserver.config.GeoServerDataDirectory; import org.geoserver.config.GeoServerDataDirectory;
import org.geoserver.config.impl.GeoServerLifecycleHandler; 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.FontCache;
import org.geotools.renderer.style.ImageGraphicFactory; import org.geotools.renderer.style.ImageGraphicFactory;
import org.geotools.renderer.style.SVGGraphicFactory; import org.geotools.renderer.style.SVGGraphicFactory;
Expand Down Expand Up @@ -109,22 +111,17 @@ void reloadFontCache() {


List<Font> loadFontsFromDataDirectory() { List<Font> loadFontsFromDataDirectory() {
List<Font> result = new ArrayList<Font>(); List<Font> result = new ArrayList<Font>();
try { for (Resource file : Resources.list(data.getStyles(), new Resources.ExtensionFilter("TTF"),
Collection<File> files = FileUtils.listFiles(data.findStyleDir(), new String[] { "ttf", true)) {
"TTF" }, true); try {
for (File file : files) { final Font font = Font.createFont(Font.TRUETYPE_FONT, file.file());
try { result.add(font);
final Font font = Font.createFont(Font.TRUETYPE_FONT, file); LOGGER.log(Level.INFO,
result.add(font); "Loaded font file " + file + ", loaded font '" + font.getName()
LOGGER.log(Level.INFO, + "' in family '" + font.getFamily() + "'");
"Loaded font file " + file + ", loaded font '" + font.getName() } catch (Exception e) {
+ "' in family '" + font.getFamily() + "'"); LOGGER.log(Level.WARNING, "Failed to load font file " + file, e);
} 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; return result;
Expand Down
Expand Up @@ -9,9 +9,8 @@


import java.awt.Dimension; import java.awt.Dimension;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
Expand All @@ -31,6 +30,7 @@
import org.geoserver.platform.GeoServerResourceLoader; import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Paths; import org.geoserver.platform.resource.Paths;
import org.geoserver.platform.resource.Resource; import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resource.Type;
import org.geoserver.wms.GetLegendGraphicOutputFormat; import org.geoserver.wms.GetLegendGraphicOutputFormat;
import org.geoserver.wms.GetLegendGraphicRequest; import org.geoserver.wms.GetLegendGraphicRequest;
import org.geoserver.wms.legendgraphic.BufferedImageLegendGraphic; import org.geoserver.wms.legendgraphic.BufferedImageLegendGraphic;
Expand Down Expand Up @@ -65,13 +65,13 @@ public class LegendSampleImpl implements CatalogListener, LegendSample,


private Set<String> invalidated = new HashSet<String>(); private Set<String> invalidated = new HashSet<String>();


File baseDir; Resource baseDir;


public LegendSampleImpl(Catalog catalog, GeoServerResourceLoader loader) { public LegendSampleImpl(Catalog catalog, GeoServerResourceLoader loader) {
super(); super();
this.catalog = catalog; this.catalog = catalog;
this.loader = loader; this.loader = loader;
this.baseDir = loader.getBaseDirectory(); this.baseDir = loader.get(Paths.BASE);
this.clean(); this.clean();
this.catalog.addListener(this); this.catalog.addListener(this);
} }
Expand All @@ -83,7 +83,7 @@ private void clean() {
for (StyleInfo style : catalog.getStyles()) { for (StyleInfo style : catalog.getStyles()) {
synchronized (style) { synchronized (style) {
Resource styleResource = getStyleResource(style); Resource styleResource = getStyleResource(style);
File sampleFile; Resource sampleFile;
try { try {
// remove old samples // remove old samples
sampleFile = getSampleFile(style); sampleFile = getSampleFile(style);
Expand All @@ -106,10 +106,10 @@ private void clean() {
* @return * @return
*/ */
private boolean isStyleNewerThanSample(Resource styleResource, private boolean isStyleNewerThanSample(Resource styleResource,
File sampleFile) { Resource sampleFile) {
return isSampleExisting(sampleFile) return isSampleExisting(sampleFile)
&& styleResource.getType() == Resource.Type.RESOURCE && styleResource.getType() == Resource.Type.RESOURCE
&& styleResource.lastmodified() > sampleFile.lastModified(); && styleResource.lastmodified() > sampleFile.lastmodified();
} }


/** /**
Expand All @@ -120,7 +120,7 @@ private boolean isStyleNewerThanSample(Resource styleResource,
* @return * @return
* @throws IOException * @throws IOException
*/ */
private File getSampleFile(StyleInfo style) throws IOException { private Resource getSampleFile(StyleInfo style) throws IOException {
String fileName = getSampleFileName(style); String fileName = getSampleFileName(style);
return getSampleFile(fileName); return getSampleFile(fileName);
} }
Expand All @@ -131,8 +131,8 @@ private File getSampleFile(StyleInfo style) throws IOException {
* @param fileName * @param fileName
* @return * @return
*/ */
private File getSampleFile(String fileName) { private Resource getSampleFile(String fileName) {
return new File(getSamplesFolder(), fileName); return getSamplesFolder().get(fileName);
} }


/** /**
Expand Down Expand Up @@ -179,7 +179,7 @@ public Dimension getLegendURLSize(StyleInfo style) throws Exception {
synchronized (style) { synchronized (style) {
GetLegendGraphicOutputFormat pngOutputFormat = new PNGLegendOutputFormat(); GetLegendGraphicOutputFormat pngOutputFormat = new PNGLegendOutputFormat();


File sampleLegend = getSampleFile(style); Resource sampleLegend = getSampleFile(style);
if (isSampleExisting(sampleLegend) if (isSampleExisting(sampleLegend)
&& !isStyleSampleInvalid(style)) { && !isStyleSampleInvalid(style)) {
// using existing sample if sld has not been updated from // using existing sample if sld has not been updated from
Expand All @@ -193,8 +193,8 @@ public Dimension getLegendURLSize(StyleInfo style) throws Exception {
} }
} }


private boolean isSampleExisting(File sampleLegend) { private boolean isSampleExisting(Resource sampleLegend) {
return sampleLegend != null && sampleLegend.exists(); return sampleLegend != null && sampleLegend.getType() == Type.RESOURCE;
} }


/** /**
Expand All @@ -210,7 +210,7 @@ private boolean isSampleExisting(File sampleLegend) {
private Dimension createNewSample(StyleInfo style, private Dimension createNewSample(StyleInfo style,
GetLegendGraphicOutputFormat pngOutputFormat) throws Exception { GetLegendGraphicOutputFormat pngOutputFormat) throws Exception {
GetLegendGraphicRequest legendGraphicRequest = new GetLegendGraphicRequest(); GetLegendGraphicRequest legendGraphicRequest = new GetLegendGraphicRequest();
File sampleLegendFolder = getSamplesFolder(); Resource sampleLegendFolder = getSamplesFolder();


legendGraphicRequest.setStrict(false); legendGraphicRequest.setStrict(false);
legendGraphicRequest.setLayer((FeatureType) null); legendGraphicRequest.setLayer((FeatureType) null);
Expand All @@ -223,14 +223,10 @@ private Dimension createNewSample(StyleInfo style,
.getLegend(); .getLegend();


PNGWriter writer = new PNGWriter(); PNGWriter writer = new PNGWriter();
FileOutputStream outStream = null; OutputStream outStream = null;
try { try {
File sampleFile = new File(sampleLegendFolder.getAbsolutePath() + File.separator + Resource sampleFile = sampleLegendFolder.get(getSampleFileName(style));
getSampleFileName(style)); outStream = sampleFile.out();
if(!sampleFile.getParentFile().exists()) {
sampleFile.getParentFile().mkdirs();
}
outStream = new FileOutputStream(sampleFile);
writer.writePNG(image, outStream, 0.0f, FilterType.FILTER_NONE); writer.writePNG(image, outStream, 0.0f, FilterType.FILTER_NONE);
removeStyleSampleInvalidation(style); removeStyleSampleInvalidation(style);
return new Dimension(image.getWidth(), image.getHeight()); return new Dimension(image.getWidth(), image.getHeight());
Expand All @@ -245,21 +241,21 @@ private Dimension createNewSample(StyleInfo style,
return null; return null;
} }


private File getSamplesFolder() { private Resource getSamplesFolder() {
return new File(baseDir + File.separator + LEGEND_SAMPLES_FOLDER); return baseDir.get(LEGEND_SAMPLES_FOLDER);
} }


/** /**
* *
* @param sampleLegendFile * @param sampleLegendFile
* @return * @return
*/ */
private Dimension getSizeFromSample(File sampleLegendFile) { private Dimension getSizeFromSample(Resource sampleLegendFile) {
PngReader pngReader = null; PngReader pngReader = null;
try { try {
// reads size using PNGJ reader, that can read metadata without reading // reads size using PNGJ reader, that can read metadata without reading
// the full image // the full image
pngReader = new PngReader(sampleLegendFile); pngReader = new PngReader(sampleLegendFile.file());
return new Dimension(pngReader.imgInfo.cols, pngReader.imgInfo.rows); return new Dimension(pngReader.imgInfo.cols, pngReader.imgInfo.rows);
} finally { } finally {
if (pngReader != null) { if (pngReader != null) {
Expand Down
Expand Up @@ -13,7 +13,6 @@
import java.awt.Point; import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Shape; import java.awt.Shape;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
Expand All @@ -23,6 +22,7 @@


import org.geoserver.platform.GeoServerExtensions; import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.ServiceException; import org.geoserver.platform.ServiceException;
import org.geoserver.platform.resource.Resource;
import org.geoserver.wms.WMSMapContent; import org.geoserver.wms.WMSMapContent;
import org.jdom.Document; import org.jdom.Document;
import org.jdom.Element; import org.jdom.Element;
Expand Down Expand Up @@ -245,12 +245,12 @@ public MapDecorationLayout() {
* @return a new MapDecorationLayout containing the MapDecorations specified * @return a new MapDecorationLayout containing the MapDecorations specified
* @throws Exception if the configuration is invalid or other errors occur while parsing * @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 MapDecorationLayout dl = tiled
? new MetatiledMapDecorationLayout() ? new MetatiledMapDecorationLayout()
: new MapDecorationLayout(); : new MapDecorationLayout();


Document confFile = new SAXBuilder().build(f); Document confFile = new SAXBuilder().build(f.file());


for (Element e : (List<Element>)confFile.getRootElement().getChildren("decoration")){ for (Element e : (List<Element>)confFile.getRootElement().getChildren("decoration")){
Map<String, String> m = new HashMap<String,String>(); Map<String, String> m = new HashMap<String,String>();
Expand Down
49 changes: 21 additions & 28 deletions src/wms/src/main/java/org/geoserver/wms/kvp/PALFileLoader.java
Expand Up @@ -8,14 +8,16 @@
import java.awt.Color; import java.awt.Color;
import java.awt.image.IndexColorModel; import java.awt.image.IndexColorModel;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; 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}. * Loads a JASC Pal files into an {@link IndexColorModel}.
* *
Expand Down Expand Up @@ -58,35 +60,26 @@ class PALFileLoader {
protected IndexColorModel indexColorModel; protected IndexColorModel indexColorModel;


/** /**
* {@link PALFileLoader} constructor that accept a file path as a string. * {@link PALFileLoader} constructor that accept a resource.
*
* <p> * <p>
* Note that the transparentIndex pixel should not exceed the last * Note that the transparentIndex pixel should not exceed the last
* zero-based index available for the colormap we area going to create. If * 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 happens we might get very bad behaviour. Note also that if we set
* this parameter to -1 we'll get an opaque {@link IndexColorModel}. * this parameter to -1 we'll get an opaque {@link IndexColorModel}.
* *
* @param filePath * @param filePath
* to the aplette file. * to the aplette file.
* @param transparentIndex * @param transparentIndex
* transparent pixel index (zero-based). * transparent pixel index (zero-based).
*/ */
public PALFileLoader(final String filePath) { public PALFileLoader(Resource file) {
this(new File(filePath)); if (file.getType() != Type.RESOURCE)
}

/**
* {@link PALFileLoader} constructor that accept a file.
*
* @see #PALFileLoader(String, int)
*/
public PALFileLoader(File file) {
if (!file.exists() | !file.canRead())
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The provided file cannot be read."); "The provided file does not exist.");
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = new BufferedReader(new FileReader(file)); reader = new BufferedReader(new InputStreamReader(file.in()));
// header // header
boolean loadNext = false; boolean loadNext = false;
String temp = reader.readLine().trim(); String temp = reader.readLine().trim();
Expand Down
24 changes: 10 additions & 14 deletions src/wms/src/main/java/org/geoserver/wms/kvp/PaletteManager.java
Expand Up @@ -8,8 +8,6 @@
import java.awt.image.ColorModel; import java.awt.image.ColorModel;
import java.awt.image.DataBuffer; import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel; import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
Expand All @@ -26,6 +24,7 @@
import org.geoserver.platform.GeoServerExtensions; import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader; import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Resource; import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resources;
import org.geotools.image.palette.InverseColorMapOp; import org.geotools.image.palette.InverseColorMapOp;
import org.geotools.util.SoftValueHashMap; import org.geotools.util.SoftValueHashMap;


Expand Down Expand Up @@ -93,8 +92,6 @@ public static IndexColorModel getPalette(String name) throws Exception {
// users // users
// adds the paletteInverter dir with a running Geoserver, we won't find it // adds the paletteInverter dir with a running Geoserver, we won't find it
// anymore... // anymore...
// final File root = GeoserverDataDirectory.getGeoserverDataDirectory();
// final File paletteDir = GeoserverDataDirectory.findConfigDir(root,"palettes");
GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class); GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);


Resource palettes = loader.get("palettes"); Resource palettes = loader.get("palettes");
Expand All @@ -113,17 +110,16 @@ public static IndexColorModel getPalette(String name) throws Exception {
// scan the files found (we may have multiple files with different // scan the files found (we may have multiple files with different
// extensions and return the first paletteInverter you find // extensions and return the first paletteInverter you find
for (Resource resource : paletteFiles) { for (Resource resource : paletteFiles) {
final File file = resource.file(); final String fileName = resource.name();
final String fileName = file.getName();
if (fileName.endsWith("pal")) { if (fileName.endsWith("pal")) {
final IndexColorModel icm = new PALFileLoader(file).getIndexColorModel(); final IndexColorModel icm = new PALFileLoader(resource).getIndexColorModel();


if (icm != null) { if (icm != null) {
paletteCache.put(name, new PaletteCacheEntry(file, icm)); paletteCache.put(name, new PaletteCacheEntry(resource, icm));
return icm; return icm;
} }
} else { } else {
ImageInputStream iis = ImageIO.createImageInputStream(file); ImageInputStream iis = ImageIO.createImageInputStream(resource.in());
final Iterator it = ImageIO.getImageReaders(iis); final Iterator it = ImageIO.getImageReaders(iis);
if (it.hasNext()) { if (it.hasNext()) {
final ImageReader reader = (ImageReader) it.next(); final ImageReader reader = (ImageReader) it.next();
Expand All @@ -132,7 +128,7 @@ public static IndexColorModel getPalette(String name) throws Exception {
.getColorModel(); .getColorModel();
if (cm instanceof IndexColorModel) { if (cm instanceof IndexColorModel) {
final IndexColorModel icm = (IndexColorModel) cm; final IndexColorModel icm = (IndexColorModel) cm;
paletteCache.put(name, new PaletteCacheEntry(file, icm)); paletteCache.put(name, new PaletteCacheEntry(resource, icm));
return icm; return icm;
} }
} }
Expand Down Expand Up @@ -200,17 +196,17 @@ static IndexColorModel buildDefaultPalette() {
* too * too
*/ */
private static class PaletteCacheEntry { private static class PaletteCacheEntry {
File file; Resource file;


long lastModified; long lastModified;


IndexColorModel icm; IndexColorModel icm;


public PaletteCacheEntry(File file, public PaletteCacheEntry(Resource file,
IndexColorModel icm) { IndexColorModel icm) {
this.file = file; this.file = file;
this.icm = icm; this.icm = icm;
this.lastModified = file.lastModified(); this.lastModified = file.lastmodified();
} }


/** /**
Expand All @@ -220,7 +216,7 @@ public PaletteCacheEntry(File file,
* @return * @return
*/ */
public boolean isStale() { public boolean isStale() {
return !file.exists() || (file.lastModified() != lastModified); return !Resources.exists(file) || (file.lastmodified() != lastModified);
} }
} }
} }
Expand Up @@ -51,8 +51,6 @@ public class RasterLayerLegendHelper {
try { try {
GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class); GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);
Resource rasterLegend = loader.get( Paths.path("styles","rasterLegend.png")); 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 ){ if (rasterLegend.getType() == Type.RESOURCE ){
imgShape = ImageIO.read(rasterLegend.file()); imgShape = ImageIO.read(rasterLegend.file());
} }
Expand Down

0 comments on commit e0b3778

Please sign in to comment.