/**
   * Add an already created container.
   *
   * @param container the EZBContainer to be added.
   */
  public void addContainer(final EZBContainer container) {
    // Add extensions
    callJContainerConfigExtensions(container.getConfiguration());

    String id = container.getId();
    this.containers.put(id, container);
  }
  /**
   * Creates and adds an ejb3 container to the managed container.
   *
   * @param deployable the container deployable.
   * @return the created container.
   */
  public EZBContainer createContainer(final IDeployable<?> deployable) {
    EZBContainerConfig jConfig = new JContainerConfig(deployable);
    jConfig.setEZBServer(this);
    EZBContainer container = new JContainer3(jConfig);
    container.setClassLoader(this.classLoader);
    addContainer(container);

    return container;
  }
 /**
  * Gets a container managed by this server.
  *
  * @param archive the archive used by the given container.
  * @return the container if it is found, else null.
  */
 public EZBContainer findContainer(final IArchive archive) {
   // Invalid archive
   if (archive == null) {
     return null;
   }
   // Search a container for the given archive.
   for (EZBContainer container : this.containers.values()) {
     if (archive.equals(container.getArchive())) {
       return container;
     }
   }
   return null;
 }
 /**
  * Remove a given container.
  *
  * @param container the container to be removed.
  */
 public void removeContainer(final EZBContainer container) {
   this.containers.remove(container.getId());
 }
  /**
   * Stops the EJB3 server.
   *
   * @throws EmbeddedException if container cannot be stopped.
   */
  public synchronized void stop() throws EmbeddedException {
    // ensure started
    if (!this.started) {
      throw new EmbeddedException("Cannot stop the server as it is not started.");
    }
    this.stopping = true;

    if (this.dispatcher != null) {
      this.dispatcher.dispatch(new EventLifeCycleStopping(this.j2eeManagedObjectId));
    }

    // Stop the containers
    // Use a ListIterator to allow us to safely remove EZBContainers
    // from the List being processed.
    List<EZBContainer> containersList = new ArrayList<EZBContainer>(this.containers.values());
    ListIterator<EZBContainer> li = containersList.listIterator();
    while (li.hasNext()) {
      EZBContainer container = li.next();
      container.stop();
      removeContainer(container);
    }

    // Unregister MBeans
    if (this.serverConfig.isUsingMBeans()) {

      if (this.serverConfig.isStartJMXConnector()) {
        // Stop the JMX Connector
        try {
          JMXRemoteHelper.stopConnector();
        } catch (JMXRemoteException e) {
          // Only log the Exception

          // and continue ...
        }
      }

      // Unregister the Deployer
      if (this.serverConfig.isRegisterDeployerMBean()) {
        try {
          MBeansHelper.getInstance().unregisterMBean(this.deployer);
        } catch (MBeansException e) {
          // Only log the Exception

        }
      }

      // Unregister the Server
      if (this.serverConfig.isRegisterJ2EEServerMBean()) {
        try {
          MBeansHelper.getInstance().unregisterMBean(this);
        } catch (MBeansException e) {
          // Only log the Exception
        }
      }
    }

    // Unbind the RPCInvoker Remote Object
    try {
      new InitialContext().unbind(RMIServerRPC.RPC_JNDI_NAME);
    } catch (NamingException e) {
      // Only log the Exception
    }

    // Unexport
    try {
      PortableRemoteObject.unexportObject(this.invoker);
    } catch (NoSuchObjectException e) {
      // Only log the Exception
    }

    // Dispatch lifecycle event.
    this.dispatcher.dispatch(new EventLifeCycleStopped(this.j2eeManagedObjectId));

    // Unregister from statistic component.
    EZBStatisticComponent statisticComponent = getComponent(EZBStatisticComponent.class);
    if (statisticComponent != null) {
      statisticComponent.unregisterJ2EEManagedObject(this);
    }

    // Unregister from jmx component will be done by the mbean itself.

    // Unregister from event component.
    EZBEventComponent eventComponent = getComponent(EZBEventComponent.class);
    if (eventComponent != null) {
      eventComponent.unregisterJ2EEManagedObject(this);

      // Unregister the NamingExtensions (used to fill the java:comp Context)
      for (IEventListener extension : this.defaultNamingExtensions) {
        eventComponent.getEventService().unregisterListener(extension);
      }
    }

    // Destroy the event dispatcher.
    this.dispatcher.stop();
    this.dispatcher = null;

    // Stop the components
    if (this.serverConfig.isStopComponentsDuringShutdown()) {
      this.componentManager.stopComponents();
    }

    // Stop JACC
    if (this.serverConfig.initJACC()) {
      try {
        PolicyProvider.stop();
      } catch (Exception e) {
        // Only log the Exception
      }
    }

    // EasyBeans is stopped
    this.started = false;
    this.stopped = true;
    this.stopping = false;
  }