Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xml] Feature thing xml constructor injection #1167

Merged
merged 2 commits into from
Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*/
package org.eclipse.smarthome.core.thing.xml.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionProvider;
import org.eclipse.smarthome.config.core.i18n.ConfigI18nLocalizationService;
import org.eclipse.smarthome.config.xml.AbstractXmlConfigDescriptionProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -25,22 +27,18 @@
* @author Simon Kaufmann - Initial contribution
*/
@Component(service = ConfigDescriptionProvider.class, immediate = true, property = { "esh.scope=core.xml.thing" })
@NonNullByDefault
public class ThingXmlConfigDescriptionProvider extends AbstractXmlConfigDescriptionProvider {

private ConfigI18nLocalizationService configI18nLocalizerService;
private final ConfigI18nLocalizationService configI18nService;

@Reference
public void setConfigI18nLocalizerService(ConfigI18nLocalizationService configI18nLocalizerService) {
this.configI18nLocalizerService = configI18nLocalizerService;
}

public void unsetConfigI18nLocalizerService(ConfigI18nLocalizationService configI18nLocalizerService) {
this.configI18nLocalizerService = null;
@Activate
public ThingXmlConfigDescriptionProvider(final @Reference ConfigI18nLocalizationService configI18nService) {
this.configI18nService = configI18nService;
}

@Override
protected ConfigI18nLocalizationService getConfigI18nLocalizerService() {
return configI18nLocalizerService;
return configI18nService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import java.util.Collection;
import java.util.Locale;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.xml.AbstractXmlBasedProvider;
import org.eclipse.smarthome.core.thing.UID;
import org.eclipse.smarthome.core.thing.i18n.ChannelGroupTypeI18nLocalizationService;
import org.eclipse.smarthome.core.thing.type.ChannelGroupType;
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeProvider;
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID;
import org.osgi.framework.Bundle;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -35,36 +37,29 @@
public class XmlChannelGroupTypeProvider extends AbstractXmlBasedProvider<UID, ChannelGroupType>
implements ChannelGroupTypeProvider {

private ChannelGroupTypeI18nLocalizationService channelGroupTypeI18nLocalizationService;
private final ChannelGroupTypeI18nLocalizationService channelGroupTypeI18nLocalizationService;

@Activate
public XmlChannelGroupTypeProvider(
final @Reference ChannelGroupTypeI18nLocalizationService channelGroupTypeI18nLocalizationService) {
this.channelGroupTypeI18nLocalizationService = channelGroupTypeI18nLocalizationService;
}

@Override
public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
@Nullable Locale locale) {
return get(channelGroupTypeUID, locale);
}

@Override
public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
return getAll(locale);
}

@Reference
public void setChannelGroupTypeI18nLocalizationService(
final ChannelGroupTypeI18nLocalizationService channelGroupTypeI18nLocalizationService) {
this.channelGroupTypeI18nLocalizationService = channelGroupTypeI18nLocalizationService;
}

public void unsetChannelGroupTypeI18nLocalizationService(
final ChannelGroupTypeI18nLocalizationService channelGroupTypeI18nLocalizationService) {
this.channelGroupTypeI18nLocalizationService = null;
}

@Override
protected ChannelGroupType localize(Bundle bundle, ChannelGroupType channelGroupType, Locale locale) {
if (channelGroupTypeI18nLocalizationService == null) {
return null;
}
protected @Nullable ChannelGroupType localize(Bundle bundle, ChannelGroupType channelGroupType,
@Nullable Locale locale) {
return channelGroupTypeI18nLocalizationService.createLocalizedChannelGroupType(bundle, channelGroupType,
locale);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import java.util.Collection;
import java.util.Locale;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.xml.AbstractXmlBasedProvider;
import org.eclipse.smarthome.core.thing.UID;
import org.eclipse.smarthome.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.eclipse.smarthome.core.thing.type.ChannelType;
import org.eclipse.smarthome.core.thing.type.ChannelTypeProvider;
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID;
import org.osgi.framework.Bundle;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -37,35 +39,26 @@
@Component(property = { "esh.scope=core.xml.channels" })
public class XmlChannelTypeProvider extends AbstractXmlBasedProvider<UID, ChannelType> implements ChannelTypeProvider {

private ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService;
private final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService;

@Activate
public XmlChannelTypeProvider(
final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
}

@Override
public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
return get(channelTypeUID, locale);
}

@Override
public synchronized Collection<ChannelType> getChannelTypes(Locale locale) {
public synchronized Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
return getAll(locale);
}

@Reference
public void setChannelTypeI18nLocalizationService(
final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
}

public void unsetChannelTypeI18nLocalizationService(
final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.channelTypeI18nLocalizationService = null;
}

@Override
protected ChannelType localize(Bundle bundle, ChannelType channelType, Locale locale) {
if (channelTypeI18nLocalizationService == null) {
return null;
}
protected @Nullable ChannelType localize(Bundle bundle, ChannelType channelType, @Nullable Locale locale) {
return channelTypeI18nLocalizationService.createLocalizedChannelType(bundle, channelType, locale);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigDescriptionProvider;
import org.eclipse.smarthome.config.xml.AbstractXmlBasedProvider;
import org.eclipse.smarthome.config.xml.AbstractXmlConfigDescriptionProvider;
Expand Down Expand Up @@ -60,60 +61,57 @@ public class XmlThingTypeProvider extends AbstractXmlBasedProvider<UID, ThingTyp
private static final String XML_DIRECTORY = "/ESH-INF/thing/";
public static final String READY_MARKER = "esh.xmlThingTypes";

private ThingTypeI18nLocalizationService thingTypeI18nLocalizationService;
private AbstractXmlConfigDescriptionProvider configDescriptionProvider;
private final ThingTypeI18nLocalizationService thingTypeI18nLocalizationService;
private XmlChannelTypeProvider channelTypeProvider;
private XmlChannelGroupTypeProvider channelGroupTypeProvider;

private XmlDocumentBundleTracker<List<?>> thingTypeTracker;
private ReadyService readyService;

private AbstractXmlConfigDescriptionProvider configDescriptionProvider;
private @Nullable XmlDocumentBundleTracker<List<?>> thingTypeTracker;
private final ReadyService readyService;
private final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(XmlDocumentBundleTracker.THREAD_POOL_NAME);
private Future<?> trackerJob;
private @Nullable Future<?> trackerJob;

@Activate
public XmlThingTypeProvider(final @Reference ThingTypeI18nLocalizationService thingTypeI18nLocalizationService,
final @Reference ReadyService readyService) {
this.thingTypeI18nLocalizationService = thingTypeI18nLocalizationService;
this.readyService = readyService;
}

@Activate
protected void activate(BundleContext bundleContext) {
XmlDocumentReader<List<?>> thingTypeReader = new ThingDescriptionReader();
thingTypeTracker = new XmlDocumentBundleTracker<>(bundleContext, XML_DIRECTORY, thingTypeReader, this,
READY_MARKER, readyService);

trackerJob = scheduler.submit(() -> {
thingTypeTracker.open();
});
}

@Deactivate
protected void deactivate() {
if (trackerJob != null && !trackerJob.isDone()) {
trackerJob.cancel(true);
Future<?> localTrackerJob = trackerJob;
if (localTrackerJob != null && !localTrackerJob.isDone()) {
localTrackerJob.cancel(true);
trackerJob = null;
}
thingTypeTracker.close();
thingTypeTracker = null;
XmlDocumentBundleTracker<List<?>> localThingTypeTracker = thingTypeTracker;
if (localThingTypeTracker != null) {
localThingTypeTracker.close();
thingTypeTracker = null;
}
}

@Override
public ThingType getThingType(ThingTypeUID thingTypeUID, Locale locale) {
public @Nullable ThingType getThingType(ThingTypeUID thingTypeUID, @Nullable Locale locale) {
return get(thingTypeUID, locale);
}

@Override
public synchronized Collection<ThingType> getThingTypes(Locale locale) {
public synchronized Collection<ThingType> getThingTypes(@Nullable Locale locale) {
return getAll(locale);
}

@Reference
public void setThingTypeI18nLocalizationService(
final ThingTypeI18nLocalizationService thingTypeI18nLocalizationService) {
this.thingTypeI18nLocalizationService = thingTypeI18nLocalizationService;
}

public void unsetThingTypeI18nLocalizationService(
final ThingTypeI18nLocalizationService thingTypeI18nLocalizationService) {
this.thingTypeI18nLocalizationService = null;
}

@Reference(target = "(esh.scope=core.xml.thing)")
public void setConfigDescriptionProvider(ConfigDescriptionProvider configDescriptionProvider) {
this.configDescriptionProvider = (AbstractXmlConfigDescriptionProvider) configDescriptionProvider;
Expand Down Expand Up @@ -141,20 +139,8 @@ public void unsetChannelGroupTypeProvider(ChannelGroupTypeProvider channelGroupT
this.channelGroupTypeProvider = null;
}

@Reference
public void setReadyService(ReadyService readyService) {
this.readyService = readyService;
}

public void unsetReadyService(ReadyService readyService) {
this.readyService = null;
}

@Override
protected ThingType localize(Bundle bundle, ThingType thingType, Locale locale) {
if (thingTypeI18nLocalizationService == null) {
return null;
}
protected @Nullable ThingType localize(Bundle bundle, ThingType thingType, @Nullable Locale locale) {
return thingTypeI18nLocalizationService.createLocalizedThingType(bundle, thingType, locale);
}

Expand All @@ -163,5 +149,4 @@ public XmlDocumentProvider<List<?>> createDocumentProvider(Bundle bundle) {
return new ThingTypeXmlProvider(bundle, configDescriptionProvider, this, channelTypeProvider,
channelGroupTypeProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public DefaultSystemChannelTypeProvider(
}

@Override
public @Nullable Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
final List<ChannelType> allChannelTypes = new ArrayList<>();
final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.net.URI;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigDescriptionAliasProvider;
Expand Down Expand Up @@ -78,7 +77,7 @@ protected void unsetChannelTypeRegistry(ChannelTypeRegistry channelTypeRegistry)
}

@Override
public @Nullable URI getAlias(@NonNull URI uri) {
public @Nullable URI getAlias(URI uri) {
// If this is not a concrete thing, then return
if (uri.getScheme() == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ public interface ChannelGroupTypeProvider {
/**
* @see ChannelTypeRegistry#getChannelGroupTypes(Locale)
*/
@Nullable
Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public interface ChannelTypeProvider {
/**
* @see ChannelTypeRegistry#getChannelTypes(Locale)
*/
@Nullable
Collection<ChannelType> getChannelTypes(@Nullable Locale locale);

/**
Expand All @@ -54,7 +53,7 @@ public interface ChannelTypeProvider {
* @deprecated The {@link ChannelGroupTypeProvider} is now to be implemented/used instead.
*/
@Deprecated
default @Nullable Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
default Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
return Collections.emptyList();
}
}