Use constructor injection to simplify lifecycle (#994)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2019-08-24 23:45:46 +02:00 committed by Kai Kreuzer
parent be1cd25743
commit e2132a7a46
4 changed files with 18 additions and 29 deletions

View File

@ -76,7 +76,7 @@ import com.google.gson.JsonSyntaxException;
* Last but not least, a pid can be defined in the first line of a cfg file by prefixing it with "pid:", e.g.
* "pid: com.acme.smarthome.security".
*
* @author Kai Kreuzer - Initial contribution and API
* @author Kai Kreuzer - Initial contribution
* @author Petar Valchev - Added sort by modification time, when configuration files are read
* @author Ana Dimova - Reduce to a single watch thread for all class instances
* @author Henning Treu - Delete orphan exclusive configuration from configAdmin
@ -116,10 +116,15 @@ public class ConfigDispatcher {
private ExclusivePIDMap exclusivePIDMap;
private ConfigurationAdmin configAdmin;
private final ConfigurationAdmin configAdmin;
private File exclusivePIDStore;
@Activate
public ConfigDispatcher(final @Reference ConfigurationAdmin configAdmin) {
this.configAdmin = configAdmin;
}
@Activate
public void activate(BundleContext bundleContext) {
exclusivePIDStore = bundleContext.getDataFile(EXCLUSIVE_PID_STORE_FILE);
@ -431,15 +436,6 @@ public class ConfigDispatcher {
}
}
@Reference
public void setConfigurationAdmin(ConfigurationAdmin configAdmin) {
this.configAdmin = configAdmin;
}
public void unsetConfigurationAdmin(ConfigurationAdmin configAdmin) {
this.configAdmin = null;
}
/**
* Represents a result of parseLine().
*/

View File

@ -29,9 +29,8 @@ import org.osgi.service.component.annotations.Reference;
/**
* Watches file-system events and passes them to our {@link ConfigDispatcher}
*
* @author Kai Kreuzer - Initial contribution and API
* @author Kai Kreuzer - Initial contribution
* @author Stefan Triller - factored out this code from {@link ConfigDispatcher}
*
*/
@Component(immediate = true)
public class ConfigDispatcherFileWatcher extends AbstractWatchService {
@ -42,10 +41,12 @@ public class ConfigDispatcherFileWatcher extends AbstractWatchService {
/** The default folder name of the configuration folder of services */
public static final String SERVICES_FOLDER = "services";
private ConfigDispatcher configDispatcher;
private final ConfigDispatcher configDispatcher;
public ConfigDispatcherFileWatcher() {
@Activate
public ConfigDispatcherFileWatcher(final @Reference ConfigDispatcher configDispatcher) {
super(getPathToWatch());
this.configDispatcher = configDispatcher;
}
private static String getPathToWatch() {
@ -57,8 +58,8 @@ public class ConfigDispatcherFileWatcher extends AbstractWatchService {
}
}
@Override
@Activate
@Override
public void activate() {
super.activate();
configDispatcher.processConfigFile(getSourcePath().toFile());
@ -98,13 +99,4 @@ public class ConfigDispatcherFileWatcher extends AbstractWatchService {
}
}
@Reference
public void setConfigDispatcher(ConfigDispatcher configDispatcher) {
this.configDispatcher = configDispatcher;
}
public void unsetConfigDispatcher(ConfigDispatcher configDispatcher) {
this.configDispatcher = null;
}
}

View File

@ -38,8 +38,7 @@ public class ConfigDispatcherFileWatcherTest {
public void setUp() throws Exception {
initMocks(this);
configDispatcherFileWatcher = new TestConfigDispatcherFileWatcher();
configDispatcherFileWatcher.setConfigDispatcher(configDispatcher);
configDispatcherFileWatcher = new TestConfigDispatcherFileWatcher(configDispatcher);
}
@Test
@ -97,6 +96,9 @@ public class ConfigDispatcherFileWatcherTest {
}
public class TestConfigDispatcherFileWatcher extends ConfigDispatcherFileWatcher {
public TestConfigDispatcherFileWatcher(ConfigDispatcher configDispatcher) {
super(configDispatcher);
}
@Override
protected void processWatchEvent(WatchEvent<?> event, Kind<?> kind, Path path) {

View File

@ -69,8 +69,7 @@ public class ConfigDispatcherOSGiTest extends JavaOSGiTest {
configAdmin = getService(ConfigurationAdmin.class);
assertThat(configAdmin, is(notNullValue()));
cd = new ConfigDispatcher();
cd.setConfigurationAdmin(configAdmin);
cd = new ConfigDispatcher(configAdmin);
}
@After