DefaultSourceFactory implements SourceFactory { @SuppressWarnings("unchecked") @Override public synchronized Source create(String name, String type) throws FlumeException { Preconditions.checkNotNull(name); Preconditions.checkNotNull(type); logger.debug("Creating instance of source {}, type {}", name, type); String sourceClassName = type; SourceType srcType = SourceType.OTHER; try { srcType = SourceType.valueOf(type.toUpperCase()); } catch (IllegalArgumentException ex) { logger.debug("Source type {} is a custom type", type); } if (!srcType.equals(SourceType.OTHER)) { sourceClassName = srcType.getSourceClassName(); } Class<? extends Source> sourceClass = null; try { sourceClass = (Class<? extends Source>) Class.forName(sourceClassName); } catch (Exception ex) { throw new FlumeException("Unable to load source type: " + type + ", class: " + sourceClassName, ex); } Map<String, Source> sourceMap = sources.get(sourceClass); if (sourceMap == null) { sourceMap = new HashMap<String, Source>(); sources.put(sourceClass, sourceMap); } Source source = sourceMap.get(name); if (source == null) { try { source = sourceClass.newInstance(); source.setName(name); sourceMap.put(name, source); } catch (Exception ex) { sources.remove(sourceClass); throw new FlumeException("Unable to create source: " + name +", type: " + type + ", class: " + sourceClassName, ex); } } return source; } DefaultSourceFactory(); @Override synchronized boolean unregister(Source source); @SuppressWarnings("unchecked") @Override synchronized Source create(String name, String type); synchronized Map<Class<?>, Map<String, Source>> getRegistryClone();  }
@Test public void testDuplicateCreate() { Source avroSource1 = sourceFactory.create("avroSource1", "avro"); Source avroSource2 = sourceFactory.create("avroSource2", "avro"); Assert.assertNotNull(avroSource1); Assert.assertNotNull(avroSource2); Assert.assertNotSame(avroSource1, avroSource2); Assert.assertTrue(avroSource1 instanceof AvroSource); Assert.assertTrue(avroSource2 instanceof AvroSource); Source s1 = sourceFactory.create("avroSource1", "avro"); Source s2 = sourceFactory.create("avroSource2", "avro"); Assert.assertSame(avroSource1, s1); Assert.assertSame(avroSource2, s2); }