List of usage examples for java.util ServiceLoader load
@CallerSensitive public static <S> ServiceLoader<S> load(Class<S> service)
From source file:org.apache.pig.ExecTypeProvider.java
public static ExecType selectExecType(Properties properties) throws PigException { ServiceLoader<ExecType> frameworkLoader = ServiceLoader.load(ExecType.class); for (ExecType execType : frameworkLoader) { log.info("Trying ExecType : " + execType); if (execType.accepts(properties)) { log.info("Picked " + execType + " as the ExecType"); return getSingleton(execType); } else {/*from w ww .j a v a2 s . c o m*/ log.debug("Cannot pick " + execType + " as the ExecType"); } } throw new PigException("Unknown exec type: " + properties.getProperty("exectype"), 2040); }
From source file:org.sventon.service.SVNProvider.java
/** * Loads the first available SVN provider on the classpath. * * @return Loaded SVN provider/*from www .j ava 2 s . c o m*/ */ public static SVNProvider loadProvider() { final ServiceLoader<SVNProvider> loader = ServiceLoader.load(SVNProvider.class); for (SVNProvider svnProvider : loader) { LOGGER.info("Found SVNProvider: " + svnProvider.getClass()); return svnProvider; } throw new IllegalStateException("Unable to find a SVNProvider"); }
From source file:org.apache.hadoop.hbase.CompatibilityFactory.java
public static synchronized <T> T getInstance(Class<T> klass) { T instance = null;/*from w w w . j av a 2 s. c o 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:de.shadowhunt.subversion.RepositoryFactory.java
/** * Create a new {@link RepositoryFactory} instance each time the method is called. * * @return the new {@link RepositoryFactory} instance * * @throws SubversionException if no {@link RepositoryFactory} can be created *//*from w w w .j a v a 2 s . c o m*/ @SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop") public static RepositoryFactory getInstance() throws SubversionException { for (final RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) { return factory; } throw new SubversionException("Can not find a RepositoryFactory"); }
From source file:therian.Therian.java
/** * Return an instance configured as {@link Therian#standard()} + {@link TherianModule}s discovered using the * {@link ServiceLoader} mechanism.//from www . j av a 2s . co m * * @return Therian */ public static synchronized Therian usingDiscoveredModules() { if (usingDiscoveredModules == null) { final List<TherianModule> modules = new ArrayList<>(); modules.add(DEFAULT_MODULE); for (TherianModule module : ServiceLoader.load(TherianModule.class)) { modules.add(module); } usingDiscoveredModules = new Therian(modules.toArray(new TherianModule[modules.size()])); } return usingDiscoveredModules; }
From source file:org.jbake.service.plugin.impl.DefaultJBakePluginService.java
private DefaultJBakePluginService() { //load all the classes in the classpath that have implemented the interface serviceLoader = ServiceLoader.load(AbstractJBakePlugin.class); }
From source file:org.cache2k.benchmark.impl2015.util.Log.java
public static synchronized Log getLog(String s) { Log l = loggers.get(s);/*ww w . ja v a 2 s. c om*/ if (l != null) { return l; } if (logFactory != null) { l = logFactory.getLog(s); loggers.put(s, l); return l; } ServiceLoader<LogFactory> loader = ServiceLoader.load(LogFactory.class); for (LogFactory lf : loader) { logFactory = lf; getLog(Log.class.getName()).debug("New instance, using: " + logFactory); return getLog(s); } try { final org.apache.commons.logging.LogFactory cl = org.apache.commons.logging.LogFactory.getFactory(); logFactory = new LogFactory() { @Override public Log getLog(String s) { return new CommonsLogger(cl.getInstance(s)); } }; getLog(Log.class.getName()).debug("New instance, using: " + logFactory); return getLog(s); } catch (NoClassDefFoundError ignore) { } logFactory = new LogFactory() { @Override public Log getLog(String s) { return new JdkLogger(Logger.getLogger(s)); } }; getLog(Log.class.getName()).debug("New instance, using: " + logFactory); return getLog(s); }
From source file:org.apache.geode.geospatial.utils.ToolBox.java
public static <T> T getService(Class clazz) { ServiceLoader<T> loader = ServiceLoader.load(clazz); T returnValue = null;//from w w w .j a v a 2 s.co m Iterator<T> it = loader.iterator(); //In theory the service providers are loaded lazily - so if there is a problem with a given service provider we // will log it and try the next. while (it.hasNext()) { try { returnValue = it.next(); if (returnValue != null) { break; } } catch (Exception e) { logger.error(e.getMessage(), e); logger.error("Trying to find another ", clazz.getName()); } } return returnValue; }
From source file:org.apache.hadoop.hbase.CompatibilitySingletonFactory.java
/** * Get the singleton instance of Any classes defined by compatibiliy jar's * * @return the singleton/*from w w w . j a va 2 s . co 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:com.fluidops.iwb.api.valueresolver.ValueResolverUtil.java
/** * This method initializes custom {@link ValueResolver} extensions * using the Java {@link ServiceLoader} mechanism. It is invoked * on startup of the application, and looks up all registered * {@link ValueResolverFactory} in META-INF. See {@link ValueResolver} * for details.// w w w. j a va2 s . c om */ public static void initializeValueResolverExtensions() { logger.info("Initializing value resolver extensions"); ServiceLoader<ValueResolverFactory> serviceLoader = ServiceLoader.load(ValueResolverFactory.class); Iterator<ValueResolverFactory> iter = serviceLoader.iterator(); while (iter.hasNext()) { ValueResolverFactory vrf = iter.next(); ValueResolver valueResolver = vrf.create(); logger.debug("Registered custom value resolver: " + valueResolver.name()); ValueResolverRegistry.getInstance().register(valueResolver); } }