private Configuration buildConfiguration(
      final ExporterAttributes attributes, ConsoleConfiguration cc, IWorkspaceRoot root) {
    final boolean reveng = attributes.isReverseEngineer();
    final String reverseEngineeringStrategy = attributes.getRevengStrategy();
    final boolean preferBasicCompositeids = attributes.isPreferBasicCompositeIds();
    final IResource revengres = PathHelper.findMember(root, attributes.getRevengSettings());

    if (reveng) {
      Configuration configuration = null;
      if (cc.hasConfiguration()) {
        configuration = cc.getConfiguration();
      } else {
        configuration = cc.buildWith(null, false);
      }

      final JDBCMetaDataConfiguration cfg = new JDBCMetaDataConfiguration();
      Properties properties = configuration.getProperties();
      cfg.setProperties(properties);
      cc.buildWith(cfg, false);

      cfg.setPreferBasicCompositeIds(preferBasicCompositeids);

      cc.execute(
          new Command() { // need to execute in the consoleconfiguration to let it handle classpath
                          // stuff!

            public Object execute() {
              // todo: factor this setup of revengstrategy to core
              ReverseEngineeringStrategy res = new DefaultReverseEngineeringStrategy();

              OverrideRepository repository = null;

              if (revengres != null) {
                File file = PathHelper.getLocation(revengres).toFile();
                repository = new OverrideRepository();
                repository.addFile(file);
              }

              if (repository != null) {
                res = repository.getReverseEngineeringStrategy(res);
              }

              if (reverseEngineeringStrategy != null
                  && reverseEngineeringStrategy.trim().length() > 0) {
                res = loadreverseEngineeringStrategy(reverseEngineeringStrategy, res);
              }

              ReverseEngineeringSettings qqsettings =
                  new ReverseEngineeringSettings(res)
                      .setDefaultPackageName(attributes.getPackageName())
                      .setDetectManyToMany(attributes.detectManyToMany())
                      .setDetectOneToOne(attributes.detectOneToOne())
                      .setDetectOptimisticLock(attributes.detectOptimisticLock());

              res.setSettings(qqsettings);

              cfg.setReverseEngineeringStrategy(res);

              cfg.readFromJDBC();
              cfg.buildMappings();
              return null;
            }
          });

      return cfg;
    } else {
      cc.build();
      cc.buildMappings();
      return cc.getConfiguration();
    }
  }
  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$
     }
   }
 }
  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();
    }*/

  }
  public Object getPropertyValue(Object id) {
    try {
      if ("name".equals(id)) { // $NON-NLS-1$
        return cfg.getName();
      }
      // TODO: bring back more eclipse friendly file names
      ConsoleConfigurationPreferences preferences = cfg.getPreferences();
      if ("project".equals(id)) { // $NON-NLS-1$
        try {
          ILaunchConfiguration lc =
              HibernateConsolePlugin.getDefault().findLaunchConfig(cfg.getName());
          if (lc != null) {
            String projectName =
                lc.getAttribute(
                    IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); // $NON-NLS-1$
            return Arrays.binarySearch(getSortedProjectNames(), projectName);
          } else {
            HibernateConsolePlugin.getDefault()
                .log(
                    "Can't find Console Configuration \""
                        + cfg.getName()
                        + "\""); //$NON-NLS-1$//$NON-NLS-2$
          }
        } catch (CoreException e) {
          HibernateConsolePlugin.getDefault().log(e);
        }
      }
      if ("mode".equals(id)) { // $NON-NLS-1$
        String[] values = ConfigurationMode.values();
        String value = preferences.getConfigurationMode().toString();
        for (int i = 0; i < values.length; i++) {
          if (value.equals(values[i])) {
            return i;
          }
        }
        return new RuntimeException("Unknown ConsoleConfiguration mode: " + value); // $NON-NLS-1$
      }
      if ("connection".equals(id)) { // $NON-NLS-1$
        try {
          ILaunchConfiguration lc =
              HibernateConsolePlugin.getDefault().findLaunchConfig(cfg.getName());
          if (lc != null) {
            String connectionName =
                lc.getAttribute(
                    IConsoleConfigurationLaunchConstants.CONNECTION_PROFILE_NAME, (String) null);
            if (connectionName == null) {
              connectionName =
                  lc.getAttribute(
                      IConsoleConfigurationLaunchConstants.USE_JPA_PROJECT_PROFILE,
                      Boolean.FALSE.toString());
              if (Boolean.TRUE.toString().equalsIgnoreCase(connectionName)) {
                connectionName =
                    HibernateConsoleMessages.ConnectionProfileCtrl_JPAConfiguredConnection;
              } else {
                connectionName =
                    HibernateConsoleMessages.ConnectionProfileCtrl_HibernateConfiguredConnection;
              }
            }
            String[] values = getConnectionNames();
            for (int i = 0; i < values.length; i++) {
              if (values[i].equals(connectionName)) {
                return i;
              }
            }
          } else {
            HibernateConsolePlugin.getDefault()
                .log(
                    "Can't find Console Configuration \""
                        + cfg.getName()
                        + "\""); //$NON-NLS-1$//$NON-NLS-2$
          }
        } catch (CoreException e) {
          HibernateConsolePlugin.getDefault().log(e);
        }
      }
      if ("hibernate.cfg.xml".equals(id)) { // $NON-NLS-1$
        return preferences.getConfigXMLFile();
      }
      if ("hibernate.properties".equals(id)) { // $NON-NLS-1$
        return preferences.getPropertyFile();
      }
      if ("mapping.files".equals(id)) { // $NON-NLS-1$
        return Integer.valueOf(preferences.getMappingFiles().length);
      }

      return null;
    } catch (RuntimeException e) {
      return HibernateConsoleMessages.ConsoleConfigurationPropertySource_error + e.getMessage();
    }
  }