Skip to content

Commit

Permalink
geosolutions-it#79: SPI registration and plugins ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
dromagnoli authored and lveci committed Mar 18, 2020
1 parent 3759e80 commit e1dbcab
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,41 @@ public static List<ImageReaderWriterSpi> getJDKImageReaderWriterSPI(ServiceRegis

return list;
}


/**
* Method to return core ImageReaderSPI/ImageWriterSPI for a given formatName.
*/
public static List<ImageReaderWriterSpi> getImageReaderWriterSPI(ServiceRegistry registry,
ServiceRegistry.Filter filter, String formatName, boolean isReader) {

IIORegistry iioRegistry = (IIORegistry) registry;

final Class<? extends ImageReaderWriterSpi> spiClass = isReader ? ImageReaderSpi.class : ImageWriterSpi.class;

final Iterator<? extends ImageReaderWriterSpi> iter = iioRegistry
.getServiceProviders(spiClass, filter, true); // useOrdering

String formatNames[];
ImageReaderWriterSpi provider;

ArrayList<ImageReaderWriterSpi> list = new ArrayList<ImageReaderWriterSpi>();
while (iter.hasNext()) {
provider = (ImageReaderWriterSpi) iter.next();
{
// Get the formatNames supported by this Spi
formatNames = provider.getFormatNames();
for (int i = 0; i < formatNames.length; i++) {
if (formatNames[i].equalsIgnoreCase(formatName)) {
list.add(provider);
break;
}
}
}
}

return list;
}

/**
* Replace the original provider with name originalProviderName with the provider with name
* customProviderName for the class providerClass and for the provided format .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,21 @@
*/
package it.geosolutions.imageioimpl.plugins.tiff;

import it.geosolutions.imageio.utilities.ImageIOUtilities;

import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.spi.ImageReaderWriterSpi;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.spi.ServiceRegistry;
import javax.imageio.stream.ImageInputStream;

import com.sun.media.imageioimpl.common.PackageUtil;

import it.geosolutions.imageio.utilities.ImageIOUtilities;

public class TIFFImageReaderSpi extends ImageReaderSpi {

private static final String[] names = { "tif", "TIF", "tiff", "TIFF", "btiff", "BTIFF" };
Expand Down Expand Up @@ -165,13 +166,44 @@ public void onRegistration(ServiceRegistry registry, Class category) {
return;
}
registered = true;
Iterator<ImageReaderWriterSpi> readers = ImageIOUtilities.getJDKImageReaderWriterSPI(registry, "TIFF", true).iterator();
Iterator<ImageReaderWriterSpi> readers = ImageIOUtilities
.getImageReaderWriterSPI(registry, new TIFFFilter(true), "TIFF", true).iterator();
while (readers.hasNext()) {
final ImageReaderSpi spi = (ImageReaderSpi) readers.next();
if (spi == this)
if (spi == this) {
continue;
}
registry.deregisterServiceProvider(spi);
registry.setOrdering(category, this, spi);
}
}

/**
* Filter which returns <code>true</code> if and only if the provider is an ImageReader/WriterSpi which supports the TIFF format.
*/
static class TIFFFilter implements ServiceRegistry.Filter {
boolean isReader;

TIFFFilter(boolean isReader) {
this.isReader = isReader;
}

public boolean filter(Object provider) {
boolean isSupportedSpi = isReader ? provider instanceof ImageReaderSpi
: provider instanceof ImageWriterSpi;
if (!isSupportedSpi) {
return false;
}

ImageReaderWriterSpi spi = (ImageReaderWriterSpi) provider;
String[] formatNames = spi.getFormatNames();
for (int i = 0; i < formatNames.length; i++) {
if (formatNames[i].equalsIgnoreCase("TIFF")) {
return true;
}
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@
*/
package it.geosolutions.imageioimpl.plugins.tiff;

import it.geosolutions.imageio.utilities.ImageIOUtilities;
import java.util.Iterator;
import java.util.Locale;

import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.spi.ImageReaderWriterSpi;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.spi.ServiceRegistry;

import com.sun.media.imageioimpl.common.PackageUtil;

import it.geosolutions.imageio.utilities.ImageIOUtilities;

public class TIFFImageWriterSpi extends ImageWriterSpi {

private static final String[] names = { "tif", "TIF", "tiff", "TIFF", "btiff", "BTIFF" };
Expand Down Expand Up @@ -142,11 +144,13 @@ public void onRegistration(ServiceRegistry registry, Class category) {
return;
}
registered = true;
final Iterator<ImageReaderWriterSpi> writers = ImageIOUtilities.getJDKImageReaderWriterSPI(registry, "TIFF", true).iterator();
final Iterator<ImageReaderWriterSpi> writers = ImageIOUtilities.getImageReaderWriterSPI(
registry, new TIFFImageReaderSpi.TIFFFilter(false), "TIFF", true).iterator();
while (writers.hasNext()) {
final ImageWriterSpi spi = (ImageWriterSpi) writers.next();
if (spi == this)
if (spi == this) {
continue;
}
registry.deregisterServiceProvider(spi);
registry.setOrdering(category, this, spi);
}
Expand Down

0 comments on commit e1dbcab

Please sign in to comment.