|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.axis2.jpa.schema; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 24 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileInputStream; |
| 28 | +import java.io.FileWriter; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.InputStream; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.LinkedHashMap; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +/** |
| 36 | + * Batch generator that scans a directory of Hibernate XML mapping files |
| 37 | + * ({@code *.hbm.xml}) and produces a single JSON file containing OpenAPI |
| 38 | + * 3.0 component schemas for every entity found. |
| 39 | + * |
| 40 | + * <p>Designed to run as a standalone build tool — invoked from Ant, Maven, |
| 41 | + * Gradle, or the command line. The output file is a self-contained OpenAPI |
| 42 | + * {@code components/schemas} fragment that can be merged into an OpenAPI |
| 43 | + * specification or served directly by an Axis2 service. |
| 44 | + * |
| 45 | + * <h3>Command-line usage</h3> |
| 46 | + * <pre>{@code |
| 47 | + * java -cp axis2-jpa-schema.jar:jackson-*.jar \ |
| 48 | + * org.apache.axis2.jpa.schema.HbmBatchSchemaGenerator \ |
| 49 | + * src/main/resources \ |
| 50 | + * build/openapi-schemas.json |
| 51 | + * }</pre> |
| 52 | + * |
| 53 | + * <h3>Ant integration</h3> |
| 54 | + * <pre>{@code |
| 55 | + * <target name="openapi-schema" |
| 56 | + * description="Generate OpenAPI schemas from HBM XML mappings"> |
| 57 | + * <java classname="org.apache.axis2.jpa.schema.HbmBatchSchemaGenerator" |
| 58 | + * fork="true" failonerror="true"> |
| 59 | + * <classpath> |
| 60 | + * <fileset dir="lib" includes="axis2-jpa-schema*.jar,jackson-*.jar,commons-logging*.jar"/> |
| 61 | + * </classpath> |
| 62 | + * <arg value="src/main/resources"/> |
| 63 | + * <arg value="build/openapi-schemas.json"/> |
| 64 | + * </java> |
| 65 | + * </target> |
| 66 | + * }</pre> |
| 67 | + * |
| 68 | + * <h3>Output format</h3> |
| 69 | + * <pre>{@code |
| 70 | + * { |
| 71 | + * "openapi": "3.0.1", |
| 72 | + * "info": { |
| 73 | + * "title": "Generated from 146 HBM XML mappings", |
| 74 | + * "version": "1.0.0" |
| 75 | + * }, |
| 76 | + * "components": { |
| 77 | + * "schemas": { |
| 78 | + * "CompanyBO": { "type": "object", "properties": { ... } }, |
| 79 | + * "CompanyBOWrite": { "type": "object", "properties": { ... } }, |
| 80 | + * "DepartmentBO": { ... }, |
| 81 | + * "DepartmentBOWrite": { ... }, |
| 82 | + * ... |
| 83 | + * } |
| 84 | + * } |
| 85 | + * } |
| 86 | + * }</pre> |
| 87 | + * |
| 88 | + * <p>Each entity produces two schemas: a read schema (all fields, IDs |
| 89 | + * marked {@code readOnly}) and a write schema (excludes |
| 90 | + * {@code @GeneratedValue} IDs, {@code @Version} fields, and any fields |
| 91 | + * annotated with custom write-exclude annotations). |
| 92 | + * |
| 93 | + * <h3>Exit codes</h3> |
| 94 | + * <ul> |
| 95 | + * <li>0 — success</li> |
| 96 | + * <li>1 — invalid arguments</li> |
| 97 | + * <li>2 — input directory does not exist or contains no HBM files</li> |
| 98 | + * <li>3 — I/O error writing output</li> |
| 99 | + * </ul> |
| 100 | + */ |
| 101 | +public class HbmBatchSchemaGenerator { |
| 102 | + |
| 103 | + public static void main(String[] args) { |
| 104 | + if (args.length != 2) { |
| 105 | + System.err.println("Usage: HbmBatchSchemaGenerator <hbm-dir> <output-json>"); |
| 106 | + System.err.println(); |
| 107 | + System.err.println(" hbm-dir Directory containing *.hbm.xml files"); |
| 108 | + System.err.println(" output-json Output file path (e.g., openapi-schemas.json)"); |
| 109 | + System.err.println(); |
| 110 | + System.err.println("Example:"); |
| 111 | + System.err.println(" java -cp ... org.apache.axis2.jpa.schema.HbmBatchSchemaGenerator \\"); |
| 112 | + System.err.println(" src/main/resources build/openapi-schemas.json"); |
| 113 | + System.exit(1); |
| 114 | + } |
| 115 | + |
| 116 | + File inputDir = new File(args[0]); |
| 117 | + File outputFile = new File(args[1]); |
| 118 | + |
| 119 | + if (!inputDir.isDirectory()) { |
| 120 | + System.err.println("ERROR: Not a directory: " + inputDir.getAbsolutePath()); |
| 121 | + System.exit(2); |
| 122 | + } |
| 123 | + |
| 124 | + File[] hbmFiles = inputDir.listFiles((dir, name) -> |
| 125 | + name.endsWith(".hbm.xml")); |
| 126 | + if (hbmFiles == null || hbmFiles.length == 0) { |
| 127 | + System.err.println("ERROR: No *.hbm.xml files found in: " + inputDir.getAbsolutePath()); |
| 128 | + System.exit(2); |
| 129 | + } |
| 130 | + |
| 131 | + // Sort for deterministic output ordering |
| 132 | + Arrays.sort(hbmFiles); |
| 133 | + |
| 134 | + HbmXmlIntrospector introspector = new HbmXmlIntrospector(); |
| 135 | + Map<String, ObjectNode> allSchemas = new LinkedHashMap<>(); |
| 136 | + |
| 137 | + int successCount = 0; |
| 138 | + int errorCount = 0; |
| 139 | + |
| 140 | + for (File hbmFile : hbmFiles) { |
| 141 | + try (InputStream is = new FileInputStream(hbmFile)) { |
| 142 | + EntitySchemaModel model = introspector.introspect(is, hbmFile.getName()); |
| 143 | + if (model == null) { |
| 144 | + System.err.println(" SKIP " + hbmFile.getName() + " (no entity found)"); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + Map<String, ObjectNode> schemas = JpaSchemaGenerator.generateBothSchemas(model); |
| 149 | + allSchemas.putAll(schemas); |
| 150 | + successCount++; |
| 151 | + |
| 152 | + int fieldCount = model.getFields().size(); |
| 153 | + int relCount = (int) model.getFields().values().stream() |
| 154 | + .filter(f -> f.getRefEntityName() != null).count(); |
| 155 | + System.out.println(" OK " + hbmFile.getName() |
| 156 | + + " → " + model.getEntityName() |
| 157 | + + " (" + fieldCount + " fields, " + relCount + " relationships)"); |
| 158 | + |
| 159 | + } catch (IOException e) { |
| 160 | + System.err.println(" FAIL " + hbmFile.getName() + " (I/O): " + e.getMessage()); |
| 161 | + errorCount++; |
| 162 | + } catch (RuntimeException e) { |
| 163 | + System.err.println(" FAIL " + hbmFile.getName() + " (unexpected):"); |
| 164 | + e.printStackTrace(System.err); |
| 165 | + errorCount++; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (allSchemas.isEmpty()) { |
| 170 | + System.err.println("ERROR: No schemas generated from " + hbmFiles.length + " files"); |
| 171 | + System.exit(2); |
| 172 | + } |
| 173 | + |
| 174 | + // Build the OpenAPI 3.0 document structure |
| 175 | + ObjectMapper mapper = new ObjectMapper(); |
| 176 | + mapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 177 | + |
| 178 | + ObjectNode root = mapper.createObjectNode(); |
| 179 | + root.put("openapi", "3.0.1"); |
| 180 | + |
| 181 | + ObjectNode info = root.putObject("info"); |
| 182 | + info.put("title", "Generated from " + successCount + " HBM XML mappings"); |
| 183 | + info.put("version", "1.0.0"); |
| 184 | + info.put("description", |
| 185 | + "Auto-generated OpenAPI component schemas from Hibernate XML mappings. " |
| 186 | + + "Each entity has a read schema (all fields) and a write schema " |
| 187 | + + "(excludes server-managed fields like generated IDs, version, " |
| 188 | + + "and audit timestamps)."); |
| 189 | + |
| 190 | + ObjectNode components = root.putObject("components"); |
| 191 | + ObjectNode schemas = components.putObject("schemas"); |
| 192 | + for (Map.Entry<String, ObjectNode> entry : allSchemas.entrySet()) { |
| 193 | + schemas.set(entry.getKey(), entry.getValue()); |
| 194 | + } |
| 195 | + |
| 196 | + // Write output |
| 197 | + try { |
| 198 | + File parentDir = outputFile.getParentFile(); |
| 199 | + if (parentDir != null && !parentDir.exists()) { |
| 200 | + parentDir.mkdirs(); |
| 201 | + } |
| 202 | + try (FileWriter writer = new FileWriter(outputFile)) { |
| 203 | + writer.write(mapper.writeValueAsString(root)); |
| 204 | + } |
| 205 | + } catch (IOException e) { |
| 206 | + System.err.println("ERROR: Failed to write output: " + e.getMessage()); |
| 207 | + System.exit(3); |
| 208 | + } |
| 209 | + |
| 210 | + System.out.println(); |
| 211 | + System.out.println("Generated " + allSchemas.size() + " schemas" |
| 212 | + + " (" + successCount + " entities × 2 [read + write])" |
| 213 | + + " from " + hbmFiles.length + " HBM files"); |
| 214 | + if (errorCount > 0) { |
| 215 | + System.out.println("WARNING: " + errorCount + " files had errors"); |
| 216 | + } |
| 217 | + System.out.println("Output: " + outputFile.getAbsolutePath()); |
| 218 | + } |
| 219 | +} |
0 commit comments