try
{
// simple case - single descriptor package (e.g. xxx-service.xml)
if (vmTemplate != null )
{
VelocityContext ctx = createTemplateContext(ci, properties);
BufferedWriter out = new BufferedWriter(new FileWriter(outputModule));
try {
boolean success = ve.mergeTemplate(template + '/' + vmTemplate, ctx, out);
if (success == true)
{
String errorMsg = (String)ctx.get(TEMPLATE_ERROR_PARAM);
if (errorMsg.length() > 0)
throw new Exception("Template error: " + errorMsg);
else
log.debug("created module '" + outputModule.getName() + "' based on template '" + template + "'");
}
else
throw new Exception("Failed to create module '" + outputModule.getName());
}
finally
{
out.close();
}
}
else
{
// complex case - many descriptors and possibly files to copy
// now output will be a directory instead of a plain descriptor (e.g. xxx.sar)
VelocityContext ctx = createTemplateContext(ci, properties);
// deep copy files if copydir specified
String copydir = ci.getCopydir();
File sourceDir = new File(this.templateDir, template + '/' + copydir);
deepCopy(sourceDir, outputModule);
// go through all declared templates
List templateList = ci.getTemplateInfoList();
for (Iterator i = templateList.iterator(); i.hasNext(); )
{
TemplateInfo ti = (TemplateInfo)i.next();
File outputFile = new File(outputModule, ti.getOutput());
File outputPath = outputFile.getParentFile();
if (!outputPath.exists())
if (!outputPath.mkdirs())
throw new IOException("cannot create directory: " + outputPath);
BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));
try {
boolean success = ve.mergeTemplate(template + '/' + ti.getInput(), ctx, out);
if (success == true)
{
String errorMsg = (String)ctx.get(TEMPLATE_ERROR_PARAM);
if (errorMsg.length() > 0)
throw new Exception("Template error: " + errorMsg);
else
log.debug("created module '" + outputModule.getName() + "' based on template '" + template + "'");