shared_ptr<cms::Destination>
Destination::toCMSDestination(cms::Session &session) const {
    if (isTopic())
        return shared_ptr<cms::Destination>(session.createTopic(str()));
    else
        return shared_ptr<cms::Destination>(session.createQueue(str()));
}
void
OutputPlugin::connect(const std::string &topic)
{
	try {
		current_connection = getConnection();
		if(session == NULL) {
			session = current_connection->createSession(/* TODO: ackMode */);
			destination = session->createTopic(topic);
			producer = session->createProducer(destination);
		}
		current_connection->start();
		releaseConnection();
	} catch (cms::CMSException &e) {
		glite_common_log(IL_LOG_CATEGORY, LOG_PRIORITY_DEBUG, "OutputPlugin::connect exception: %s", e.what());
		releaseConnection();
		cleanup();
		throw e;
	}
}
void
OutputPlugin::close()
{
	if(producer != NULL) {
		delete producer;
		producer = NULL;
	}
	if(destination != NULL) {
		delete destination;
		destination = NULL;
	}
	if(session != NULL) {
		session->close();
		delete session;
		session = NULL;
	}
}
cms::Message *
OutputPlugin::createMessage(edg_wll_JobStat &state_out)
{
	cms::TextMessage *cms_msg = session->createTextMessage();
	char *s;
	unsigned int i;
	std::ostringstream body;
	bool first = true;

	body << "{";
	/* jobid */
	s = glite_jobid_unparse(state_out.jobId);
	if(s) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"jobid\" : \"" << s << "\"";
		free(s);
	}
	/* ownerDn */
	if(state_out.owner) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"ownerDn\" : \"" << state_out.owner << "\"";
		// cms_msg->setStringProperty("ownerDn", val);
	}
	/* voname */
	s = edg_wll_JDLField(&state_out,"VirtualOrganisation");
	if(s) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"VirtualOrganisation\" : \"" << s << "\"";
		free(s);
	}
	/* bkHost */
	glite_jobid_getServerParts(state_out.jobId, &s, &i);
	if(s) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"bkHost\" : \"" << s << "\"";
		free(s);
	}
	/* networkServer */
	/* TODO: XXX cut out hostname */
	if(state_out.network_server) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"networkHost\" : \"" << state_out.network_server << "\"";
	}
	timeval2str(&state_out.lastUpdateTime, &s);
	if(s) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"lastUpdateTime\" : \"" << s << "\"";
		free(s);
	}
	/* stateName */
	s = edg_wll_StatToString(state_out.state);
	if(s) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"stateName\" : \"" << s << "\"";
		free(s);
	}
	timeval2str(&state_out.stateEnterTime, &s);
	if(s) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"stateStartTime\" : \"" << s << "\"";
		free(s);
	}
	/* condorId */
	if(state_out.condorId) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"condorId\" : \"" << state_out.condorId << "\"";
	}
	/* destSite */
	if(state_out.destination) {
		if (first) { first = false; } else { body << ", "; }
		if (trio_asprintf(&s, "%|Js", state_out.destination) == -1) s = NULL;
		body << "\"destSite\" : \"" << s << "\"";
		free(s);
	}
	/* exitCode */
	if (first) { first = false; } else { body << ", "; }
	body << "\"exitCode\" : " << state_out.exit_code;
	/* doneCode */
	if (first) { first = false; } else { body << ", "; }
	body << "\"doneCode\" : " << state_out.done_code;
	/* statusReason */
	if(state_out.reason) {
		if (first) { first = false; } else { body << ", "; }
		if (trio_asprintf(&s, "%|Js", state_out.reason) == -1) s = NULL;
		body << "\"statusReason\" : \"" << s << "\"";
		free(s);
	}
	/* summaries */
	if(state_out.history) {
		if (first) { first = false; } else { body << ", "; }
		body << "\"history\" : " << state_out.history;
	}
	body << "}";

	cms_msg->setText(body.str().c_str());
	cms_msg->setStringProperty("Content-type", "text/javascript");

	return cms_msg;
}