private void reportException(Throwable re, int count, ExporterTask generatorTask) {
    log(
        "An exception occurred while running exporter #" + count + ":" + generatorTask.getName(),
        Project.MSG_ERR);
    log("To get the full stack trace run ant with -verbose", Project.MSG_ERR);

    log(re.toString(), Project.MSG_ERR);
    String ex = new String();
    Throwable cause = re.getCause();
    while (cause != null) {
      ex += cause.toString() + "\n";
      if (cause == cause.getCause()) {
        break; // we reached the top.
      } else {
        cause = cause.getCause();
      }
    }
    if (StringHelper.isNotEmpty(ex)) {
      log(ex, Project.MSG_ERR);
    }

    String newbieMessage = getProbableSolutionOrCause(re);
    if (newbieMessage != null) {
      log(newbieMessage);
    }

    if (re instanceof BuildException) {
      throw (BuildException) re;
    } else {
      throw new BuildException(re, getLocation());
    }
  }
  public void validateParameters() {
    super.validateParameters();
    if (StringHelper.isEmpty(query) && queries.isEmpty()) {
      throw new BuildException("Need to specify at least one query.");
    }

    for (Iterator iter = queries.iterator(); iter.hasNext(); ) {
      HQL hql = (HQL) iter.next();
      if (StringHelper.isEmpty(hql.query)) {
        throw new BuildException("Query must not be empty");
      }
    }
  }
 protected Exporter configureExporter(Exporter exp) {
   QueryExporter exporter = (QueryExporter) exp;
   List queryStrings = new ArrayList();
   if (StringHelper.isNotEmpty(query)) {
     queryStrings.add(query);
   }
   for (Iterator iter = queries.iterator(); iter.hasNext(); ) {
     HQL hql = (HQL) iter.next();
     if (StringHelper.isNotEmpty(hql.query)) {
       queryStrings.add(hql.query);
     }
   }
   exporter.setQueries(queryStrings);
   exporter.setFilename(filename);
   super.configureExporter(exp);
   return exporter;
 }
  protected Exporter configureExporter(Exporter exp) {
    super.configureExporter(exp);

    if (exp instanceof GenericExporter) {
      GenericExporter exporter = (GenericExporter) exp;
      if (filePattern != null) {
        exporter.setFilePattern(filePattern);
      }
      if (templateName != null) {
        exporter.setTemplateName(templateName);
      }
      if (forEach != null) {
        exporter.setForEach(forEach);
      }
    }

    return exp;
  }
 public void execute() {
   parent.log("Executing: [" + query + "]");
   super.execute();
 }
  public void execute() {
    AntClassLoader loader;
    MergeFileSystemAndClassPathXMLApplicationContext mergeContext;
    try {
      ConfigurationOnlyState state = new ConfigurationOnlyState();
      state.setConfigurationOnly(true);
      ConfigurationOnlyState.setState(state);

      loader = getProject().createClassLoader(classPath);
      ClassLoader classLoader = this.getClass().getClassLoader();
      loader.setParent(
          classLoader); // if this is not set, classes from the taskdef cannot be found - which is
                        // crucial for e.g. annotations.
      loader.setThreadContextLoader();
      // launch the service merge application context to get the entity configuration for the entire
      // framework
      String[] contexts =
          StandardConfigLocations.retrieveAll(StandardConfigLocations.TESTCONTEXTTYPE);
      String[] otherContexts = new String[classPathApplicationContexts.size()];
      for (int j = 0; j < otherContexts.length; j++) {
        otherContexts[j] = classPathApplicationContexts.get(j).getPath();
      }
      contexts = (String[]) ArrayUtils.addAll(contexts, otherContexts);

      String[] fileSystemItems = new String[fileSystemApplicationContexts.size()];
      for (int j = 0; j < fileSystemItems.length; j++) {
        fileSystemItems[j] = fileSystemApplicationContexts.get(j).getPath();
      }
      mergeContext =
          new MergeFileSystemAndClassPathXMLApplicationContext(contexts, fileSystemItems);
    } catch (Exception e) {
      throw new BuildException(e, getLocation());
    } finally {
      ConfigurationOnlyState.setState(null);
    }
    int count = 1;
    ExporterTask generatorTask = null;
    try {
      for (Object configuration : configurationTasks) {
        JPAConfigurationTask configurationTask = (JPAConfigurationTask) configuration;
        log("Executing Hibernate Tool with a " + configurationTask.getDescription());
        @SuppressWarnings("rawtypes")
        Iterator iterator = generators.iterator();
        while (iterator.hasNext()) {
          generatorTask = (ExporterTask) iterator.next();
          log(count++ + ". task: " + generatorTask.getName());
          generatorTask.setOutputFileName(
              configurationTask.getDialect()
                  + "_"
                  + configurationTask.getPersistenceUnit()
                  + ".sql");
          generatorTask.setDestdir(destDir);
          generatorTask.setConfiguration(configurationTask.createConfiguration(mergeContext));
          generatorTask.execute();
        }
      }
    } catch (RuntimeException re) {
      reportException(re, count, generatorTask);
    } finally {
      if (loader != null) {
        loader.resetThreadContextLoader();
        loader.cleanup();
      }
    }
    try {
      if (combinePersistenceUnits) {
        ArrayList<File> combine = new ArrayList<File>();
        for (Object configuration : configurationTasks) {
          JPAConfigurationTask configurationTask = (JPAConfigurationTask) configuration;
          File[] sqlFiles = destDir.listFiles(new SqlFileFilter());
          for (File file : sqlFiles) {
            if (file.getName().startsWith(configurationTask.getDialect())) {
              combine.add(file);
            }
          }
          combineFiles(combine);
          combine.clear();
        }
      }
      if (refineFileNames) {
        File[] sqlFiles = destDir.listFiles(new SqlFileFilter());
        for (File file : sqlFiles) {
          String filename = file.getName();
          String[] starters = {"org.hibernate.dialect.", "org.broadleafcommerce.profile.util.sql."};
          for (String starter : starters) {
            if (filename.startsWith(starter)) {
              String newFileName = filename.substring(starter.length(), filename.length());
              file.renameTo(new File(destDir, newFileName));
            }
          }
        }
      }
    } catch (Exception e) {
      throw new BuildException(e, getLocation());
    }
  }