|
22 | 22 | import java.io.File; |
23 | 23 | import java.io.FileInputStream; |
24 | 24 | import java.io.IOException; |
| 25 | +import java.io.Serial; |
25 | 26 | import java.util.ArrayList; |
26 | 27 | import java.util.List; |
| 28 | +import java.util.Objects; |
27 | 29 |
|
28 | 30 | import org.apache.maven.plugin.AbstractMojo; |
29 | | -import org.apache.maven.plugin.MojoExecutionException; |
30 | | -import org.apache.maven.plugin.MojoFailureException; |
31 | 31 | import org.apache.maven.plugins.annotations.Mojo; |
32 | 32 | import org.apache.maven.plugins.annotations.Parameter; |
33 | 33 | import org.apache.maven.plugins.annotations.ResolutionScope; |
|
54 | 54 | defaultPhase = GENERATE_RESOURCES, |
55 | 55 | requiresDependencyResolution = ResolutionScope.RUNTIME) |
56 | 56 | public class TransformHbmMojo extends AbstractMojo { |
57 | | - |
| 57 | + |
58 | 58 | @Parameter(defaultValue = "${project.basedir}/src/main/resources") |
59 | | - private File inputFolder; |
60 | | - |
61 | | - @Override |
62 | | - public void execute() throws MojoExecutionException, MojoFailureException { |
63 | | - MappingBinder mappingBinder = new MappingBinder( |
64 | | - MappingBinder.class.getClassLoader()::getResourceAsStream, |
65 | | - UnsupportedFeatureHandling.ERROR); |
66 | | - List<File> hbmFiles = getHbmFiles(inputFolder); |
67 | | - List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder); |
68 | | - performTransformation(hbmMappings, mappingBinder, createServiceRegistry()); |
69 | | - } |
70 | | - |
71 | | - private ServiceRegistry createServiceRegistry() { |
72 | | - StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); |
73 | | - ssrb.clearSettings(); |
74 | | - ssrb.applySetting(JdbcSettings.ALLOW_METADATA_ON_BOOT, false); |
75 | | - // Choose the H2 dialect by default, make this configurable |
76 | | - ssrb.applySetting(JdbcSettings.DIALECT, H2Dialect.class.getName()); |
77 | | - return ssrb.build(); |
78 | | - } |
79 | | - |
80 | | - private void performTransformation( |
81 | | - List<Binding<JaxbHbmHibernateMapping>> hbmBindings, |
82 | | - MappingBinder mappingBinder, |
83 | | - ServiceRegistry serviceRegistry) { |
84 | | - Marshaller marshaller = createMarshaller(mappingBinder); |
85 | | - MetadataSources metadataSources = new MetadataSources( serviceRegistry ); |
86 | | - hbmBindings.forEach( metadataSources::addHbmXmlBinding ); |
87 | | - List<Binding<JaxbEntityMappingsImpl>> transformedBindings = HbmXmlTransformer.transform( |
88 | | - hbmBindings, |
89 | | - (MetadataImplementor) metadataSources.buildMetadata(), |
90 | | - UnsupportedFeatureHandling.ERROR |
91 | | - ); |
92 | | - for (int i = 0; i < hbmBindings.size(); i++) { |
93 | | - Binding<JaxbHbmHibernateMapping> hbmBinding = hbmBindings.get( i ); |
94 | | - Binding<JaxbEntityMappingsImpl> transformedBinding = transformedBindings.get( i ); |
95 | | - |
96 | | - HbmXmlOrigin origin = (HbmXmlOrigin)hbmBinding.getOrigin(); |
97 | | - File hbmXmlFile = origin.getHbmXmlFile(); |
98 | | - |
99 | | - marshall(marshaller, transformedBinding.getRoot(), hbmXmlFile); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - private List<Binding<JaxbHbmHibernateMapping>> getHbmMappings(List<File> hbmXmlFiles, MappingBinder mappingBinder) { |
104 | | - List<Binding<JaxbHbmHibernateMapping>> result = new ArrayList<Binding<JaxbHbmHibernateMapping>>(); |
105 | | - hbmXmlFiles.forEach((hbmXmlFile) -> { |
106 | | - final String fullPath = hbmXmlFile.getAbsolutePath(); |
107 | | - getLog().info("Adding file: '" + fullPath + "' to the list to be transformed."); |
108 | | - Origin origin = new HbmXmlOrigin(hbmXmlFile); |
109 | | - Binding<JaxbHbmHibernateMapping> binding = bindMapping( mappingBinder, hbmXmlFile, origin ); |
110 | | - result.add(binding); |
111 | | - }); |
112 | | - return result; |
113 | | - } |
114 | | - |
115 | | - private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, File hbmXmlFile) { |
116 | | - File mappingXmlFile = new File( |
117 | | - hbmXmlFile.getParentFile(), |
118 | | - hbmXmlFile.getName().replace(".hbm.xml", ".mapping.xml")); |
| 59 | + private File inputFolder; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void execute() { |
| 63 | + MappingBinder mappingBinder = new MappingBinder( |
| 64 | + MappingBinder.class.getClassLoader()::getResourceAsStream, |
| 65 | + UnsupportedFeatureHandling.ERROR); |
| 66 | + List<File> hbmFiles = getHbmFiles(inputFolder); |
| 67 | + List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder); |
| 68 | + performTransformation(hbmMappings, mappingBinder, createServiceRegistry()); |
| 69 | + } |
| 70 | + |
| 71 | + private ServiceRegistry createServiceRegistry() { |
| 72 | + StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); |
| 73 | + ssrb.clearSettings(); |
| 74 | + ssrb.applySetting(JdbcSettings.ALLOW_METADATA_ON_BOOT, false); |
| 75 | + // Choose the H2 dialect by default, make this configurable |
| 76 | + ssrb.applySetting(JdbcSettings.DIALECT, H2Dialect.class.getName()); |
| 77 | + return ssrb.build(); |
| 78 | + } |
| 79 | + |
| 80 | + private void performTransformation( |
| 81 | + List<Binding<JaxbHbmHibernateMapping>> hbmBindings, |
| 82 | + MappingBinder mappingBinder, |
| 83 | + ServiceRegistry serviceRegistry) { |
| 84 | + Marshaller marshaller = createMarshaller(mappingBinder); |
| 85 | + MetadataSources metadataSources = new MetadataSources( serviceRegistry ); |
| 86 | + hbmBindings.forEach( metadataSources::addHbmXmlBinding ); |
| 87 | + List<Binding<JaxbEntityMappingsImpl>> transformedBindings = HbmXmlTransformer.transform( |
| 88 | + hbmBindings, |
| 89 | + (MetadataImplementor) metadataSources.buildMetadata(), |
| 90 | + UnsupportedFeatureHandling.ERROR |
| 91 | + ); |
| 92 | + for (int i = 0; i < hbmBindings.size(); i++) { |
| 93 | + Binding<JaxbHbmHibernateMapping> hbmBinding = hbmBindings.get( i ); |
| 94 | + Binding<JaxbEntityMappingsImpl> transformedBinding = transformedBindings.get( i ); |
| 95 | + |
| 96 | + HbmXmlOrigin origin = (HbmXmlOrigin)hbmBinding.getOrigin(); |
| 97 | + File hbmXmlFile = origin.getHbmXmlFile(); |
| 98 | + |
| 99 | + marshall(marshaller, transformedBinding.getRoot(), hbmXmlFile); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private List<Binding<JaxbHbmHibernateMapping>> getHbmMappings(List<File> hbmXmlFiles, MappingBinder mappingBinder) { |
| 104 | + List<Binding<JaxbHbmHibernateMapping>> result = new ArrayList<>(); |
| 105 | + hbmXmlFiles.forEach((hbmXmlFile) -> { |
| 106 | + final String fullPath = hbmXmlFile.getAbsolutePath(); |
| 107 | + getLog().info("Adding file: '" + fullPath + "' to the list to be transformed."); |
| 108 | + Origin origin = new HbmXmlOrigin( hbmXmlFile ); |
| 109 | + Binding<JaxbHbmHibernateMapping> binding = bindMapping( mappingBinder, hbmXmlFile, origin ); |
| 110 | + result.add(binding); |
| 111 | + }); |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, File hbmXmlFile) { |
| 116 | + File mappingXmlFile = new File( |
| 117 | + hbmXmlFile.getParentFile(), |
| 118 | + hbmXmlFile.getName().replace(".hbm.xml", ".mapping.xml")); |
119 | 119 | getLog().info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath()); |
120 | | - try { |
121 | | - marshaller.marshal( mappings, mappingXmlFile ); |
122 | | - } |
123 | | - catch (JAXBException e) { |
124 | | - throw new RuntimeException( |
125 | | - "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`", |
126 | | - e |
127 | | - ); |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - private Binding<JaxbHbmHibernateMapping> bindMapping( |
132 | | - MappingBinder mappingBinder, File hbmXmlFile, Origin origin) { |
133 | | - try ( final FileInputStream fileStream = new FileInputStream(hbmXmlFile) ) { |
134 | | - return mappingBinder.bind( fileStream, origin ); |
135 | | - } |
136 | | - catch (IOException e) { |
137 | | - getLog().warn( "Unable to open hbm.xml file `" + hbmXmlFile.getAbsolutePath() + "` for transformation", e ); |
138 | | - return null; |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - private Marshaller createMarshaller(MappingBinder mappingBinder) { |
143 | | - try { |
144 | | - return mappingBinder.mappingJaxbContext().createMarshaller(); |
145 | | - } catch (JAXBException e) { |
146 | | - throw new RuntimeException("Unable to create JAXB Marshaller", e); |
147 | | - } |
148 | | - } |
149 | | - |
150 | | - private List<File> getHbmFiles(File f) { |
151 | | - List<File> result = new ArrayList<File>(); |
152 | | - if (f.isFile()) { |
153 | | - if (f.getName().endsWith("hbm.xml")) { |
154 | | - result.add(f); |
155 | | - } |
156 | | - } else { |
157 | | - for (File child : f.listFiles()) { |
158 | | - result.addAll(getHbmFiles(child)); |
159 | | - } |
160 | | - } |
161 | | - return result; |
162 | | - } |
163 | | - |
164 | | - private class HbmXmlOrigin extends Origin { |
165 | | - |
166 | | - private static final long serialVersionUID = 1L; |
167 | | - |
168 | | - private final File hbmXmlFile; |
169 | | - |
170 | | - public HbmXmlOrigin(File hbmXmlFile) { |
171 | | - super( SourceType.FILE, hbmXmlFile.getAbsolutePath() ); |
172 | | - this.hbmXmlFile = hbmXmlFile; |
173 | | - } |
174 | | - |
175 | | - public File getHbmXmlFile() { |
176 | | - return hbmXmlFile; |
177 | | - } |
178 | | - |
179 | | - } |
180 | | - |
| 120 | + try { |
| 121 | + marshaller.marshal( mappings, mappingXmlFile ); |
| 122 | + } |
| 123 | + catch (JAXBException e) { |
| 124 | + throw new RuntimeException( |
| 125 | + "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`", |
| 126 | + e |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private Binding<JaxbHbmHibernateMapping> bindMapping( |
| 132 | + MappingBinder mappingBinder, File hbmXmlFile, Origin origin) { |
| 133 | + try ( final FileInputStream fileStream = new FileInputStream(hbmXmlFile) ) { |
| 134 | + return mappingBinder.bind( fileStream, origin ); |
| 135 | + } |
| 136 | + catch (IOException e) { |
| 137 | + getLog().warn( "Unable to open hbm.xml file `" + hbmXmlFile.getAbsolutePath() + "` for transformation", e ); |
| 138 | + return null; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private Marshaller createMarshaller(MappingBinder mappingBinder) { |
| 143 | + try { |
| 144 | + return mappingBinder.mappingJaxbContext().createMarshaller(); |
| 145 | + } |
| 146 | + catch (JAXBException e) { |
| 147 | + throw new RuntimeException("Unable to create JAXB Marshaller", e); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private List<File> getHbmFiles(File f) { |
| 152 | + List<File> result = new ArrayList<>(); |
| 153 | + if (f.isFile()) { |
| 154 | + if (f.getName().endsWith("hbm.xml")) { |
| 155 | + result.add(f); |
| 156 | + } |
| 157 | + } |
| 158 | + else { |
| 159 | + for (File child : Objects.requireNonNull( f.listFiles() ) ) { |
| 160 | + result.addAll(getHbmFiles(child)); |
| 161 | + } |
| 162 | + } |
| 163 | + return result; |
| 164 | + } |
| 165 | + |
| 166 | + private static class HbmXmlOrigin extends Origin { |
| 167 | + |
| 168 | + @Serial |
| 169 | + private static final long serialVersionUID = 1L; |
| 170 | + |
| 171 | + private final File hbmXmlFile; |
| 172 | + |
| 173 | + public HbmXmlOrigin(File hbmXmlFile) { |
| 174 | + super( SourceType.FILE, hbmXmlFile.getAbsolutePath() ); |
| 175 | + this.hbmXmlFile = hbmXmlFile; |
| 176 | + } |
| 177 | + |
| 178 | + public File getHbmXmlFile() { |
| 179 | + return hbmXmlFile; |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
181 | 184 | } |
0 commit comments