1414
1515import static org .openhab .core .config .discovery .addon .AddonFinderConstants .*;
1616
17- import java .io .IOException ;
1817import java .util .ArrayList ;
1918import java .util .Collection ;
20- import java .util .Collections ;
21- import java .util .Dictionary ;
2219import java .util .List ;
2320import java .util .Locale ;
2421import java .util .Map ;
2522import java .util .Map .Entry ;
2623import java .util .Set ;
2724import java .util .concurrent .ConcurrentHashMap ;
28- import java .util .concurrent .ScheduledExecutorService ;
29- import java .util .concurrent .ScheduledFuture ;
30- import java .util .concurrent .TimeUnit ;
31- import java .util .function .Function ;
3225import java .util .stream .Collectors ;
3326
3427import org .eclipse .jdt .annotation .NonNullByDefault ;
3528import org .eclipse .jdt .annotation .Nullable ;
3629import org .openhab .core .OpenHAB ;
3730import org .openhab .core .addon .AddonInfo ;
3831import org .openhab .core .addon .AddonInfoProvider ;
39- import org .openhab .core .common .ThreadPoolManager ;
4032import org .openhab .core .config .core .ConfigParser ;
4133import org .openhab .core .i18n .LocaleProvider ;
42- import org .osgi .service .cm .ConfigurationAdmin ;
4334import org .osgi .service .component .annotations .Activate ;
4435import org .osgi .service .component .annotations .Component ;
4536import org .osgi .service .component .annotations .Deactivate ;
@@ -66,35 +57,22 @@ public class AddonSuggestionService {
6657 private final Logger logger = LoggerFactory .getLogger (AddonSuggestionService .class );
6758
6859 private final Set <AddonInfoProvider > addonInfoProviders = ConcurrentHashMap .newKeySet ();
69- private final List <AddonFinder > addonFinders = Collections .synchronizedList (new ArrayList <>());
70- private final ConfigurationAdmin configurationAdmin ;
60+
61+ // All access must be guarded by "addonFinders"
62+ private final List <AddonFinder > addonFinders = new ArrayList <>();
7163 private final LocaleProvider localeProvider ;
7264 private volatile @ Nullable AddonFinderService addonFinderService ;
73- private @ Nullable Map <String , Object > config ;
74- private final ScheduledExecutorService scheduler ;
7565 private final Map <String , Boolean > baseFinderConfig = new ConcurrentHashMap <>();
76- private final ScheduledFuture <?> cfgRefreshTask ;
7766
7867 @ Activate
79- public AddonSuggestionService (final @ Reference ConfigurationAdmin configurationAdmin ,
80- @ Reference LocaleProvider localeProvider , Map <String , Object > config ) {
81- this .configurationAdmin = configurationAdmin ;
68+ public AddonSuggestionService (@ Reference LocaleProvider localeProvider , Map <String , Object > config ) {
8269 this .localeProvider = localeProvider ;
83-
8470 SUGGESTION_FINDERS .forEach (f -> baseFinderConfig .put (f , false ));
85-
86- // Changes to the configuration are expected to call the {@link modified} method. This works well when running
87- // in Eclipse. Running in Karaf, the method was not consistently called. Therefore regularly check for changes
88- // in configuration.
89- // This pattern and code was re-used from {@link org.openhab.core.karaf.internal.FeatureInstaller}
9071 modified (config );
91- scheduler = ThreadPoolManager .getScheduledPool (ThreadPoolManager .THREAD_POOL_NAME_COMMON );
92- cfgRefreshTask = scheduler .scheduleWithFixedDelay (this ::syncConfiguration , 1 , 1 , TimeUnit .MINUTES );
9372 }
9473
9574 @ Deactivate
9675 public void deactivate () {
97- cfgRefreshTask .cancel (true );
9876 synchronized (addonFinders ) {
9977 addonFinders .clear ();
10078 }
@@ -142,14 +120,6 @@ public void modified(@Nullable final Map<String, Object> config) {
142120 }
143121 }
144122 });
145- this .config = config ;
146- }
147- }
148-
149- private void syncConfiguration () {
150- final Map <String , Object > cfg = getConfiguration ();
151- if (cfg != null && !cfg .equals (config )) {
152- modified (cfg );
153123 }
154124 }
155125
@@ -176,20 +146,6 @@ private void initAddonFinderService() {
176146 }
177147 }
178148
179- private @ Nullable Map <String , Object > getConfiguration () {
180- try {
181- Dictionary <String , Object > cfg = configurationAdmin .getConfiguration (OpenHAB .ADDONS_SERVICE_PID )
182- .getProperties ();
183- if (cfg != null ) {
184- List <String > keys = Collections .list (cfg .keys ());
185- return keys .stream ().collect (Collectors .toMap (Function .identity (), cfg ::get ));
186- }
187- } catch (IOException | IllegalStateException e ) {
188- logger .debug ("Exception occurred while trying to get the configuration: {}" , e .getMessage ());
189- }
190- return null ;
191- }
192-
193149 private boolean isFinderEnabled (AddonFinder finder ) {
194150 if (finder instanceof BaseAddonFinder baseFinder ) {
195151 return baseFinderConfig .getOrDefault (baseFinder .getServiceName (), true );
@@ -224,7 +180,8 @@ public void removeAddonFinder(AddonFinder addonFinder) {
224180 }
225181
226182 private void changed () {
227- List <AddonInfo > candidates = addonInfoProviders .stream ().map (p -> p .getAddonInfos (localeProvider .getLocale ()))
183+ Locale locale = localeProvider .getLocale ();
184+ List <AddonInfo > candidates = addonInfoProviders .stream ().map (p -> p .getAddonInfos (locale ))
228185 .flatMap (Collection ::stream ).toList ();
229186 synchronized (addonFinders ) {
230187 addonFinders .stream ().filter (this ::isFinderEnabled ).forEach (f -> f .setAddonCandidates (candidates ));
0 commit comments