private static boolean loadProviderAsService() { Iterator i = Service.providers(SelectorProvider.class, ClassLoader.getSystemClassLoader()); for (;;) { try { if (!i.hasNext()) return false; provider = (SelectorProvider)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 boolean loadProviderAsService() { Iterator i = Service.providers(HttpServerProvider.class, ClassLoader.getSystemClassLoader()); for (;;) { try { if (!i.hasNext()) return false; provider = (HttpServerProvider)i.next(); return true; } catch (ServiceConfigurationError sce) { if (sce.getCause() instanceof SecurityException) { // Ignore the security exception, try the next provider continue; } throw sce; } } }
@SuppressWarnings("rawtypes") private Iterator<String> parse(Class service, URL u) throws ServiceConfigurationError { InputStream in = null; BufferedReader r = null; ArrayList<String> names = new ArrayList<String>(); try { in = u.openStream(); r = new BufferedReader(new InputStreamReader(in, "utf-8")); int lc = 1; while ((lc = parseLine(service, u, r, lc, names)) >= 0) ; } catch (IOException x) { fail(service, "Error reading configuration file", x); } finally { try { if (r != null) r.close(); if (in != null) in.close(); } catch (IOException y) { fail(service, "Error closing configuration file", y); } } return names.iterator(); }
private static PreferencesFactory factory1() { // 2. Try service provider interface Iterator i = Service.providers(PreferencesFactory.class, ClassLoader.getSystemClassLoader()); // choose first provider instance while (i.hasNext()) { try { return (PreferencesFactory) i.next(); } catch (ServiceConfigurationError sce) { if (sce.getCause() instanceof SecurityException) { // Ignore the security exception, try the next provider continue; } throw sce; } } // 3. Use platform-specific system-wide default String platformFactory = System.getProperty("os.name").startsWith("Windows") ? "java.util.prefs.WindowsPreferencesFactory" : "java.util.prefs.FileSystemPreferencesFactory"; try { return (PreferencesFactory) Class.forName(platformFactory, false, null).newInstance(); } catch (Exception e) { InternalError error = new InternalError( "Can't instantiate platform default Preferences factory " + platformFactory); error.initCause(e); throw error; } }
@SuppressWarnings("rawtypes") private int parseLine(Class service, URL u, BufferedReader r, int lc, List<String> names) throws IOException, ServiceConfigurationError { String ln = r.readLine(); if (ln == null) { return -1; } int ci = ln.indexOf('#'); if (ci >= 0) ln = ln.substring(0, ci); ln = ln.trim(); int n = ln.length(); if (n != 0) { if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) fail(service, u, lc, "Illegal configuration-file syntax"); int cp = ln.codePointAt(0); if (!Character.isJavaIdentifierStart(cp)) fail(service, u, lc, "Illegal provider-class name: " + ln); for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) { cp = ln.codePointAt(i); if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) fail(service, u, lc, "Illegal provider-class name: " + ln); } if (!providers.containsKey(ln) && !names.contains(ln)) names.add(ln); } return lc + 1; }
private static Iterator providers() { return new Iterator() { Class c = java.nio.charset.spi.CharsetProvider.class; ClassLoader cl = ClassLoader.getSystemClassLoader(); Iterator i = Service.providers(c, cl); Object next = null; private boolean getNext() { while (next == null) { try { if (!i.hasNext()) return false; next = i.next(); } catch (ServiceConfigurationError sce) { if (sce.getCause() instanceof SecurityException) { // Ignore security exceptions continue; } throw sce; } } return true; } public boolean hasNext() { return getNext(); } public Object next() { if (!getNext()) throw new NoSuchElementException(); Object n = next; next = null; return n; } public void remove() { throw new UnsupportedOperationException(); } }; }
@SuppressWarnings("rawtypes") private static void fail(Class service, String msg, Throwable cause) throws ServiceConfigurationError { throw new ServiceConfigurationError(cause); }
@SuppressWarnings("rawtypes") private static void fail(Class service, String msg) throws ServiceConfigurationError { throw new ServiceConfigurationError(service.getName() + ": " + msg); }
@SuppressWarnings("rawtypes") private static void fail(Class service, URL u, int line, String msg) throws ServiceConfigurationError { fail(service, u + ":" + line + ": " + msg); }