Download [#DOCUMENTATION-4068] Update archived articles recommended

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Price discrimination wikipedia , lookup

Transcript
[DOCUMENTATION-4068] Update archived articles recommended by Writing
a WSO2 ESB mediator documentation Created: 22/Nov/16 Updated: 09/Mar/17
Status:
Project:
Component/s:
Affects
Version/s:
Fix Version/s:
In Progress
WSO2 Documentation
ESB Docs
None
Type:
Reporter:
Resolution:
Labels:
Remaining
Estimate:
Time Spent:
Original
Estimate:
Improvement
Nadheesh Jihan
Unresolved
None
Not Specified
Severity:
Estimated
Complexity:
Affects Docs:
Major
Moderate
None
Priority:
Assignee:
Votes:
Normal
Nisrin Sheriff
0
Not Specified
Not Specified
Yes
Description
The two articles;
1. Writing a Mediator in WSO2 ESB - Part 1
2. Writing a Mediator in WSO2 ESB - Part 2
recommended by the documentation are outdated. Currently these two articles are archived, but
I found that these two articles can be very useful for anyone who wants to create an ESB
mediator. It is better if we can update them according to the latest version of ESB.
The code examples and the sample sources files provided with the first article need to be
updated in order to make them compatible with the latest release of ESB.
SurchargeStockQuoteMediator class (The example codes and the source has to be change)
1. Incompatible setter definition
2.
3. public void setDefaultPercentage(Double defaultPercentage) {
4.
this.defaultPercentage = defaultPercentage;
5. }
6. Methods used for logging are deprecated
7.
8. isTraceOn()
9. isTraceOrDebugOn()
10. traceOrDebug()
Updated SurchargeStockQuoteMediator class (The example codes and the source has to be change)
1. Changed the setter method parameter type java.lang.Double to primitive type double
2. Removed the deprecated logging methods and use SynapseLog instance for logging
Updated SurchargeStockQuoteMediator.java
package org.wso2.esb.tutorial.mediators;
import java.util.HashMap;
import java.util.Map;
import
import
import
import
import
import
import
import
import
org.apache.axiom.om.OMElement;
org.apache.axiom.om.xpath.AXIOMXPath;
org.apache.axiom.soap.SOAPBody;
org.apache.synapse.ManagedLifecycle;
org.apache.synapse.MessageContext;
org.apache.synapse.SynapseLog;
org.apache.synapse.core.SynapseEnvironment;
org.apache.synapse.mediators.AbstractMediator;
org.jaxen.JaxenException;
/**
* custom mediator to read the stock price and add up surcharge
*/
public class SurchargeStockQuoteMediator extends AbstractMediator implements
ManagedLifecycle {
// symbol -> surcharge percentage
private Map<String, Double> surcharges = new HashMap<String, Double>();
// default surcharge percentage to use if symbol not found in surcharges
map
private Double defaultPercentage = 0d;
public boolean mediate(MessageContext synCtx) {
// establish log levels
SynapseLog logger = getLog(synCtx);
boolean traceOn = logger.isTraceEnabled();
boolean traceOrDebugOn = logger.isTraceOrDebugEnabled();
// write log messages
if (traceOrDebugOn) {
logger.traceOrDebug("Start : SurchargeStockQuote mediator");
if (traceOn && trace.isTraceEnabled()) {
trace.trace("Message : " + synCtx.getEnvelope());
}
}
// get symbol, last elements of SOAP envelope
SOAPBody body = synCtx.getEnvelope().getBody();
OMElement firstElement = body.getFirstElement();
OMElement symbolElement = null;
try {
AXIOMXPath xPathSymbol = new AXIOMXPath("//ns:symbol");
xPathSymbol.addNamespace("ns", "http://services.samples/xsd");
symbolElement = (OMElement)
xPathSymbol.selectSingleNode(firstElement);
} catch (JaxenException e) {
handleException("element symbol error", e, synCtx);
}
if (symbolElement == null) {
handleException("element symbol not found", synCtx);
}
OMElement lastElement = null;
try {
AXIOMXPath xPathLast = new AXIOMXPath("//ns:last");
xPathLast.addNamespace("ns", "http://services.samples/xsd");
lastElement = (OMElement)
xPathLast.selectSingleNode(firstElement);
} catch (JaxenException e) {
handleException("element last error", e, synCtx);
}
if (lastElement == null) {
handleException("element last not found", synCtx);
}
// lookup surcharge percentage from surcharges map
// if not found apply default surcharge percentage
Double surchargePercentage = getDefaultPercentage();
String symbol = symbolElement.getText();
Double givenSurchargePercentage = surcharges.get(symbol);
if (givenSurchargePercentage != null) {
surchargePercentage = givenSurchargePercentage;
}
String text = lastElement.getText();
Double price = Double.valueOf(text);
Double newPrice = price.doubleValue()
+ (price.doubleValue() * surchargePercentage.doubleValue() /
100);
// write back new stock price
lastElement.setText(String.valueOf(newPrice));
// print log message
log.info("symbol:" + symbol + " original price:" + price + "
surcharge:"
+ surchargePercentage + "% new price:" + newPrice);
// write log messages
if (traceOrDebugOn) {
logger.traceOrDebug("End : SurchargeStockQuote mediator");
}
// proceed with next mediator
return true;
}
public void init(SynapseEnvironment synapseEnvironment) {
// initializing surcharges map with some symbols
surcharges.put("IBM", 15d);
surcharges.put("MSFT", 20d);
surcharges.put("SUN", 25d);
}
public void destroy() {
// clearing the surcharges contents
surcharges.clear();
}
public double getDefaultPercentage() {
return defaultPercentage;
}
public void setDefaultPercentage(double defaultPercentage) {
this.defaultPercentage = defaultPercentage;
}
}
SurchargeStockQuoteMediatorFactory.java file from the sample source
This class override a method called createMediator from AbstractMediatorFactory. But this
method is no longer defined in AbstractMediatorFactory since it has been replaced by a new
abstract method called createSpecificMediator. Therefore the
SurchargeStockQuoteMediatorFactory class should implement the method
createSpecificMediator.
SurchargeStockQuoteMediatorSerializer.java file from the sample source
This class also overrides a method called serializeMediator from the
AbstractMediatorSerializer which is no longer available in AbstractMediatorSerializer. It has
been replaced by the abstract method called serializeSpecificMediator which is required to be
implemented in SurchargeStockQuoteMediatorSerializer class.
Overall Changes
These changes seems to be common for both articles and for the sample source of the both
articles. Even in the second article we have to change the parameter type of all the setter
methods that is used to define options, from its class type to primitive type(as shown in the
updated SurchargeStockQuoteMediator class setter method), since the Synapse version used in
the latest ESB can not recognize the setter methods with the class type parameters instead of
primitive type parameters.
Moreover, the second article may has further changes, therefore it may need to be reviewed and
update accordingly.
Comments
Comment by Gillian Dass [ 09/Mar/17 ]
Marketing has updated the articles. Awaiting content confirmation from product team.
Generated at Sat May 13 18:52:12 IST 2017 using JIRA 7.2.2#72004sha1:9d5132893cc8c728a3601a9034a1f8547ef5c7be.