Extensions - the code

Starting with version 3

Parts of the extension

Extensions have two code parts to them:

  1. The Extension, and
  2. The Validators

The Extension

The extension must extend synapticloop.h2zero.extension.Extension, i.e.:

public class BasicExtension extends Extension

package synapticloop.h2zero.extension;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import synapticloop.h2zero.model.Database;
import synapticloop.h2zero.model.Options;
import synapticloop.h2zero.validator.BaseValidator;
import synapticloop.h2zero.validator.BasicValidator;
import synapticloop.templar.Parser;
import synapticloop.templar.exception.ParseException;
import synapticloop.templar.exception.RenderException;
import synapticloop.templar.utils.TemplarContext;

public class BasicExtension extends Extension {

	@Override
	public void generate(JSONObject extensionOptions, Database database, Options options, File outFile, boolean verbose)
			throws RenderException, ParseException {

		// logging
		logInfo("Log an information method.");
		logDebug("Log a debug message.");
		logError("Log an error message.");
		logFatal("Log a fatal message.");

		// getting the context
		TemplarContext templarContext = getDefaultTemplarContext(extensionOptions, database, options);

		// getting the templar file
		Parser parser = getParser("/path-and-name-of-template.templar", verbose);

		// options as to which output directory is required
		String outputBuild = options.getOutputBuild(); // goes to the build directory - for reports and statistics (e.g. build)
		String outputCode = options.getOutputCode(); // goes to the code directory (e.g.src/main/java)
		String outputResources = options.getOutputResources(); // goes to the resources directory (e.g. src/main/resources/)

		// render out the file
		renderToFile(templarContext, parser, "path/to/output/file.extension", verbose);
	}

	@Override
	public List getValidators() {
		List validators = new ArrayList();
		validators.add(new BasicValidator());
		return(validators);
	}
}

Looking through the above code, the built in features and functionality are as follows:

  • Loggers: built in loggers to output messages in the standard format, and that automatically generate the statistics for the h2zero generation.
    • logInfo("Log an information method.");
    • logDebug("Log a debug message.");
    • logError("Log an error message.");
    • logFatal("Log a fatal message.");
  • The templar context: safely retrieve the templar context with objects already placed into it.
    • TemplarContext templarContext = getDefaultTemplarContext(extensionOptions, database, options);
  • The templar parser: safely retrieve the templar parser, from theclasspath.
    • Parser parser = getParser("/path-and-name-of-template.templar", verbose);
  • The templar renderer: safely render the file to the correct location.
    • renderToFile(templarContext, parser, "path/to/output/file.extension", verbose);
    • There are three main output paths which can be retrieved from the h2zero options object - these may be over-ridden by the user.
      • String outputBuild = options.getOutputBuild(); this goes to the build directory - for reports and statistics (e.g. build)
      • String outputCode = options.getOutputCode(); this goes to the code directory (e.g.src/main/java)
      • String outputResources = options.getOutputResources(); this goes to the resources directory (e.g. src/main/resources/)

The Validator

Validators are not required, but, should the exists, will be run through the after the h2zero file has been parsed. Each validator must extend BaseValidator, e.g.

public class BasicValidator extends BaseValidator

package synapticloop.h2zero.validator;

import java.util.List;

import synapticloop.h2zero.model.Database;
import synapticloop.h2zero.model.Options;
import synapticloop.h2zero.model.Table;
import synapticloop.h2zero.model.View;

public class BasicValidator extends BaseValidator {

	@Override
	public void validate(Database database, Options options) {
		// messages
		addInfoMessage("This is an informational message.");
		addWarnMessage("This is a warning message.");
		addFatalMessage("This is a fatal message, which will end the h2zero generation.");

		// get and iterate over the tables
		List<Table> tables = database.getTables();
		for (Table table : tables) {
			// do some validation
			table.getName();
		}

		// get and iterate over the views
		List<View> views = database.getViews();
		for (View view : views) {
			// do some validation
			view.getName();
		}
	}
}

The validator has access to the database object which contains the tables and views, and the options object for configuration items.

All of these items can be used to validate that the extension will work against the h2zero file.