public MessageConnection connectionOpen(MessageConnection mc)
throws IOException {
boolean sendError = true;
HandshakeErrorMessage error = null;
try {
// Begin Handshake
//
HandshakeBeginMessage begin = null;
Message msg = mc.readMessage();
if (msg instanceof HandshakeBeginMessage) {
begin = (HandshakeBeginMessage) msg;
} else if (msg instanceof HandshakeErrorMessage) {
// Throw exception and let GenericConnector
// close the connection
//
sendError = false;
error = (HandshakeErrorMessage) msg;
throwExceptionOnError(error);
} else {
throw new IOException("Unexpected message: " +
msg.getClass().getName());
}
String serverProfiles = begin.getProfiles();
String serverVersion = begin.getVersion();
if (logger.traceOn()) {
logger.trace("connectionOpen", ">>>>> Handshake Begin <<<<<");
logger.trace("connectionOpen", "Server Supported Profiles [ " +
serverProfiles + " ]");
logger.trace("connectionOpen", "Server JMXMP Version [ " +
serverVersion + " ]");
}
// Negotiate JMXMP protocol version
//
String clientVersion = "1.0";
if (!clientVersion.equals(serverVersion)) {
if (clientVersion.compareTo(serverVersion) > 0) {
throw new IOException("The client is already using the " +
"lowest JMXMP protocol version [" +
clientVersion + "]");
} else {
VersionMessage cjmxmp = new VersionMessage(clientVersion);
mc.writeMessage(cjmxmp);
msg = mc.readMessage();
if (msg instanceof VersionMessage) {
VersionMessage sjmxmp = (VersionMessage) msg;
if (!clientVersion.equals(sjmxmp.getVersion())) {
throw new IOException("Protocol version " +
"mismatch: Client [" +
clientVersion +
"] vs. Server [" +
sjmxmp.getVersion() + "]");
}
} else if (msg instanceof HandshakeErrorMessage) {
// Throw exception and let GenericConnector
// close the connection
//
sendError = false;
error = (HandshakeErrorMessage) msg;
throwExceptionOnError(error);
} else {
throw new IOException("Unexpected message: " +
msg.getClass().getName());
}
}
}
// Execute client selected profiles
//
List profileList = selectProfiles(serverProfiles);
for (Iterator i = profileList.iterator(); i.hasNext(); ) {
String profile = (String) i.next();
ProfileClient p =
ProfileClientFactory.createProfile(profile, env);
if (logger.traceOn()) {
logger.trace("connectionOpen",
">>>>> Profile " +
p.getClass().getName() +
" <<<<<");
}
ProfileMessage pm = null;
p.initialize(mc);
while (!p.isComplete()) {
pm = p.produceMessage();
mc.writeMessage(pm);
msg = mc.readMessage();
if (msg instanceof ProfileMessage) {
p.consumeMessage((ProfileMessage)msg);
} else if (msg instanceof HandshakeErrorMessage) {
// Throw exception and let GenericConnector
// close the connection
//
sendError = false;
error = (HandshakeErrorMessage) msg;
throwExceptionOnError(error);
} else {
throw new IOException("Unexpected message: " +
msg.getClass().getName());
}
}
p.activate();
profilesList.add(p);
}
// Send client handshake end
//
Object ccontext = (Object) env.get("jmx.remote.context");
HandshakeEndMessage cend = new HandshakeEndMessage(ccontext, null);
if (logger.traceOn()) {
logger.trace("connectionOpen",
">>>>> Handshake End <<<<<");
logger.trace("connectionOpen",
"Client Context Object [ " + ccontext + " ]");
}
mc.writeMessage(cend);
// Wait for server handshake end
//
HandshakeEndMessage send = null;
msg = mc.readMessage();
if (msg instanceof HandshakeEndMessage) {
send = (HandshakeEndMessage) msg;
} else if (msg instanceof HandshakeErrorMessage) {
// Throw exception and let GenericConnector
// close the connection
//
sendError = false;
error = (HandshakeErrorMessage) msg;
throwExceptionOnError(error);
} else {
throw new IOException("Unexpected message: " +
msg.getClass().getName());
}
Object scontext = send.getContext();
connectionId = send.getConnectionId();
if (logger.traceOn()) {
logger.trace("connectionOpen",
"Server Context Object [ " + scontext + " ]");
logger.trace("connectionOpen",
"Server Connection Id [ " + connectionId + " ]");
}
} catch (Exception e) {
if (sendError) {
try {
mc.writeMessage(new HandshakeErrorMessage(e.toString()));
} catch (Exception hsem) {
if (logger.debugOn()) {
logger.debug(
"connectionOpen",
"Could not send HandshakeErrorMessage to the server",