package org.jboss.as.ejb3.deployment;

import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;


/**
 * Repository for information about deployed modules. This includes information on all the deployed EJB's in the module
 *
 * @author Stuart Douglas
 */
public class DeploymentRepository implements Service<DeploymentRepository> {

    public static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("ee", "deploymentRepository");

    /**
     * All deployed modules. This is a copy on write map that is updated infrequently and read often.
     */
    private volatile Map<DeploymentModuleIdentifier, ModuleDeployment> modules;

    private final List<DeploymentRepositoryListener> listeners = new ArrayList<DeploymentRepositoryListener>();


    @Override
    public void start(StartContext context) throws StartException {
        modules = Collections.emptyMap();
    }

    @Override
    public void stop(StopContext context) {
        modules = null;
    }

    @Override
    public DeploymentRepository getValue() throws IllegalStateException, IllegalArgumentException {
        return this;
    }

    public synchronized void add(DeploymentModuleIdentifier identifier, ModuleDeployment deployment) {
        final Map<DeploymentModuleIdentifier, ModuleDeployment> modules = new HashMap<DeploymentModuleIdentifier, ModuleDeployment>(this.modules);
        modules.put(identifier, deployment);
        this.modules = Collections.unmodifiableMap(modules);
        for(final DeploymentRepositoryListener listener : listeners) {
            listener.deploymentAvailable(identifier, deployment);
        }
    }

    public synchronized void addListener(final DeploymentRepositoryListener listener) {
        listener.listenerAdded(this);
        listeners.add(listener);
    }

    public synchronized void removeListener(final DeploymentRepositoryListener listener) {
        listeners.remove(listener);
    }

    public synchronized void remove(DeploymentModuleIdentifier identifier) {
        final Map<DeploymentModuleIdentifier, ModuleDeployment> modules = new HashMap<DeploymentModuleIdentifier, ModuleDeployment>(this.modules);
        modules.remove(identifier);
        this.modules = Collections.unmodifiableMap(modules);
        for(final DeploymentRepositoryListener listener : listeners) {
            listener.deploymentRemoved(identifier);
        }
    }

    public Map<DeploymentModuleIdentifier, ModuleDeployment> getModules() {
        return modules;
    }

}

@@ -28,24 +28,187 @@
 eployment;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Aimport org.jboss.logging.Logger;%0A
 import org.j
@@ -410,216 +410,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.concurrent.CopyOnWriteArraySet;%0A%0A
 %0A/**
@@ -750,16 +750,104 @@
 ory%22);%0A%0A
+    private static final Logger logger = Logger.getLogger(DeploymentRepository.class);%0A%0A
     /**%0A
@@ -1932,32 +1932,54 @@
  : listeners) %7B%0A
+            try %7B%0A    
             list
@@ -2020,32 +2020,162 @@
 r, deployment);%0A
+            %7D catch (Throwable t) %7B%0A                logger.error(%22Exception calling deployment added listener%22, t);%0A            %7D%0A
         %7D%0A    %7D%0A
@@ -2859,32 +2859,54 @@
  : listeners) %7B%0A
+            try %7B%0A    
             list
@@ -2933,32 +2933,164 @@
 ed(identifier);%0A
+            %7D catch (Throwable t) %7B%0A                logger.error(%22Exception calling deployment removal listener%22, t);%0A            %7D%0A
         %7D%0A    %7D%0A

Handle deployement repository listener errors