throw new AgentRegistrationException(msg);
}
}
}
Server registeringServer = getServerManager().getServer();
// if the agent is registering with a loopback address, log a warning since you probably do not want this in production
try {
String address = request.getAddress();
String fullEndpoint = request.getRemoteEndpoint();
boolean loopbackRegistration = false;
if (address.equals("127.0.0.1") || address.equalsIgnoreCase("localhost")) {
loopbackRegistration = true;
} else {
// jboss/remoting transport params might be telling us to have our client connect to a different address
if (fullEndpoint != null) {
if (fullEndpoint.contains("127.0.0.1") || fullEndpoint.contains("localhost")) {
loopbackRegistration = true;
}
}
}
if (loopbackRegistration) {
log.warn("An agent [" + request.getName()
+ "] has registered with a loopback address. This should only be done "
+ "for testing or demo purposes - this agent can only ever interact with this server. " + request);
}
} catch (Exception ignore) {
}
if (agentByName != null) {
log.info("Got agent registration request for existing agent: " + agentByName.getName() + "["
+ agentByName.getAddress() + ":" + agentByName.getPort() + "][" + request.getAgentVersion()
+ "] - Will " + (request.getRegenerateToken() ? "" : "not") + " regenerate a new token");
final String oldAddress = agentByName.getAddress();
final int oldPort = agentByName.getPort();
final String oldRemoteEndpoint = agentByName.getRemoteEndpoint();
agentByName.setServer(registeringServer);
agentByName.setAddress(request.getAddress());
agentByName.setPort(request.getPort());
agentByName.setRemoteEndpoint(request.getRemoteEndpoint());
if (request.getRegenerateToken()) {
agentByName.setAgentToken(generateAgentToken());
}
try {
agentManager.updateAgent(agentByName);
} catch (Exception e) {
log.warn("Could not update the agent in database", e);
throw new AgentRegistrationException(new WrappedRemotingException(e));
}
// if agent is re-registering in order to change its remote endpoint, destroy our old client.
if (!oldAddress.equals(request.getAddress()) || oldPort != request.getPort()
|| !oldRemoteEndpoint.equals(request.getRemoteEndpoint())) {
try {
final Agent oldAgent = new Agent();
oldAgent.setName(agentByName.getName());
oldAgent.setAddress(oldAddress);
oldAgent.setPort(oldPort);
oldAgent.setRemoteEndpoint(oldRemoteEndpoint);
agentManager.destroyAgentClient(oldAgent);
} catch (Exception e) {
log.warn("Could not destroy the agent client - will continue but agent comm may be broken", e);
}
}
} else {
log.info("Got agent registration request for new agent: " + request.getName() + "[" + request.getAddress()
+ ":" + request.getPort() + "][" + request.getAgentVersion() + "]");
// the agent does not yet exist, we need to create it
try {
agentByName = new Agent(request.getName(), request.getAddress(), request.getPort(),
request.getRemoteEndpoint(), generateAgentToken());
agentByName.setServer(registeringServer);
agentManager.createAgent(agentByName);
} catch (Exception e) {
log.warn("Failed to create agent in database", e);
throw new AgentRegistrationException(new WrappedRemotingException(e));
}
}
// get existing or generate new server list for the registering agent
FailoverListComposite failoverList = getPartitionEventManager().agentPartitionEvent(
getSubjectManager().getOverlord(), agentByName.getName(), PartitionEventType.AGENT_REGISTRATION,
agentByName.getName() + " - " + registeringServer.getName());
AgentRegistrationResults results = new AgentRegistrationResults();
results.setAgentToken(agentByName.getAgentToken());
results.setFailoverList(failoverList);