add PingerRegistryComponent and FetcherComponent.java

This commit is contained in:
Andriy Kryvtsun 2015-08-05 18:39:46 -04:00
parent c15e29e3a8
commit e417e55e15
11 changed files with 105 additions and 60 deletions

View File

@ -41,7 +41,7 @@ public class ComponentRegistry {
private boolean containerStarted;
@Inject
public ComponentRegistry(java.util.List<Class> plugins, java.util.List<Class> fetchers) {
public ComponentRegistry(java.util.List<Class> plugins) {
MutablePicoContainer container = new DefaultPicoContainer();
this.container = container;
@ -60,12 +60,9 @@ public class ComponentRegistry {
container.registerComponentImplementation(ConfigDetector.class);
container.registerComponentInstance(DaggerExporterComponent.create().get());
// container.registerComponentImplementation(ExporterRegistry.class);
// registerComponentImplementations(container, exporters);
container.registerComponentImplementation(TXTExporter.class);
container.registerComponentImplementation(FetcherRegistry.class, FetcherRegistry.class);
registerComponentImplementations(container, fetchers);
container.registerComponentInstance(DaggerFetcherComponent.create().get());
container.registerComponentImplementation(PingerRegistry.class, PingerRegistry.class);
container.registerComponentImplementation(ScanningResultList.class);

View File

@ -3,6 +3,9 @@
*/
package net.azib.ipscan.config;
import dagger.Module;
import dagger.Provides;
import java.util.Locale;
import java.util.prefs.Preferences;

View File

@ -7,14 +7,31 @@ import net.azib.ipscan.exporters.Exporter;
import javax.inject.Named;
import java.util.List;
import java.util.prefs.Preferences;
/**
* Created by englishman on 6/6/15.
*/
@Module
public class ConfigModule {
@Provides
public ComponentRegistry getRegistry(@Named("plugins") List<Class> plugins, @Named("fetchers") List<Class> fetchers) {
return new ComponentRegistry(plugins, fetchers);
public Preferences providePreferences() {
return Config.getConfig().getPreferences();
}
@Provides
public ScannerConfig provideScannerConfig() {
return Config.getConfig().forScanner();
}
@Provides
public CommentsConfig provideCommentsConfig(Preferences preferences) {
return new CommentsConfig(preferences);
}
@Provides
public ComponentRegistry getRegistry(List<Class> plugins) {
return new ComponentRegistry(plugins);
}
}

View File

@ -36,7 +36,7 @@ import java.util.logging.Logger;
public class PluginLoader {
private static final Logger LOG = LoggerFactory.getLogger();
@Provides @Named("plugins")
@Provides
public List<Class> getClasses() {
List<Class> container = new ArrayList<Class>();

View File

@ -0,0 +1,15 @@
package net.azib.ipscan.core.net;
import dagger.Component;
import net.azib.ipscan.config.Config;
import net.azib.ipscan.config.ConfigModule;
import net.azib.ipscan.fetchers.FetcherModule;
import net.azib.ipscan.fetchers.FetcherRegistry;
/**
* @author Andriy Kryvtsun
*/
@Component(modules = {PingerRegistryModule.class, ConfigModule.class})
public interface PingerRegistryComponent {
PingerRegistry get();
}

View File

@ -0,0 +1,17 @@
package net.azib.ipscan.core.net;
import dagger.Module;
import dagger.Provides;
import net.azib.ipscan.config.ScannerConfig;
/**
* @author Andriy Kryvtsun
*/
@Module
public class PingerRegistryModule {
@Provides
public PingerRegistry providePingerRegistry(ScannerConfig scannerConfig) {
return new PingerRegistry(scannerConfig);
}
}

View File

@ -13,39 +13,12 @@ import java.util.List;
public class ExporterModule {
@Provides
public Exporter[] getDefaultExporters() {
public Exporter[] provideExporters() {
return new Exporter[] {new TXTExporter(), new CSVExporter(), new XMLExporter(), new IPListExporter()};
}
// @Provides
// public TXTExporter getTXTExporter() {
// return new TXTExporter();
// }
// @Provides
// public Class<Exporter>[] getDefaultExporters() {
// return new Class[] {TXTExporter.class, CSVExporter.class, XMLExporter.class, IPListExporter.class};
// }
//
// @Provides
// public ExporterRegistry getExporterRegistry(Class<Exporter>[] exporters) {
// List<Exporter> list = new ArrayList<Exporter>();
// for (Class<Exporter> ec: exporters) {
// try {
// list.add(ec.newInstance());
// }
// catch (InstantiationException e) {
// e.printStackTrace();
// }
// catch (IllegalAccessException e) {
// e.printStackTrace();
// }
// }
// return new ExporterRegistry(list.toArray(new Exporter[exporters.length]));
// }
@Provides
public ExporterRegistry getExporterRegistry(Exporter[] exporters) {
public ExporterRegistry provideExporterRegistry(Exporter[] exporters) {
return new ExporterRegistry(exporters);
}
}

View File

@ -0,0 +1,13 @@
package net.azib.ipscan.fetchers;
import dagger.Component;
import net.azib.ipscan.config.ConfigModule;
import net.azib.ipscan.core.net.PingerRegistryModule;
/**
* @author Andriy Kryvtsun
*/
@Component(modules = {FetcherModule.class, ConfigModule.class, PingerRegistryModule.class})
public interface FetcherComponent {
FetcherRegistry get();
}

View File

@ -2,12 +2,16 @@ package net.azib.ipscan.fetchers;
import dagger.Module;
import dagger.Provides;
import net.azib.ipscan.config.CommentsConfig;
import net.azib.ipscan.config.Platform;
import net.azib.ipscan.config.ScannerConfig;
import net.azib.ipscan.core.net.PingerRegistry;
import net.azib.ipscan.exporters.*;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
/**
* @author Andriy Kryvtsun
@ -15,24 +19,29 @@ import java.util.List;
@Module
public class FetcherModule {
@Provides @Named("fetchers")
public List<Class> provideClasses() {
@Provides
public Fetcher[] provideFetchers(PingerRegistry pingerRegistry, ScannerConfig scannerConfig, CommentsConfig commentsConfig) {
List<Class> classes = new ArrayList<Class>();
MACFetcher macFetcher = Platform.WINDOWS ? new WinMACFetcher() : new UnixMACFetcher();
classes.add(IPFetcher.class);
classes.add(PingFetcher.class);
classes.add(PingTTLFetcher.class);
classes.add(HostnameFetcher.class);
classes.add(PortsFetcher.class);
classes.add(FilteredPortsFetcher.class);
classes.add(WebDetectFetcher.class);
classes.add(HTTPSenderFetcher.class);
classes.add(CommentFetcher.class);
classes.add(NetBIOSInfoFetcher.class);
classes.add(Platform.WINDOWS ? WinMACFetcher.class : UnixMACFetcher.class);
classes.add(MACVendorFetcher.class);
return new Fetcher[] {
new IPFetcher(),
new PingFetcher(pingerRegistry, scannerConfig),
new PingTTLFetcher(pingerRegistry, scannerConfig),
new HostnameFetcher(),
new PortsFetcher(scannerConfig),
new FilteredPortsFetcher(scannerConfig),
new WebDetectFetcher(scannerConfig),
new HTTPSenderFetcher(scannerConfig),
new CommentFetcher(commentsConfig),
new NetBIOSInfoFetcher(),
macFetcher,
new MACVendorFetcher(macFetcher),
};
}
return classes;
@Provides
public FetcherRegistry provideFetcherRegistry(Fetcher[] fetchers, Preferences preferences) {
return new FetcherRegistry(fetchers, preferences);
}
}

View File

@ -29,14 +29,15 @@ public class FetcherRegistry {
private Map<String, Fetcher> selectedFetchers;
/** PicoContainer for FetcherPrefs components */
// TODO remove local PicoContainer usage here
private PicoContainer prefsContainer;
/** A collection of update listeners - observers of FetcherRegistry */
private List<FetcherRegistryUpdateListener> updateListeners = new ArrayList<FetcherRegistryUpdateListener>();
public FetcherRegistry(Fetcher[] registeredFetchers, Preferences preferences, PicoContainer parentContainer) {
public FetcherRegistry(Fetcher[] registeredFetchers, Preferences preferences) {
this.preferences = preferences;
MutablePicoContainer prefsContainer = new DefaultPicoContainer(parentContainer);
MutablePicoContainer prefsContainer = new DefaultPicoContainer();
this.registeredFetchers = new LinkedHashMap<String, Fetcher>(registeredFetchers.length);
for (Fetcher fetcher : registeredFetchers) {

View File

@ -39,7 +39,7 @@ public class FetcherRegistryTest {
hostnameFetcher = new HostnameFetcher();
commentFetcher = new CommentFetcher(null);
portsFetcher = new PortsFetcher(null);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, pingFetcher, hostnameFetcher, commentFetcher, portsFetcher}, preferences, null);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, pingFetcher, hostnameFetcher, commentFetcher, portsFetcher}, preferences);
}
@After
@ -72,18 +72,18 @@ public class FetcherRegistryTest {
@Test
public void testLoadPreferences() throws Exception {
preferences.remove(FetcherRegistry.PREFERENCE_SELECTED_FETCHERS);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences, null);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
assertEquals(4, fetcherRegistry.getSelectedFetchers().size());
preferences.put(FetcherRegistry.PREFERENCE_SELECTED_FETCHERS, hostnameFetcher.getId() + "###" + commentFetcher.getId());
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences, null);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, hostnameFetcher, commentFetcher}, preferences);
assertEquals(2, fetcherRegistry.getSelectedFetchers().size());
Iterator<?> iterator = fetcherRegistry.getSelectedFetchers().iterator();
assertSame(hostnameFetcher, iterator.next());
assertSame(commentFetcher, iterator.next());
preferences.put(FetcherRegistry.PREFERENCE_SELECTED_FETCHERS, "not-existing-fetcher###" + hostnameFetcher.getId());
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, hostnameFetcher}, preferences, null);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, hostnameFetcher}, preferences);
assertEquals(1, fetcherRegistry.getSelectedFetchers().size());
}
@ -126,7 +126,7 @@ public class FetcherRegistryTest {
container.registerComponentInstance(message);
Fetcher editableFetcher = new EditableFetcher();
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, editableFetcher}, preferences, container);
fetcherRegistry = new FetcherRegistry(new Fetcher[] {ipFetcher, editableFetcher}, preferences);
EditableFetcherPrefs.calledWithMessage = null;
fetcherRegistry.openPreferencesEditor(editableFetcher);