private static void loadFileSystems() { synchronized (FileSystem.class) { if (!FILE_SYSTEMS_LOADED) { ServiceLoader<FileSystem> serviceLoader = ServiceLoader.load(FileSystem.class); Iterator<FileSystem> it = serviceLoader.iterator(); while (it.hasNext()) { FileSystem fs = null; try { fs = it.next(); try { SERVICE_FILE_SYSTEMS.put(fs.getScheme(), fs.getClass()); } catch (Exception e) { LOG.warn("Cannot load: " + fs + " from " + ClassUtil.findContainingJar(fs.getClass()), e); } } catch (ServiceConfigurationError ee) { LOG.warn("Cannot load filesystem", ee); } } FILE_SYSTEMS_LOADED = true; } } }
private LuaFunction findLoader(String modName) { try { for (T service : serviceLoader) { if (matches(modName, service)) { LuaFunction loader = getLoader(service); if (loader != null) { return loader; } } } } catch (ServiceConfigurationError error) { // TODO: maybe we should just let the VM crash? throw new LuaRuntimeException(error); } // not found return null; }
/** * Tests issue #TRUEZIP-154. * * @see <a href="http://java.net/jira/browse/TRUEZIP-154">ServiceConfigurationError: Unknown file system scheme for path without a extension</a> */ @Test public void testIssue154() { for (String param : new String[] { "mok:file:/foo!/", "mok:mok:file:/foo!/bar!/", }) { FsNodePath path = FsNodePath.create(URI.create(param)); try { assertIssue154(new TFile(path)); assertIssue154(new TFile(path.getUri())); } catch (ServiceConfigurationError error) { throw new AssertionError(param, error); } } }
/** * Reloads the internal SPI list from the given {@link ClassLoader}. * Changes to the service list are visible after the method ends, all * iterators ({@link #iterator()},...) stay consistent. * * <p><b>NOTE:</b> Only new service providers are added, existing ones are * never removed or replaced. * * <p><em>This method is expensive and should only be called for discovery * of new service providers on the given classpath/classloader!</em> */ public synchronized void reload(ClassLoader classloader) { final LinkedHashMap<String,S> services = new LinkedHashMap<>(this.services); final SPIClassIterator<S> loader = SPIClassIterator.get(clazz, classloader); while (loader.hasNext()) { final Class<? extends S> c = loader.next(); try { final S service = c.newInstance(); final String name = service.getName(); // only add the first one for each name, later services will be ignored // this allows to place services before others in classpath to make // them used instead of others if (!services.containsKey(name)) { checkServiceName(name); services.put(name, service); } } catch (Exception e) { throw new ServiceConfigurationError("Cannot instantiate SPI class: " + c.getName(), e); } } this.services = Collections.unmodifiableMap(services); }
/** * Use the ServiceLoader mechanism to load the default RowSetFactory * @return default RowSetFactory Implementation */ static private RowSetFactory loadViaServiceLoader() throws SQLException { RowSetFactory theFactory = null; try { trace("***in loadViaServiceLoader():"); for (RowSetFactory factory : ServiceLoader.load(javax.sql.rowset.RowSetFactory.class)) { trace(" Loading done by the java.util.ServiceLoader :" + factory.getClass().getName()); theFactory = factory; break; } } catch (ServiceConfigurationError e) { throw new SQLException( "RowSetFactory: Error locating RowSetFactory using Service " + "Loader API: " + e, e); } return theFactory; }
private static boolean loadProviderAsService() { ServiceLoader<SelectorProvider> sl = ServiceLoader.load(SelectorProvider.class, ClassLoader.getSystemClassLoader()); Iterator<SelectorProvider> i = sl.iterator(); for (;;) { try { if (!i.hasNext()) return false; provider = i.next(); return true; } catch (ServiceConfigurationError sce) { if (sce.getCause() instanceof SecurityException) { // Ignore the security exception, try the next provider continue; } throw sce; } } }
private static AsynchronousChannelProvider loadProviderAsService() { ServiceLoader<AsynchronousChannelProvider> sl = ServiceLoader.load(AsynchronousChannelProvider.class, ClassLoader.getSystemClassLoader()); Iterator<AsynchronousChannelProvider> i = sl.iterator(); for (;;) { try { return (i.hasNext()) ? i.next() : null; } catch (ServiceConfigurationError sce) { if (sce.getCause() instanceof SecurityException) { // Ignore the security exception, try the next provider continue; } throw sce; } } }
private static boolean loadProviderFromProperty() { String cm = System.getProperty("sun.net.ftpClientProvider"); if (cm == null) { return false; } try { Class<?> c = Class.forName(cm, true, null); provider = (FtpClientProvider) c.newInstance(); return true; } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SecurityException x) { throw new ServiceConfigurationError(x.toString()); } }
private static boolean loadProviderFromProperty() { String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider"); if (cn == null) return false; try { Class<?> c = Class.forName(cn, true, ClassLoader.getSystemClassLoader()); provider = (HttpServerProvider)c.newInstance(); return true; } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SecurityException x) { throw new ServiceConfigurationError(null, x); } }
private static boolean loadProviderAsService() { Iterator<HttpServerProvider> i = ServiceLoader.load(HttpServerProvider.class, ClassLoader.getSystemClassLoader()) .iterator(); for (;;) { try { if (!i.hasNext()) return false; provider = i.next(); return true; } catch (ServiceConfigurationError sce) { if (sce.getCause() instanceof SecurityException) { // Ignore the security exception, try the next provider continue; } throw sce; } } }
private static <T> T findServiceProvider(final Class<T> type) throws DatatypeConfigurationException { try { return AccessController.doPrivileged(new PrivilegedAction<T>() { public T run() { final ServiceLoader<T> serviceLoader = ServiceLoader.load(type); final Iterator<T> iterator = serviceLoader.iterator(); if (iterator.hasNext()) { return iterator.next(); } else { return null; } } }); } catch(ServiceConfigurationError e) { final DatatypeConfigurationException error = new DatatypeConfigurationException( "Provider for " + type + " cannot be found", e); throw error; } }
private static boolean loadProviderFromProperty() { String cm = System.getProperty("sun.net.ftpClientProvider"); if (cm == null) { return false; } try { @SuppressWarnings("deprecation") Object o = Class.forName(cm, true, null).newInstance(); provider = (FtpClientProvider)o; return true; } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SecurityException x) { throw new ServiceConfigurationError(x.toString()); } }
private static boolean loadProviderFromProperty() { String cn = System.getProperty("com.sun.net.httpserver.HttpServerProvider"); if (cn == null) return false; try { @SuppressWarnings("deprecation") Object o = Class.forName(cn, true, ClassLoader.getSystemClassLoader()).newInstance(); provider = (HttpServerProvider)o; return true; } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | SecurityException x) { throw new ServiceConfigurationError(null, x); } }
/** * Test Module::addUses */ public void testAddUses() { Module thisModule = Main.class.getModule(); assertFalse(thisModule.canUse(Service.class)); try { ServiceLoader.load(Service.class); assertTrue(false); } catch (ServiceConfigurationError expected) { } Module result = thisModule.addUses(Service.class); assertTrue(result== thisModule); assertTrue(thisModule.canUse(Service.class)); ServiceLoader.load(Service.class); // no exception }
@Test(dataProvider = "badfactories", expectedExceptions = ServiceConfigurationError.class) public void testBadFactory(String testName, String ignore) throws Exception { Path mods = compileTest(TEST1_MODULE); // compile the bad factory Path source = BADFACTORIES_DIR.resolve(testName); Path output = Files.createTempDirectory(USER_DIR, "tmp"); boolean compiled = CompilerUtils.compile(source, output); assertTrue(compiled); // copy the compiled class into the module Path classFile = Paths.get("p", "ProviderFactory.class"); Files.copy(output.resolve(classFile), mods.resolve(TEST1_MODULE).resolve(classFile), StandardCopyOption.REPLACE_EXISTING); // load providers and instantiate each one loadProviders(mods, TEST1_MODULE).forEach(Provider::get); }
@Test(dataProvider = "badproviders", expectedExceptions = ServiceConfigurationError.class) public void testBadProvider(String testName, String ignore) throws Exception { Path mods = compileTest(TEST2_MODULE); // compile the bad provider Path source = BADPROVIDERS_DIR.resolve(testName); Path output = Files.createTempDirectory(USER_DIR, "tmp"); boolean compiled = CompilerUtils.compile(source, output); assertTrue(compiled); // copy the compiled class into the module Path classFile = Paths.get("p", "Provider.class"); Files.copy(output.resolve(classFile), mods.resolve(TEST2_MODULE).resolve(classFile), StandardCopyOption.REPLACE_EXISTING); // load providers and instantiate each one loadProviders(mods, TEST2_MODULE).forEach(Provider::get); }
private static <T> T findServiceProvider(final Class<T> type, final ClassLoader loader) throws SAXException { ClassLoader cl = Objects.requireNonNull(loader); try { return AccessController.doPrivileged((PrivilegedAction<T>) () -> { final ServiceLoader<T> serviceLoader; serviceLoader = ServiceLoader.load(type, cl); final Iterator<T> iterator = serviceLoader.iterator(); if (iterator.hasNext()) { return iterator.next(); } else { return null; } }); } catch(ServiceConfigurationError e) { final RuntimeException x = new RuntimeException( "Provider for " + type + " cannot be created", e); throw new SAXException("Provider for " + type + " cannot be created", x); } }
/** * Gets the JVMCI provider for a given service for which at most one provider must be available. * * @param service the service whose provider is being requested * @param required specifies if an {@link InternalError} should be thrown if no provider of * {@code service} is available */ public static <S> S loadSingle(Class<S> service, boolean required) { Iterable<S> providers = ServiceLoader.load(service); S singleProvider = null; try { for (Iterator<S> it = providers.iterator(); it.hasNext();) { singleProvider = it.next(); if (it.hasNext()) { throw new InternalError(String.format("Multiple %s providers found", service.getName())); } } } catch (ServiceConfigurationError e) { // If the service is required we will bail out below. } if (singleProvider == null && required) { String javaHome = System.getProperty("java.home"); String vmName = System.getProperty("java.vm.name"); Formatter errorMessage = new Formatter(); errorMessage.format("The VM does not expose required service %s.%n", service.getName()); errorMessage.format("Currently used Java home directory is %s.%n", javaHome); errorMessage.format("Currently used VM configuration is: %s", vmName); throw new UnsupportedOperationException(errorMessage.toString()); } return singleProvider; }