After the user has configured Huong Dan Java Logger, processing to retrieve the final value of Correlation Id and Category is necessary. Because in the Global Configuration, the value of these two cells is mandatory, so these will be the default values for these two.
Because the Global Configuration Override section in the main configuration can override the value of Correlation Id and Category, we need to check this section first, if they are not configured we will return the default value.
Detail code for obtaining Correlation Id and Category from configuration is as follows:
1 2 3 4 5 6 7 |
private String getCorrelationId(ConnectorConfig cc, String globalCorrelationIdOverride) { if (!StringUtils.isEmpty(globalCorrelationIdOverride)) { return globalCorrelationIdOverride; } return cc.getCorrelationId(); } |
1 2 3 4 5 6 7 |
private String getCategory(ConnectorConfig cc, String globalCategoryOverride) { if (!StringUtils.isEmpty(globalCategoryOverride)) { return globalCategoryOverride; } return cc.getCategory(); } |
In the Category, its value is fixed so we do not need to support the Mule Expression Language (MEL) for it, but the Correlation Id is mandatory because the value of Correlation Id can be obtained from Mule Flow or Mule Message as the default value in the Global Configuration of this information.
I will add the following method to help us get the final value after processing the MEL:
1 2 3 4 5 |
private String evaluateMEL(MuleEvent event, String text) { ExpressionManager em = event.getMuleContext().getExpressionManager(); return em.parse(text, event); } |
In Mule ESB, the ExpressionManager object is responsible for parsing the MEL to get the final value that we need.
Here I will modify the getCorrelationId() method to support MEL and by the way, rename this method into getEvaluatedCorrelationId:
1 2 3 4 5 6 7 |
private String getEvaluatedCorrelationId(MuleEvent event, ConnectorConfig cc, String globalCorrelationIdOverride) { if (!StringUtils.isEmpty(globalCorrelationIdOverride)) { return evaluateMEL(event, globalCorrelationIdOverride); } return evaluateMEL(event, cc.getCorrelationId()); } |
Here we can get the Correlation Id and Category in the main part of the Processor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.huongdanjava.hdjlogger; import org.apache.commons.lang3.StringUtils; import org.mule.api.MuleEvent; import org.mule.api.annotations.Category; import org.mule.api.annotations.Config; import org.mule.api.annotations.Connector; import org.mule.api.annotations.Processor; import org.mule.api.annotations.display.FriendlyName; import org.mule.api.annotations.display.Placement; import org.mule.api.annotations.param.Default; import org.mule.api.annotations.param.Optional; import org.mule.api.expression.ExpressionManager; import com.huongdanjava.hdjlogger.config.ConnectorConfig; import com.huongdanjava.hdjlogger.constant.LoggingLevel; import com.huongdanjava.hdjlogger.constant.Text; @Connector(name="huong-dan-java-logger-component", friendlyName="Huong Dan Java Logger") @Category(name = "org.mule.tooling.category.core", description = "Components") public class HuongDanJavaLoggerComponentConnector { @Config ConnectorConfig config; @Processor public String log( @Placement(group = Text.GENERIC, order = 1) @Optional String message, @Placement(group = Text.GENERIC, order = 2) @Default("INFO") @FriendlyName(Text.LEVEL) LoggingLevel loggingLevel, @Placement(group = Text.OVERRIDE, order = 1) @Optional @FriendlyName(Text.OVVERIDE_CORRELATION_ID) String globalCorrelationIdOverride, @Placement(group = Text.OVERRIDE, order = 2) @Optional @FriendlyName(Text.OVVERIDE_CATEGORY) String globalCategoryOverride, MuleEvent event) { String category = getCategory(config, globalCategoryOverride); String correlationId = getEvaluatedCorrelationId(event, config, globalCorrelationIdOverride); return config.getCorrelationId(); } private String getEvaluatedCorrelationId(MuleEvent event, ConnectorConfig cc, String globalCorrelationIdOverride) { if (!StringUtils.isEmpty(globalCorrelationIdOverride)) { return evaluateMEL(event, globalCorrelationIdOverride); } return evaluateMEL(event, cc.getCorrelationId()); } private String getCategory(ConnectorConfig cc, String globalCategoryOverride) { if (!StringUtils.isEmpty(globalCategoryOverride)) { return globalCategoryOverride; } return cc.getCategory(); } private String evaluateMEL(MuleEvent event, String text) { ExpressionManager em = event.getMuleContext().getExpressionManager(); return em.parse(text, event); } public ConnectorConfig getConfig() { return config; } public void setConfig(ConnectorConfig config) { this.config = config; } } |