Skip to content

Commit

Permalink
Use constructor injection to simplify lifecycle (#1187)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored and kaikreuzer committed Nov 6, 2019
1 parent 07d10db commit be37862
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
*/
package org.eclipse.smarthome.io.http.servlet;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.osgi.service.http.HttpContext;

/**
* Base class for HTTP servlets which share certain {@link HttpContext} instance.
*
* @author Łukasz Dywicki - Initial contribution
*/
@NonNullByDefault
public abstract class SmartHomeServlet extends BaseSmartHomeServlet {

private static final long serialVersionUID = 6854521240046714164L;

/**
* Http context.
*/
protected HttpContext httpContext;
protected @NonNullByDefault({}) HttpContext httpContext;

protected void setHttpContext(HttpContext httpContext) {
this.httpContext = httpContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<String, Long> 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<String, Long> 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<String, ChartProvider> chartProviders = new ConcurrentHashMap<>();
protected static final Map<String, ChartProvider> 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<String, ChartProvider> getChartProviders() {
return chartProviders;
return CHART_PROVIDERS;
}

@Activate
Expand Down Expand Up @@ -209,7 +193,7 @@ private void applyConfig(Map<String, Object> config) {
}
}

@SuppressWarnings({ "null" })
@SuppressWarnings("null")
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
logger.debug("Received incoming chart request: {}", req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.");
Expand Down Expand Up @@ -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<HistoricItem> result;
Expand Down

0 comments on commit be37862

Please sign in to comment.