Search in sources :

Example 1 with IExporter

use of org.jboss.tools.hibernate.runtime.spi.IExporter in project jbosstools-hibernate by jbosstools.

the class ConsoleExtension method runExporters.

private IArtifactCollector 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 IConfiguration cfg = buildConfiguration(attributes, cc, ResourcesPlugin.getWorkspace().getRoot());
    monitor.worked(1);
    if (monitor.isCanceled())
        return null;
    return (IArtifactCollector) cc.execute(new Command() {

        public Object execute() {
            IArtifactCollector artifactCollector = hibernateExtension.getHibernateService().newArtifactCollector();
            // Global properties
            Properties props = new Properties();
            props.put(CodeGenerationStrings.EJB3, // $NON-NLS-1$
            "" + attributes.isEJB3Enabled());
            props.put(CodeGenerationStrings.JDK5, // $NON-NLS-1$
            "" + attributes.isJDK5Enabled());
            for (int i = 0; i < exporterFactories.length; i++) {
                monitor.subTask(exporterFactories[i].getExporterDefinition().getDescription());
                Properties globalProperties = new Properties();
                globalProperties.putAll(props);
                IExporter exporter;
                try {
                    exporter = exporterFactories[i].createConfiguredExporter(cfg, attributes.getOutputPath(), attributes.getTemplatePath(), globalProperties, outputDirectories, artifactCollector, hibernateExtension.getHibernateService());
                } 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;
        }
    });
}
Also used : ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) CoreException(org.eclipse.core.runtime.CoreException) Command(org.hibernate.console.execution.ExecutionContext.Command) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) Properties(java.util.Properties)

Example 2 with IExporter

use of org.jboss.tools.hibernate.runtime.spi.IExporter in project jbosstools-hibernate by jbosstools.

the class ExporterFactory method createConfiguredExporter.

/**
 * Creates exporter with the specified settings; also resolves any relevant properties via Eclipse VariablesPlugin.
 * @param collector
 * @throws CoreException in case of resolve variables issues.
 */
public IExporter createConfiguredExporter(IConfiguration cfg, String defaultOutputDirectory, String customTemplatePath, Properties globalProperties, Set<String> outputDirectories, IArtifactCollector collector, IService service) throws CoreException {
    IExporter exporter = getExporterDefinition().createExporterInstance(service);
    Properties extract = new Properties();
    Properties props = new Properties();
    props.putAll(globalProperties);
    props.putAll(getProperties());
    extractExporterProperties(getExporterDefinitionId(), props, extract);
    cfg.addProperties(props);
    exporter.setArtifactCollector(collector);
    String outputPath = defaultOutputDirectory;
    if (extract.containsKey(ExporterFactoryStrings.OUTPUTDIR)) {
        outputPath = extract.getProperty(ExporterFactoryStrings.OUTPUTDIR);
    }
    String resolvedOutputDir = resolve(outputPath);
    String loc = PathHelper.getLocationAsStringPath(resolvedOutputDir);
    if (outputPath != null && loc == null) {
        String out = NLS.bind(HibernateConsoleMessages.ExporterFactory_output_dir_in_does_not_exist, resolvedOutputDir, getExporterDefinition().getDescription());
        throw new HibernateConsoleRuntimeException(out);
    }
    if (StringHelper.isNotEmpty(loc)) {
        // only set if something valid found
        outputDirectories.add(loc);
        exporter.setOutputDirectory(new File(loc));
    }
    exporter.setConfiguration(cfg);
    List<String> templatePathList = new ArrayList<String>();
    if (extract.containsKey(ExporterFactoryStrings.TEMPLATE_PATH)) {
        String resolveTemplatePath = resolve(extract.getProperty(ExporterFactoryStrings.TEMPLATE_PATH));
        // $NON-NLS-1$
        StringTokenizer st = new StringTokenizer(resolveTemplatePath, ";");
        // $NON-NLS-1$
        String out = "";
        while (st.hasMoreTokens()) {
            String locationAsStringPath = PathHelper.getLocationAsStringPath(st.nextToken());
            if (locationAsStringPath == null) {
                out += NLS.bind(HibernateConsoleMessages.ExporterFactory_template_dir_in_does_not_exist, resolveTemplatePath, getExporterDefinition().getDescription()) + '\n';
            } else {
                templatePathList.add(locationAsStringPath);
            }
        }
        if (out.length() > 0) {
            out = out.substring(0, out.length() - 1);
            throw new HibernateConsoleRuntimeException(out);
        }
    }
    if (StringHelper.isNotEmpty(customTemplatePath)) {
        String resolvedCustomTemplatePath = resolve(customTemplatePath);
        // $NON-NLS-1$
        StringTokenizer st = new StringTokenizer(resolvedCustomTemplatePath, ";");
        // $NON-NLS-1$
        String out = "";
        while (st.hasMoreTokens()) {
            String locationAsStringPath = PathHelper.getLocationAsStringPath(st.nextToken());
            if (locationAsStringPath != null) {
                templatePathList.add(locationAsStringPath);
            } else {
                out = NLS.bind(HibernateConsoleMessages.ExporterFactory_template_dir_in_does_not_exist, resolvedCustomTemplatePath, getExporterDefinition().getDescription());
            }
        }
        if (!("".equals(out))) {
            // $NON-NLS-1$
            out = out.substring(0, out.length() - 1);
            throw new HibernateConsoleRuntimeException(out);
        }
    }
    exporter.setTemplatePath(templatePathList.toArray(new String[templatePathList.size()]));
    // special handling for GenericExporter (TODO: be delegated via plugin.xml)
    if (getExporterDefinitionId().equals("org.hibernate.tools.hbmtemplate")) {
        // $NON-NLS-1$
        IGenericExporter ge = exporter.getGenericExporter();
        ge.setFilePattern(extract.getProperty(ExporterFactoryStrings.FILE_PATTERN));
        ge.setTemplateName(extract.getProperty(ExporterFactoryStrings.TEMPLATE_NAME));
        ge.setForEach(extract.getProperty(ExporterFactoryStrings.FOR_EACH));
    }
    // special handling for Hbm2DDLExporter
    if (getExporterDefinitionId().equals("org.hibernate.tools.hbm2ddl")) {
        // $NON-NLS-1$
        IHbm2DDLExporter ddlExporter = exporter.getHbm2DDLExporter();
        // avoid users to delete their databases with a single click
        ddlExporter.setExport(Boolean.parseBoolean(extract.getProperty(ExporterFactoryStrings.EXPORTTODATABASE)));
    }
    // special handling for QueryExporter
    if (getExporterDefinitionId().equals("org.hibernate.tools.query")) {
        // $NON-NLS-1$
        IQueryExporter queryExporter = exporter.getQueryExporter();
        List<String> queryStrings = new ArrayList<String>();
        // $NON-NLS-1$
        queryStrings.add(extract.getProperty(ExporterFactoryStrings.QUERY_STRING, ""));
        queryExporter.setQueries(queryStrings);
        queryExporter.setFilename(extract.getProperty(ExporterFactoryStrings.OUTPUTFILENAME));
    }
    return exporter;
}
Also used : StringTokenizer(java.util.StringTokenizer) IHbm2DDLExporter(org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter) ArrayList(java.util.ArrayList) IQueryExporter(org.jboss.tools.hibernate.runtime.spi.IQueryExporter) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) Properties(java.util.Properties) File(java.io.File) IGenericExporter(org.jboss.tools.hibernate.runtime.spi.IGenericExporter)

Example 3 with IExporter

use of org.jboss.tools.hibernate.runtime.spi.IExporter in project jbosstools-hibernate by jbosstools.

the class ServiceImpl method createExporter.

@Override
public IExporter createExporter(String exporterClassName) {
    Exporter exporter = (Exporter) Util.getInstance(exporterClassName, facadeFactory.getClassLoader());
    exporter.setMetadataDescriptor(new ConfigurationMetadataDescriptor(newDefaultConfiguration()));
    return facadeFactory.createExporter(exporter);
}
Also used : ConfigurationMetadataDescriptor(org.jboss.tools.hibernate.runtime.v_5_3.internal.util.ConfigurationMetadataDescriptor) IHibernateMappingExporter(org.jboss.tools.hibernate.runtime.spi.IHibernateMappingExporter) Exporter(org.hibernate.tool.hbm2x.Exporter) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter)

Example 4 with IExporter

use of org.jboss.tools.hibernate.runtime.spi.IExporter in project jbosstools-hibernate by jbosstools.

the class ExporterAttributesTest method checkCorrectLaunchConfigurationFile.

private void checkCorrectLaunchConfigurationFile(final String fileName) {
    // IWorkspace ws = ResourcesPlugin.getWorkspace();
    String str1, str2;
    ILaunchConfiguration launchConfig = loadLaunchConfigFromFile(fileName);
    Map<String, Object> attrMap = null;
    try {
        attrMap = launchConfig.getAttributes();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(attrMap);
    ExporterAttributes exporterAttributes = null;
    try {
        exporterAttributes = new ExporterAttributes(launchConfig);
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(exporterAttributes);
    // check is configuration correct
    Assert.assertNull(exporterAttributes.checkExporterAttributes());
    List<ExporterFactory> exporterFactories = exporterAttributes.getExporterFactories();
    Set<ExporterFactory> selectedExporters = new HashSet<ExporterFactory>();
    selectedExporters.addAll(exporterFactories);
    Set<String> deletedExporterIds = new HashSet<String>();
    ILaunchConfigurationWorkingCopy launchConfigWC = null;
    try {
        launchConfigWC = launchConfig.getWorkingCopy();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(launchConfigWC);
    // 
    str1 = project.getSample(fileName);
    str1 = ResourceReadUtils.adjustXmlText(str1);
    // 
    Assert.assertNotNull(project);
    Assert.assertNotNull(project.getIProject());
    Assert.assertNotNull(project.getIProject().getFile(fileName));
    // 
    InputStream is = null;
    try {
        is = project.getIProject().getFile(fileName).getContents();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(is);
    str2 = ResourceReadUtils.readStream(is);
    str2 = ResourceReadUtils.adjustXmlText(str2);
    Assert.assertEquals(str1, str2);
    // update and save lc - so fileName from the project updated
    try {
        ExporterAttributes.saveExporterFactories(launchConfigWC, exporterFactories, selectedExporters, deletedExporterIds);
        launchConfigWC.doSave();
    } catch (CoreException e) {
        Assert.fail(e.getMessage());
    }
    // 
    is = null;
    try {
        is = project.getIProject().getFile(fileName).getContents();
    } catch (CoreException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(is);
    str2 = ResourceReadUtils.readStream(is);
    str2 = ResourceReadUtils.adjustXmlText(str2);
    Assert.assertEquals(str1, str2);
    // 
    IArtifactCollector artifactCollector = service.newArtifactCollector();
    ExporterAttributes expAttr = exporterAttributes;
    // Global properties
    Properties props = new Properties();
    // $NON-NLS-1$
    props.put(CodeGenerationStrings.EJB3, "" + expAttr.isEJB3Enabled());
    // $NON-NLS-1$
    props.put(CodeGenerationStrings.JDK5, "" + expAttr.isJDK5Enabled());
    consoleCfg.build();
    IConfiguration cfg = consoleCfg.getConfiguration();
    Assert.assertNotNull(cfg);
    Set<String> outputDirectories = new HashSet<String>();
    for (int i = 0; i < exporterFactories.size(); i++) {
        Properties globalProperties = new Properties();
        globalProperties.putAll(props);
        ExporterFactory ef = exporterFactories.get(i);
        // 
        Properties propsForTesting = new Properties();
        propsForTesting.putAll(globalProperties);
        propsForTesting.putAll(ef.getProperties());
        // 
        IExporter exporter = null;
        outputDirectories.clear();
        try {
            exporter = ef.createConfiguredExporter(cfg, expAttr.getOutputPath(), expAttr.getTemplatePath(), globalProperties, outputDirectories, artifactCollector, service);
        } catch (CoreException e) {
            e.printStackTrace();
        }
        Assert.assertNotNull(exporter);
        Assert.assertTrue(outputDirectories.size() > 0);
        Properties propsFromExporter = exporter.getProperties();
        String exporterDefinitionId = ef.getExporterDefinitionId();
        // test special handling for GenericExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.hbmtemplate")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.FILE_PATTERN));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.TEMPLATE_NAME));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.FOR_EACH));
            IGenericExporter ge = exporter.getGenericExporter();
            Assert.assertNotNull(ge);
            Assert.assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.FILE_PATTERN), ge.getFilePattern());
            Assert.assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.TEMPLATE_NAME), ge.getTemplateName());
        // to test GenericExporter should provide public getter but it doesn't
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.FOR_EACH), ge.getForEach());
        }
        // test special handling for Hbm2DDLExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.hbm2ddl")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.EXPORTTODATABASE));
            IHbm2DDLExporter ddlExporter = exporter.getHbm2DDLExporter();
            Assert.assertNotNull(ddlExporter);
        // to test Hbm2DDLExporter should provide public getter but it doesn't
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.EXPORTTODATABASE), ddlExporter.getExport());
        }
        // test special handling for QueryExporter
        if (exporterDefinitionId.equals("org.hibernate.tools.query")) {
            // $NON-NLS-1$
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.QUERY_STRING));
            Assert.assertNull(propsFromExporter.getProperty(ExporterFactoryStrings.OUTPUTFILENAME));
        // IQueryExporter queryExporter = exporter.getQueryExporter();
        // to test QueryExporter should provide public getter but it doesn't
        // List<String> queryStrings = queryExporter.getQueries();
        // assertEquals(1, queryStrings.size());
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.QUERY_STRING), queryStrings.get(0));
        // assertEquals(propsForTesting.getProperty(ExporterFactoryStrings.OUTPUTFILENAME), queryExporter.getFileName());
        }
    // try {
    // exporter.start();
    // } catch (HibernateException he) {
    // he.printStackTrace();
    // }
    }
    Assert.assertTrue(artifactCollector.getFileTypes().size() == 0);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) InputStream(java.io.InputStream) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) Properties(java.util.Properties) ExporterAttributes(org.hibernate.eclipse.launch.ExporterAttributes) CoreException(org.eclipse.core.runtime.CoreException) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) IHbm2DDLExporter(org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IGenericExporter(org.jboss.tools.hibernate.runtime.spi.IGenericExporter) HashSet(java.util.HashSet)

Example 5 with IExporter

use of org.jboss.tools.hibernate.runtime.spi.IExporter in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateExporter.

@Test
public void testCreateExporter() {
    Exporter exporter = (Exporter) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { Exporter.class }, new TestInvocationHandler());
    IExporter facade = facadeFactory.createExporter(exporter);
    Assert.assertSame(exporter, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) IHibernateMappingExporter(org.jboss.tools.hibernate.runtime.spi.IHibernateMappingExporter) QueryExporter(org.hibernate.tool.hbm2x.QueryExporter) HibernateMappingExporter(org.hibernate.tool.hbm2x.HibernateMappingExporter) Hbm2DDLExporter(org.hibernate.tool.hbm2x.Hbm2DDLExporter) IQueryExporter(org.jboss.tools.hibernate.runtime.spi.IQueryExporter) IGenericExporter(org.jboss.tools.hibernate.runtime.spi.IGenericExporter) GenericExporter(org.hibernate.tool.hbm2x.GenericExporter) Exporter(org.hibernate.tool.hbm2x.Exporter) IHbm2DDLExporter(org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) Test(org.junit.Test)

Aggregations

IExporter (org.jboss.tools.hibernate.runtime.spi.IExporter)18 Test (org.junit.Test)12 IGenericExporter (org.jboss.tools.hibernate.runtime.spi.IGenericExporter)10 IHbm2DDLExporter (org.jboss.tools.hibernate.runtime.spi.IHbm2DDLExporter)10 Exporter (org.hibernate.tool.hbm2x.Exporter)9 IHibernateMappingExporter (org.jboss.tools.hibernate.runtime.spi.IHibernateMappingExporter)9 IQueryExporter (org.jboss.tools.hibernate.runtime.spi.IQueryExporter)9 PersistentClass (org.hibernate.mapping.PersistentClass)8 RootClass (org.hibernate.mapping.RootClass)8 GenericExporter (org.hibernate.tool.hbm2x.GenericExporter)8 Hbm2DDLExporter (org.hibernate.tool.hbm2x.Hbm2DDLExporter)8 HibernateMappingExporter (org.hibernate.tool.hbm2x.HibernateMappingExporter)8 QueryExporter (org.hibernate.tool.hbm2x.QueryExporter)8 POJOClass (org.hibernate.tool.hbm2x.pojo.POJOClass)8 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)8 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)8 Properties (java.util.Properties)4 POJOExporter (org.hibernate.tool.hbm2x.POJOExporter)4 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)4 CoreException (org.eclipse.core.runtime.CoreException)3