forked from loafle/openapi-generator-original
[maven] mark Mojo threadSafe=true + fix concurrency issue in Co… (#5898)
This commit is contained in:
parent
c5472be422
commit
84099eefdc
@ -70,7 +70,7 @@ import com.google.common.io.Files;
|
||||
* Goal which generates client/server code from a OpenAPI json/yaml definition.
|
||||
*/
|
||||
@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
|
||||
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
|
||||
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
|
||||
public class CodeGenMojo extends AbstractMojo {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CodeGenMojo.class);
|
||||
|
@ -79,69 +79,74 @@ public class CodegenConfigurator {
|
||||
public static CodegenConfigurator fromFile(String configFile, Module... modules) {
|
||||
|
||||
if (isNotEmpty(configFile)) {
|
||||
ObjectMapper mapper;
|
||||
DynamicSettings settings = readDynamicSettings(configFile, modules);
|
||||
|
||||
if (FilenameUtils.isExtension(configFile, new String[]{"yml", "yaml"})) {
|
||||
mapper = Yaml.mapper();
|
||||
} else {
|
||||
mapper = Json.mapper();
|
||||
CodegenConfigurator configurator = new CodegenConfigurator();
|
||||
|
||||
GeneratorSettings generatorSettings = settings.getGeneratorSettings();
|
||||
WorkflowSettings workflowSettings = settings.getWorkflowSettings();
|
||||
|
||||
// We copy "cached" properties into configurator so it is appropriately configured with all settings in external files.
|
||||
// FIXME: target is to eventually move away from CodegenConfigurator properties except gen/workflow settings.
|
||||
configurator.generatorName = generatorSettings.getGeneratorName();
|
||||
configurator.inputSpec = workflowSettings.getInputSpec();
|
||||
configurator.templatingEngineName = workflowSettings.getTemplatingEngineName();
|
||||
if (workflowSettings.getSystemProperties() != null) {
|
||||
configurator.systemProperties.putAll(workflowSettings.getSystemProperties());
|
||||
}
|
||||
if(generatorSettings.getInstantiationTypes() != null) {
|
||||
configurator.instantiationTypes.putAll(generatorSettings.getInstantiationTypes());
|
||||
}
|
||||
if(generatorSettings.getTypeMappings() != null) {
|
||||
configurator.typeMappings.putAll(generatorSettings.getTypeMappings());
|
||||
}
|
||||
if(generatorSettings.getAdditionalProperties() != null) {
|
||||
configurator.additionalProperties.putAll(generatorSettings.getAdditionalProperties());
|
||||
}
|
||||
if(generatorSettings.getImportMappings() != null) {
|
||||
configurator.importMappings.putAll(generatorSettings.getImportMappings());
|
||||
}
|
||||
if(generatorSettings.getLanguageSpecificPrimitives() != null) {
|
||||
configurator.languageSpecificPrimitives.addAll(generatorSettings.getLanguageSpecificPrimitives());
|
||||
}
|
||||
if(generatorSettings.getReservedWordMappings() != null) {
|
||||
configurator.reservedWordMappings.putAll(generatorSettings.getReservedWordMappings());
|
||||
}
|
||||
if(generatorSettings.getServerVariables() != null) {
|
||||
configurator.serverVariables.putAll(generatorSettings.getServerVariables());
|
||||
}
|
||||
|
||||
if (modules != null && modules.length > 0) {
|
||||
mapper.registerModules(modules);
|
||||
}
|
||||
configurator.generatorSettingsBuilder = GeneratorSettings.newBuilder(generatorSettings);
|
||||
configurator.workflowSettingsBuilder = WorkflowSettings.newBuilder(workflowSettings);
|
||||
|
||||
mapper.registerModule(new GuavaModule());
|
||||
|
||||
try {
|
||||
DynamicSettings settings = mapper.readValue(new File(configFile), DynamicSettings.class);
|
||||
CodegenConfigurator configurator = new CodegenConfigurator();
|
||||
|
||||
GeneratorSettings generatorSettings = settings.getGeneratorSettings();
|
||||
WorkflowSettings workflowSettings = settings.getWorkflowSettings();
|
||||
|
||||
// We copy "cached" properties into configurator so it is appropriately configured with all settings in external files.
|
||||
// FIXME: target is to eventually move away from CodegenConfigurator properties except gen/workflow settings.
|
||||
configurator.generatorName = generatorSettings.getGeneratorName();
|
||||
configurator.inputSpec = workflowSettings.getInputSpec();
|
||||
configurator.templatingEngineName = workflowSettings.getTemplatingEngineName();
|
||||
if (workflowSettings.getSystemProperties() != null) {
|
||||
configurator.systemProperties.putAll(workflowSettings.getSystemProperties());
|
||||
}
|
||||
if(generatorSettings.getInstantiationTypes() != null) {
|
||||
configurator.instantiationTypes.putAll(generatorSettings.getInstantiationTypes());
|
||||
}
|
||||
if(generatorSettings.getTypeMappings() != null) {
|
||||
configurator.typeMappings.putAll(generatorSettings.getTypeMappings());
|
||||
}
|
||||
if(generatorSettings.getAdditionalProperties() != null) {
|
||||
configurator.additionalProperties.putAll(generatorSettings.getAdditionalProperties());
|
||||
}
|
||||
if(generatorSettings.getImportMappings() != null) {
|
||||
configurator.importMappings.putAll(generatorSettings.getImportMappings());
|
||||
}
|
||||
if(generatorSettings.getLanguageSpecificPrimitives() != null) {
|
||||
configurator.languageSpecificPrimitives.addAll(generatorSettings.getLanguageSpecificPrimitives());
|
||||
}
|
||||
if(generatorSettings.getReservedWordMappings() != null) {
|
||||
configurator.reservedWordMappings.putAll(generatorSettings.getReservedWordMappings());
|
||||
}
|
||||
if(generatorSettings.getServerVariables() != null) {
|
||||
configurator.serverVariables.putAll(generatorSettings.getServerVariables());
|
||||
}
|
||||
|
||||
configurator.generatorSettingsBuilder = GeneratorSettings.newBuilder(generatorSettings);
|
||||
configurator.workflowSettingsBuilder = WorkflowSettings.newBuilder(workflowSettings);
|
||||
|
||||
return configurator;
|
||||
} catch (IOException ex) {
|
||||
LOGGER.error(ex.getMessage());
|
||||
throw new RuntimeException("Unable to deserialize config file: " + configFile);
|
||||
}
|
||||
return configurator;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static DynamicSettings readDynamicSettings(String configFile, Module... modules) {
|
||||
ObjectMapper mapper;
|
||||
|
||||
if (FilenameUtils.isExtension(configFile.toLowerCase(Locale.ROOT), new String[]{"yml", "yaml"})) {
|
||||
mapper = Yaml.mapper().copy();
|
||||
} else {
|
||||
mapper = Json.mapper().copy();
|
||||
}
|
||||
|
||||
if (modules != null && modules.length > 0) {
|
||||
mapper.registerModules(modules);
|
||||
}
|
||||
|
||||
mapper.registerModule(new GuavaModule());
|
||||
|
||||
try {
|
||||
return mapper.readValue(new File(configFile), DynamicSettings.class);
|
||||
} catch (IOException ex) {
|
||||
LOGGER.error(ex.getMessage());
|
||||
throw new RuntimeException("Unable to deserialize config file: " + configFile);
|
||||
}
|
||||
}
|
||||
|
||||
public CodegenConfigurator addServerVariable(String key, String value) {
|
||||
this.serverVariables.put(key, value);
|
||||
generatorSettingsBuilder.withServerVariable(key, value);
|
||||
|
Loading…
x
Reference in New Issue
Block a user