diff --git a/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/SmartHomeServlet.java b/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/SmartHomeServlet.java index 8e6d24479c8..a2f0f6551b3 100644 --- a/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/SmartHomeServlet.java +++ b/bundles/org.openhab.core.io.http/src/main/java/org/eclipse/smarthome/io/http/servlet/SmartHomeServlet.java @@ -12,6 +12,7 @@ */ package org.eclipse.smarthome.io.http.servlet; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.osgi.service.http.HttpContext; /** @@ -19,6 +20,7 @@ * * @author Ɓukasz Dywicki - Initial contribution */ +@NonNullByDefault public abstract class SmartHomeServlet extends BaseSmartHomeServlet { private static final long serialVersionUID = 6854521240046714164L; @@ -26,7 +28,7 @@ public abstract class SmartHomeServlet extends BaseSmartHomeServlet { /** * Http context. */ - protected HttpContext httpContext; + protected @NonNullByDefault({}) HttpContext httpContext; protected void setHttpContext(HttpContext httpContext) { this.httpContext = httpContext; diff --git a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/ChartServlet.java b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/ChartServlet.java index 7dc73c17d2a..36c255d99b7 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/ChartServlet.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/ChartServlet.java @@ -17,11 +17,15 @@ import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.AbstractMap.SimpleEntry; +import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.imageio.IIOException; import javax.imageio.ImageIO; @@ -32,9 +36,11 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.BooleanUtils; +import org.eclipse.smarthome.config.core.ConfigurableService; import org.eclipse.smarthome.core.items.ItemNotFoundException; import org.eclipse.smarthome.io.http.servlet.SmartHomeServlet; import org.eclipse.smarthome.ui.chart.ChartProvider; +import org.osgi.framework.Constants; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; @@ -64,8 +70,10 @@ * @author Holger Reichert - Support for themes, DPI, legend hiding */ @Component(immediate = true, service = ChartServlet.class, configurationPid = "org.eclipse.smarthome.chart", property = { - "service.pid=org.eclipse.smarthome.chart", "service.config.description.uri=system:chart", - "service.config.label=Charts", "service.config.category=system" }) + Constants.SERVICE_PID + "=org.eclipse.smarthome.chart", + ConfigurableService.SERVICE_PROPERTY_CATEGORY + "=system", + ConfigurableService.SERVICE_PROPERTY_LABEL + "=Charts", + ConfigurableService.SERVICE_PROPERTY_DESCRIPTION_URI + "=" + "system:chart" }) public class ChartServlet extends SmartHomeServlet { private static final long serialVersionUID = 7700873790924746422L; @@ -82,59 +90,35 @@ public class ChartServlet extends SmartHomeServlet { // The URI of this servlet public static final String SERVLET_NAME = "/chart"; - protected static final Map PERIODS = new HashMap<>(); - - static { - PERIODS.put("h", 3600000L); - PERIODS.put("4h", 14400000L); - PERIODS.put("8h", 28800000L); - PERIODS.put("12h", 43200000L); - PERIODS.put("D", 86400000L); - PERIODS.put("2D", 172800000L); - PERIODS.put("3D", 259200000L); - PERIODS.put("W", 604800000L); - PERIODS.put("2W", 1209600000L); - PERIODS.put("M", 2592000000L); - PERIODS.put("2M", 5184000000L); - PERIODS.put("4M", 10368000000L); - PERIODS.put("Y", 31536000000L); - } + protected static final Map PERIODS = Collections.unmodifiableMap(Stream.of( // + new SimpleEntry<>("h", 3600000L), new SimpleEntry<>("4h", 14400000L), // + new SimpleEntry<>("8h", 28800000L), new SimpleEntry<>("12h", 43200000L), // + new SimpleEntry<>("D", 86400000L), new SimpleEntry<>("2D", 172800000L), // + new SimpleEntry<>("3D", 259200000L), new SimpleEntry<>("W", 604800000L), // + new SimpleEntry<>("2W", 1209600000L), new SimpleEntry<>("M", 2592000000L), // + new SimpleEntry<>("2M", 5184000000L), new SimpleEntry<>("4M", 10368000000L), // + new SimpleEntry<>("Y", 31536000000L)// + ).collect(Collectors.toMap(Entry::getKey, Entry::getValue))); - protected static Map chartProviders = new ConcurrentHashMap<>(); + protected static final Map CHART_PROVIDERS = new ConcurrentHashMap<>(); - @Override - @Reference - public void setHttpService(HttpService httpService) { + @Activate + public ChartServlet(final @Reference HttpService httpService, final @Reference HttpContext httpContext) { super.setHttpService(httpService); - } - - @Override - public void unsetHttpService(HttpService httpService) { - super.unsetHttpService(httpService); - } - - @Override - @Reference - public void setHttpContext(HttpContext httpContext) { super.setHttpContext(httpContext); } - @Override - public void unsetHttpContext(HttpContext httpContext) { - super.unsetHttpContext(httpContext); - } - @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) public void addChartProvider(ChartProvider provider) { - chartProviders.put(provider.getName(), provider); + CHART_PROVIDERS.put(provider.getName(), provider); } public void removeChartProvider(ChartProvider provider) { - chartProviders.remove(provider.getName()); + CHART_PROVIDERS.remove(provider.getName()); } public static Map getChartProviders() { - return chartProviders; + return CHART_PROVIDERS; } @Activate @@ -209,7 +193,7 @@ private void applyConfig(Map config) { } } - @SuppressWarnings({ "null" }) + @SuppressWarnings("null") @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { logger.debug("Received incoming chart request: {}", req); diff --git a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java index 4972ce4be71..6dab7ed1756 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/eclipse/smarthome/ui/internal/chart/defaultchartprovider/DefaultChartProvider.java @@ -70,9 +70,9 @@ public class DefaultChartProvider implements ChartProvider { private final Logger logger = LoggerFactory.getLogger(DefaultChartProvider.class); - private TimeZoneProvider timeZoneProvider; - protected ItemUIRegistry itemUIRegistry; - private PersistenceServiceRegistry persistenceServiceRegistry; + private final TimeZoneProvider timeZoneProvider; + protected final ItemUIRegistry itemUIRegistry; + private final PersistenceServiceRegistry persistenceServiceRegistry; private int legendPosition = 0; @@ -83,33 +83,15 @@ public class DefaultChartProvider implements ChartProvider { public static final int DPI_DEFAULT = 96; - @Reference - public void setItemUIRegistry(ItemUIRegistry itemUIRegistry) { + @Activate + public DefaultChartProvider(final @Reference TimeZoneProvider timeZoneProvider, + final @Reference ItemUIRegistry itemUIRegistry, + final @Reference PersistenceServiceRegistry persistenceServiceRegistry) { + this.timeZoneProvider = timeZoneProvider; this.itemUIRegistry = itemUIRegistry; - } - - public void unsetItemUIRegistry(ItemUIRegistry itemUIRegistry) { - this.itemUIRegistry = null; - } - - @Reference - protected void setPersistenceServiceRegistry(PersistenceServiceRegistry persistenceServiceRegistry) { this.persistenceServiceRegistry = persistenceServiceRegistry; } - protected void unsetPersistenceServiceRegistry(PersistenceServiceRegistry persistenceServiceRegistry) { - this.persistenceServiceRegistry = null; - } - - @Reference - public void setTimeZoneProvider(TimeZoneProvider timeZoneProvider) { - this.timeZoneProvider = timeZoneProvider; - } - - public void unsetTimeZoneProvider(TimeZoneProvider timeZoneProvider) { - this.timeZoneProvider = null; - } - @Activate protected void activate() { logger.debug("Starting up default chart provider."); @@ -296,16 +278,11 @@ boolean addItem(Chart chart, QueryablePersistenceService service, Date timeBegin Color color = chartTheme.getLineColor(seriesCounter); // Get the item label - String label = null; - if (itemUIRegistry != null) { - // Get the item label - label = itemUIRegistry.getLabel(item.getName()); - if (label != null && label.contains("[") && label.contains("]")) { - label = label.substring(0, label.indexOf('[')); - } - } + String label = itemUIRegistry.getLabel(item.getName()); if (label == null) { label = item.getName(); + } else if (label.contains("[") && label.contains("]")) { + label = label.substring(0, label.indexOf('[')); } Iterable result;