2020
2121import org .apache .axiom .om .OMAttribute ;
2222import org .apache .axiom .om .OMElement ;
23+ import org .apache .commons .lang3 .StringUtils ;
2324import org .apache .synapse .Mediator ;
25+ import org .apache .synapse .SynapseException ;
2426import org .apache .synapse .mediators .eip .Target ;
2527import org .apache .synapse .mediators .v2 .ScatterGather ;
2628import org .jaxen .JaxenException ;
3436 * different message contexts and aggregate the responses back.
3537 *
3638 * <pre>
37- * <scatter-gather parallel-execution=(true | false)>
39+ * <scatter-gather parallel-execution=(true | false) result-target=(body | variable) content-type=(JSON | XML) >
3840 * <aggregation value="expression" condition="expression" timeout="long"
3941 * min-messages="expression" max-messages="expression"/>
4042 * <sequence>
@@ -59,6 +61,8 @@ public class ScatterGatherMediatorFactory extends AbstractMediatorFactory {
5961 private static final QName ATT_MAX_MESSAGES = new QName ("max-messages" );
6062 private static final QName SEQUENCE_Q = new QName (XMLConfigConstants .SYNAPSE_NAMESPACE , "sequence" );
6163 private static final QName PARALLEL_EXEC_Q = new QName ("parallel-execution" );
64+ private static final QName RESULT_TARGET_Q = new QName ("result-target" );
65+ private static final QName CONTENT_TYPE_Q = new QName ("content-type" );
6266
6367 private static final SequenceMediatorFactory fac = new SequenceMediatorFactory ();
6468
@@ -73,10 +77,36 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
7377 if (parallelExecAttr != null && parallelExecAttr .getAttributeValue ().equals ("false" )) {
7478 asynchronousExe = false ;
7579 }
76-
7780 mediator .setParallelExecution (asynchronousExe );
7881
82+ OMAttribute contentTypeAttr = elem .getAttribute (CONTENT_TYPE_Q );
83+ if (contentTypeAttr == null || StringUtils .isBlank (contentTypeAttr .getAttributeValue ())) {
84+ String msg = "The 'content-type' attribute is required for the configuration of a Scatter Gather mediator" ;
85+ throw new SynapseException (msg );
86+ } else {
87+ if ("JSON" .equals (contentTypeAttr .getAttributeValue ())) {
88+ mediator .setContentType (ScatterGather .JSON_TYPE );
89+ } else if ("XML" .equals (contentTypeAttr .getAttributeValue ())) {
90+ mediator .setContentType (ScatterGather .XML_TYPE );
91+ } else {
92+ String msg = "The 'content-type' attribute should be either 'JSON' or 'XML'" ;
93+ throw new SynapseException (msg );
94+ }
95+ }
96+
97+ OMAttribute resultTargetAttr = elem .getAttribute (RESULT_TARGET_Q );
98+ if (resultTargetAttr == null || StringUtils .isBlank (resultTargetAttr .getAttributeValue ())) {
99+ String msg = "The 'result-target' attribute is required for the configuration of a Scatter Gather mediator" ;
100+ throw new SynapseException (msg );
101+ } else {
102+ mediator .setResultTarget (resultTargetAttr .getAttributeValue ());
103+ }
104+
79105 Iterator sequenceListElements = elem .getChildrenWithName (SEQUENCE_Q );
106+ if (!sequenceListElements .hasNext ()) {
107+ String msg = "A 'sequence' element is required for the configuration of a Scatter Gather mediator" ;
108+ throw new SynapseException (msg );
109+ }
80110 while (sequenceListElements .hasNext ()) {
81111 OMElement sequence = (OMElement ) sequenceListElements .next ();
82112 if (sequence != null ) {
@@ -97,6 +127,9 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
97127 } catch (JaxenException e ) {
98128 handleException ("Unable to load the aggregating expression" , e );
99129 }
130+ } else {
131+ String msg = "The 'value' attribute is required for the configuration of a Scatter Gather mediator" ;
132+ throw new SynapseException (msg );
100133 }
101134
102135 OMAttribute conditionExpr = aggregateElement .getAttribute (ATT_CONDITION );
@@ -123,6 +156,9 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
123156 if (maxMessages != null ) {
124157 mediator .setMaxMessagesToComplete (new ValueFactory ().createValue ("max-messages" , aggregateElement ));
125158 }
159+ } else {
160+ String msg = "The 'aggregation' element is required for the configuration of a Scatter Gather mediator" ;
161+ throw new SynapseException (msg );
126162 }
127163 addAllCommentChildrenToList (elem , mediator .getCommentsList ());
128164 return mediator ;
0 commit comments