|
| 1 | +/** |
| 2 | + * Copyright (c) 2010-2020 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.ui.internal; |
| 14 | + |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import org.openhab.core.net.HttpServiceUtil; |
| 18 | +import org.osgi.framework.BundleContext; |
| 19 | +import org.osgi.service.component.ComponentContext; |
| 20 | +import org.osgi.service.component.annotations.Activate; |
| 21 | +import org.osgi.service.component.annotations.Component; |
| 22 | +import org.osgi.service.component.annotations.Deactivate; |
| 23 | +import org.osgi.service.component.annotations.Reference; |
| 24 | +import org.osgi.service.http.HttpContext; |
| 25 | +import org.osgi.service.http.HttpService; |
| 26 | +import org.osgi.service.http.NamespaceException; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * This service register the default UI's resources in the HTTP context. |
| 32 | + * |
| 33 | + * @author Yannick Schaus - Initial contribution |
| 34 | + */ |
| 35 | +@Component(immediate = true, name = "org.openhab.ui") |
| 36 | +public class UIService { |
| 37 | + |
| 38 | + private final Logger logger = LoggerFactory.getLogger(UIService.class); |
| 39 | + |
| 40 | + protected HttpService httpService; |
| 41 | + |
| 42 | + @Activate |
| 43 | + protected void activate(ComponentContext componentContext, Map<String, Object> properties) { |
| 44 | + BundleContext bundleContext = componentContext.getBundleContext(); |
| 45 | + HttpContext httpContext = httpService.createDefaultHttpContext(); |
| 46 | + try { |
| 47 | + httpService.registerResources("/", "app", httpContext); |
| 48 | + if (HttpServiceUtil.getHttpServicePort(bundleContext) > 0) { |
| 49 | + logger.info("Started UI on port {}", HttpServiceUtil.getHttpServicePort(bundleContext)); |
| 50 | + } else { |
| 51 | + logger.info("Started UI"); |
| 52 | + } |
| 53 | + } catch (NamespaceException e) { |
| 54 | + logger.error("Error during UI startup: {}", e.getMessage()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Deactivate |
| 59 | + protected void deactivate(ComponentContext componentContext) { |
| 60 | + httpService.unregister("/"); |
| 61 | + logger.info("Stopped UI"); |
| 62 | + } |
| 63 | + |
| 64 | + @Reference |
| 65 | + protected void setHttpService(HttpService httpService) { |
| 66 | + this.httpService = httpService; |
| 67 | + } |
| 68 | + |
| 69 | + protected void unsetHttpService(HttpService httpService) { |
| 70 | + this.httpService = null; |
| 71 | + } |
| 72 | +} |
0 commit comments