Skip to content

Commit

Permalink
Added constructor injection to config xml (openhab#1168)
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 Oct 28, 2019
1 parent 3ae82d1 commit 80dce80
Showing 1 changed file with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionProvider;
import org.eclipse.smarthome.config.core.i18n.ConfigI18nLocalizationService;
Expand All @@ -40,25 +42,30 @@
* @author Simon Kaufmann - Initial contribution
*/
@Component(service = ConfigDescriptionProvider.class, immediate = true, property = { "esh.scope=core.xml.config" })
@NonNullByDefault
public class ConfigXmlConfigDescriptionProvider extends AbstractXmlConfigDescriptionProvider
implements XmlDocumentProviderFactory<List<ConfigDescription>> {

private static final String XML_DIRECTORY = "/ESH-INF/config/";
public static final String READY_MARKER = "esh.xmlConfig";

private XmlDocumentBundleTracker<List<ConfigDescription>> configDescriptionTracker;

private ConfigI18nLocalizationService configI18nLocalizerService;
private ReadyService readyService;

private ScheduledExecutorService scheduler = ThreadPoolManager
private final ConfigI18nLocalizationService configI18nService;
private @Nullable XmlDocumentBundleTracker<List<ConfigDescription>> configDescriptionTracker;
private final ReadyService readyService;
private final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(XmlDocumentBundleTracker.THREAD_POOL_NAME);
private Future<?> trackerJob;
private @Nullable Future<?> trackerJob;

@Activate
public ConfigXmlConfigDescriptionProvider(final @Reference ConfigI18nLocalizationService configI18nService,
final @Reference ReadyService readyService) {
this.configI18nService = configI18nService;
this.readyService = readyService;
}

@Activate
public void activate(ComponentContext componentContext) {
XmlDocumentReader<List<ConfigDescription>> configDescriptionReader = new ConfigDescriptionReader();

configDescriptionTracker = new XmlDocumentBundleTracker<>(componentContext.getBundleContext(), XML_DIRECTORY,
configDescriptionReader, this, READY_MARKER, readyService);
trackerJob = scheduler.submit(() -> {
Expand All @@ -68,40 +75,25 @@ public void activate(ComponentContext componentContext) {

@Deactivate
public void deactivate() {
if (trackerJob != null && !trackerJob.isDone()) {
trackerJob.cancel(true);
Future<?> localTrackerJob = trackerJob;
if (localTrackerJob != null && !localTrackerJob.isDone()) {
localTrackerJob.cancel(true);
trackerJob = null;
}
configDescriptionTracker.close();
configDescriptionTracker = null;
}

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

public void unsetConfigI18nLocalizerService(ConfigI18nLocalizationService configI18nLocalizerService) {
this.configI18nLocalizerService = null;
}

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

public void unsetReadyService(ReadyService readyService) {
this.readyService = null;
XmlDocumentBundleTracker<List<ConfigDescription>> localConfigDescriptionTracker = configDescriptionTracker;
if (localConfigDescriptionTracker != null) {
localConfigDescriptionTracker.close();
configDescriptionTracker = null;
}
}

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

@Override
public XmlDocumentProvider<List<ConfigDescription>> createDocumentProvider(Bundle bundle) {
return new ConfigDescriptionXmlProvider(bundle, this);
}

}

0 comments on commit 80dce80

Please sign in to comment.