From f68aab658cfe532669dd7c6e06eac94261404409 Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Thu, 23 May 2024 15:05:35 +0300 Subject: [PATCH] [#1370] Operator fails on startup Rework ProductsDomainRegistry --- ...al.products.core.ProductDomainRegistry.xml | 6 +-- .../products/core/ProductDomainRegistry.java | 43 ++++++++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/bundles/org.eclipse.passage.loc.products.core/OSGI-INF/org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry.xml b/bundles/org.eclipse.passage.loc.products.core/OSGI-INF/org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry.xml index e8c0f994f..fb0b36fc2 100644 --- a/bundles/org.eclipse.passage.loc.products.core/OSGI-INF/org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry.xml +++ b/bundles/org.eclipse.passage.loc.products.core/OSGI-INF/org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry.xml @@ -1,12 +1,12 @@ - + + - - + \ No newline at end of file diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductDomainRegistry.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductDomainRegistry.java index 4444d7e45..ef8d2fa72 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductDomainRegistry.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductDomainRegistry.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2018, 2022 ArSysOp + * Copyright (c) 2018, 2024 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -48,11 +48,12 @@ import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.event.EventAdmin; @Component(property = { EditingDomainRegistryAccess.PROPERTY_DOMAIN_NAME + '=' + ProductsPackage.eNAME, EditingDomainRegistryAccess.PROPERTY_FILE_EXTENSION + '=' + "products_xmi" }) -public class ProductDomainRegistry extends BaseDomainRegistry +public final class ProductDomainRegistry extends BaseDomainRegistry implements ProductRegistry, EditingDomainRegistry { private final Map lines = new HashMap<>(); @@ -60,8 +61,16 @@ public class ProductDomainRegistry extends BaseDomainRegistry private final Map> versions = new HashMap<>(); private final Map>> features = new HashMap<>(); - @Reference - private EventAdmin events; + private final List events = new ArrayList<>(); + + @Reference(cardinality = ReferenceCardinality.MANDATORY) + public void bindEventAdmin(EventAdmin admin) { + this.events.add(admin); + } + + public void unbindEventAdmin(EventAdmin admin) { + this.events.remove(admin); + } @Override @Reference @@ -74,15 +83,13 @@ public void unbindGear(OperatorGearSupplier supplier) { super.unbindGear(supplier); } - @Override @Activate - public void activate(Map properties) { + public void load(Map properties) { super.activate(properties); } @Deactivate - @Override - public void deactivate(Map properties) { + public void unload(Map properties) { for (Map> map : features.values()) { map.clear(); } @@ -164,7 +171,7 @@ void registerProductLine(ProductLine line) { Platform.getLog(getClass()) .warn(NLS.bind(ProductsCoreMessages.ProductDomain_instance_duplication_message, existing, line)); } - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_LINE_CREATE, line).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_LINE_CREATE, line).get()); line.getProducts().forEach(p -> registerProduct(p)); } @@ -175,7 +182,7 @@ void registerProduct(Product product) { String msg = NLS.bind(ProductsCoreMessages.ProductDomain_instance_duplication_message, existing, product); Platform.getLog(getClass()).warn(msg); } - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_CREATE, product).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_CREATE, product).get()); product.getProductVersions().forEach(pv -> registerProductVersion(product, pv)); } @@ -186,7 +193,7 @@ void registerProductVersion(Product product, ProductVersion version) { Platform.getLog(getClass()) .warn(NLS.bind(ProductsCoreMessages.ProductDomain_instance_duplication_message, existing, version)); } - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_CREATE, version).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_CREATE, version).get()); } void registerProductVersionFeature(Product product, ProductVersion version, ProductVersionFeature feature) { @@ -198,13 +205,13 @@ void registerProductVersionFeature(Product product, ProductVersion version, Prod Platform.getLog(getClass()) .warn(NLS.bind(ProductsCoreMessages.ProductDomain_instance_duplication_message, existing, feature)); } - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_FEATURE_CREATE, feature).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_FEATURE_CREATE, feature).get()); } void unregisterProductLine(String id) { ProductLine removed = lines.remove(id); if (removed != null) { - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_LINE_DELETE, removed).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_LINE_DELETE, removed).get()); removed.getProducts().forEach(p -> unregisterProduct(p.getIdentifier())); } } @@ -212,7 +219,7 @@ void unregisterProductLine(String id) { void unregisterProduct(String id) { Product removed = products.remove(id); if (removed != null) { - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_DELETE, removed).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_DELETE, removed).get()); removed.getProductVersions().forEach(pv -> unregisterProductVersion(id, pv.getVersion())); } } @@ -222,7 +229,7 @@ void unregisterProductVersion(String product, String version) { if (found != null) { ProductVersion removed = found.remove(version); if (removed != null) { - events.postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_DELETE, removed).get()); + events().postEvent(new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_DELETE, removed).get()); removed.getProductVersionFeatures() .forEach(pvf -> unregisterProductVersionFeature(product, version, pvf.getFeatureIdentifier())); } @@ -239,7 +246,7 @@ void unregisterProductVersionFeature(String product, String version, String feat if (map != null) { ProductVersionFeature removed = map.remove(feature); if (removed != null) { - events.postEvent( + events().postEvent( new EquinoxEvent(ProductRegistryEvents.PRODUCT_VERSION_FEATURE_DELETE, removed).get()); } if (map.isEmpty()) { @@ -252,6 +259,10 @@ void unregisterProductVersionFeature(String product, String version, String feat } } + private EventAdmin events() { + return events.stream().findAny().get(); + } + @Override public EClass getContentClassifier() { return ProductsPackage.eINSTANCE.getProductLine();