8365292: Remove javax.imageio.spi.ServiceRegistry.finalize()

8359391: Remove ThreadGroup sandboxing from javax.imageio

Reviewed-by: serb, jdv, azvegint
This commit is contained in:
Phil Race 2025-08-25 17:01:43 +00:00
parent d24449f696
commit 040cc7aee0
3 changed files with 12 additions and 184 deletions

View File

@ -44,7 +44,6 @@ import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
import com.sun.imageio.plugins.wbmp.WBMPImageWriterSpi;
import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi;
import com.sun.imageio.plugins.tiff.TIFFImageWriterSpi;
import sun.awt.AppContext;
import java.util.List;
import java.util.ServiceLoader;
import java.util.ServiceConfigurationError;
@ -105,25 +104,15 @@ public final class IIORegistry extends ServiceRegistry {
registerApplicationClasspathSpis();
}
private static final IIORegistry registry = new IIORegistry();
/**
* Returns the default {@code IIORegistry} instance used by
* the Image I/O API. This instance should be used for all
* registry functions.
*
* <p> Each {@code ThreadGroup} will receive its own instance.
*
* @return the default registry for the current
* {@code ThreadGroup}.
* @return the default registry for the Image I/O API
*/
public static IIORegistry getDefaultInstance() {
AppContext context = AppContext.getAppContext();
IIORegistry registry =
(IIORegistry)context.get(IIORegistry.class);
if (registry == null) {
// Create an instance for this AppContext
registry = new IIORegistry();
context.put(IIORegistry.class, registry);
}
return registry;
}

View File

@ -78,7 +78,7 @@ import java.util.ServiceLoader;
* proxy for the heavyweight service.
*
* <p> An application may customize the contents of a registry as it
* sees fit, so long as it has the appropriate runtime permission.
* sees fit.
*
* <p> For information on how to create and deploy service providers,
* refer to the documentation on {@link java.util.ServiceLoader ServiceLoader}
@ -283,8 +283,7 @@ public class ServiceRegistry {
* {@code onRegistration} method will be called once for each
* category it is registered under. Its
* {@code onDeregistration} method will be called each time
* it is deregistered from a category or when the registry is
* finalized.
* it is deregistered from a category.
*
* @param provider the service provider object to be registered.
*
@ -313,8 +312,7 @@ public class ServiceRegistry {
* {@code onRegistration} method will be called once for each
* category it is registered under. Its
* {@code onDeregistration} method will be called each time
* it is deregistered from a category or when the registry is
* finalized.
* it is deregistered from a category.
*
* @param providers an Iterator containing service provider
* objects to be registered.
@ -660,6 +658,12 @@ public class ServiceRegistry {
/**
* Deregisters all currently registered service providers from all
* categories.
* <p>
* If an application creates a new {@code ServiceRegistry} instance and registers providers,
* and at some point no longer needs the instance, it should call this method to ensure
* that all providers which are instances of {@link RegisterableService}
* receive a {@link RegisterableService#onDeregistration(ServiceRegistry, Class<?>)} call back,
* before allowing the instance to be garbage collected.
*/
public void deregisterAll() {
for (SubRegistry reg : categoryMap.values()) {
@ -667,26 +671,6 @@ public class ServiceRegistry {
}
}
/**
* Finalizes this object prior to garbage collection. The
* {@code deregisterAll} method is called to deregister all
* currently registered service providers. This method should not
* be called from application code.
*
* @throws Throwable if an error occurs during superclass
* finalization.
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
public void finalize() throws Throwable {
deregisterAll();
super.finalize();
}
/**
* Checks whether the provided class is one of the allowed
* ImageIO service provider classes. If it is, returns normally.
@ -821,10 +805,6 @@ class SubRegistry {
poset.clear();
}
@SuppressWarnings("removal")
public synchronized void finalize() {
clear();
}
}

View File

@ -1,141 +0,0 @@
/*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4421190
* @summary Tests that Image I/O statics may be referenced properly from
* multiple AppContexts, as would be the case for multiple Applets in a
* single VM. Each AppContext should get its own copy of the registry
* and the caching parameters in the ImageIO class.
* @modules java.desktop/sun.awt
*/
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.spi.IIORegistry;
import sun.awt.SunToolkit;
class TestThread extends Thread {
IIORegistry registry;
boolean useCache;
File cacheDirectory;
boolean cacheSettingsOK = false;
String threadName;
boolean gotCrosstalk = false;
public TestThread(ThreadGroup tg,
boolean useCache, File cacheDirectory,
String threadName) {
super(tg, threadName);
this.useCache = useCache;
this.cacheDirectory = cacheDirectory;
this.threadName = threadName;
}
public void run() {
// System.out.println("Thread " + threadName + " in thread group " +
// getThreadGroup().getName());
// Create a new AppContext as though we were an applet
SunToolkit.createNewAppContext();
// Get default registry and store reference
this.registry = IIORegistry.getDefaultInstance();
for (int i = 0; i < 10; i++) {
// System.out.println(threadName +
// ": setting cache parameters to " +
// useCache + ", " + cacheDirectory);
ImageIO.setUseCache(useCache);
ImageIO.setCacheDirectory(cacheDirectory);
try {
sleep(1000L);
} catch (InterruptedException e) {
}
// System.out.println(threadName + ": reading cache parameters");
boolean newUseCache = ImageIO.getUseCache();
File newCacheDirectory = ImageIO.getCacheDirectory();
if (newUseCache != useCache ||
newCacheDirectory != cacheDirectory) {
// System.out.println(threadName + ": got " +
// newUseCache + ", " +
// newCacheDirectory);
// System.out.println(threadName + ": crosstalk encountered!");
gotCrosstalk = true;
}
}
}
public IIORegistry getRegistry() {
return registry;
}
public boolean gotCrosstalk() {
return gotCrosstalk;
}
}
public class AppContextTest {
public AppContextTest() {
ThreadGroup tg0 = new ThreadGroup("ThreadGroup0");
ThreadGroup tg1 = new ThreadGroup("ThreadGroup1");
TestThread t0 =
new TestThread(tg0, false, null, "TestThread 0");
TestThread t1 =
new TestThread(tg1, true, new File("."), "TestThread 1");
t0.start();
t1.start();
try {
t0.join();
} catch (InterruptedException ie0) {
}
try {
t1.join();
} catch (InterruptedException ie1) {
}
if (t0.gotCrosstalk() || t1.gotCrosstalk()) {
throw new RuntimeException("ImageIO methods had crosstalk!");
}
if (t0.getRegistry() == t1.getRegistry()) {
throw new RuntimeException("ThreadGroups had same IIORegistry!");
}
}
public static void main(String[] args) throws IOException {
new AppContextTest();
}
}