Skip to content

Commit

Permalink
[#1370] Operator fails on startup
Browse files Browse the repository at this point in the history
Rework ProductsDomainRegistry
  • Loading branch information
ruspl-afed committed May 23, 2024
1 parent 33207bc commit f68aab6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="load" deactivate="unload" name="org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry">
<property name="org.eclipse.passage.lic.emf.edit.domain.name" value="products"/>
<property name="org.eclipse.passage.lic.emf.edit.file.extension" value="products_xmi"/>
<service>
<provide interface="org.eclipse.passage.loc.internal.products.ProductRegistry"/>
<provide interface="org.eclipse.passage.loc.internal.emf.EditingDomainRegistry"/>
</service>
<reference bind="bindEventAdmin" cardinality="1..1" interface="org.osgi.service.event.EventAdmin" name="EventAdmin" unbind="unbindEventAdmin"/>
<reference bind="bindGear" interface="org.eclipse.passage.loc.internal.api.OperatorGearSupplier" name="Gear" unbind="unbindGear"/>
<reference cardinality="1..1" field="events" interface="org.osgi.service.event.EventAdmin" name="events"/>
<implementation class="org.eclipse.passage.loc.internal.products.core.ProductDomainRegistry"/>
</scr:component>
</scr:component>
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -48,20 +48,29 @@
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<ProductLine>
public final class ProductDomainRegistry extends BaseDomainRegistry<ProductLine>
implements ProductRegistry, EditingDomainRegistry<ProductLine> {

private final Map<String, ProductLine> lines = new HashMap<>();
private final Map<String, Product> products = new HashMap<>();
private final Map<String, Map<String, ProductVersion>> versions = new HashMap<>();
private final Map<String, Map<String, Map<String, ProductVersionFeature>>> features = new HashMap<>();

@Reference
private EventAdmin events;
private final List<EventAdmin> 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
Expand All @@ -74,15 +83,13 @@ public void unbindGear(OperatorGearSupplier supplier) {
super.unbindGear(supplier);
}

@Override
@Activate
public void activate(Map<String, Object> properties) {
public void load(Map<String, Object> properties) {
super.activate(properties);
}

@Deactivate
@Override
public void deactivate(Map<String, Object> properties) {
public void unload(Map<String, Object> properties) {
for (Map<String, Map<String, ProductVersionFeature>> map : features.values()) {
map.clear();
}
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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) {
Expand All @@ -198,21 +205,21 @@ 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()));
}
}

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()));
}
}
Expand All @@ -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()));
}
Expand All @@ -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()) {
Expand All @@ -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();
Expand Down

0 comments on commit f68aab6

Please sign in to comment.