public void removeOrganization(String key) {
    try {
      Organization org = organizations.get(key);

      // Delete all subOrganizations
      if (!org.getAllSubOrganizations().isEmpty() && org.getAllSubOrganizations().size() > 0) {
        Enumeration<String> subOrgs = org.getAllSubOrganizations().keys();
        while (subOrgs.hasMoreElements()) {
          String subOrg = (String) subOrgs.nextElement();
          removeOrganization(subOrg);
        }
      }

      Enumeration<String> orgRoleNames = org.getAllAgentRoles().keys();
      AgentRole orgRole = null;

      while (orgRoleNames.hasMoreElements()) {
        String orgRoleName = (String) orgRoleNames.nextElement();
        orgRole = org.getAgentRole(orgRoleName);
        GenericAgent agent = orgRole.getPlayer();

        Enumeration<String> aRoleNames = agent.getAllAgentRoles().keys();
        AgentRole aRole = null;
        boolean hasOtherOrgs = false;

        // Cancel the role with agents
        while (aRoleNames.hasMoreElements()) {
          String aRoleName = (String) aRoleNames.nextElement();
          aRole = agent.getAgentRole(aRoleName);

          if (aRole.getOwner() != org) {
            aRole.getOwner().getContainerController().acceptNewAgent(agent.getName(), agent);
            hasOtherOrgs = true;
            break;
          }
        }

        // When agent have not other organizations, it will be deleted
        if (!hasOtherOrgs) removeAgent(agent.getName());

        // Removes the instance of AgentRole
        org.removeAgentRole(orgRoleName);
      }

      // Delete the container on JADE and JAMDER
      org.getContainerController().kill();
      organizations.remove(key);
    } catch (ControllerException exception) {
      exception.printStackTrace();
    }
  }
  public Environment(String name, String host, String port) {
    this.name = name;
    this.host = host;
    this.port = port;

    try {
      Runtime runtime = Runtime.instance();
      profile = new ProfileImpl(host, Integer.valueOf(port), name);
      mainConteiner = runtime.createMainContainer(profile);
      AgentController rma =
          mainConteiner.createNewAgent("rma", "jade.tools.rma.rma", new Object[0]);
      rma.start();
    } catch (ControllerException exception) {
      exception.printStackTrace();
    }
  }
  public void addAgent(String key, GenericAgent agent) {
    try {
      if (agent instanceof Organization) return;

      AgentRole role = agent.getAllAgentRoles().values().iterator().next();
      AgentController control = role.getOwner().getContainerController().acceptNewAgent(key, agent);
      // control.start();

      if (!control.getState().getName().equals("Active")) control.start();

      agents.put(key, agent);
    } catch (ControllerException exception) {
      exception.printStackTrace();
    } catch (RuntimeException exception) {
      exception.printStackTrace();
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      nickname = extras.getString("nickname");
    }

    try {
      chatClientInterface =
          MicroRuntime.getAgent(nickname).getO2AInterface(ChatClientInterface.class);
    } catch (StaleProxyException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ControllerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    myReceiver = new MyReceiver();

    IntentFilter refreshParticipantsFilter = new IntentFilter();
    refreshParticipantsFilter.addAction("jade.demo.chat.REFRESH_PARTICIPANTS");
    registerReceiver(myReceiver, refreshParticipantsFilter);

    setContentView(R.layout.participants);

    setListAdapter(
        new ArrayAdapter<String>(
            this, R.layout.participant, chatClientInterface.getParticipantNames()));

    ListView listView = getListView();
    listView.setTextFilterEnabled(true);
    listView.setOnItemClickListener(listViewtListener);
  }