}
StreamResult streamResult = new StreamResult(os);
final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = transformerFactory.newTransformerHandler();
Transformer serializer = handler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, SyncopeConstants.DEFAULT_ENCODING);
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(streamResult);
handler.startDocument();
handler.startElement("", "", ROOT_ELEMENT, new AttributesImpl());
Connection conn = null;
ResultSet rs = null;
try {
conn = DataSourceUtils.getConnection(dataSource);
final DatabaseMetaData meta = conn.getMetaData();
final String schema = dbSchema;
rs = meta.getTables(null, schema, null, new String[] {"TABLE"});
final Set<String> tableNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
LOG.debug("Found table {}", tableName);
if (isTableAllowed(tableName)) {
tableNames.add(tableName);
}
}
LOG.debug("Tables to be exported {}", tableNames);
// then sort tables based on foreign keys and dump
for (String tableName : sortByForeignKeys(conn, tableNames)) {
try {
doExportTable(handler, conn, tableName, TABLES_TO_BE_FILTERED.get(tableName.toUpperCase()));
} catch (Exception e) {
LOG.error("Failure exporting table {}", tableName, e);
}
}
} catch (SQLException e) {
LOG.error("While exporting database content", e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
LOG.error("While closing tables result set", e);
}
}
DataSourceUtils.releaseConnection(conn, dataSource);
if (conn != null) {
try {
if (!conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
LOG.error("While releasing connection", e);
}
}
}
handler.endElement("", "", ROOT_ELEMENT);
handler.endDocument();
}