<commit_before>/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be usefubut
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package jade.wrapper;

import jade.wrapper.PlatformController.Listener;
import jade.core.AID;
import jade.core.AgentContainerImpl;
import jade.core.NotFoundException;
import jade.core.IMTPException;
import jade.core.AgentManager;

import jade.core.event.*;

import jade.mtp.MTPException;

import java.util.Vector;
import java.util.Enumeration;

/**
   This class is a Proxy class, allowing access to a JADE agent
   container. Invoking methods on instances of this class, it is
   possible to request services from <it>in-process</it> agent
   containers.
   This class must not be instantiated by applications. Instead, use
   the <code>createContainer()</code> method in class
   <code>Runtime</code>.
   <br>
   <b>NOT available in MIDP</b>
   <br>
   @see jade.core.Runtime#createAgentContainer(Profile)

   @author <NAME> - Universita' di Parma

 */
public class AgentContainer implements PlatformController {

  private AgentContainerImpl myImpl;
  private String myPlatformName;
  private State platformState = PlatformState.PLATFORM_STATE_VOID;
	private ListenerManager myListenerManager = new ListenerManager();
	
  /**
     Public constructor. This constructor requires a concrete
     implementation of a JADE agent container, which cannot be
     instantiated by applications, so it cannot be meaningfully called
     from application code. The proper way to create an agent
     container from an application is to call the
     <code>Runtime.createContainer()</code> method.
     @see jade.core.Runtime#createAgentContainer(Profile)
     @param impl A concrete implementation of a JADE agent container.
     @param platformName the name of the platform
   */
  public AgentContainer(AgentContainerImpl impl, String platformName) {
    myImpl = impl;
    myPlatformName = platformName;
    platformState = PlatformState.PLATFORM_STATE_READY;
  }


  /**
   * Get agent proxy to local agent given its name.
   * @param localAgentName The short local name of the desired agent.
   * @throws ControllerException If any probelms occur obtaining this proxy.
   */
  public AgentController getAgent(String localAgentName) throws ControllerException {
      // FIXME. To check for security permissions
    if(myImpl == null) {
      throw new ControllerException("Stale proxy.");
    }
    AID agentID = new AID(localAgentName, AID.ISLOCALNAME);
    AgentController a = myImpl.getAgent(agentID);
    if (a == null) {
      throw new ControllerException("Agent " + localAgentName + " not found.");
    } 
    return a; 
  }




  /**
     Creates a new JADE agent, running within this container, 
     @param nickname A platform-unique nickname for the newly created
     agent. The agent will be given a FIPA compliant agent identifier
     using the nickname and the ID of the platform it is running on.
     @param className The fully qualified name of the class that
     implements the agent.
     @param args An object array, containing initialization parameters
     to pass to the new agent. 
     @return A proxy object, allowing to call state-transition forcing
     methods on the real agent instance.*/
  public AgentController createNewAgent(String nickname, String className, Object[] args) throws StaleProxyException {
    if(myImpl == null)
      throw new StaleProxyException();
    try {
      jade.core.Agent a = (jade.core.Agent)Class.forName(new String(className)).newInstance();
      a.setArguments(args);
      AID agentID = new AID(nickname, AID.ISLOCALNAME);
      myImpl.initAgent(agentID, a, false);

      Agent result = new Agent(agentID, a);
      return result;
    }
    catch(Exception e) {
      throw new StaleProxyException(e); // it would have been better throwing a ControllerException but that would have broken backward-compatibilityfor 
    }

  }

    // HP Patch begin ----------------------------------------------------------------------------------
    /**
     * Add an Agent to this container. Typically Agent would be some class extending
     * Agent which was instantiated and configured.
     * @param nickname A platform-unique nickname for the newly created agent.
     * The agent will be given a FIPA compliant agent identifier using the nickname and
     * the ID of the platform it is running on.
     * @param anAgent The agent to be added to this agent container.
     * @return An AgentController, allowing to call state-transition forcing methods on the real agent instance.
     */
    public Agent acceptNewAgent(String nickname, jade.core.Agent anAgent)
                                          throws StaleProxyException {
        if (myImpl == null) {
            throw new StaleProxyException();
        }
        AID agentID = new AID(nickname, AID.ISLOCALNAME);
        try {
            myImpl.initAgent(agentID, anAgent, false);
        }
        catch(Exception e) {
            throw new StaleProxyException(e);
        }
        return new Agent(agentID, anAgent);
    }

    /**
     * Kill a particular agent.
     * @param nickname A platform-unique nickname of the agent to kill.

    public void killAgent(String nickname)
            throws StaleProxyException, IMTPException, NotFoundException {
        if (myImpl == null) {
            throw new StaleProxyException();
        }
        AID agentID = new AID(nickname, AID.ISLOCALNAME);

        myImpl.killAgent(agentID);
    }
    */
    // HP Patch end ------------------------------------------------------------------------------------


  /**<commit_msg>Replaced old security code by using the Credential interface.<commit_after>/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be usefubut
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package jade.wrapper;

import jade.wrapper.PlatformController.Listener;
import jade.core.AID;
import jade.core.AgentContainerImpl;
import jade.core.NotFoundException;
import jade.core.IMTPException;
import jade.core.AgentManager;

import jade.core.event.*;

import jade.mtp.MTPException;

import java.util.Vector;
import java.util.Enumeration;
import jade.security.JADEPrincipal;
import jade.security.Credentials;

/**
   This class is a Proxy class, allowing access to a JADE agent
   container. Invoking methods on instances of this class, it is
   possible to request services from <it>in-process</it> agent
   containers.
   This class must not be instantiated by applications. Instead, use
   the <code>createContainer()</code> method in class
   <code>Runtime</code>.
   <br>
   <b>NOT available in MIDP</b>
   <br>
   @see jade.core.Runtime#createAgentContainer(Profile)

   @author <NAME> - Universita' di Parma

 */
public class AgentContainer implements PlatformController {

  private AgentContainerImpl myImpl;
  private String myPlatformName;
  private State platformState = PlatformState.PLATFORM_STATE_VOID;
	private ListenerManager myListenerManager = new ListenerManager();
	
  /**
     Public constructor. This constructor requires a concrete
     implementation of a JADE agent container, which cannot be
     instantiated by applications, so it cannot be meaningfully called
     from application code. The proper way to create an agent
     container from an application is to call the
     <code>Runtime.createContainer()</code> method.
     @see jade.core.Runtime#createAgentContainer(Profile)
     @param impl A concrete implementation of a JADE agent container.
     @param platformName the name of the platform
   */
  public AgentContainer(AgentContainerImpl impl, String platformName) {
    myImpl = impl;
    myPlatformName = platformName;
    platformState = PlatformState.PLATFORM_STATE_READY;
  }


  /**
   * Get agent proxy to local agent given its name.
   * @param localAgentName The short local name of the desired agent.
   * @throws ControllerException If any probelms occur obtaining this proxy.
   */
  public AgentController getAgent(String localAgentName) throws ControllerException {
      // FIXME. To check for security permissions
    if(myImpl == null) {
      throw new ControllerException("Stale proxy.");
    }
    AID agentID = new AID(localAgentName, AID.ISLOCALNAME);
    AgentController a = myImpl.getAgent(agentID);
    if (a == null) {
      throw new ControllerException("Agent " + localAgentName + " not found.");
    } 
    return a; 
  }




  /**
     Creates a new JADE agent, running within this container, 
     @param nickname A platform-unique nickname for the newly created
     agent. The agent will be given a FIPA compliant agent identifier
     using the nickname and the ID of the platform it is running on.
     @param className The fully qualified name of the class that
     implements the agent.
     @param args An object array, containing initialization parameters
     to pass to the new agent. 
     @return A proxy object, allowing to call state-transition forcing
     methods on the real agent instance.*/
  public AgentController createNewAgent(String nickname, String className, Object[] args) throws StaleProxyException {
    if(myImpl == null)
      throw new StaleProxyException();
    try {
      jade.core.Agent a = (jade.core.Agent)Class.forName(new String(className)).newInstance();
      a.setArguments(args);
      AID agentID = new AID(nickname, AID.ISLOCALNAME);
      myImpl.initAgent(agentID, a, false, (JADEPrincipal)null, (Credentials)null);

      Agent result = new Agent(agentID, a);
      return result;
    }
    catch(Exception e) {
      throw new StaleProxyException(e); // it would have been better throwing a ControllerException but that would have broken backward-compatibilityfor 
    }

  }

    // HP Patch begin ----------------------------------------------------------------------------------
    /**
     * Add an Agent to this container. Typically Agent would be some class extending
     * Agent which was instantiated and configured.
     * @param nickname A platform-unique nickname for the newly created agent.
     * The agent will be given a FIPA compliant agent identifier using the nickname and
     * the ID of the platform it is running on.
     * @param anAgent The agent to be added to this agent container.
     * @return An AgentController, allowing to call state-transition forcing methods on the real agent instance.
     */
    public Agent acceptNewAgent(String nickname, jade.core.Agent anAgent)
                                          throws StaleProxyException {
        if (myImpl == null) {
            throw new StaleProxyException();
        }
        AID agentID = new AID(nickname, AID.ISLOCALNAME);
        try {
            myImpl.initAgent(agentID, anAgent, false, (JADEPrincipal)null, (Credentials)null);
        }
        catch(Exception e) {
            throw new StaleProxyException(e);
        }
        return new Agent(agentID, anAgent);
    }

    /**
     * Kill a particular agent.
     * @param nickname A platform-unique nickname of the agent to kill.

    public void killAgent(String nickname)
            throws StaleProxyException, IMTPException, NotFoundException {
        if (myImpl == null) {
            throw new StaleProxyException();
        }
        AID agentID = new AID(nickname, AID.ISLOCALNAME);

        myImpl.killAgent(agentID);
    }
    */
    // HP Patch end ------------------------------------------------------------------------------------


  /**
{'fraction_non_alphanumeric': 0.10970623864324652, 'fraction_numerical': 0.0030284675953967293, 'mean_word_length': 2.95005980861244, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1440521', 'n_tokens_mistral': 3419, 'n_tokens_neox': 3259, 'n_words': 1350}