@Test public void testGetInstance() throws Exception { List<TestCompatibilitySingletonFactoryCallable> callables = new ArrayList<TestCompatibilitySingletonFactoryCallable>(ITERATIONS); List<String> resultStrings = new ArrayList<String>(ITERATIONS); for (int i = 0; i < ITERATIONS; i++) { callables.add(new TestCompatibilitySingletonFactoryCallable()); } ExecutorService executorService = Executors.newFixedThreadPool(100); List<Future<String>> futures = executorService.invokeAll(callables); for (Future<String> f : futures) { resultStrings.add(f.get()); } String firstString = resultStrings.get(0); for (String s : resultStrings) { assertEquals(firstString, s); } assertNotEquals(new RandomStringGeneratorImpl().getRandString(), firstString); }
@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 (instance == null) { throw new RuntimeException(createExceptionString(klass)); } SingletonStorage.INSTANCE.instances.put(klass, instance); } return instance; } }
CompatibilitySingletonFactory extends CompatibilityFactory { @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 (instance == null) { throw new RuntimeException(createExceptionString(klass)); } SingletonStorage.INSTANCE.instances.put(klass, instance); } return instance; } } }
CompatibilitySingletonFactory extends CompatibilityFactory { @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 (instance == null) { throw new RuntimeException(createExceptionString(klass)); } SingletonStorage.INSTANCE.instances.put(klass, instance); } return instance; } } protected  CompatibilitySingletonFactory(); }
CompatibilitySingletonFactory extends CompatibilityFactory { @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 (instance == null) { throw new RuntimeException(createExceptionString(klass)); } SingletonStorage.INSTANCE.instances.put(klass, instance); } return instance; } } protected  CompatibilitySingletonFactory(); @SuppressWarnings("unchecked") static T getInstance(Class<T> klass); }
CompatibilitySingletonFactory extends CompatibilityFactory { @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 (instance == null) { throw new RuntimeException(createExceptionString(klass)); } SingletonStorage.INSTANCE.instances.put(klass, instance); } return instance; } } protected  CompatibilitySingletonFactory(); @SuppressWarnings("unchecked") static T getInstance(Class<T> klass);  }