Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and accommodate configuration2mermaid.xsl and make changes to adapter2mermaid.xsl #3190

Merged
merged 15 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/src/main/java/nl/nn/adapterframework/core/IListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import javax.annotation.Nonnull;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.doc.ElementType;
import nl.nn.adapterframework.doc.FrankDocGroup;
import nl.nn.adapterframework.doc.ElementType.ElementTypes;
import nl.nn.adapterframework.receivers.RawMessageWrapper;
import nl.nn.adapterframework.stream.Message;

Expand All @@ -32,6 +34,7 @@
* @since 4.2
*/
@FrankDocGroup(order = 30, name = "Listeners")
@ElementType(ElementTypes.LISTENER)
public interface IListener<M> extends IConfigurable {

/**
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/nl/nn/adapterframework/core/ISender.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package nl.nn.adapterframework.core;

import nl.nn.adapterframework.configuration.ConfigurationException;
import nl.nn.adapterframework.doc.ElementType;
import nl.nn.adapterframework.doc.FrankDocGroup;
import nl.nn.adapterframework.doc.ElementType.ElementTypes;
import nl.nn.adapterframework.stream.Message;

/**
Expand All @@ -26,6 +28,7 @@
* @author Gerrit van Brakel
*/
@FrankDocGroup(order = 20, name = "Senders")
@ElementType(ElementTypes.ENDPOINT)
public interface ISender extends IConfigurable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import nl.nn.adapterframework.core.PipeLineSession;
import nl.nn.adapterframework.core.SenderException;
import nl.nn.adapterframework.doc.Protected;
import nl.nn.adapterframework.http.HttpMessageEntity;
import nl.nn.adapterframework.http.HttpResponseHandler;
import nl.nn.adapterframework.http.HttpSenderBase;
Expand All @@ -50,8 +51,8 @@
* This class ensures that Microsoft Authentication Library (MSAL) requests are sent through the configured proxy and the correct SSLSocketFactory.
* @see ExchangeFileSystem
*
* @ff.protected
*/
@Protected
public class MsalClientAdapter extends HttpSenderBase implements IHttpClient {
private static final String METHOD_SESSION_KEY = "HTTP_METHOD";
private static final String REQUEST_HEADERS_SESSION_KEY = "HTTP_REQUEST_HEADERS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
*
* @author John Dekker
*/
public class FtpSession implements IConfigurable, HasKeystore, HasTruststore {
public abstract class FtpSession implements IConfigurable, HasKeystore, HasTruststore {
private static final Logger LOG = LogUtil.getLogger(FtpSession.class);
private @Getter ClassLoader configurationClassLoader = Thread.currentThread().getContextClassLoader();
private @Getter @Setter ApplicationContext applicationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import nl.nn.adapterframework.core.IValidator;
import nl.nn.adapterframework.core.IWrapperPipe;
import nl.nn.adapterframework.doc.Category;
import nl.nn.adapterframework.doc.ElementType;
import nl.nn.adapterframework.doc.Reintroduce;
import nl.nn.adapterframework.doc.ElementType.ElementTypes;

/**
* Plain extension to {@link MessageSendingPipe} that can be used directly in configurations.
Expand All @@ -33,6 +35,7 @@
* @author Dennis van Loon
*/
@Category("Basic")
@ElementType(ElementTypes.ENDPOINT)
public class SenderPipe extends MessageSendingPipe {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 WeAreFrank!
Copyright 2022-2023 WeAreFrank!

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,50 +17,199 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.transform.TransformerException;

import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import lombok.extern.log4j.Log4j2;
import nl.nn.adapterframework.core.IConfigurable;
import nl.nn.adapterframework.core.Resource;
import nl.nn.adapterframework.util.LogUtil;
import nl.nn.adapterframework.doc.ElementType;
import nl.nn.adapterframework.doc.Protected;
import nl.nn.adapterframework.filesystem.FileSystemListener;
import nl.nn.adapterframework.filesystem.FileSystemSender;
import nl.nn.adapterframework.senders.IbisJavaSender;
import nl.nn.adapterframework.senders.IbisLocalSender;
import nl.nn.adapterframework.util.StringUtil;
import nl.nn.adapterframework.util.TransformerPool;
import nl.nn.adapterframework.util.XmlUtils;
import nl.nn.adapterframework.xml.SaxDocumentBuilder;
import nl.nn.adapterframework.xml.SaxElementBuilder;

/**
* Flow generator to create MERMAID files
*/
@Log4j2
public class MermaidFlowGenerator implements IFlowGenerator {
protected static Logger log = LogUtil.getLogger(MermaidFlowGenerator.class);

private static final String MERMAID_XSLT = "/xml/xsl/adapter2mermaid.xsl";
private static final String ADAPTER2MERMAID_XSLT = "/xml/xsl/adapter2mermaid.xsl";
private static final String CONFIGURATION2MERMAID_XSLT = "/xml/xsl/configuration2mermaid.xsl";

private TransformerPool transformerPool;
private final List<String> resourceMethods;
private Document frankElements;

private TransformerPool transformerPoolAdapter;
private TransformerPool transformerPoolConfig;

public MermaidFlowGenerator() {
resourceMethods = List.of(
"setAction", "setWsdl", "setSchema", "setSchemaLocation", "setDirection", "setOutputFormat",
"setResponseRoot", "setXpathExpression", "setStyleSheetName", "setStyleSheetNameSessionKey"
);
}

@Override
public void afterPropertiesSet() throws Exception {
Resource xsltSourceConfig = Resource.getResource(MERMAID_XSLT);
transformerPool = TransformerPool.getInstance(xsltSourceConfig, 2);
String frankElementsList = compileFrankElementList();
frankElements = XmlUtils.buildDomDocument(new InputSource(new StringReader(frankElementsList)), true);

Resource xsltSourceAdapter = Resource.getResource(ADAPTER2MERMAID_XSLT);
transformerPoolAdapter = TransformerPool.getInstance(xsltSourceAdapter, 2);

Resource xsltSourceConfig = Resource.getResource(CONFIGURATION2MERMAID_XSLT);
transformerPoolConfig = TransformerPool.getInstance(xsltSourceConfig, 2);
}

private String compileFrankElementList() throws SAXException, ClassNotFoundException {
try (SaxDocumentBuilder builder = new SaxDocumentBuilder("root")) {
List<String> classNames = findAllFrankElements();
for(String className : classNames) {
Class<?> clazz = Class.forName(className);
ElementType type = AnnotationUtils.findAnnotation(clazz, ElementType.class);
if(type != null) {
try (SaxElementBuilder classElement = builder.startElement(className)) {
try (SaxElementBuilder typeElement = classElement.startElement("type")) {
typeElement.addValue(type.value().name().toLowerCase());
}
try (SaxElementBuilder modifierElement = classElement.startElement("modifier")) {
modifierElement.addValue(""+deduceModifier(clazz));
}
addResourceMethods(classElement, clazz.getMethods());
}
}
else {
log.debug("skipping class [{}]", clazz);
}
}
builder.endElement();
return builder.toString();
}
}

/**
* Returns a classifier use by the Mermaid XSLT, which in turn is used to change the style in the diagram.
*/
private int deduceModifier(Class<?> clazz) {
String packageName = clazz.getPackageName();
if(packageName.contains(".http")) {
return 0;
} else if(packageName.contains(".jms") || packageName.contains(".esb")) {
return 1;
} else if(packageName.contains(".jdbc")) {
return 2;
} else if(FileSystemListener.class.isAssignableFrom(clazz) || FileSystemSender.class.isAssignableFrom(clazz)) {
return 3;
} else if(IbisJavaSender.class.isAssignableFrom(clazz) || IbisLocalSender.class.isAssignableFrom(clazz)) {
return 4;
} else if(packageName.contains(".sap")) {
return 5;
}
return 7;
}

private void addResourceMethods(SaxElementBuilder element, Method[] methods) throws SAXException {
for(Method method : methods) {
String methodName = method.getName();
if(method.getParameterTypes().length == 1 && resourceMethods.contains(methodName)) {
String attributeName = StringUtil.lcFirst(methodName.substring(3));
addAttribute(element, attributeName);
}
}
}

private void addAttribute(SaxElementBuilder element, String attributeValue) throws SAXException {
try (SaxElementBuilder attributeElement = element.startElement("attribute")) {
attributeElement.addAttribute("name", attributeValue);
}
}

private List<String> findAllFrankElements() {
BeanDefinitionRegistry beanDefinitionRegistry = new SimpleBeanDefinitionRegistry();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanDefinitionRegistry);
scanner.setIncludeAnnotationConfig(false);
scanner.resetFilters(false);
scanner.addIncludeFilter(new AssignableTypeFilter(IConfigurable.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(ElementType.class));
scanner.addExcludeFilter(this::matchesTestClassPath); //Exclude test classpath
scanner.addExcludeFilter((i,e) -> i.getClassMetadata().getClassName().contains("$")); //Exclude inner classes
scanner.addExcludeFilter(new AnnotationTypeFilter(Protected.class)); //Exclude protected FrankElements

BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator() {
@Override
protected String buildDefaultBeanName(BeanDefinition definition) {
String beanClassName = definition.getBeanClassName();
Assert.state(beanClassName != null, "No bean class name set");
return beanClassName;
}
};
scanner.setBeanNameGenerator(beanNameGenerator);

int numberOfBeans = scanner.scan("nl.nn.adapterframework");
log.debug("found [{}] beans registered", numberOfBeans);

String[] bdn = scanner.getRegistry().getBeanDefinitionNames();
return Arrays.asList(bdn);
}

private boolean matchesTestClassPath(MetadataReader reader, MetadataReaderFactory factory) throws IOException {
return reader.getResource().getURI().toString().contains("/test-classes/");
}

@Override
public void generateFlow(String xml, OutputStream outputStream) throws FlowGenerationException {
try {
String flow = generateDot(xml);
String flow = generateMermaid(xml);

outputStream.write(flow.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new FlowGenerationException(e);
}
}

protected String generateDot(String xml) throws FlowGenerationException {
protected String generateMermaid(String xml) throws FlowGenerationException {
try {
return transformerPool.transform(xml, null);
Map<String, Object> xsltParams = new HashMap<>(1);//frankElements
xsltParams.put("frankElements", frankElements);
if(xml.startsWith("<adapter")) {
return transformerPoolAdapter.transform(xml, xsltParams);
} else {
return transformerPoolConfig.transform(xml, xsltParams);
}
} catch (IOException | TransformerException | SAXException e) {
throw new FlowGenerationException("error transforming [xml] to [dot]", e);
throw new FlowGenerationException("error transforming [xml] to [mermaid]", e);
}
}

Expand All @@ -76,8 +225,13 @@ public MediaType getMediaType() {

@Override
public void destroy() {
if(transformerPool != null) {
transformerPool.close();
if(transformerPoolAdapter != null) {
transformerPoolAdapter.close();
}

if(transformerPoolConfig != null) {
transformerPoolConfig.close();
}
frankElements = null;
}
}
Loading
Loading