hello.message=Hello, World!
import org.osgi.service.configurator.ConfigurationException;
import org.osgi.service.configurator.Configurator;
import org.osgi.service.configurator.ConfiguratorException;
import org.osgi.service.configurator.Processor;
import org.osgi.service.configurator.annotation.RequireConfigurator;
import org.osgi.util.tracker.ServiceTracker;
@RequireConfigurator
public class MyConfigProvider {
private static final String CONFIG_FILE = "config.properties";
private ServiceTracker<Configurator, Configurator> tracker;
public void start() {
try {
tracker = new ServiceTracker<>(MyConfigProvider.class,
tracker -> {
try {
return tracker.create(CONFIG_FILE);
} catch (Exception e) {
throw new RuntimeException("Failed to create configurator", e);
}
},
context);
tracker.open();
throw new RuntimeException("Failed to start MyConfigProvider", e);
}
}
public void stop() {
tracker.close();
}
public void getConfig() {
Configurator configurator = tracker.getService();
Processor processor = configurator.requireProcessor(MyConfigProcessor.class);
try {
processor.process(configurator.read(CONFIG_FILE));
throw new RuntimeException("Failed to process configuration", e);
}
}
}
public class MyConfigProcessor implements Processor {
public void process(Dictionary<String, Object> config) {
String message = (String) config.get("hello.message");
System.out.println(message);
}
}