boolean hide_loopback_warning = Boolean.getBoolean("rhq.hide-agent-localhost-warning");
boolean hide_failover_list_warning = false;
while (retry) {
try {
ClientCommandSender sender = getClientCommandSender();
if ((sender == null) || Thread.currentThread().isInterrupted()) {
LOG.debug(AgentI18NResourceKeys.AGENT_REGISTRATION_ABORTED);
retry = false;
} else {
String token = getAgentSecurityToken();
ClientRemotePojoFactory pojo_factory = sender.getClientRemotePojoFactory();
CoreServerService remote_pojo = pojo_factory.getRemotePojo(CoreServerService.class);
AgentConfiguration agent_config = getConfiguration();
ServiceContainerConfiguration server_config = agent_config.getServiceContainerPreferences();
String agent_name = agent_config.getAgentName();
String address = server_config.getConnectorBindAddress();
int port = server_config.getConnectorBindPort();
String remote_endpoint = server_config.getConnectorRemoteEndpoint();
AgentVersion agentVersion = getAgentVersion();
String installId = System.getProperty(AgentRegistrationRequest.SYSPROP_INSTALL_ID);
String installLocation = getAgentHomeDirectory();
if (installLocation != null && installLocation.trim().length() == 0) {
installLocation = null; // tells the server we don't know it
}
AgentRegistrationRequest request = new AgentRegistrationRequest(agent_name, address, port,
remote_endpoint, regenerate_token, token, agentVersion, installId, installLocation);
Thread.sleep(retry_interval);
if (sender.isSending()) {
LOG.debug(AgentI18NResourceKeys.AGENT_REGISTRATION_ATTEMPT, request);
if (!hide_loopback_warning) {
if (remote_endpoint.contains("localhost") || remote_endpoint.contains("127.0.0.")) {
String msg_id = AgentI18NResourceKeys.REGISTERING_WITH_LOOPBACK;
LOG.warn(msg_id, remote_endpoint);
getOut().println(MSG.getMsg(msg_id, remote_endpoint));
getOut().println();
hide_loopback_warning = true; // don't bother to tell the user more than once
}
}
// delete any old token so request is unauthenticated to get server to accept it
agent_config.setAgentSecurityToken(null);
m_commServices.addCustomData(
SecurityTokenCommandAuthenticator.CMDCONFIG_PROP_SECURITY_TOKEN, null);
FailoverListComposite failover_list = null;
try {
AgentRegistrationResults results = remote_pojo.registerAgent(request);
failover_list = results.getFailoverList();
token = results.getAgentToken(); // make sure our finally block gets this - BZ 963982
// Try to do a simple connect to each server in the failover list
// If only some of the servers are unreachable, just keep going;
// the agent will eventually switchover to one of the live servers.
// But if all servers in the list are unreachable, we need to keep retrying hoping
// someone fixes the servers' public endpoints so the agent can reach one or more of them.
boolean test_failover_list = m_configuration.isTestFailoverListAtStartupEnabled();
if (test_failover_list) {
List<String> failed = testFailoverList(failover_list);
if (failed.size() > 0) {
if (failed.size() == failover_list.size()) {
retry = true;
retry_interval = 30000L;
if (!hide_failover_list_warning) {
String msg_id = AgentI18NResourceKeys.FAILOVER_LIST_CHECK_FAILED;
LOG.warn(msg_id, failed.size(), failed.toString());
getOut().println(
MSG.getMsg(msg_id, failed.size(), failed.toString()));
getOut().println();
hide_failover_list_warning = true; // don't bother logging more than once
}
continue; // immediately go back and start the retry
}
}
} else {
LOG.info(AgentI18NResourceKeys.TEST_FAILOVER_LIST_AT_STARTUP_DISABLED);
}
m_registration = results;
got_registered = true;
retry = false;
LOG.info(AgentI18NResourceKeys.AGENT_REGISTRATION_RESULTS, results);
} finally {
// stores the new one if successful; restores the old one if we failed for some reason to register
// Note that we don't retry even if storing the token fails since this kind
// of failure is probably not recoverable even if we try again.
agent_config.setAgentSecurityToken(token);
m_commServices.addCustomData(
SecurityTokenCommandAuthenticator.CMDCONFIG_PROP_SECURITY_TOKEN, token);
LOG.debug(AgentI18NResourceKeys.NEW_SECURITY_TOKEN, token);
}
storeServerFailoverList(failover_list);
m_serverFailoverList = failover_list;
// switch away from the registration server and point this agent to the top of the list
// - this is our primary server that we should connect to
// note that if we are already pointing to the one at the head of the failover list,
// we don't have to failover to another server; the current one is the one we already want
if (failover_list.hasNext()) {
String currentAddress = agent_config.getServerBindAddress();
int currentPort = agent_config.getServerBindPort();
String currentTransport = agent_config.getServerTransport();
ServerEntry nextServer = failover_list.peek();
if (currentAddress.equals(nextServer.address)
&& currentPort == (SecurityUtil.isTransportSecure(currentTransport) ? nextServer.securePort
: nextServer.port)) {
// we are already pointing to the primary server, so all we have to do is
// call next to move the index to the next in the list for when we have to failover in the future
nextServer = failover_list.next();
// [mazz] I don't think we need to do this here anymore - with the addition of
// the remote communicator's initialize callback, this connect request
// will be made the next time a command is sent by the sender
//sendConnectRequestToServer(sender.getRemoteCommunicator(), false);
} else {
failoverToNewServer(sender.getRemoteCommunicator());
}
}
}
}
} catch (AgentNotSupportedException anse) {