rest/sse: Use constructor injection to simplify lifecycle (#838)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2019-05-28 17:46:20 +02:00 committed by Markus Rathgeb
parent 8b59505145
commit 3ab9bab2f4

View File

@ -15,10 +15,13 @@ package org.eclipse.smarthome.io.rest.sse.internal.listeners;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.events.Event; import org.eclipse.smarthome.core.events.Event;
import org.eclipse.smarthome.core.events.EventFilter; import org.eclipse.smarthome.core.events.EventFilter;
import org.eclipse.smarthome.core.events.EventSubscriber; import org.eclipse.smarthome.core.events.EventSubscriber;
import org.eclipse.smarthome.io.rest.sse.SseResource; import org.eclipse.smarthome.io.rest.sse.SseResource;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
@ -29,28 +32,25 @@ import org.osgi.service.component.annotations.Reference;
* @author Stefan Bußweiler - Initial contribution * @author Stefan Bußweiler - Initial contribution
*/ */
@Component @Component
@NonNullByDefault
public class SseEventSubscriber implements EventSubscriber { public class SseEventSubscriber implements EventSubscriber {
private final Set<String> subscribedEventTypes = Collections.singleton(EventSubscriber.ALL_EVENT_TYPES); private final Set<String> subscribedEventTypes = Collections.singleton(EventSubscriber.ALL_EVENT_TYPES);
private SseResource sseResource; private final SseResource sseResource;
@Reference @Activate
protected void setSseResource(SseResource sseResource) { public SseEventSubscriber(final @Reference SseResource sseResource) {
this.sseResource = sseResource; this.sseResource = sseResource;
} }
protected void unsetSseResource(SseResource sseResource) {
this.sseResource = null;
}
@Override @Override
public Set<String> getSubscribedEventTypes() { public Set<String> getSubscribedEventTypes() {
return subscribedEventTypes; return subscribedEventTypes;
} }
@Override @Override
public EventFilter getEventFilter() { public @Nullable EventFilter getEventFilter() {
return null; return null;
} }
@ -58,5 +58,4 @@ public class SseEventSubscriber implements EventSubscriber {
public void receive(Event event) { public void receive(Event event) {
sseResource.broadcastEvent(event); sseResource.broadcastEvent(event);
} }
} }