Author: hiranya
Date: Mon Dec 22 21:26:41 2008
New Revision: 728873

URL: http://svn.apache.org/viewvc?rev=728873&view=rev
Log:
Fixing the issues described in SYNAPSE-433.

1. algorithm attribute is now read properly
2. policy attribute is now read properly
3. round robin algorithm is no longer hard coded into the LB algorithm factory

(Most of the code for this commit was from a patch submitted by Azeez to 
SYNAPSE-433 on 17/08/2008. So kudos to Azeez :-))


Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/LoadbalanceAlgorithmFactory.java

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/LoadbalanceAlgorithmFactory.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/LoadbalanceAlgorithmFactory.java?rev=728873&r1=728872&r2=728873&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/LoadbalanceAlgorithmFactory.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/LoadbalanceAlgorithmFactory.java
 Mon Dec 22 21:26:41 2008
@@ -19,14 +19,16 @@
 
 package org.apache.synapse.config.xml.endpoints.utils;
 
-import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.XMLConfigConstants;
 import org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm;
 import org.apache.synapse.endpoints.algorithms.RoundRobin;
-import org.apache.synapse.config.xml.XMLConfigConstants;
 
 import javax.xml.namespace.QName;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -35,20 +37,48 @@
  */
 public class LoadbalanceAlgorithmFactory {
 
+    private static Log log = 
LogFactory.getLog(LoadbalanceAlgorithmFactory.class);
+
     public static LoadbalanceAlgorithm createLoadbalanceAlgorithm(OMElement 
loadbalanceElement, List endpoints) {
 
-        LoadbalanceAlgorithm algorithm = null;
+        //default algorithm is round robin
+        LoadbalanceAlgorithm algorithm = new RoundRobin(endpoints);
 
-        String algorithmName = "roundRobin";
-        OMAttribute algoAttribute = loadbalanceElement.getAttribute(new 
QName(null, XMLConfigConstants.ALGORITHM_NAME));
-        if(algoAttribute != null) {
-            algorithmName = algoAttribute.getAttributeValue();
+        OMAttribute policyAttribute = loadbalanceElement.getAttribute(new 
QName(null,
+                XMLConfigConstants.LOADBALANCE_POLICY));
+        OMAttribute algoAttribute = loadbalanceElement.getAttribute(new 
QName(null,
+                XMLConfigConstants.LOADBALANCE_ALGORITHM));
+
+        if (policyAttribute != null && algoAttribute != null) {
+            String msg = "You cannot specify both the 'policy' & 'algorithm' 
in the configuration. " +
+                         "It is sufficient to provide only the 'algorithm'.";
+            log.fatal(msg); // We cannot continue execution. Hence it is 
logged at fatal level
+            throw new SynapseException(msg);
         }
 
-        if(algorithmName.equalsIgnoreCase("roundRobin")) {
-                algorithm = new RoundRobin(endpoints);
+        if (algoAttribute != null) {
+            String algorithmStr = algoAttribute.getAttributeValue().trim();
+            try {
+                algorithm = (LoadbalanceAlgorithm) 
Class.forName(algorithmStr).newInstance();
+                algorithm.setEndpoints(endpoints);
+            } catch (Exception e) {
+                String msg = "Cannot instantiate LoadbalanceAlgorithm 
implementation class " +
+                             algorithmStr;
+                log.fatal(msg, e); // We cannot continue execution. Hence it 
is logged at fatal level
+                throw new SynapseException(msg, e);
+            }
+
+        } else if (policyAttribute != null) {
+            //currently only the roundRobin policy is supported
+            if 
(!policyAttribute.getAttributeValue().trim().equals("roundRobin")) {
+                String msg = "Unsupported algorithm " + 
policyAttribute.getAttributeValue().trim() +
+                             " specified. Please use the 'algorithm' attribute 
to specify the " +
+                             "correct loadbalance algorithm implementation.";
+                log.fatal(msg); // We cannot continue execution. Hence it is 
logged at fatal level
+                throw new SynapseException(msg);
+            }
         }
-
+        
         return algorithm;
     }
 }


Reply via email to