0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A//
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/A
0N/Apublic class Load {
0N/A
0N/A private static PrintStream out = System.err;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A boolean installedOnly = false;
0N/A
0N/A List<String> expected = new ArrayList<String>(Arrays.asList(args));
0N/A if (!expected.isEmpty() && expected.get(0).equals("-i")) {
0N/A expected.remove(0);
0N/A installedOnly = true;
0N/A }
0N/A if (expected.isEmpty())
0N/A throw new Exception("usage: Load [-i] ( fail | provider-class-name )*");
0N/A
0N/A ServiceLoader<FooService> sl = (installedOnly
0N/A ? ServiceLoader.loadInstalled(FooService.class)
0N/A : ServiceLoader.load(FooService.class));
0N/A out.format("%s%n", sl);
0N/A Iterator<FooService> sli = sl.iterator();
0N/A Iterator<String> ei = expected.iterator();
0N/A
0N/A for (;; ei.remove()) {
0N/A FooService fp = null;
0N/A try {
0N/A if (!sli.hasNext())
0N/A break;
0N/A fp = sli.next();
0N/A } catch (ServiceConfigurationError x) {
0N/A if (ei.next().equals("fail")) {
0N/A out.format("Failed as expected: %s%n", x);
0N/A continue;
0N/A }
0N/A throw x;
0N/A }
0N/A String ec = ei.next();
0N/A if (!fp.getClass().getName().equals(ec))
0N/A throw new
0N/A Exception(String.format("Wrong provider %s; expected %s",
0N/A fp.getClass().getName(), ec));
0N/A out.format("Provider found: %s%n", fp.getClass().getName());
0N/A }
0N/A
0N/A if (ei.hasNext())
0N/A throw new Exception("Missing providers: " + expected);
0N/A
0N/A }
0N/A
0N/A}