}
private void handleInformCloned(VerticalCommand cmd) throws IMTPException, NotFoundException, NameClashException, JADESecurityException { // HandleInformCloned start
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
Location where = (Location)params[1];
String newName = (String)params[2];
try {
//#J2ME_EXCLUDE_BEGIN
//Register the clone in the Code Locator in the case its father is a jar agent
String containerName = myContainer.getID().getName();
Agent agent = myContainer.acquireLocalAgent(agentID);
String codeContainerName = getClassSite(agent);
myContainer.releaseLocalAgent(agentID);
//Check if the code is in the same container or in a remote one.
AgentManagementService amSrv = (AgentManagementService) myFinder.findService(AgentManagementService.NAME);
CodeLocator codeLocator = amSrv.getCodeLocator();
if (codeContainerName == null) codeContainerName = containerName;
if (containerName.equals(codeContainerName)) {
if (codeLocator.isRegistered(agentID)) {
if(myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE," adding clone " + newName + " to code locator.");
}
codeLocator.cloneAgent(agentID, new AID(newName,AID.ISLOCALNAME));
}
} else {
//Send a CLONE_CODE_LOCATOR_ENTRY command to the container with the agent code.
AgentMobilitySlice codeSlice = (AgentMobilitySlice) getSlice(codeContainerName);
try {
codeSlice.cloneCodeLocatorEntry(agentID, new AID(newName,AID.ISLOCALNAME));
} catch (IMTPException imtpe) {
// Try to get a newer slice and repeat...
codeSlice = (AgentMobilitySlice) getSlice(codeContainerName);
codeSlice.cloneCodeLocatorEntry(agentID, new AID(newName,AID.ISLOCALNAME));
}
}
//#J2ME_EXCLUDE_END
//log("Cloning agent " + agentID + " on container " + where.getName(), 1);
if(myLogger.isLoggable(Logger.CONFIG))
myLogger.log(Logger.CONFIG,"Cloning agent " + agentID + " on container " + where.getName());
Agent a = myContainer.acquireLocalAgent(agentID);
if (a == null) {
//System.out.println("Internal error: handleClone() called with a wrong name (" + agentID + ") !!!");
if(myLogger.isLoggable(Logger.SEVERE))
myLogger.log(Logger.SEVERE,"Internal error: handleClone() called with a wrong name (" + agentID + ") !!!");
return;
}
AgentMobilitySlice dest = (AgentMobilitySlice)getSlice(where.getName());
if (dest == null) {
myLogger.log(Logger.SEVERE,"Destination "+where.getName()+" does not exist or does not support mobility");
return;
}
if(myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Destination container for agent " + agentID + " found");
// Serialize the agent
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream encoder = new ObjectOutputStream(out);
encoder.writeObject(a);
byte[] bytes = out.toByteArray();
//log("Agent " + agentID + " correctly serialized", 2);
if(myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Agent " + agentID + " correctly serialized");
// Gets the container where the agent classes can be retrieved
String classSiteName = (String)sites.get(a);
if (classSiteName == null) {
// The agent was born on this container
classSiteName = getLocalNode().getName();
}
// Create the agent on the destination container with the new AID
AID newID = new AID(newName, AID.ISLOCALNAME);
try {
dest.createAgent(newID, bytes, classSiteName, CLONING, CREATE_AND_START);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
dest = (AgentMobilitySlice)getFreshSlice(where.getName());
dest.createAgent(newID, bytes, classSiteName, CLONING, CREATE_AND_START);
}
if(myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Cloned Agent " + newID + " correctly created on destination container");