in log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/generator/InterfacesGenerator.java [124:312]
public void generateSource() throws Exception {
boolean errors = false;
CatalogData catalogData = catalogReader.read();
if (catalogData != null) {
List<Event> events = catalogData.getEvents();
Map<String, Attribute> requestContextAttrs = new HashMap<>();
Map<String, Boolean> requestContextIsRequired = new HashMap<>();
Map<String, Attribute> attributes = catalogReader.getAttributes();
Map<String, String> importedTypes = new HashMap<>();
boolean anyConstraints = false;
for (Attribute attribute : attributes.values()) {
if (attribute.isRequestContext()) {
String name = attribute.getName();
if (name.startsWith(REQCTX)) {
name = name.substring(REQCTX.length());
}
requestContextAttrs.put(name, attribute);
requestContextIsRequired.put(name, attribute.isRequired());
}
}
for (Event event : events) {
String maxLen = Integer.toString(enterpriseId);
int maxNameLength = maxKeyLength - maxLen.length() - 1;
if (event.getName().length() > maxNameLength) {
LOGGER.error("{} exceeds maximum length of {} for an event name", event.getName(), maxNameLength);
errors = true;
continue;
}
ClassGenerator classGenerator = new ClassGenerator(
NamingUtils.getClassName(event.getName()), outputDirectory);
classGenerator.setClass(false);
classGenerator.setPackageName(packageName);
classGenerator.setParentClassName(PARENT_CLASS);
classGenerator.setJavadocComment(event.getDescription());
classGenerator.setVerbose(verbose);
Set<String> imports = classGenerator.getImports();
imports.add(PARENT_IMPORT);
StringBuilder annotations = new StringBuilder();
imports.add(EVENT_NAME_IMPORT);
annotations.append("@EventName(\"").append(event.getName()).append("\")\n");
imports.add(MAX_LENGTH_IMPORT);
annotations.append("@MaxLength(").append(maxKeyLength).append(")");
List<EventAttribute> eventAttributes = event.getAttributes();
boolean anyRequired = false;
if (eventAttributes != null) {
for (EventAttribute eventAttribute : eventAttributes) {
Attribute attribute = attributes.get(eventAttribute.getName());
if (attribute == null) {
LOGGER.error("Unable to locate attribute name {}", eventAttribute.getName());
errors = true;
continue;
}
if (attribute.isRequestContext() && attribute.isRequired()) {
String name = eventAttribute.getName();
if (name.startsWith(REQCTX)) {
name = name.substring(REQCTX.length());
}
requestContextIsRequired.put(name, Boolean.TRUE);
continue;
}
String name = attribute.getName();
if (EVENT_ID.equals(name) || EVENT_TYPE.equals(name) || TIMESTAMP.equals(name)) {
continue;
}
if (name.indexOf('.') != -1) {
name = name.replaceAll("\\.", "");
}
if (name.indexOf('/') != -1) {
name = name.replaceAll("/", "");
}
if (name.length() > maxKeyLength) {
LOGGER.error("{} exceeds maximum length of {} for an attribute name", name, maxKeyLength);
errors = true;
continue;
}
String type = attribute.getDataType().getTypeName();
MethodDefinition definition = new MethodDefinition("void",
NamingUtils.getMutatorName(name));
if (!attribute.isRequestContext() && attribute.getDataType().getImportClass() != null) {
if (!importedTypes.containsKey(attribute.getDataType().getTypeName())) {
importedTypes.put(attribute.getDataType().getTypeName(), attribute.getDataType().getImportClass());
}
}
definition.addParameter(new Parameter(name, type, attribute.getDescription()));
definition.setInterface(true);
definition.setVisability("public");
definition.setJavadocComments(attribute.getDisplayName()
+ " : " + attribute.getDescription());
StringBuilder buffer = new StringBuilder();
Set<Constraint> constraints = attribute.getConstraints();
boolean first = true;
if (attribute.isRequired() || eventAttribute.isRequired()) {
anyRequired = true;
buffer.append(REQUIRED);
first = false;
}
if (constraints != null && constraints.size() > 0) {
anyConstraints = true;
for (Constraint constraint : constraints) {
if (!first) {
buffer.append("\n ");
}
first = false;
appendConstraint(constraint, buffer);
}
}
if (buffer.length() > 0) {
definition.setAnnotation(buffer.toString());
}
classGenerator.addMethod(definition);
}
}
if (importedTypes.size() > 0) {
imports.addAll(importedTypes.values());
}
if (anyRequired) {
imports.add(REQUIRED_IMPORT);
}
boolean firstReqCtx = true;
if (requestContextAttrs.size() > 0) {
imports.add(REQUEST_CONTEXT_IMPORT);
StringBuilder reqCtx = new StringBuilder();
for (Map.Entry<String, Attribute> entry : requestContextAttrs.entrySet()) {
if (!firstReqCtx) {
reqCtx.append(")\n");
}
firstReqCtx = false;
reqCtx.append(REQCTX_ANN);
reqCtx.append(KEY).append(entry.getKey()).append("\"");
Attribute attrib = entry.getValue();
String name = attrib.getName();
if (name.startsWith(REQCTX)) {
name = name.substring(REQCTX.length());
}
Boolean isRequired = null;
final String attrName = name;
if (event.getAttributes() != null) {
Optional<EventAttribute> optional = event.getAttributes().stream().filter(a -> attrName.equals(a.getName())).findFirst();
if (optional.isPresent()) {
isRequired = optional.get().isRequired();
}
}
if ((isRequired != null && isRequired) ||
(isRequired == null && requestContextIsRequired.get(name))) {
reqCtx.append(", ").append(REQUIRED_ATTR);
}
Set<Constraint> constraints = entry.getValue().getConstraints();
if (constraints != null && constraints.size() > 0) {
anyConstraints = true;
reqCtx.append(CONSTRAINTS_ATTR);
boolean first = true;
for (Constraint constraint : constraints) {
if (!first) {
reqCtx.append(", ");
}
first = false;
appendConstraint(constraint, reqCtx);
}
reqCtx.append("}");
}
}
reqCtx.append(")");
if (annotations.length() > 0) {
annotations.append("\n");
}
annotations.append(reqCtx.toString());
}
if (anyConstraints) {
imports.add(CONSTRAINT_IMPORT);
}
if (annotations.length() > 0) {
classGenerator.setAnnotations(annotations.toString());
}
classGenerator.generate();
}
}
if (errors) {
throw new IllegalStateException("Errors were encountered during code generation");
}
}