use of org.alfresco.util.schemacomp.XMLToSchema in project alfresco-repository by Alfresco.
the class SchemaBootstrap method attemptValidateSchema.
/**
* Validates and compares current DB schema with schema reference definition, specified in <code>referenceResource</code> parameter.<br />
* <br />
* The method supports two mechanisms to report validation results:
* <ol>
* <li>using an external output stream, specified as <code>out</code>;</li>
* <li>using specially created {@link FileOutputStream}, which represents temporary file with name, formatted in accordance with <code>outputFileNameTemplate</code> template.</li>
* </ol>
* It is necessary to take care about freeing resources of output stream in case of the 1st approach.<br />
* <b>N.B.:</b> The method only writes messages of the report. And it <b>doesn't flush and doesn't close</b> the specified output stream!<br />
* <br />
*
* @param referenceResource - {@link Resource} instance, which determines file of reference schema
* @param outputFileNameTemplate - {@link String} value, which determines template of temporary filename for validation report. <b>It can't be <code>null</code> if
* <code>out</code> is <code>null</code></b>!
* @param out - {@link PrintWriter} instance, which represents an external output stream for writing a validation report. This stream is never closed or flushed. <b>It can't be
* <code>null</code> if <code>outputFileNameTemplate</code> is <code>null</code></b>!
* @return {@link Integer} value, which determines amount of errors or warnings that were detected during validation
*/
private int attemptValidateSchema(Resource referenceResource, String outputFileNameTemplate, PrintWriter out) {
Date startTime = new Date();
InputStream is = null;
try {
is = new BufferedInputStream(referenceResource.getInputStream());
} catch (IOException e) {
throw new RuntimeException("Unable to open schema reference file: " + referenceResource);
}
XMLToSchema xmlToSchema = new XMLToSchema(is);
xmlToSchema.parse();
Schema reference = xmlToSchema.getSchema();
ExportDb exporter = new ExportDb(dataSource, dialect, descriptorService, databaseMetaDataHelper);
exporter.setDbSchemaName(dbSchemaName);
// Ensure that the database objects we're validating are filtered
// by the same prefix as the reference file.
exporter.setNamePrefix(reference.getDbPrefix());
exporter.execute();
Schema target = exporter.getSchema();
SchemaComparator schemaComparator = new SchemaComparator(reference, target, dialect);
schemaComparator.validateAndCompare();
Results results = schemaComparator.getComparisonResults();
Object[] outputFileNameParams = new Object[] { dialect.getClass().getSimpleName(), reference.getDbPrefix() };
PrintWriter pw = null;
File outputFile = null;
try {
if (out == null) {
String outputFileName = MessageFormat.format(outputFileNameTemplate, outputFileNameParams);
outputFile = TempFileProvider.createTempFile(outputFileName, ".txt");
try {
pw = new PrintWriter(outputFile, SchemaComparator.CHAR_SET);
} catch (FileNotFoundException error) {
throw new RuntimeException("Unable to open file for writing: " + outputFile);
} catch (UnsupportedEncodingException error) {
throw new RuntimeException("Unsupported char set: " + SchemaComparator.CHAR_SET, error);
}
} else {
pw = out;
}
// Populate the file with details of the comparison's results.
for (Result result : results) {
pw.print(result.describe());
pw.print(SchemaComparator.LINE_SEPARATOR);
}
} finally {
// We care only about output streams for reporting, which are created specially for current reference resource...
if (null == out) {
pw.close();
}
}
if (results.size() == 0) {
LogUtil.info(logger, INFO_SCHEMA_COMP_ALL_OK, referenceResource);
} else {
int numProblems = results.size();
if (outputFile == null) {
LogUtil.warn(logger, WARN_SCHEMA_COMP_PROBLEMS_FOUND_NO_FILE, numProblems);
} else {
LogUtil.warn(logger, WARN_SCHEMA_COMP_PROBLEMS_FOUND, numProblems, outputFile);
}
}
Date endTime = new Date();
long durationMillis = endTime.getTime() - startTime.getTime();
LogUtil.debug(logger, DEBUG_SCHEMA_COMP_TIME_TAKEN, durationMillis);
return results.size();
}
Aggregations