@Test(expected = IllegalArgumentException.class) public void loadExtensions_erroneous() throws Exception { CommandLineUtil.loadExtensions(getClass().getClassLoader(), Arrays.asList( new ExtendedArgument(ErroneousExtensionHandler.TAG, "?"))); }
public static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments) { List<Extension> results = new ArrayList<>(); ServiceLoader<ExtensionHandler> services = ServiceLoader.load(ExtensionHandler.class, classLoader); for (ExtendedArgument argument : arguments) { String name = argument.getName(); String value = argument.getValue(); boolean consumed = false; for (ExtensionHandler handler : services) { try { Extension extension = handler.handle(name, value); if (extension != null) { results.add(extension); consumed = true; break; } } catch (IOException e) { YSLOG.error(e, "E99001", name, value); for (Extension ext : results) { try { ext.close(); } catch (IOException inner) { e.addSuppressed(inner); } } throw new IllegalArgumentException(MessageFormat.format( "invalid extended argument \"{0}\"", argument), e); } } if (consumed == false) { YSLOG.error("E99001", name, value); throw new IllegalArgumentException(MessageFormat.format( "there is no available extension handler \"{0}\"", argument)); } } return results; }
CommandLineUtil { public static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments) { List<Extension> results = new ArrayList<>(); ServiceLoader<ExtensionHandler> services = ServiceLoader.load(ExtensionHandler.class, classLoader); for (ExtendedArgument argument : arguments) { String name = argument.getName(); String value = argument.getValue(); boolean consumed = false; for (ExtensionHandler handler : services) { try { Extension extension = handler.handle(name, value); if (extension != null) { results.add(extension); consumed = true; break; } } catch (IOException e) { YSLOG.error(e, "E99001", name, value); for (Extension ext : results) { try { ext.close(); } catch (IOException inner) { e.addSuppressed(inner); } } throw new IllegalArgumentException(MessageFormat.format( "invalid extended argument \"{0}\"", argument), e); } } if (consumed == false) { YSLOG.error("E99001", name, value); throw new IllegalArgumentException(MessageFormat.format( "there is no available extension handler \"{0}\"", argument)); } } return results; } }
CommandLineUtil { public static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments) { List<Extension> results = new ArrayList<>(); ServiceLoader<ExtensionHandler> services = ServiceLoader.load(ExtensionHandler.class, classLoader); for (ExtendedArgument argument : arguments) { String name = argument.getName(); String value = argument.getValue(); boolean consumed = false; for (ExtensionHandler handler : services) { try { Extension extension = handler.handle(name, value); if (extension != null) { results.add(extension); consumed = true; break; } } catch (IOException e) { YSLOG.error(e, "E99001", name, value); for (Extension ext : results) { try { ext.close(); } catch (IOException inner) { e.addSuppressed(inner); } } throw new IllegalArgumentException(MessageFormat.format( "invalid extended argument \"{0}\"", argument), e); } } if (consumed == false) { YSLOG.error("E99001", name, value); throw new IllegalArgumentException(MessageFormat.format( "there is no available extension handler \"{0}\"", argument)); } } return results; } private  CommandLineUtil(); }
CommandLineUtil { public static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments) { List<Extension> results = new ArrayList<>(); ServiceLoader<ExtensionHandler> services = ServiceLoader.load(ExtensionHandler.class, classLoader); for (ExtendedArgument argument : arguments) { String name = argument.getName(); String value = argument.getValue(); boolean consumed = false; for (ExtensionHandler handler : services) { try { Extension extension = handler.handle(name, value); if (extension != null) { results.add(extension); consumed = true; break; } } catch (IOException e) { YSLOG.error(e, "E99001", name, value); for (Extension ext : results) { try { ext.close(); } catch (IOException inner) { e.addSuppressed(inner); } } throw new IllegalArgumentException(MessageFormat.format( "invalid extended argument \"{0}\"", argument), e); } } if (consumed == false) { YSLOG.error("E99001", name, value); throw new IllegalArgumentException(MessageFormat.format( "there is no available extension handler \"{0}\"", argument)); } } return results; } private  CommandLineUtil(); static void prepareLogContext(); static Properties loadProperties(File path); static List<File> parseFileList(String fileListOrNull); static ClassLoader buildPluginLoader(ClassLoader parent, List<File> files); static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments); }
CommandLineUtil { public static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments) { List<Extension> results = new ArrayList<>(); ServiceLoader<ExtensionHandler> services = ServiceLoader.load(ExtensionHandler.class, classLoader); for (ExtendedArgument argument : arguments) { String name = argument.getName(); String value = argument.getValue(); boolean consumed = false; for (ExtensionHandler handler : services) { try { Extension extension = handler.handle(name, value); if (extension != null) { results.add(extension); consumed = true; break; } } catch (IOException e) { YSLOG.error(e, "E99001", name, value); for (Extension ext : results) { try { ext.close(); } catch (IOException inner) { e.addSuppressed(inner); } } throw new IllegalArgumentException(MessageFormat.format( "invalid extended argument \"{0}\"", argument), e); } } if (consumed == false) { YSLOG.error("E99001", name, value); throw new IllegalArgumentException(MessageFormat.format( "there is no available extension handler \"{0}\"", argument)); } } return results; } private  CommandLineUtil(); static void prepareLogContext(); static Properties loadProperties(File path); static List<File> parseFileList(String fileListOrNull); static ClassLoader buildPluginLoader(ClassLoader parent, List<File> files); static List<Extension> loadExtensions(ClassLoader classLoader, List<ExtendedArgument> arguments); static final String LOG_CONTEXT_PREFIX; static final String SCHEME_CLASSPATH; }