2424import java .util .UUID ;
2525import java .util .concurrent .ConcurrentHashMap ;
2626
27- import org .eclipse .emf .common .util .BasicEList ;
28- import org .eclipse .emf .common .util .EList ;
2927import org .eclipse .jdt .annotation .NonNullByDefault ;
3028import org .eclipse .jdt .annotation .Nullable ;
29+ import org .openhab .core .common .registry .RegistryChangeListener ;
3130import org .openhab .core .events .Event ;
3231import org .openhab .core .events .EventSubscriber ;
3332import org .openhab .core .i18n .TimeZoneProvider ;
3635import org .openhab .core .items .GroupItem ;
3736import org .openhab .core .items .Item ;
3837import org .openhab .core .items .events .ItemStatePredictedEvent ;
39- import org .openhab .core .model .core .EventType ;
40- import org .openhab .core .model .core .ModelRepositoryChangeListener ;
41- import org .openhab .core .model .sitemap .SitemapProvider ;
42- import org .openhab .core .model .sitemap .sitemap .LinkableWidget ;
43- import org .openhab .core .model .sitemap .sitemap .Sitemap ;
44- import org .openhab .core .model .sitemap .sitemap .Widget ;
38+ import org .openhab .core .sitemap .LinkableWidget ;
39+ import org .openhab .core .sitemap .Sitemap ;
40+ import org .openhab .core .sitemap .Widget ;
41+ import org .openhab .core .sitemap .registry .SitemapRegistry ;
4542import org .openhab .core .thing .events .ChannelDescriptionChangedEvent ;
4643import org .openhab .core .ui .items .ItemUIRegistry ;
4744import org .osgi .framework .BundleContext ;
5148import org .osgi .service .component .annotations .Deactivate ;
5249import org .osgi .service .component .annotations .Modified ;
5350import org .osgi .service .component .annotations .Reference ;
54- import org .osgi .service .component .annotations .ReferenceCardinality ;
55- import org .osgi .service .component .annotations .ReferencePolicy ;
5651import org .slf4j .Logger ;
5752import org .slf4j .LoggerFactory ;
5853
6661 * Subscribing to whole sitemaps is discouraged, since a large number of item updates may result in a high SSE traffic.
6762 *
6863 * @author Kai Kreuzer - Initial contribution
64+ * @author Mark Herwege - Implement sitemap registry
6965 */
7066@ Component (service = { SitemapSubscriptionService .class ,
7167 EventSubscriber .class }, configurationPid = "org.openhab.sitemapsubscription" )
7268@ NonNullByDefault
73- public class SitemapSubscriptionService implements ModelRepositoryChangeListener , EventSubscriber {
69+ public class SitemapSubscriptionService implements RegistryChangeListener < Sitemap > , EventSubscriber {
7470
7571 private static final String SITEMAP_PAGE_SEPARATOR = "#" ;
7672 private static final String SITEMAP_SUFFIX = ".sitemap" ;
@@ -88,10 +84,9 @@ public interface SitemapSubscriptionCallback {
8884 }
8985
9086 private final ItemUIRegistry itemUIRegistry ;
87+ private final SitemapRegistry sitemapRegistry ;
9188 private final TimeZoneProvider timeZoneProvider ;
9289
93- private final List <SitemapProvider > sitemapProviders = new ArrayList <>();
94-
9590 /* subscription id -> sitemap+page */
9691 private final Map <String , String > scopeOfSubscription = new ConcurrentHashMap <>();
9792
@@ -109,15 +104,19 @@ public interface SitemapSubscriptionCallback {
109104
110105 @ Activate
111106 public SitemapSubscriptionService (Map <String , Object > config , final @ Reference ItemUIRegistry itemUIRegistry ,
112- final @ Reference TimeZoneProvider timeZoneProvider , BundleContext bundleContext ) {
107+ final @ Reference SitemapRegistry sitemapRegistry , final @ Reference TimeZoneProvider timeZoneProvider ,
108+ BundleContext bundleContext ) {
113109 this .itemUIRegistry = itemUIRegistry ;
110+ this .sitemapRegistry = sitemapRegistry ;
114111 this .timeZoneProvider = timeZoneProvider ;
115112 this .bundleContext = bundleContext ;
116113 applyConfig (config );
114+ sitemapRegistry .addRegistryChangeListener (this );
117115 }
118116
119117 @ Deactivate
120118 protected void deactivate () {
119+ sitemapRegistry .removeRegistryChangeListener (this );
121120 scopeOfSubscription .clear ();
122121 callbacks .clear ();
123122 creationInstants .clear ();
@@ -144,17 +143,6 @@ private void applyConfig(Map<String, Object> config) {
144143 }
145144 }
146145
147- @ Reference (cardinality = ReferenceCardinality .MULTIPLE , policy = ReferencePolicy .DYNAMIC )
148- public void addSitemapProvider (SitemapProvider provider ) {
149- sitemapProviders .add (provider );
150- provider .addModelChangeListener (this );
151- }
152-
153- public void removeSitemapProvider (SitemapProvider provider ) {
154- sitemapProviders .remove (provider );
155- provider .removeModelChangeListener (this );
156- }
157-
158146 /**
159147 * Creates a new subscription with the given id.
160148 *
@@ -275,8 +263,8 @@ private void addCallbackToListener(String sitemapName, @Nullable String pageId,
275263 listener .widgetsChangeListener ().addCallback (callback );
276264 }
277265
278- public EList <Widget > collectWidgets (String sitemapName , @ Nullable String pageId ) {
279- EList <Widget > widgets = new BasicEList <>();
266+ public List <Widget > collectWidgets (String sitemapName , @ Nullable String pageId ) {
267+ List <Widget > widgets = new ArrayList <>();
280268
281269 Sitemap sitemap = getSitemap (sitemapName );
282270 if (sitemap == null ) {
@@ -329,28 +317,28 @@ private String getScopeIdentifier(String sitemapName, @Nullable String pageId) {
329317 }
330318
331319 private @ Nullable Sitemap getSitemap (String sitemapName ) {
332- for (SitemapProvider provider : sitemapProviders ) {
333- Sitemap sitemap = provider .getSitemap (sitemapName );
334- if (sitemap != null ) {
335- return sitemap ;
336- }
337- }
338- return null ;
320+ return sitemapRegistry .get (sitemapName );
339321 }
340322
341323 @ Override
342- public void modelChanged (String modelName , EventType type ) {
343- if (type != EventType .MODIFIED || !modelName .endsWith (SITEMAP_SUFFIX )) {
344- return ; // we process only sitemap modifications here
345- }
324+ public void added (Sitemap element ) {
325+ // Nothing to do
326+ }
327+
328+ @ Override
329+ public void removed (Sitemap element ) {
330+ // Nothing to do
331+ }
346332
347- String changedSitemapName = modelName .substring (0 , modelName .length () - SITEMAP_SUFFIX .length ());
333+ @ Override
334+ public void updated (Sitemap oldElement , Sitemap element ) {
335+ String changedSitemapName = oldElement .getName ();
348336
349337 for (Entry <String , ListenerRecord > listenerEntry : pageChangeListeners .entrySet ()) {
350338 String sitemapWithPage = listenerEntry .getKey ();
351339 String sitemapName = extractSitemapName (sitemapWithPage );
352340
353- EList <Widget > widgets ;
341+ List <Widget > widgets ;
354342 if (sitemapName .equals (changedSitemapName )) {
355343 if (isPageListener (sitemapWithPage )) {
356344 String pageId = extractPageId (sitemapWithPage );
0 commit comments