Skip to content

Commit

Permalink
[maven] mark Mojo threadSafe=true + fix concurrency issue in Co… (#5898)
Browse files Browse the repository at this point in the history
  • Loading branch information
famod authored Apr 19, 2020
1 parent c5472be commit 84099ee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,67 +79,72 @@ public 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();

if (modules != null && modules.length > 0) {
mapper.registerModules(modules);
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());
}

mapper.registerModule(new GuavaModule());
configurator.generatorSettingsBuilder = GeneratorSettings.newBuilder(generatorSettings);
configurator.workflowSettingsBuilder = WorkflowSettings.newBuilder(workflowSettings);

try {
DynamicSettings settings = mapper.readValue(new File(configFile), DynamicSettings.class);
CodegenConfigurator configurator = new CodegenConfigurator();
return configurator;
}
return null;
}

GeneratorSettings generatorSettings = settings.getGeneratorSettings();
WorkflowSettings workflowSettings = settings.getWorkflowSettings();
private static DynamicSettings readDynamicSettings(String configFile, Module... modules) {
ObjectMapper mapper;

// 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 (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);
}

configurator.generatorSettingsBuilder = GeneratorSettings.newBuilder(generatorSettings);
configurator.workflowSettingsBuilder = WorkflowSettings.newBuilder(workflowSettings);
mapper.registerModule(new GuavaModule());

return configurator;
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
throw new RuntimeException("Unable to deserialize config file: " + configFile);
}
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);
}
return null;
}

public CodegenConfigurator addServerVariable(String key, String value) {
Expand Down

0 comments on commit 84099ee

Please sign in to comment.