Upgrade to Karaf 4.4.3 (#14040)
* Rework Servlets to use Http Whiteboard annotations in favor of proprietary `org.openhab.core.io.http.servlet` classes * Resolve itest runbundles * Fix dependency issues * Catch proper exception when starting hueemulation UpnpServer Also-by: Jan N. Klug <github@klug.nrw> Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
@@ -104,7 +104,7 @@ public class WundergroundUpdateReceiverDiscoveryService extends AbstractDiscover
|
||||
setScanning(true);
|
||||
if (servletControls != null && !servletControls.isActive()) {
|
||||
servletWasInactive = true;
|
||||
servletControls.activate();
|
||||
servletControls.enable();
|
||||
}
|
||||
thinglessStationIds.keySet().forEach(this::createDiscoveryResult);
|
||||
}
|
||||
@@ -114,7 +114,7 @@ public class WundergroundUpdateReceiverDiscoveryService extends AbstractDiscover
|
||||
super.stopScan();
|
||||
thinglessStationIds.keySet().forEach(this::createDiscoveryResult);
|
||||
if (!isBackgroundDiscoveryEnabled() && servletControls != null && servletWasInactive) {
|
||||
servletControls.deactivate();
|
||||
servletControls.disable();
|
||||
}
|
||||
setScanning(false);
|
||||
}
|
||||
|
||||
@@ -42,24 +42,21 @@ public class WundergroundUpdateReceiverHandlerFactory extends BaseThingHandlerFa
|
||||
private final WundergroundUpdateReceiverDiscoveryService discoveryService;
|
||||
private final ChannelTypeRegistry channelTypeRegistry;
|
||||
private final WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider;
|
||||
private final WundergroundUpdateReceiverServlet wunderGroundUpdateReceiverServlet;
|
||||
private final ManagedThingProvider managedThingProvider;
|
||||
private final WundergroundUpdateReceiverServlet wunderGroundUpdateReceiverServlet;
|
||||
|
||||
@Activate
|
||||
public WundergroundUpdateReceiverHandlerFactory(@Reference HttpService httpService,
|
||||
@Reference WundergroundUpdateReceiverDiscoveryService discoveryService,
|
||||
@Reference WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider,
|
||||
@Reference ChannelTypeRegistry channelTypeRegistry, @Reference ManagedThingProvider managedThingProvider) {
|
||||
@Reference ChannelTypeRegistry channelTypeRegistry, @Reference ManagedThingProvider managedThingProvider,
|
||||
@Reference WundergroundUpdateReceiverServlet wunderGroundUpdateReceiverServlet) {
|
||||
this.discoveryService = discoveryService;
|
||||
this.channelTypeRegistry = channelTypeRegistry;
|
||||
this.channelTypeProvider = channelTypeProvider;
|
||||
this.managedThingProvider = managedThingProvider;
|
||||
this.wunderGroundUpdateReceiverServlet = new WundergroundUpdateReceiverServlet(httpService,
|
||||
this.discoveryService);
|
||||
this.discoveryService.servletControls = this.wunderGroundUpdateReceiverServlet;
|
||||
if (this.discoveryService.isBackgroundDiscoveryEnabled()) {
|
||||
this.wunderGroundUpdateReceiverServlet.activate();
|
||||
}
|
||||
this.wunderGroundUpdateReceiverServlet = wunderGroundUpdateReceiverServlet;
|
||||
this.discoveryService.servletControls = wunderGroundUpdateReceiverServlet;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,8 +79,7 @@ public class WundergroundUpdateReceiverHandlerFactory extends BaseThingHandlerFa
|
||||
|
||||
@Override
|
||||
protected void deactivate(ComponentContext componentContext) {
|
||||
this.wunderGroundUpdateReceiverServlet.deactivate();
|
||||
this.wunderGroundUpdateReceiverServlet.dispose();
|
||||
this.wunderGroundUpdateReceiverServlet.disable();
|
||||
super.deactivate(componentContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,25 +20,26 @@ import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.Dictionary;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.core.io.http.servlet.BaseOpenHABServlet;
|
||||
import org.osgi.service.http.HttpContext;
|
||||
import org.osgi.service.http.HttpService;
|
||||
import org.osgi.service.http.NamespaceException;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Deactivate;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletName;
|
||||
import org.osgi.service.http.whiteboard.propertytypes.HttpWhiteboardServletPattern;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -49,7 +50,10 @@ import org.slf4j.LoggerFactory;
|
||||
* @author Daniel Demus - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class WundergroundUpdateReceiverServlet extends BaseOpenHABServlet
|
||||
@HttpWhiteboardServletName(WundergroundUpdateReceiverServlet.SERVLET_URL)
|
||||
@HttpWhiteboardServletPattern(WundergroundUpdateReceiverServlet.SERVLET_URL)
|
||||
@Component(immediate = true, service = { Servlet.class, WundergroundUpdateReceiverServlet.class })
|
||||
public class WundergroundUpdateReceiverServlet extends HttpServlet
|
||||
implements WundergroundUpdateReceiverServletControls {
|
||||
|
||||
public static final String SERVLET_URL = "/weatherstation/updateweatherstation.php";
|
||||
@@ -65,10 +69,12 @@ public class WundergroundUpdateReceiverServlet extends BaseOpenHABServlet
|
||||
private boolean active = false;
|
||||
private String errorDetail = "";
|
||||
|
||||
public WundergroundUpdateReceiverServlet(HttpService httpService,
|
||||
WundergroundUpdateReceiverDiscoveryService discoveryService) {
|
||||
super(httpService);
|
||||
@Activate
|
||||
public WundergroundUpdateReceiverServlet(
|
||||
final @Reference WundergroundUpdateReceiverDiscoveryService discoveryService) {
|
||||
this.discoveryService = discoveryService;
|
||||
errorDetail = "";
|
||||
active = discoveryService.isBackgroundDiscoveryEnabled();
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
@@ -87,10 +93,6 @@ public class WundergroundUpdateReceiverServlet extends BaseOpenHABServlet
|
||||
return this.handlers.keySet();
|
||||
}
|
||||
|
||||
public void activate() {
|
||||
activate(SERVLET_URL, httpService.createDefaultHttpContext());
|
||||
}
|
||||
|
||||
public void addHandler(WundergroundUpdateReceiverHandler handler) {
|
||||
synchronized (this.handlers) {
|
||||
if (this.handlers.containsKey(handler.getStationId())) {
|
||||
@@ -102,7 +104,7 @@ public class WundergroundUpdateReceiverServlet extends BaseOpenHABServlet
|
||||
this.handlers.put(handler.getStationId(), handler);
|
||||
errorDetail = "";
|
||||
if (!isActive()) {
|
||||
activate();
|
||||
enable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,22 +116,20 @@ public class WundergroundUpdateReceiverServlet extends BaseOpenHABServlet
|
||||
this.handlers.remove(stationId);
|
||||
}
|
||||
if (this.handlers.isEmpty() && !this.discoveryService.isBackgroundDiscoveryEnabled()) {
|
||||
deactivate();
|
||||
disable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deactivate() {
|
||||
synchronized (LOCK) {
|
||||
logger.debug("Stopping servlet {} at {}", getClass().getSimpleName(), SERVLET_URL);
|
||||
try {
|
||||
super.deactivate(SERVLET_URL);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// SERVLET_URL is already unregistered
|
||||
}
|
||||
errorDetail = "";
|
||||
active = false;
|
||||
}
|
||||
@Override
|
||||
public void enable() {
|
||||
active = true;
|
||||
}
|
||||
|
||||
@Deactivate
|
||||
public void disable() {
|
||||
errorDetail = "";
|
||||
active = false;
|
||||
}
|
||||
|
||||
public void handlerConfigUpdated(WundergroundUpdateReceiverHandler handler) {
|
||||
@@ -150,28 +150,7 @@ public class WundergroundUpdateReceiverServlet extends BaseOpenHABServlet
|
||||
synchronized (this.handlers) {
|
||||
Set<String> stationIds = new HashSet<>(getStationIds());
|
||||
stationIds.forEach(this::removeHandler);
|
||||
deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void activate(String alias, HttpContext httpContext) {
|
||||
synchronized (LOCK) {
|
||||
try {
|
||||
logger.debug("Starting servlet {} at {}", getClass().getSimpleName(), alias);
|
||||
Dictionary<String, String> props = new Hashtable<>(1, 10);
|
||||
httpService.registerServlet(alias, this, props, httpContext);
|
||||
errorDetail = "";
|
||||
active = true;
|
||||
} catch (NamespaceException e) {
|
||||
active = false;
|
||||
errorDetail = "Servlet couldn't be registered - alias " + alias + " already in use";
|
||||
logger.warn("Error during servlet registration - alias {} already in use", alias, e);
|
||||
} catch (ServletException e) {
|
||||
active = false;
|
||||
errorDetail = "Servlet couldn't be registered - " + e.getMessage();
|
||||
logger.warn("Error during servlet registration", e);
|
||||
}
|
||||
disable();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
@NonNullByDefault
|
||||
public interface WundergroundUpdateReceiverServletControls {
|
||||
|
||||
void activate();
|
||||
void enable();
|
||||
|
||||
void deactivate();
|
||||
void disable();
|
||||
|
||||
boolean isActive();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user