/* * Copyright (c) 2015, 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. */ // import java.awt.AWTError; import java.awt.Toolkit; import java.util.ArrayList; import java.util.List; import java.util.ServiceLoader; import javax.accessibility.AccessibilityProvider; public class Load { public static void main(String[] args) { // args[0]: "pass" or "fail" // args[1]: "" // args[2]: "" boolean passExpected = args[0].equals("pass"); // load one or more AccessibilityProviders. try { Toolkit.getDefaultToolkit(); } catch (AWTError e) { if (passExpected) { throw e; } } // Fill Set with names that were requested. // They may or may not be available: // - available: FooProvider, BarProvider // - not available: NoProvider List requestedNames = new ArrayList<>(); for (int i = 1; i < args.length; ++i) { requestedNames.add(args[i]); } // Toolkit.getDefaultToolkit() already went through all the service // providers, loading and activating the requested ones, but now we need // to reload all of them to see if they actually got activated. When the // service providers are activated they change their names to append the // string "-activated" // Go through the providers that are available, for each one: // If it was activated // If it was requested pass, else fail (throw exception) // else (it was not activated) // If it was not requested pass, else fail (throw exception) // If the service loader throws an exception, fail (rethrow) // If the requested provider is not found, fail (throw exception) ClassLoader cl = ClassLoader.getSystemClassLoader(); try { boolean foundRequestedProvider = false; for (AccessibilityProvider p : ServiceLoader.load(AccessibilityProvider.class, cl)) { String providerName; String s = p.getName(); int i = s.indexOf("-activated"); if (i != -1) { // "-activated" found // An activated provider was found, but was it requested? providerName = s.substring(0, i); if (requestedNames.contains(providerName)) { foundRequestedProvider = true; } else { throw new RuntimeException("Unrequested but activated provider found."); } } else { // "-activated" not found // An un-activated provider was found; was it requested? providerName = s; if (requestedNames.contains(providerName)) { throw new RuntimeException("Requested provider found but not activated."); } } } // if get to here, no issues, so try next provider if (!foundRequestedProvider) { throw new RuntimeException("Didn't find requested/activated provider"); } } catch (java.util.ServiceConfigurationError | RuntimeException e) { if (passExpected) { throw e; } } } }