From 8797846be307bf6a74c21d626016c33ae2dc8f9f Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Fri, 9 Jun 2017 12:38:46 +0200 Subject: [PATCH] moved the swagger servlet registration to its own component (#156) This should avoid potential circular dependencies at startup as the dashboard tile does not depend on HttpService anymore. Signed-off-by: Kai Kreuzer --- .../OSGI-INF/dashboardtile.xml | 3 +- .../OSGI-INF/swaggerservice.xml | 15 ++++++ .../rest/docs/internal/RESTDashboardTile.java | 27 +--------- .../io/rest/docs/internal/SwaggerService.java | 50 +++++++++++++++++++ 4 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 bundles/org.openhab.io.rest.docs/OSGI-INF/swaggerservice.xml create mode 100644 bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/SwaggerService.java diff --git a/bundles/org.openhab.io.rest.docs/OSGI-INF/dashboardtile.xml b/bundles/org.openhab.io.rest.docs/OSGI-INF/dashboardtile.xml index 34fd8f10ebdb7..20e178abac454 100644 --- a/bundles/org.openhab.io.rest.docs/OSGI-INF/dashboardtile.xml +++ b/bundles/org.openhab.io.rest.docs/OSGI-INF/dashboardtile.xml @@ -1,7 +1,7 @@ + + + + diff --git a/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/RESTDashboardTile.java b/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/RESTDashboardTile.java index 5bd5053ffab11..98dcc40074209 100644 --- a/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/RESTDashboardTile.java +++ b/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/RESTDashboardTile.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2015-2016 by the respective copyright holders. + * Copyright (c) 2015-2017 by the respective copyright holders. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -9,44 +9,19 @@ package org.openhab.io.rest.docs.internal; import org.openhab.ui.dashboard.DashboardTile; -import org.osgi.service.http.HttpService; -import org.osgi.service.http.NamespaceException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * The dashboard tile for the REST API, - * also registers the Swagger UI as a web resource on the HTTP service * * @author Kai Kreuzer * */ public class RESTDashboardTile implements DashboardTile { - private static final String ALIAS = "/doc"; - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - private HttpService httpService; - - protected void setHttpService(HttpService httpService) { - this.httpService = httpService; - } - - protected void unsetHttpService(HttpService httpService) { - this.httpService = null; - } - protected void activate() { - try { - httpService.registerResources(ALIAS, "swagger", httpService.createDefaultHttpContext()); - } catch (NamespaceException e) { - logger.error("Could not start up REST documentation service: {}", e.getMessage()); - } } protected void deactivate() { - httpService.unregister(ALIAS); } @Override diff --git a/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/SwaggerService.java b/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/SwaggerService.java new file mode 100644 index 0000000000000..65dd1a6af593b --- /dev/null +++ b/bundles/org.openhab.io.rest.docs/src/main/java/org/openhab/io/rest/docs/internal/SwaggerService.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2015-2017 by the respective copyright holders. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.openhab.io.rest.docs.internal; + +import org.osgi.service.http.HttpService; +import org.osgi.service.http.NamespaceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This service registers the Swagger UI as a web resource on the HTTP service. + * + * @author Kai Kreuzer + * + */ +public class SwaggerService { + + private static final String ALIAS = "/doc"; + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private HttpService httpService; + + protected void setHttpService(HttpService httpService) { + this.httpService = httpService; + } + + protected void unsetHttpService(HttpService httpService) { + this.httpService = null; + } + + protected void activate() { + try { + httpService.registerResources(ALIAS, "swagger", httpService.createDefaultHttpContext()); + } catch (NamespaceException e) { + logger.error("Could not start up REST documentation service: {}", e.getMessage()); + } + } + + protected void deactivate() { + httpService.unregister(ALIAS); + } + +}