SQLException {
if (server == null) {
throw new SyncDatabaseException("error.argument");
}
DatabaseResource db = null;
Connection conn = null;
try {
// get replica server connection
db = new DatabaseResource(server);
conn = db.getConnection();
final ArrayList<ReplicaStatus> replicas = SyncDatabaseDAO
.getReplicaStatus(conn, schema, table);
final StringBuilder status = new StringBuilder();
status.append("replica status\n");
// get display size
final int[] widths = new int[REPLICASTATUS_COLUMNS];
for (int i = 0; i < REPLICASTATUS_COLUMNS; i++) {
widths[i] = ReplicaStatus.HEADERS[i].length();
}
widths[REPLICASTATUS_LAST_REFRESH] = TIMESTAMP_WIDTH;
for (final ReplicaStatus replicaStatus : replicas) {
if (replicaStatus.getSchema() != null) {
widths[REPLICASTATUS_SCHEMA] = Math.max(
widths[REPLICASTATUS_SCHEMA], replicaStatus
.getSchema().getBytes().length);
}
if (replicaStatus.getTable() != null) {
widths[REPLICASTATUS_TABLE] = Math.max(
widths[REPLICASTATUS_TABLE], replicaStatus
.getTable().getBytes().length);
}
widths[REPLICASTATUS_MASTER] = Math.max(
widths[REPLICASTATUS_MASTER], replicaStatus.getMaster()
.getBytes().length);
}
// append header
int i;
for (i = 0; i < REPLICASTATUS_MASTER; i++) {
appendStringValue(status, ReplicaStatus.HEADERS[i], widths[i],
false);
}
appendStringValue(status,
ReplicaStatus.HEADERS[REPLICASTATUS_MASTER],
widths[REPLICASTATUS_MASTER], !cost);
if (cost) {
appendStringValue(status,
ReplicaStatus.HEADERS[REPLICASTATUS_COST],
widths[REPLICASTATUS_COST], true);
}
// append separator
for (i = 0; i < REPLICASTATUS_MASTER; i++) {
appendSeparator(status, widths[i], false);
}
appendSeparator(status, widths[REPLICASTATUS_MASTER], !cost);
if (cost) {
appendSeparator(status, widths[REPLICASTATUS_COST], true);
}
// append subscription data
for (final ReplicaStatus replicaStatus : replicas) {
appendStringValue(status, replicaStatus.getSchema(),
widths[REPLICASTATUS_SCHEMA], false);
appendStringValue(status, replicaStatus.getTable(),
widths[REPLICASTATUS_TABLE], false);
appendTimestampValue(status, replicaStatus.getLastRefresh(),
false);
appendStringValue(status, replicaStatus.getMaster(),
widths[REPLICASTATUS_MASTER], !cost);
if (cost) {
double incrementalCost = Double.NaN;
try {
incrementalCost = getCost(replicaStatus.getMaster(),
replicaStatus.getSubsID());
} catch (Exception e) {
log.debug(e.getMessage());
log.warn(mProperty
.getMessage("warning.master.broken",
SyncDatabaseDAO.getTablePrint(
replicaStatus.getSchema(),
replicaStatus.getTable())));
}
appendDoubleValue(status, incrementalCost,
widths[REPLICASTATUS_COST], true);
}
}
return status.toString();
} finally {
// release resources
if (conn != null) {
conn.close();
}
if (db != null) {
db.stop();
}
}
}