List of usage examples for java.util ServiceLoader iterator
public Iterator<S> iterator()
From source file:org.apache.hadoop.gateway.deploy.impl.ShiroDeploymentContributorTest.java
@Test public void testServiceLoader() throws Exception { ServiceLoader loader = ServiceLoader.load(ProviderDeploymentContributor.class); Iterator iterator = loader.iterator(); assertThat("Service iterator empty.", iterator.hasNext()); while (iterator.hasNext()) { Object object = iterator.next(); if (object instanceof ShiroDeploymentContributor) { return; }//from w w w.j a v a 2 s.com } fail("Failed to find " + ShiroDeploymentContributor.class.getName() + " via service loader."); }
From source file:org.apache.hadoop.gateway.hostmap.impl.HostmapDeploymentContributorTest.java
@Test public void testServiceLoader() throws Exception { ServiceLoader loader = ServiceLoader.load(ProviderDeploymentContributor.class); Iterator iterator = loader.iterator(); assertThat("Service iterator empty.", iterator.hasNext()); while (iterator.hasNext()) { Object object = iterator.next(); if (object instanceof HostmapDeploymentContributor) { return; }/*from w ww. j a v a 2 s. c o m*/ } fail("Failed to find " + HostmapDeploymentContributor.class.getName() + " via service loader."); }
From source file:org.apache.hadoop.hbase.CompatibilityFactory.java
public static synchronized <T> T getInstance(Class<T> klass) { T instance = null;//from www. j av a 2s .co m try { ServiceLoader<T> loader = ServiceLoader.load(klass); Iterator<T> it = loader.iterator(); instance = it.next(); if (it.hasNext()) { StringBuilder msg = new StringBuilder(); msg.append("ServiceLoader provided more than one implementation for class: ").append(klass) .append(", using implementation: ").append(instance.getClass()) .append(", other implementations: {"); while (it.hasNext()) { msg.append(it.next()).append(" "); } msg.append("}"); LOG.warn(msg); } } catch (Exception e) { throw new RuntimeException(createExceptionString(klass), e); } catch (Error e) { throw new RuntimeException(createExceptionString(klass), e); } // If there was nothing returned and no exception then throw an exception. if (instance == null) { throw new RuntimeException(createExceptionString(klass)); } return instance; }
From source file:org.apache.hadoop.hbase.CompatibilitySingletonFactory.java
/** * Get the singleton instance of Any classes defined by compatibiliy jar's * * @return the singleton/* w w w. ja v a2 s . c o m*/ */ @SuppressWarnings("unchecked") public static <T> T getInstance(Class<T> klass) { synchronized (SingletonStorage.INSTANCE.lock) { T instance = (T) SingletonStorage.INSTANCE.instances.get(klass); if (instance == null) { try { ServiceLoader<T> loader = ServiceLoader.load(klass); Iterator<T> it = loader.iterator(); instance = it.next(); if (it.hasNext()) { StringBuilder msg = new StringBuilder(); msg.append("ServiceLoader provided more than one implementation for class: ").append(klass) .append(", using implementation: ").append(instance.getClass()) .append(", other implementations: {"); while (it.hasNext()) { msg.append(it.next()).append(" "); } msg.append("}"); LOG.warn(msg); } } catch (Exception e) { throw new RuntimeException(createExceptionString(klass), e); } catch (Error e) { throw new RuntimeException(createExceptionString(klass), e); } // If there was nothing returned and no exception then throw an exception. if (instance == null) { throw new RuntimeException(createExceptionString(klass)); } SingletonStorage.INSTANCE.instances.put(klass, instance); } return instance; } }
From source file:org.apache.jackrabbit.oak.run.osgi.OakOSGiRepositoryFactory.java
private PojoServiceRegistry createServiceRegistry(Map<String, Object> config) { try {//from www. j a va 2s . c o m ServiceLoader<PojoServiceRegistryFactory> loader = ServiceLoader.load(PojoServiceRegistryFactory.class); return loader.iterator().next().newPojoServiceRegistry(config); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.cellprofiler.preferences.CellProfilerPreferences.java
/** * Get the preferences factory supplied by the JRE or * provided as a service./* w ww. ja va 2 s .c o m*/ * * @return the default preferences factory. */ static private PreferencesFactory getJREPreferencesFactory() { synchronized (lock) { if (delegatePreferencesFactory == null) { do { /* * First, see if there is a PreferencesFactory * provided as a service. */ final ServiceLoader<PreferencesFactory> pfServiceLoader = ServiceLoader .loadInstalled(PreferencesFactory.class); final Iterator<PreferencesFactory> pfIter = pfServiceLoader.iterator(); if (pfIter.hasNext()) { delegatePreferencesFactory = pfIter.next(); break; } /* * Next, try the WindowsPreferencesFactory if OS is Windows. */ String pfName = (SystemUtils.IS_OS_WINDOWS) ? "java.util.prefs.WindowsPreferencesFactory" : "java.util.prefs.FilePreferencesFactory"; try { Class<?> pfClass = Class.forName("java.util.prefs.WindowsPreferencesFactory", false, null); Class<?> pfFuckYou = Class.forName("java.util.prefs.WindowsPreferences", true, null); Constructor<?>[] pfConstructors = pfClass.getDeclaredConstructors(); for (Constructor<?> c : pfConstructors) { if (c.getParameterTypes().length == 0) { /* * Bad boy - it's package-private AND I CALL IT ANYWAY BAH HA HA HA HA HA HA */ c.setAccessible(true); delegatePreferencesFactory = (PreferencesFactory) c.newInstance(new Object[0]); break; } } break; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * And as a last resort, there's always our headless * preferences factory. */ delegatePreferencesFactory = new HeadlessPreferencesFactory(); } while (false); } } return delegatePreferencesFactory; }
From source file:org.commonjava.maven.cartographer.ftest.AbstractCartographerTCK.java
@Before public void before() throws Exception { final ServiceLoader<CartoTCKDriver> driverLoader = ServiceLoader.load(CartoTCKDriver.class); final Iterator<CartoTCKDriver> driverIter = driverLoader.iterator(); if (!driverIter.hasNext()) { throw new IllegalStateException("No TCK driver found!"); }//from www .j av a 2 s .c om driver = driverIter.next(); carto = driver.start(temp); }
From source file:org.commonjava.maven.cartographer.ftest.LocalTestDriver.java
@Override public Cartographer start(TemporaryFolder temp) throws Exception { sourceManager = new SourceManagerImpl(); temp.create();//w ww .ja v a 2 s. c o m File homeDir = temp.newFolder("carto-home"); File configDir = new File(homeDir, "etc"); configDir.mkdirs(); configurator.accept(configDir); final ServiceLoader<BootOptions> loader = ServiceLoader.load(BootOptions.class); final BootOptions opts = loader.iterator().next(); options = (Options) opts; options.setPort(-1); options.setHomeDir(homeDir.getAbsolutePath()); // Should not need this; the configurator should be smart enough to try ${carto.home}/etc/main.conf on its own. // options.setConfig( new File( configDir, MAIN_CONF ).getAbsolutePath() ); booter = new Booter(); bootStatus = booter.start(options); if (bootStatus == null) { fail("No boot status"); } Throwable t = bootStatus.getError(); if (t != null) { throw new RuntimeException("Failed to start Cartographer test server.", t); } assertThat(bootStatus.isSuccess(), equalTo(true)); WeldContainer container = booter.getContainer(); sourceManager = container.instance().select(SourceManagerImpl.class).get(); passwordManager = new MemoryPasswordManager(); siteConfig = new SiteConfigBuilder("local-test", formatUrl().toString()).withRequestTimeoutSeconds(30) .build(); httpFactory = new HttpFactory(passwordManager); carto = new ClientCartographer(siteConfig, httpFactory); return carto; }
From source file:org.commonjava.test.compile.CompilerFixxxtureTest.java
@Test public void compileDependingOnlyOnJDK_UseServiceLoader() throws Exception { final CompilerResult result = compiler.compileSourceDirWithThisClass("jdk-only-service", "org.test.Hello"); FileUtils.write(Paths.get(result.getClasses().getAbsolutePath(), "META-INF", "services", "org.test.IHello") .toFile(), "org.test.Hello"); final URLClassLoader ucl = result.getClassLoader(); final Class<?> cls = ucl.loadClass("org.test.IHello"); final ServiceLoader<?> loader = ServiceLoader.load(cls, ucl); final Object object = loader.iterator().next(); final Method method = cls.getMethod("hello", new Class[] { String[].class }); System.out.println(method);/*from ww w.ja v a2s . c o m*/ method.invoke(object, new Object[] { new String[] { "Tester" } }); }
From source file:org.deegree.services.csw.CSWController.java
private GetRecordByIdHandler getGetRecordByIdHandler() { ServiceLoader<GetRecordByIdHandler> serviceLoader = ServiceLoader.load(GetRecordByIdHandler.class); Iterator<GetRecordByIdHandler> iterator = serviceLoader.iterator(); if (iterator.hasNext()) return iterator.next(); return new DefaultGetRecordByIdHandler(); }