switched RacingEventServiceImpl to WindTrackerFactory discovery through OSGi service lookup

This commit is contained in:
Axel Uhl committed 2013-12-09 17:33:57 +01:00
1 parent 1749cd54dd
commit b1678d914c
2 files changed
+45 -13

No files matched your search

@@ -19,7 +19,7 @@ public class Activator implements BundleActivator {
private static ExtenderBundleTracker extenderBundleTracker;
private final RacingEventService racingEventService;
private final RacingEventServiceImpl racingEventService;
public Activator() {
boolean clearPersistentCompetitors = Boolean.valueOf(System.getProperty(CLEAR_PERSISTENT_COMPETITORS_PROPERTY_NAME, ""+true));
@@ -33,6 +33,7 @@ public class Activator implements BundleActivator {
extenderBundleTracker.open();
// register the racing service in the OSGi registry
racingEventService.setBundleContext(context);
context.registerService(RacingEventService.class.getName(), racingEventService, null);
logger.log(Level.INFO, "Started "+context.getBundle().getSymbolicName()+". Character encoding: "+
@@ -27,6 +27,9 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import com.sap.sailing.domain.base.Competitor;
import com.sap.sailing.domain.base.CompetitorStore;
import com.sap.sailing.domain.base.ControlPoint;
@@ -161,8 +164,6 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
private final com.sap.sailing.domain.base.DomainFactory baseDomainFactory;
private final WindTrackerFactory windTrackerFactory;
/**
* Holds the {@link Event} objects for those event registered with this service. Note that there may be
* {@link Event} objects that exist outside this service for events not (yet) registered here.
@@ -221,6 +222,12 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
private final WindStore windStore;
/**
* If this service runs in the context of an OSGi environment, the activator should {@link #setBundleContext set the bundle context} on this
* object so that service lookups become possible.
*/
private BundleContext bundleContext;
/**
* Constructs a {@link DomainFactory base domain factory} that uses this object's {@link #competitorStore
* competitor store} for competitor management. This base domain factory is then also used for the construction of
@@ -236,6 +243,10 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
this(true, windStore);
}
void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
/**
* Like {@link #RacingEventServiceImpl()}, but allows callers to specify that the persistent competitor collection
* be cleared before the service starts.
@@ -272,7 +283,6 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
this.baseDomainFactory = baseDomainFactory;
this.domainObjectFactory = domainObjectFactory;
this.mongoObjectFactory = mongoObjectFactory;
windTrackerFactory = ExpeditionWindTrackerFactory.getInstance();
regattasByName = new ConcurrentHashMap<String, Regatta>();
eventsById = new ConcurrentHashMap<Serializable, Event>();
regattaTrackingCache = new ConcurrentHashMap<Regatta, DynamicTrackedRegatta>();
@@ -287,7 +297,6 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
this.raceLogScoringReplicator = new RaceLogScoringReplicator(this);
this.mediaDB = mediaDb;
this.mediaLibrary = new MediaLibrary();
if (windStore == null) {
try {
windStore = MongoWindStoreFactory.INSTANCE.getMongoWindStore(mongoObjectFactory, domainObjectFactory);
@@ -310,7 +319,7 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
loadMediaLibary();
loadStoredDeviceConfigurations();
}
@Override
public com.sap.sailing.domain.base.DomainFactory getBaseDomainFactory() {
return baseDomainFactory;
@@ -1329,14 +1338,18 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
@Override
public void startTrackingWind(Regatta regatta, RaceDefinition race, boolean correctByDeclination) throws Exception {
windTrackerFactory.createWindTracker(getOrCreateTrackedRegatta(regatta), race, correctByDeclination);
for (WindTrackerFactory windTrackerFactory : getWindTrackerFactories()) {
windTrackerFactory.createWindTracker(getOrCreateTrackedRegatta(regatta), race, correctByDeclination);
}
}
@Override
public void stopTrackingWind(Regatta regatta, RaceDefinition race) throws SocketException, IOException {
WindTracker windTracker = windTrackerFactory.getExistingWindTracker(race);
if (windTracker != null) {
windTracker.stop();
for (WindTrackerFactory windTrackerFactory : getWindTrackerFactories()) {
WindTracker windTracker = windTrackerFactory.getExistingWindTracker(race);
if (windTracker != null) {
windTracker.stop();
}
}
}
@@ -1345,9 +1358,11 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
List<Triple<Regatta, RaceDefinition, String>> result = new ArrayList<Triple<Regatta, RaceDefinition, String>>();
for (Regatta regatta : getAllRegattas()) {
for (RaceDefinition race : regatta.getAllRaces()) {
WindTracker windTracker = windTrackerFactory.getExistingWindTracker(race);
if (windTracker != null) {
result.add(new Triple<Regatta, RaceDefinition, String>(regatta, race, windTracker.toString()));
for (WindTrackerFactory windTrackerFactory : getWindTrackerFactories()) {
WindTracker windTracker = windTrackerFactory.getExistingWindTracker(race);
if (windTracker != null) {
result.add(new Triple<Regatta, RaceDefinition, String>(regatta, race, windTracker.toString()));
}
}
}
}
@@ -2058,4 +2073,20 @@ public class RacingEventServiceImpl implements RacingEventService, RegattaListen
return null;
}
private Iterable<WindTrackerFactory> getWindTrackerFactories() {
final Set<WindTrackerFactory> result;
if (bundleContext == null) {
result = Collections.singleton((WindTrackerFactory) ExpeditionWindTrackerFactory.getInstance());
} else {
ServiceTracker<WindTrackerFactory, WindTrackerFactory> tracker = new ServiceTracker<WindTrackerFactory, WindTrackerFactory>(
bundleContext, WindTrackerFactory.class.getName(), null);
tracker.open();
result = new HashSet<>();
for (WindTrackerFactory factory : tracker.getServices(new WindTrackerFactory[0])) {
result.add(factory);
}
}
return result;
}
}