|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.tool.reveng.internal.export.mapping; |
| 6 | + |
| 7 | +import jakarta.xml.bind.JAXBException; |
| 8 | +import jakarta.xml.bind.Marshaller; |
| 9 | +import org.apache.commons.collections4.list.UnmodifiableList; |
| 10 | +import org.hibernate.boot.MetadataSources; |
| 11 | +import org.hibernate.boot.jaxb.Origin; |
| 12 | +import org.hibernate.boot.jaxb.SourceType; |
| 13 | +import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping; |
| 14 | +import org.hibernate.boot.jaxb.hbm.transform.HbmXmlTransformer; |
| 15 | +import org.hibernate.boot.jaxb.hbm.transform.UnsupportedFeatureHandling; |
| 16 | +import org.hibernate.boot.jaxb.internal.MappingBinder; |
| 17 | +import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl; |
| 18 | +import org.hibernate.boot.jaxb.spi.Binding; |
| 19 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 20 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 21 | +import org.hibernate.cfg.JdbcSettings; |
| 22 | +import org.hibernate.service.ServiceRegistry; |
| 23 | +import org.hibernate.tool.reveng.api.export.Exporter; |
| 24 | +import org.hibernate.tool.reveng.api.xml.XMLPrettyPrinter; |
| 25 | +import org.hibernate.tool.reveng.internal.util.DummyDialect; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.Serial; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Properties; |
| 35 | +import java.util.logging.Logger; |
| 36 | + |
| 37 | +public class MappingExporter implements Exporter { |
| 38 | + |
| 39 | + private static final Logger LOGGER = Logger.getLogger( MappingExporter.class.getName() ); |
| 40 | + |
| 41 | + private UnmodifiableList<File> hbmXmlFiles = new UnmodifiableList<>(Collections.emptyList()); |
| 42 | + private boolean formatResult = true; |
| 43 | + |
| 44 | + private final MappingBinder mappingBinder; |
| 45 | + private final Marshaller marshaller; |
| 46 | + |
| 47 | + public MappingExporter() { |
| 48 | + mappingBinder = createMappingBinder(); |
| 49 | + marshaller = createMarshaller(); |
| 50 | + } |
| 51 | + |
| 52 | + public void setHbmFiles(List<File> fileList) { |
| 53 | + hbmXmlFiles = new UnmodifiableList<>( fileList ); |
| 54 | + } |
| 55 | + |
| 56 | + public void setFormatResult(boolean formatResult) { |
| 57 | + this.formatResult = formatResult; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Properties getProperties() { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void start() { |
| 67 | + List<Binding<JaxbHbmHibernateMapping>> hbmBindings = getHbmBindings(); |
| 68 | + List<Binding<JaxbEntityMappingsImpl>> transformedBindings = transformBindings(hbmBindings); |
| 69 | + for (int i = 0; i < hbmBindings.size(); i++) { |
| 70 | + marshall( |
| 71 | + transformedBindings.get(i).getRoot(), |
| 72 | + ((HbmXmlOrigin)hbmBindings.get(i).getOrigin()).getHbmXmlFile()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private List<Binding<JaxbEntityMappingsImpl>> transformBindings( |
| 77 | + List<Binding<JaxbHbmHibernateMapping>> hbmBindings) { |
| 78 | + MetadataSources metadataSources = new MetadataSources( createServiceRegistry() ); |
| 79 | + hbmBindings.forEach( metadataSources::addHbmXmlBinding ); |
| 80 | + return HbmXmlTransformer.transform( |
| 81 | + hbmBindings, |
| 82 | + (MetadataImplementor) metadataSources.buildMetadata(), |
| 83 | + UnsupportedFeatureHandling.ERROR); |
| 84 | + } |
| 85 | + |
| 86 | + private ServiceRegistry createServiceRegistry() { |
| 87 | + StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); |
| 88 | + ssrb.clearSettings(); |
| 89 | + ssrb.applySetting(JdbcSettings.ALLOW_METADATA_ON_BOOT, false); |
| 90 | + ssrb.applySetting(JdbcSettings.DIALECT, DummyDialect.class.getName()); |
| 91 | + return ssrb.build(); |
| 92 | + } |
| 93 | + |
| 94 | + private MappingBinder createMappingBinder() { |
| 95 | + return new MappingBinder( |
| 96 | + MappingBinder.class.getClassLoader()::getResourceAsStream, |
| 97 | + UnsupportedFeatureHandling.ERROR); |
| 98 | + } |
| 99 | + |
| 100 | + private List<Binding<JaxbHbmHibernateMapping>> getHbmBindings() { |
| 101 | + List<Binding<JaxbHbmHibernateMapping>> result = new ArrayList<>(); |
| 102 | + hbmXmlFiles.forEach((hbmXmlFile) -> { |
| 103 | + final String fullPath = hbmXmlFile.getAbsolutePath(); |
| 104 | + LOGGER.info("Adding file: '" + fullPath + "' to the list to be transformed."); |
| 105 | + HbmXmlOrigin origin = new HbmXmlOrigin( hbmXmlFile ); |
| 106 | + Binding<JaxbHbmHibernateMapping> binding = bindHbmXml( origin ); |
| 107 | + result.add(binding); |
| 108 | + }); |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + private Binding<JaxbHbmHibernateMapping> bindHbmXml(HbmXmlOrigin origin) { |
| 113 | + File hbmXmlFile = origin.getHbmXmlFile(); |
| 114 | + try ( final FileInputStream fileStream = new FileInputStream(hbmXmlFile) ) { |
| 115 | + return mappingBinder.bind( fileStream, origin ); |
| 116 | + } |
| 117 | + catch (IOException e) { |
| 118 | + LOGGER.info( "Unable to open hbm.xml file `" + hbmXmlFile.getAbsolutePath() + "` for transformation"); |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private Marshaller createMarshaller() { |
| 124 | + try { |
| 125 | + return mappingBinder.mappingJaxbContext().createMarshaller(); |
| 126 | + } |
| 127 | + catch (JAXBException e) { |
| 128 | + throw new RuntimeException("Unable to create JAXB Marshaller", e); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void marshall( |
| 133 | + JaxbEntityMappingsImpl mappings, |
| 134 | + File hbmXmlFile) { |
| 135 | + File mappingXmlFile = new File( |
| 136 | + hbmXmlFile.getParentFile(), |
| 137 | + hbmXmlFile.getName().replace(".hbm.xml", ".mapping.xml")); |
| 138 | + LOGGER.info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath()); |
| 139 | + try { |
| 140 | + marshaller.marshal( mappings, mappingXmlFile ); |
| 141 | + if (formatResult) { |
| 142 | + XMLPrettyPrinter.prettyPrintFile(mappingXmlFile); |
| 143 | + } |
| 144 | + } |
| 145 | + catch (JAXBException e) { |
| 146 | + throw new RuntimeException( |
| 147 | + "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`", |
| 148 | + e); |
| 149 | + } |
| 150 | + catch (IOException e) { |
| 151 | + throw new RuntimeException( |
| 152 | + "Unable to format XML file `" + mappingXmlFile.getAbsolutePath() + "`", |
| 153 | + e); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + static class HbmXmlOrigin extends Origin { |
| 158 | + |
| 159 | + @Serial |
| 160 | + private static final long serialVersionUID = 1L; |
| 161 | + |
| 162 | + private final File hbmXmlFile; |
| 163 | + |
| 164 | + public HbmXmlOrigin(File hbmXmlFile) { |
| 165 | + super( SourceType.FILE, hbmXmlFile.getAbsolutePath() ); |
| 166 | + this.hbmXmlFile = hbmXmlFile; |
| 167 | + } |
| 168 | + |
| 169 | + public File getHbmXmlFile() { |
| 170 | + return hbmXmlFile; |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | +} |
0 commit comments