From 0dfe648a4cff0ebd74ad2fd82afd68a1018230a3 Mon Sep 17 00:00:00 2001
From: Simon Spinner <simon.spinner@uni-wuerzburg.de>
Date: Tue, 21 Jun 2016 15:08:34 +0200
Subject: [PATCH] Support removal of Containers in Wildfly Agent.

---
 .../service/ModelExtractionService.java       | 59 +++++++++++++------
 1 file changed, 41 insertions(+), 18 deletions(-)

diff --git a/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java b/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java
index 76759d7..4dff818 100644
--- a/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java
+++ b/tools.descartes.prisma.agent.wildfly/src/main/java/tools/descartes/prisma/agent/wildfly/service/ModelExtractionService.java
@@ -238,6 +238,11 @@ public class ModelExtractionService extends AgentController implements Service<M
 					if (instance instanceof RuntimeEnvironment) {
 						addNewRuntimeEnvironment((RuntimeEnvironment) instance);
 					}
+				} else if (event.getNotificationType().equals(ModelNotification.REMOVED_NOTIFICATION)) {
+					EObject instance = event.getInstance();
+					if (instance instanceof RuntimeEnvironment) {
+						removeRuntimeEnvironment((RuntimeEnvironment) instance);
+					}
 				}
 			}
 		});
@@ -251,10 +256,26 @@ public class ModelExtractionService extends AgentController implements Service<M
 		}
 	}
 
+	public void removeRuntimeEnvironment(RuntimeEnvironment env) {
+		if (env.getOfClass() == RuntimeEnvironmentClasses.OPERATING_SYSTEM
+				&& env.getName().equalsIgnoreCase(getLocalHost().getHostName())) {
+			synchronized (skeleton) {
+				// Remove all obsolete deployment contexts
+				skeleton.getContainers().remove(wildFlyContainer);
+				skeleton.getDeployment().getDeploymentContexts().clear();
+				apply(skeleton);
+				wildFlyContainer = null;
+			}
+		}
+	}
+
 	public void addNewRuntimeEnvironment(RuntimeEnvironment env) {
 		if (env.getOfClass() == RuntimeEnvironmentClasses.OPERATING_SYSTEM
 				&& env.getName().equalsIgnoreCase(getLocalHost().getHostName())) {
-			wildFlyContainer = ModelSkeletonUtil.adopt(ModelExtractionService.this, env);
+			synchronized (skeleton) {
+				wildFlyContainer = ModelSkeletonUtil.adopt(ModelExtractionService.this, env);
+				skeleton.getContainers().add(wildFlyContainer);
+			}
 		}
 	}
 
@@ -292,27 +313,29 @@ public class ModelExtractionService extends AgentController implements Service<M
 
 				boolean changed = false;
 
-				while ((current = updates.poll()) != null) {
-					if (current instanceof InvocationRecord) {
-						handleInvocation((InvocationRecord) current);
-						changed = true;
+				synchronized (skeleton) {
+					while ((current = updates.poll()) != null) {
+						if (current instanceof InvocationRecord) {
+							handleInvocation((InvocationRecord) current);
+							changed = true;
+						}
 					}
-				}
 
-				if (wildFlyContainer != null) {
-					for (AssemblyContext curContext : undeployedAssemblyContexts) {
-						DeploymentContext curDeployment = DeploymentFactory.eINSTANCE.createDeploymentContext();
-						curDeployment.setName(toName(curContext.getName() + "@" + wildFlyContainer.getName()));
-						curDeployment.setAssemblyContext(curContext);
-						curDeployment.setResourceContainer(wildFlyContainer);
-						ModelSkeletonUtil.insert(skeleton.getDeployment().getDeploymentContexts(), curDeployment);
-						changed = true;
+					if (wildFlyContainer != null) {
+						for (AssemblyContext curContext : undeployedAssemblyContexts) {
+							DeploymentContext curDeployment = DeploymentFactory.eINSTANCE.createDeploymentContext();
+							curDeployment.setName(toName(curContext.getName() + "@" + wildFlyContainer.getName()));
+							curDeployment.setAssemblyContext(curContext);
+							curDeployment.setResourceContainer(wildFlyContainer);
+							ModelSkeletonUtil.insert(skeleton.getDeployment().getDeploymentContexts(), curDeployment);
+							changed = true;
+						}
+						undeployedAssemblyContexts.clear();
 					}
-					undeployedAssemblyContexts.clear();
-				}
 
-				if (changed) {
-					apply(skeleton);
+					if (changed) {
+						apply(skeleton);
+					}
 				}
 			} catch (Throwable ex) {
 				log.error("Error updating skeleton.", ex);
-- 
GitLab