Bug 291671 - Improve Declarative Service template
Use pattern as recommended in OSGi Reference section 'Bound Service
Replacement'
see https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-bound.service.replacement
Change-Id: I95ea81f626d831f9b5195f46e11850ee48c10fd9
Signed-off-by: Karsten Thoms <karsten.thoms@itemis.de>
diff --git a/ui/org.eclipse.pde.ui.templates/templates_3.1/helloOSGiServiceComponent/java/ServiceComponent.java b/ui/org.eclipse.pde.ui.templates/templates_3.1/helloOSGiServiceComponent/java/ServiceComponent.java
index e0287d1..b30be59 100644
--- a/ui/org.eclipse.pde.ui.templates/templates_3.1/helloOSGiServiceComponent/java/ServiceComponent.java
+++ b/ui/org.eclipse.pde.ui.templates/templates_3.1/helloOSGiServiceComponent/java/ServiceComponent.java
@@ -5,10 +5,12 @@
import org.osgi.service.component.annotations.*;
+import java.util.concurrent.atomic.AtomicReference;
+
@Component
public class ServiceComponent implements CommandProvider {
- private DictionaryService dictionary;
+ final AtomicReference<DictionaryService> dictionaryRef = new AtomicReference<>();
public void _$command$(CommandInterpreter ci) {
String arg = ci.nextArgument();
@@ -17,14 +19,14 @@
return;
}
if (arg.equalsIgnoreCase("check")) {
- if(word != null && dictionary.check(word)) {
+ if(word != null && dictionaryRef.get().check(word)) {
ci.println(word + ": exists in the dictionary");
} else {
ci.println(word + ": doesn't exist in the dictionary");
}
}
if (arg.equalsIgnoreCase("languages")) {
- String[] langs = dictionary.getLanguages();
+ String[] langs = dictionaryRef.get().getLanguages();
for(int i = 0; i < langs.length; i++) {
ci.println("Languages available:");
ci.println("\t " + langs[i]);
@@ -43,11 +45,11 @@
@Reference
public void setDictionary(DictionaryService d) {
- dictionary = d;
+ dictionaryRef.set(d);
}
public void unsetDictionary(DictionaryService d) {
- dictionary = null;
+ dictionaryRef.compareAndSet(d, null);
}
}
\ No newline at end of file