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

Add rule to limit excessive use of sequential loggers #56

Merged
merged 17 commits into from
Oct 13, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import groovy.xml.slurpersupport.Node
@SuppressWarnings('SpaceAroundMapEntryColon')
class ConfigurationFile extends ProjectFile {

static final String MULE_CORE_NAMESPACE = 'http://www.mulesoft.org/schema/mule/core'
static final String ELEMENT_FLOWREF = 'flow-ref'
MuleXmlParser parser
private final GPathResult configXml
private final Boolean exists
private Map<String, String> nonGlobalConfig = ['sub-flow' : MULE_CORE_NAMESPACE,
'flow' : MULE_CORE_NAMESPACE,
'error-handler': MULE_CORE_NAMESPACE]
private Map<String, String> nonGlobalConfig = ['sub-flow' : Namespace.CORE,
'flow' : Namespace.CORE,
'error-handler': Namespace.CORE]

ConfigurationFile(File file) {
super(file)
Expand Down Expand Up @@ -134,7 +133,7 @@ class ConfigurationFile extends ProjectFile {
}

List<MuleComponent> getFlowrefs() {
return findComponents(ELEMENT_FLOWREF, MULE_CORE_NAMESPACE)
return findComponents(ELEMENT_FLOWREF, Namespace.CORE)
}

List<FlowComponent> getAllFlows() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.avioconsulting.mule.linter.model

class Namespace {
static final String CORE = "http://www.mulesoft.org/schema/mule/core"
static final String CORE_EE = "http://www.mulesoft.org/schema/mule/ee/core"
static final String EE = CORE_EE
static final String DOC = "http://www.mulesoft.org/schema/mule/documentation"
static final String ANYPOINT_MQ = "http://www.mulesoft.org/schema/mule/anypoint-mq"
static final String APIKIT = "http://www.mulesoft.org/schema/mule/mule-apikit"
static final String APIKIT_SOAP = "http://www.mulesoft.org/schema/mule/apikit-soap"
static final String BATCH = "http://www.mulesoft.org/schema/mule/batch"
static final String DATABASE = "http://www.mulesoft.org/schema/mule/db"
static final String DB = DATABASE
static final String EMAIL = "http://www.mulesoft.org/schema/mule/email"
static final String FILE = "http://www.mulesoft.org/schema/mule/file"
static final String FTP = "http://www.mulesoft.org/schema/mule/ftp"
static final String HTTP = "http://www.mulesoft.org/schema/mule/http"
static final String JAVA = "http://www.mulesoft.org/schema/mule/java"
static final String JMS = "http://www.mulesoft.org/schema/mule/jms"
static final String LDAP = "http://www.mulesoft.org/schema/mule/ldap"
static final String OAUTH = "http://www.mulesoft.org/schema/mule/oauth"
static final String OBJECT_STORE = "http://www.mulesoft.org/schema/mule/os"
static final String OS = OBJECT_STORE
static final String SCRIPTING = "http://www.mulesoft.org/schema/mule/scripting"
static final String SECURE_PROPERTIES = "http://www.mulesoft.org/schema/mule/secure-properties"
static final String SFTP = "http://www.mulesoft.org/schema/mule/sftp"
static final String SOCKETS = "http://www.mulesoft.org/schema/mule/sockets"
static final String SPRING = "http://www.mulesoft.org/schema/mule/spring"
static final String TLS = "http://www.mulesoft.org/schema/mule/tls"
static final String VALIDATIION = "http://www.mulesoft.org/schema/mule/validation"
static final String VM = "http://www.mulesoft.org/schema/mule/vm"
static final String WEB_SERVICE_CONSUMER = "http://www.mulesoft.org/schema/mule/wsc"
static final String WSC = WEB_SERVICE_CONSUMER
static final String XML_MODULE = "http://www.mulesoft.org/schema/mule/xml-module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.avioconsulting.mule.linter.model

class Version {

enum Operator {

EQUAL,
GREATER_THAN

}

private String version

Version(String version) {
this.version = version
}

void setVersion(String version) {
this.version = version
}

Boolean isEqual(String version) {
return this.version == version
}

Boolean isGreater(String version) {
return this.version < version
}

}

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.avioconsulting.mule.linter.model.configuration

import com.avioconsulting.mule.linter.model.Namespace

class FlowComponent extends MuleComponent {

final static String COMPONENT_NAMESPACE = 'http://www.mulesoft.org/schema/mule/core'
final static String COMPONENT_NAMESPACE = Namespace.CORE
final static String COMPONENT_NAME_FLOW = 'flow'
final static String COMPONENT_NAME_SUBFLOW = 'sub-flow'
static final String APIKIT_FLOW_PREFIX_REGEX = '(get:|post:|put:|patch:|delete:|head:|options:|trace:).*'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
package com.avioconsulting.mule.linter.model.configuration

import com.avioconsulting.mule.linter.model.Namespace

class LoggerComponent extends MuleComponent {

final static String COMPONENT_NAMESPACE = 'http://www.mulesoft.org/schema/mule/core'
final static String COMPONENT_NAMESPACE = Namespace.CORE
final static String COMPONENT_NAME = 'logger'
private final String docName
private final String message
private final String level
private final LogLevel level
private final String category

static enum LogLevel {
TRACE,
DEBUG,
INFO,
WARN,
ERROR
}

LoggerComponent(Map<String, String> attributes, File file) {
super(COMPONENT_NAME, COMPONENT_NAMESPACE, attributes,file)
this.docName = attributes.get('{http://www.mulesoft.org/schema/mule/documentation}name')
this.message = attributes.get('message')
this.level = attributes.get('level')
this.level = LogLevel.valueOf attributes.get('level')
this.category = attributes.get('category')
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

class ArtifactDescriptor {

String groupId
String artifactId
PomElement version
Integer lineNo
PomFile pomFile
GPathResult pluginXml

ArtifactDescriptor(GPathResult pluginXml, PomFile pomFile) {
this.pluginXml = pluginXml
this.groupId = pluginXml.groupId as String
this.artifactId = pluginXml.artifactId as String
this.lineNo = MuleXmlParser.getNodeLineNumber(pluginXml)
this.pomFile = pomFile
this.version = getAttribute('version')
}

PomElement getAttribute(String attributeName) {
PomElement pElement = null
GPathResult element = pluginXml.depthFirst().find {
it.name() == attributeName
}

if (element != null ) {
if ( isExpression(element.text()) ) {
pElement = pomFile.getPomProperty(variableName(element.text()))
} else {
pElement = new PomElement()
pElement.name = element.name()
pElement.value = element.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(element)
}
}

return pElement
}

PomElement getConfigProperty(String propertyName) {
PomElement pElement = null
pluginXml.configuration.depthFirst().each {
if (it.name() == propertyName) {
pElement = new PomElement()
pElement.name = propertyName
pElement.value = it.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(it)
}
}
return pElement
}

private Boolean isExpression(String expression) {
expression.startsWith('${')
}

private String variableName(String expression) {
expression.takeAfter('${').takeBefore('}')
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.avioconsulting.mule.linter.model.pom

import groovy.xml.slurpersupport.GPathResult

class PomDependency extends ArtifactDescriptor {

PomDependency(GPathResult pluginXml, PomFile pomFile) {
super(pluginXml, pomFile)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ class PomFile extends ProjectFile {
return plugin
}

PomDependency getDependency(String groupId, String artifactId) {
PomDependency dependency
GPathResult dependencyPath = pomXml.dependencies.dependency.find {
it.groupId == groupId && it.artifactId == artifactId
} as GPathResult

if (dependencyPath != null && dependencyPath.size() > 0) {
dependency = new PomDependency(dependencyPath, this)
}
return dependency
}

MunitMavenPlugin getMunitPlugin() {
PomPlugin pp = getPlugin(MunitMavenPlugin.GROUP_ID, MunitMavenPlugin.ARTIFACT_ID)
return pp == null ? null : new MunitMavenPlugin(pp.pluginXml, this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,11 @@
package com.avioconsulting.mule.linter.model.pom

import com.avioconsulting.mule.linter.parser.MuleXmlParser
import groovy.xml.slurpersupport.GPathResult

class PomPlugin {

String groupId
String artifactId
PomElement version
Integer lineNo
PomFile pomFile
GPathResult pluginXml
class PomPlugin extends ArtifactDescriptor {

PomPlugin(GPathResult pluginXml, PomFile pomFile) {
this.pluginXml = pluginXml
this.groupId = pluginXml.groupId as String
this.artifactId = pluginXml.artifactId as String
this.lineNo = MuleXmlParser.getNodeLineNumber(pluginXml)
this.pomFile = pomFile
this.version = getAttribute('version')
}

PomElement getAttribute(String attributeName) {
PomElement pElement = null
GPathResult element = pluginXml.depthFirst().find {
it.name() == attributeName
}

if (element != null ) {
if ( isExpression(element.text()) ) {
pElement = pomFile.getPomProperty(variableName(element.text()))
} else {
pElement = new PomElement()
pElement.name = element.name()
pElement.value = element.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(element)
}
}

return pElement
}

PomElement getConfigProperty(String propertyName) {
PomElement pElement = null
pluginXml.configuration.depthFirst().each {
if (it.name() == propertyName) {
pElement = new PomElement()
pElement.name = propertyName
pElement.value = it.text()
pElement.lineNo = MuleXmlParser.getNodeLineNumber(it)
}
}
return pElement
super(pluginXml, pomFile)
}

private Boolean isExpression(String expression) {
expression.startsWith('${')
}

private String variableName(String expression) {
expression.takeAfter('${').takeBefore('}')
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.rule.Rule
import com.avioconsulting.mule.linter.model.rule.RuleViolation

class ComponentCountRule extends Rule {
static final String RULE_ID = 'COMPONENT_COUNT_RULE'
static final String RULE_NAME = 'Component count rule'
static final String RULE_VIOLATION_MESSAGE = ' was used more times than allowed'

String ruleId
String ruleName
String component
String namespace
Integer count

ComponentCountRule(String component, String namespace, Integer count) {
this(RULE_ID, RULE_NAME, component, namespace, count)
}

ComponentCountRule(String ruleId, String ruleName, String component, String namespace, Integer count) {
this.ruleId = ruleId
this.ruleName = ruleName
this.component = component
this.namespace = namespace
this.count = count
}

@Override
List<RuleViolation> execute(Application application) {
List<RuleViolation> violations = []
if (application.findComponents(component, namespace).size() >= count) {
violations.add(new RuleViolation(this,
application.findComponents(component, namespace).last().file.path,
application.findComponents(component, namespace).last().lineNumber,
component + RULE_VIOLATION_MESSAGE))
}
return violations
}
}
Loading