public boolean preLaunchCheck(
      ILaunchConfiguration configuration, String mode, IProgressMonitor monitor)
      throws CoreException {
    ExporterAttributes attributes = new ExporterAttributes(configuration);

    String configName = attributes.getConsoleConfigurationName();
    if (StringHelper.isEmpty(configName)) {
      abort(
          HibernateConsoleMessages
                  .CodeGenerationLaunchDelegate_console_configuration_name_is_empty_in
              + configuration.getName(),
          null,
          ICodeGenerationLaunchConstants.ERR_UNSPECIFIED_CONSOLE_CONFIGURATION);
    }

    if (KnownConfigurations.getInstance().find(configName) == null) {
      String out =
          NLS.bind(
              HibernateConsoleMessages
                  .CodeGenerationLaunchDelegate_console_configuration_not_found_in,
              configName,
              configuration.getName());
      abort(out, null, ICodeGenerationLaunchConstants.ERR_CONSOLE_CONFIGURATION_NOTFOUND);
    }

    if (StringHelper.isEmpty(attributes.getOutputPath())) {
      abort(
          HibernateConsoleMessages.CodeGenerationLaunchDelegate_output_has_to_be_specified_in
              + configuration.getName(),
          null,
          ICodeGenerationLaunchConstants.ERR_OUTPUT_PATH_NOTFOUND);
    }

    List<ExporterFactory> exporterFactories = attributes.getExporterFactories();
    for (Iterator<ExporterFactory> iter = exporterFactories.iterator(); iter.hasNext(); ) {
      ExporterFactory exFactory = iter.next();
      if (exFactory.isEnabled(configuration)
          && exFactory
              .getExporterDefinitionId()
              .equals("org.hibernate.tools.query")) { // $NON-NLS-1$
        if (!exFactory.getProperties().containsKey("query_string")) { // $NON-NLS-1$
          abort(
              "Query property should be explicitly set for Query Exporter",
              null,
              ICodeGenerationLaunchConstants.ERR_OUTPUT_PATH_NOTFOUND); // $NON-NLS-1$
        }
      }
    }

    return super.preLaunchCheck(configuration, mode, monitor);
  }
  public void launch(
      ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
      throws CoreException {
    Assert.isNotNull(configuration);
    Assert.isNotNull(monitor);
    ExporterAttributes attributes = new ExporterAttributes(configuration);
    ConsoleConfiguration cc =
        KnownConfigurations.getInstance().find(attributes.getConsoleConfigurationName());
    ConsoleExtension consoleExtension =
        ConsoleExtensionManager.getConsoleExtension(cc.getHibernateExtension());
    Map<String, File[]> generatedFiles =
        consoleExtension.launchExporters(configuration, mode, launch, monitor);
    // code formatting needs to happen *after* refresh to make sure eclipse will format the uptodate
    // files!
    if (generatedFiles != null) {
      formatGeneratedCode(monitor, generatedFiles);
    }

    /* The code is moved to consoleExtension and delegated method is called instead.

    List<ExporterFactory> exporterFactories = attributes.getExporterFactories();
    for (Iterator<ExporterFactory> iter = exporterFactories.iterator(); iter.hasNext();) {
    	ExporterFactory exFactory = iter.next();
    	if (!exFactory.isEnabled(configuration)) {
    		iter.remove();
    	}
    }
    if (attributes.isUseExternalProcess()) {
    	// create temporary build.xml and then erase it after code generation complete
    	String fileName = null;
    	try {
    		fileName = getPath2GenBuildXml().toString();
        	createBuildXmlFile(configuration, fileName);
    	} catch (UnsupportedEncodingException e) {
    		throw new CoreException(HibernateConsolePlugin.throwableToStatus(e, 666));
    	} catch (IOException e) {
    		throw new CoreException(HibernateConsolePlugin.throwableToStatus(e, 666));
    	}
    	configuration = updateLaunchConfig(configuration);
    	super.launch(configuration, mode, launch, monitor);
       	//
       	final Properties props = new Properties();
              props.put(CodeGenerationStrings.EJB3, "" + attributes.isEJB3Enabled()); //$NON-NLS-1$
              props.put(CodeGenerationStrings.JDK5, "" + attributes.isJDK5Enabled()); //$NON-NLS-1$
              Set<String> outputDirs = new HashSet<String>();
    	for (Iterator<ExporterFactory> iter = exporterFactories.iterator(); iter.hasNext();) {
    		ExporterFactory exFactory = iter.next();
    		exFactory.collectOutputDirectories(attributes.getOutputPath(),
    				props, outputDirs);
    	}
    	//
    	final IProcess[] processes = launch.getProcesses();
    	// codegen listener to erase build.xml file after codegen process complete
    	CodeGenerationProcessListener refresher = new CodeGenerationProcessListener(
    		processes[0], fileName, outputDirs);
    	refresher.startBackgroundRefresh();
    	return;
       }
    try {
        Set<String> outputDirectories = new HashSet<String>();
        ExporterFactory[] exporters = exporterFactories.toArray( new ExporterFactory[exporterFactories.size()] );
              ArtifactCollector collector = runExporters(attributes, exporters, outputDirectories, monitor);

              for (String path : outputDirectories) {
              	CodeGenerationUtils.refreshOutputDir(path);
    	}

    	RefreshTab.refreshResources(configuration, monitor);

    	// code formatting needs to happen *after* refresh to make sure eclipse will format the uptodate files!
              if(collector!=null) {
              	formatGeneratedCode( monitor, collector );
    	}


    } catch(Exception e) {
    	throw new CoreException(HibernateConsolePlugin.throwableToStatus(e, 666));
    } catch(NoClassDefFoundError e) {
    	throw new CoreException(HibernateConsolePlugin.throwableToStatus(new HibernateConsoleRuntimeException(HibernateConsoleMessages.CodeGenerationLaunchDelegate_received_noclassdeffounderror,e), 666));
    } finally {
    	monitor.done();
    }*/

  }
  private ArtifactCollector runExporters(
      final ExporterAttributes attributes,
      final ExporterFactory[] exporterFactories,
      final Set<String> outputDirectories,
      final IProgressMonitor monitor)
      throws CoreException {

    monitor.beginTask(
        HibernateConsoleMessages.CodeGenerationLaunchDelegate_generating_code_for
            + attributes.getConsoleConfigurationName(),
        exporterFactories.length + 1);

    if (monitor.isCanceled()) return null;

    ConsoleConfiguration cc =
        KnownConfigurations.getInstance().find(attributes.getConsoleConfigurationName());
    if (attributes.isReverseEngineer()) {
      monitor.subTask(HibernateConsoleMessages.CodeGenerationLaunchDelegate_reading_jdbc_metadata);
    }
    final Configuration cfg =
        buildConfiguration(attributes, cc, ResourcesPlugin.getWorkspace().getRoot());

    monitor.worked(1);

    if (monitor.isCanceled()) return null;

    return (ArtifactCollector)
        cc.execute(
            new Command() {

              public Object execute() {
                ArtifactCollector artifactCollector = new ArtifactCollector();

                // Global properties
                Properties props = new Properties();
                props.put(
                    CodeGenerationStrings.EJB3, "" + attributes.isEJB3Enabled()); // $NON-NLS-1$
                props.put(
                    CodeGenerationStrings.JDK5, "" + attributes.isJDK5Enabled()); // $NON-NLS-1$

                for (int i = 0; i < exporterFactories.length; i++) {
                  monitor.subTask(exporterFactories[i].getExporterDefinition().getDescription());

                  Properties globalProperties = new Properties();
                  globalProperties.putAll(props);

                  Exporter exporter;
                  try {
                    exporter =
                        exporterFactories[i].createConfiguredExporter(
                            cfg,
                            attributes.getOutputPath(),
                            attributes.getTemplatePath(),
                            globalProperties,
                            outputDirectories,
                            artifactCollector);
                  } catch (CoreException e) {
                    throw new HibernateConsoleRuntimeException(
                        HibernateConsoleMessages.CodeGenerationLaunchDelegate_error_while_setting_up
                            + exporterFactories[i].getExporterDefinition(),
                        e);
                  }

                  try {
                    exporter.start();
                  } catch (HibernateException he) {
                    throw new HibernateConsoleRuntimeException(
                        HibernateConsoleMessages.CodeGenerationLaunchDelegate_error_while_running
                            + exporterFactories[i].getExporterDefinition().getDescription(),
                        he);
                  }
                  monitor.worked(1);
                }
                return artifactCollector;
              }
            });
  }
 public void setPropertyValue(Object id, Object value) {
   if ("name".equals(id) && value instanceof String) { // $NON-NLS-1$
     String newName = (String) value;
     if (LaunchHelper.verifyConfigurationName(newName) != null) {
       return; // just do not change name
     }
     String oldName = cfg.getName();
     try {
       ILaunchConfiguration lc = HibernateConsolePlugin.getDefault().findLaunchConfig(oldName);
       if (lc != null) {
         ILaunchConfigurationWorkingCopy wc = lc.getWorkingCopy();
         wc.rename(newName);
         wc.doSave();
         // find newly created console configuration
         cfg = KnownConfigurations.getInstance().find(newName);
       } else {
         HibernateConsolePlugin.getDefault()
             .log(
                 "Can't find Console Configuration \""
                     + oldName
                     + "\""); //$NON-NLS-1$//$NON-NLS-2$
       }
     } catch (CoreException e) {
       HibernateConsolePlugin.getDefault().log(e);
     }
   } else if ("mode".equals(id) && value instanceof Integer) { // $NON-NLS-1$
     int index = (Integer) value;
     try {
       ILaunchConfiguration lc =
           HibernateConsolePlugin.getDefault().findLaunchConfig(cfg.getName());
       if (lc != null) {
         ILaunchConfigurationWorkingCopy wc = lc.getWorkingCopy();
         wc.setAttribute(
             "org.hibernate.eclipse.launch.CONFIGURATION_FACTORY",
             ConfigurationMode.values()[index]); // //$NON-NLS-1$
         wc.doSave();
       } else {
         HibernateConsolePlugin.getDefault()
             .log(
                 "Can't find Console Configuration \""
                     + cfg.getName()
                     + "\""); //$NON-NLS-1$//$NON-NLS-2$
       }
     } catch (CoreException e) {
       HibernateConsolePlugin.getDefault()
           .log(
               "Can't find Console Configuration \""
                   + cfg.getName()
                   + "\""); //$NON-NLS-1$//$NON-NLS-2$
     }
   } else if ("project".equals(id) && value instanceof Integer) { // $NON-NLS-1$
     int index = (Integer) value;
     try {
       ILaunchConfiguration lc =
           HibernateConsolePlugin.getDefault().findLaunchConfig(cfg.getName());
       if (lc != null) {
         ILaunchConfigurationWorkingCopy wc = lc.getWorkingCopy();
         String projectName = getSortedProjectNames()[index];
         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, projectName);
         if (projectName != null) {
           wc.setAttribute(
               LaunchConfiguration.ATTR_MAPPED_RESOURCE_PATHS,
               Collections.singletonList(projectName));
           wc.setAttribute(
               LaunchConfiguration.ATTR_MAPPED_RESOURCE_TYPES,
               Collections.singletonList(Integer.toString(IResource.PROJECT)));
         } else {
           wc.removeAttribute(LaunchConfiguration.ATTR_MAPPED_RESOURCE_PATHS);
           wc.removeAttribute(LaunchConfiguration.ATTR_MAPPED_RESOURCE_TYPES);
         }
         wc.doSave();
       } else {
         HibernateConsolePlugin.getDefault()
             .log(
                 "Can't find Console Configuration \""
                     + cfg.getName()
                     + "\""); //$NON-NLS-1$//$NON-NLS-2$
       }
     } catch (CoreException e) {
       HibernateConsolePlugin.getDefault()
           .log(
               "Can't find Console Configuration \""
                   + cfg.getName()
                   + "\""); //$NON-NLS-1$//$NON-NLS-2$
     }
   } else if ("connection".equals(id) && value instanceof Integer) { // $NON-NLS-1$
     int index = (Integer) value;
     try {
       ILaunchConfiguration lc =
           HibernateConsolePlugin.getDefault().findLaunchConfig(cfg.getName());
       if (lc != null) {
         ILaunchConfigurationWorkingCopy wc = lc.getWorkingCopy();
         if (index == 0) { // jpa
           wc.setAttribute(
               IConsoleConfigurationLaunchConstants.USE_JPA_PROJECT_PROFILE,
               Boolean.TRUE.toString());
           wc.removeAttribute(IConsoleConfigurationLaunchConstants.CONNECTION_PROFILE_NAME);
         } else if (index == 1) { // hibernate
           wc.removeAttribute(IConsoleConfigurationLaunchConstants.USE_JPA_PROJECT_PROFILE);
           wc.removeAttribute(IConsoleConfigurationLaunchConstants.CONNECTION_PROFILE_NAME);
         } else { // connection profile
           String[] values = getConnectionNames();
           wc.setAttribute(
               IConsoleConfigurationLaunchConstants.CONNECTION_PROFILE_NAME, values[index]);
           wc.removeAttribute(IConsoleConfigurationLaunchConstants.USE_JPA_PROJECT_PROFILE);
         }
         wc.doSave();
       } else {
         HibernateConsolePlugin.getDefault()
             .log(
                 "Can't find Console Configuration \""
                     + cfg.getName()
                     + "\""); //$NON-NLS-1$//$NON-NLS-2$
       }
     } catch (CoreException e) {
       HibernateConsolePlugin.getDefault()
           .log(
               "Can't find Console Configuration \""
                   + cfg.getName()
                   + "\""); //$NON-NLS-1$//$NON-NLS-2$
     }
   }
 }