Repository: stratos
Updated Branches:
  refs/heads/master 1fcaecd49 -> 170911073


Introducing a distributed lock for synchronizing algorithm context map and 
moving service references to ServiceReferenceHolder


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/17091107
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/17091107
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/17091107

Branch: refs/heads/master
Commit: 1709110738ed717051097e0004becc0d407be042
Parents: 1fcaecd
Author: Imesh Gunaratne <[email protected]>
Authored: Sat Nov 29 21:44:01 2014 +0530
Committer: Imesh Gunaratne <[email protected]>
Committed: Sat Nov 29 21:44:49 2014 +0530

----------------------------------------------------------------------
 .../balancer/context/LoadBalancerContext.java   |  94 +---------
 .../context/map/AlgorithmContextMap.java        |  77 ++++++--
 .../internal/LoadBalancerServiceComponent.java  | 181 +++++++++++--------
 .../internal/ServiceReferenceHolder.java        | 148 +++++++++++++++
 4 files changed, 324 insertions(+), 176 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/17091107/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/LoadBalancerContext.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/LoadBalancerContext.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/LoadBalancerContext.java
index 6ebae04..a3a694c 100644
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/LoadBalancerContext.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/LoadBalancerContext.java
@@ -19,17 +19,9 @@
 
 package org.apache.stratos.load.balancer.context;
 
-import org.apache.axis2.context.ConfigurationContext;
-import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import 
org.apache.stratos.load.balancer.exception.TenantAwareLoadBalanceEndpointException;
 import org.apache.stratos.load.balancer.context.map.*;
-import org.apache.synapse.config.SynapseConfiguration;
-import org.wso2.carbon.caching.impl.DistributedMapProvider;
-import 
org.wso2.carbon.mediation.dependency.mgt.services.DependencyManagementService;
-import org.wso2.carbon.registry.core.session.UserRegistry;
-import org.wso2.carbon.user.core.service.RealmService;
 
 /**
  * Defines load balancer context information.
@@ -39,13 +31,6 @@ public class LoadBalancerContext {
     private static final Log log = 
LogFactory.getLog(LoadBalancerContext.class);
     private static volatile LoadBalancerContext instance;
 
-    private SynapseConfiguration synapseConfiguration;
-    private ConfigurationContext configCtxt;
-    private AxisConfiguration axisConfiguration;
-    private UserRegistry configRegistry;
-    private UserRegistry governanceRegistry;
-    private DependencyManagementService dependencyManager;
-
     // Following map is updated by the service component.
     // Map<TenantId, SynapseEnvironmentService>
     private TenantIdSynapseEnvironmentServiceMap 
tenantIdSynapseEnvironmentServiceMap;
@@ -71,7 +56,7 @@ public class LoadBalancerContext {
     // Map<MemberIp, Hostname>
     // Keep track of cluster hostnames of of all members  against their ip 
addresses
     private MemberIpHostnameMap memberIpHostnameMap;
-    private DistributedMapProvider distributedMapProvider;
+    private boolean clustered;
 
     private LoadBalancerContext() {
         tenantIdSynapseEnvironmentServiceMap = new 
TenantIdSynapseEnvironmentServiceMap();
@@ -102,79 +87,10 @@ public class LoadBalancerContext {
         multiTenantClusterMap.clear();
     }
 
-    public RealmService getRealmService() {
-        return realmService;
-    }
-
-    public void setRealmService(RealmService realmService) {
-        this.realmService = realmService;
-    }
-
-    private RealmService realmService;
-
-    public SynapseConfiguration getSynapseConfiguration() throws 
TenantAwareLoadBalanceEndpointException {
-        assertNull("SynapseConfiguration", synapseConfiguration);
-        return synapseConfiguration;
-    }
-
-    public void setSynapseConfiguration(SynapseConfiguration 
synapseConfiguration) {
-        this.synapseConfiguration = synapseConfiguration;
-    }
-
-    public AxisConfiguration getAxisConfiguration() throws 
TenantAwareLoadBalanceEndpointException {
-        assertNull("AxisConfiguration", axisConfiguration);
-        return axisConfiguration;
-    }
-
-    public void setAxisConfiguration(AxisConfiguration axisConfiguration) {
-        this.axisConfiguration = axisConfiguration;
-    }
-
-    public UserRegistry getConfigRegistry() throws 
TenantAwareLoadBalanceEndpointException {
-        assertNull("Registry", configRegistry);
-        return configRegistry;
-    }
-
-    public void setConfigRegistry(UserRegistry configRegistry) {
-        this.configRegistry = configRegistry;
-    }
-
-    public DependencyManagementService getDependencyManager() {
-        return dependencyManager;
-    }
-
-    public void setDependencyManager(DependencyManagementService 
dependencyManager) {
-        this.dependencyManager = dependencyManager;
-    }
-
-    private void assertNull(String name, Object object) throws 
TenantAwareLoadBalanceEndpointException {
-        if (object == null) {
-            String message = name + " reference in the proxy admin config 
holder is null";
-            log.error(message);
-            throw new TenantAwareLoadBalanceEndpointException(message);
-        }
-    }
-
-    public UserRegistry getGovernanceRegistry() {
-        return governanceRegistry;
-    }
-
-    public void setGovernanceRegistry(UserRegistry governanceRegistry) {
-        this.governanceRegistry = governanceRegistry;
-    }
-
     public TenantIdSynapseEnvironmentServiceMap 
getTenantIdSynapseEnvironmentServiceMap() {
         return tenantIdSynapseEnvironmentServiceMap;
     }
 
-    public ConfigurationContext getConfigCtxt() {
-        return configCtxt;
-    }
-
-    public void setConfigCtxt(ConfigurationContext configCtxt) {
-        this.configCtxt = configCtxt;
-    }
-
     public ServiceNameServiceContextMap getServiceNameServiceContextMap() {
         return serviceNameServiceContextMap;
     }
@@ -203,11 +119,11 @@ public class LoadBalancerContext {
         return memberIpHostnameMap;
     }
 
-    public void setDistributedMapProvider(DistributedMapProvider 
distributedMapProvider) {
-        this.distributedMapProvider = distributedMapProvider;
+    public boolean isClustered() {
+        return clustered;
     }
 
-    public DistributedMapProvider getDistributedMapProvider() {
-        return distributedMapProvider;
+    public void setClustered(boolean clustered) {
+        this.clustered = clustered;
     }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/17091107/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/map/AlgorithmContextMap.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/map/AlgorithmContextMap.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/map/AlgorithmContextMap.java
index 6d290f4..49ecf2f 100644
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/map/AlgorithmContextMap.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/context/map/AlgorithmContextMap.java
@@ -19,14 +19,18 @@
 
 package org.apache.stratos.load.balancer.context.map;
 
+import com.hazelcast.core.HazelcastInstance;
+import com.hazelcast.core.ILock;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.stratos.load.balancer.context.LoadBalancerContext;
+import org.apache.stratos.load.balancer.internal.ServiceReferenceHolder;
 import org.wso2.carbon.caching.impl.DistributedMapProvider;
 import org.wso2.carbon.caching.impl.MapEntryListener;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Algorithm context map is a singleton class for managing load balancing 
algorithm context
@@ -34,40 +38,49 @@ import java.util.Map;
  */
 public class AlgorithmContextMap {
     @SuppressWarnings("unused")
-       private static final Log log = 
LogFactory.getLog(AlgorithmContextMap.class);
+    private static final Log log = 
LogFactory.getLog(AlgorithmContextMap.class);
     private static final String LOAD_BALANCER_ALGORITHM_CONTEXT_MAP = 
"LOAD_BALANCER_ALGORITHM_CONTEXT_MAP";
     private static AlgorithmContextMap instance;
 
     private final Map<String, Integer> clusterMemberIndexMap;
 
     private AlgorithmContextMap() {
-        DistributedMapProvider distributedMapProvider = 
LoadBalancerContext.getInstance().getDistributedMapProvider();
-        if(distributedMapProvider != null) {
+        if (LoadBalancerContext.getInstance().isClustered()) {
+            DistributedMapProvider distributedMapProvider = 
ServiceReferenceHolder.getInstance().getDistributedMapProvider();
             clusterMemberIndexMap = 
distributedMapProvider.getMap(LOAD_BALANCER_ALGORITHM_CONTEXT_MAP,
                     new MapEntryListener() {
                         @Override
                         public <X> void entryAdded(X x) {
+                            if(log.isDebugEnabled()) {
+                                log.debug("Entry added to distributed 
algorithm context map: " + x);
+                            }
                         }
 
                         @Override
                         public <X> void entryRemoved(X x) {
+                            if(log.isDebugEnabled()) {
+                                log.debug("Entry removed from distributed 
algorithm context map: " + x);
+                            }
                         }
 
                         @Override
                         public <X> void entryUpdated(X x) {
+                            if(log.isDebugEnabled()) {
+                                log.debug("Entry updated in distributed 
algorithm context map: " + x);
+                            }
                         }
                     });
-            if(clusterMemberIndexMap != null) {
-                if(log.isInfoEnabled()) {
-                    log.info("Load balancer context map initiated in 
distributed mode");
+            if (clusterMemberIndexMap != null) {
+                if (log.isInfoEnabled()) {
+                    log.info("Load balancer context map initialized in 
distributed mode");
                 }
             } else {
                 log.error("Could not initialize algorithm context map from 
distributed map provider");
             }
         } else {
-            clusterMemberIndexMap = new HashMap<String, Integer>();
-            if(log.isInfoEnabled()) {
-                log.info("Load balancer context map initiated locally");
+            clusterMemberIndexMap = new ConcurrentHashMap<String, Integer>();
+            if (log.isInfoEnabled()) {
+                log.info("Load balancer context map initialized locally");
             }
         }
     }
@@ -87,14 +100,56 @@ public class AlgorithmContextMap {
         return String.format("%s-%s", serviceName, clusterId);
     }
 
+    private com.hazelcast.core.ILock acquireDistributedLock(Object object) {
+        if (log.isDebugEnabled()) {
+            log.debug("Acquiring distributed lock for algorithm context 
map...");
+        }
+        HazelcastInstance hazelcastInstance = 
ServiceReferenceHolder.getInstance().getHazelcastInstance();
+        ILock lock = hazelcastInstance.getLock(object);
+        if (log.isDebugEnabled()) {
+            log.debug("Distributed lock acquired for algorithm context map");
+        }
+        return lock;
+    }
+
+    private void releaseDistributedLock(ILock lock) {
+        if (log.isDebugEnabled()) {
+            log.debug("Releasing distributed lock for algorithm context 
map...");
+        }
+        lock.forceUnlock();
+        if (log.isDebugEnabled()) {
+            log.debug("Distributed lock released for algorithm context map");
+        }
+    }
+
     public void putCurrentMemberIndex(String serviceName, String clusterId, 
int currentMemberIndex) {
         String key = constructKey(serviceName, clusterId);
-        clusterMemberIndexMap.put(key, currentMemberIndex);
+        if (LoadBalancerContext.getInstance().isClustered()) {
+            ILock lock = null;
+            try {
+                lock = acquireDistributedLock(clusterMemberIndexMap);
+                clusterMemberIndexMap.put(key, currentMemberIndex);
+            } finally {
+                releaseDistributedLock(lock);
+            }
+        } else {
+            clusterMemberIndexMap.put(key, currentMemberIndex);
+        }
     }
 
     public void removeCluster(String serviceName, String clusterId) {
         String key = constructKey(serviceName, clusterId);
-        clusterMemberIndexMap.remove(key);
+        if (LoadBalancerContext.getInstance().isClustered()) {
+            ILock lock = null;
+            try {
+                lock = acquireDistributedLock(clusterMemberIndexMap);
+                clusterMemberIndexMap.remove(key);
+            } finally {
+                releaseDistributedLock(lock);
+            }
+        } else {
+            clusterMemberIndexMap.remove(key);
+        }
     }
 
     public int getCurrentMemberIndex(String serviceName, String clusterId) {

http://git-wip-us.apache.org/repos/asf/stratos/blob/17091107/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/LoadBalancerServiceComponent.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/LoadBalancerServiceComponent.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/LoadBalancerServiceComponent.java
index e94336a..bc78af4 100644
--- 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/LoadBalancerServiceComponent.java
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/LoadBalancerServiceComponent.java
@@ -19,6 +19,8 @@
 
 package org.apache.stratos.load.balancer.internal;
 
+import com.hazelcast.core.HazelcastInstance;
+import org.apache.axis2.clustering.ClusteringAgent;
 import org.apache.axis2.deployment.DeploymentEngine;
 import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.commons.logging.Log;
@@ -27,7 +29,6 @@ import 
org.apache.stratos.load.balancer.endpoint.EndpointDeployer;
 import 
org.apache.stratos.load.balancer.messaging.LoadBalancerTenantEventReceiver;
 import 
org.apache.stratos.load.balancer.messaging.LoadBalancerTopologyEventReceiver;
 import 
org.apache.stratos.load.balancer.exception.TenantAwareLoadBalanceEndpointException;
-import 
org.apache.stratos.load.balancer.common.statistics.LoadBalancerStatisticsReader;
 import 
org.apache.stratos.load.balancer.common.statistics.notifier.LoadBalancerStatisticsNotifier;
 import org.apache.stratos.load.balancer.conf.LoadBalancerConfiguration;
 import org.apache.stratos.load.balancer.conf.configurator.CEPConfigurator;
@@ -63,8 +64,10 @@ import java.util.Set;
 
 /**
  * @scr.component 
name="org.apache.stratos.load.balancer.internal.LoadBalancerServiceComponent" 
immediate="true"
+ * @scr.reference name="hazelcast.instance.service" 
interface="com.hazelcast.core.HazelcastInstance"
+ *                cardinality="0..1"policy="dynamic" 
bind="setHazelcastInstance" unbind="unsetHazelcastInstance"
  * @scr.reference name="distributedMapProvider" 
interface="org.wso2.carbon.caching.impl.DistributedMapProvider"
- *                cardinality="1..1" policy="dynamic" 
bind="setDistributedMapProvider" unbind="unsetDistributedMapProvider"
+ *                cardinality="0..1" policy="dynamic" 
bind="setDistributedMapProvider" unbind="unsetDistributedMapProvider"
  * @scr.reference name="configuration.context.service" 
interface="org.wso2.carbon.utils.ConfigurationContextService"
  *                cardinality="1..1" policy="dynamic" 
bind="setConfigurationContextService" unbind="unsetConfigurationContextService"
  * @scr.reference name="synapse.config.service" 
interface="org.wso2.carbon.mediation.initializer.services.SynapseConfigurationService"
@@ -91,10 +94,20 @@ public class LoadBalancerServiceComponent {
 
     protected void activate(ComponentContext ctxt) {
         try {
+            HazelcastInstance hazelcastInstance = 
ServiceReferenceHolder.getInstance().getHazelcastInstance();
+
+            ClusteringAgent clusteringAgent = 
ServiceReferenceHolder.getInstance().getAxisConfiguration().getClusteringAgent();
+            boolean clusteringEnabled = (clusteringAgent != null);
+            LoadBalancerContext.getInstance().setClustered(clusteringEnabled);
+
+            if(log.isInfoEnabled()) {
+                log.info(String.format("Load balancer clustering is %s", 
(clusteringEnabled ? "enabled" : "disabled")));
+            }
+
             // Register endpoint deployer
             SynapseEnvironmentService synEnvService = 
LoadBalancerContext.getInstance().getTenantIdSynapseEnvironmentServiceMap()
                     
.getSynapseEnvironmentService(MultitenantConstants.SUPER_TENANT_ID);
-            
registerDeployer(LoadBalancerContext.getInstance().getAxisConfiguration(),
+            
registerDeployer(ServiceReferenceHolder.getInstance().getAxisConfiguration(),
                     synEnvService.getSynapseEnvironment());
 
             // Configure synapse settings
@@ -108,70 +121,18 @@ public class LoadBalancerServiceComponent {
             TopologyFilterConfigurator.configure(configuration);
 
             if (configuration.isMultiTenancyEnabled()) {
-
-                tenantReceiver = new LoadBalancerTenantEventReceiver();
-                Thread tenantReceiverThread = new Thread(tenantReceiver);
-                tenantReceiverThread.start();
-                if (log.isInfoEnabled()) {
-                    log.info("Tenant receiver thread started");
-                }
+                // Start tenant event receiver
+                startTenantEventReceiver();
             }
 
             if (configuration.isTopologyEventListenerEnabled()) {
-
                 // Start topology receiver
-                topologyReceiver = new LoadBalancerTopologyEventReceiver();
-                Thread topologyReceiverThread = new Thread(topologyReceiver);
-                topologyReceiverThread.start();
-                if (log.isInfoEnabled()) {
-                    log.info("Topology receiver thread started");
-                }
-
-                if (log.isInfoEnabled()) {
-                    if (TopologyServiceFilter.getInstance().isActive()) {
-                        StringBuilder sb = new StringBuilder();
-                        for (String serviceName : 
TopologyServiceFilter.getInstance().getIncludedServiceNames()) {
-                            if (sb.length() > 0) {
-                                sb.append(", ");
-                            }
-                            sb.append(serviceName);
-                        }
-                        log.info(String.format("Service filter activated: 
[services] %s", sb.toString()));
-                    }
-                    if (TopologyClusterFilter.getInstance().isActive()) {
-                        StringBuilder sb = new StringBuilder();
-                        for (String clusterId : 
TopologyClusterFilter.getInstance().getIncludedClusterIds()) {
-                            if (sb.length() > 0) {
-                                sb.append(", ");
-                            }
-                            sb.append(clusterId);
-                        }
-                        log.info(String.format("Cluster filter activated: 
[clusters] %s", sb.toString()));
-                    }
-                    if (TopologyMemberFilter.getInstance().isActive()) {
-                        StringBuilder sb = new StringBuilder();
-                        for (String clusterId : 
TopologyMemberFilter.getInstance().getIncludedLbClusterIds()) {
-                            if (sb.length() > 0) {
-                                sb.append(", ");
-                            }
-                            sb.append(clusterId);
-                        }
-                        log.info(String.format("Member filter activated: 
[lb-cluster-ids] %s", sb.toString()));
-                    }
-                }
+                startTopologyEventReceiver();
             }
 
             if(configuration.isCepStatsPublisherEnabled()) {
-                // Get stats reader
-                LoadBalancerStatisticsReader statsReader = 
LoadBalancerStatisticsCollector.getInstance();
-
-                // Start stats notifier thread
-                statisticsNotifier = new 
LoadBalancerStatisticsNotifier(statsReader);
-                Thread statsNotifierThread = new Thread(statisticsNotifier);
-                statsNotifierThread.start();
-                if (log.isInfoEnabled()) {
-                    log.info("Load balancer statistics notifier thread 
started");
-                }
+                // Start statistics notifier
+                startStatisticsNotifier();
             }
 
             activated = true;
@@ -185,6 +146,67 @@ public class LoadBalancerServiceComponent {
         }
     }
 
+    private void startTenantEventReceiver() {
+        tenantReceiver = new LoadBalancerTenantEventReceiver();
+        Thread tenantReceiverThread = new Thread(tenantReceiver);
+        tenantReceiverThread.start();
+        if (log.isInfoEnabled()) {
+            log.info("Tenant receiver thread started");
+        }
+    }
+
+    private void startTopologyEventReceiver() {
+        topologyReceiver = new LoadBalancerTopologyEventReceiver();
+        Thread topologyReceiverThread = new Thread(topologyReceiver);
+        topologyReceiverThread.start();
+        if (log.isInfoEnabled()) {
+            log.info("Topology receiver thread started");
+        }
+
+        if (log.isInfoEnabled()) {
+            if (TopologyServiceFilter.getInstance().isActive()) {
+                StringBuilder sb = new StringBuilder();
+                for (String serviceName : 
TopologyServiceFilter.getInstance().getIncludedServiceNames()) {
+                    if (sb.length() > 0) {
+                        sb.append(", ");
+                    }
+                    sb.append(serviceName);
+                }
+                log.info(String.format("Service filter activated: [services] 
%s", sb.toString()));
+            }
+            if (TopologyClusterFilter.getInstance().isActive()) {
+                StringBuilder sb = new StringBuilder();
+                for (String clusterId : 
TopologyClusterFilter.getInstance().getIncludedClusterIds()) {
+                    if (sb.length() > 0) {
+                        sb.append(", ");
+                    }
+                    sb.append(clusterId);
+                }
+                log.info(String.format("Cluster filter activated: [clusters] 
%s", sb.toString()));
+            }
+            if (TopologyMemberFilter.getInstance().isActive()) {
+                StringBuilder sb = new StringBuilder();
+                for (String clusterId : 
TopologyMemberFilter.getInstance().getIncludedLbClusterIds()) {
+                    if (sb.length() > 0) {
+                        sb.append(", ");
+                    }
+                    sb.append(clusterId);
+                }
+                log.info(String.format("Member filter activated: 
[lb-cluster-ids] %s", sb.toString()));
+            }
+        }
+    }
+
+    private void startStatisticsNotifier() {
+        // Start stats notifier thread
+        statisticsNotifier = new 
LoadBalancerStatisticsNotifier(LoadBalancerStatisticsCollector.getInstance());
+        Thread statsNotifierThread = new Thread(statisticsNotifier);
+        statsNotifierThread.start();
+        if (log.isInfoEnabled()) {
+            log.info("Load balancer statistics notifier thread started");
+        }
+    }
+
     protected void deactivate(ComponentContext context) {
         try {
             Set<Map.Entry<Integer, SynapseEnvironmentService>> entrySet = 
LoadBalancerContext
@@ -260,21 +282,21 @@ public class LoadBalancerServiceComponent {
     }
 
     protected void setConfigurationContextService(ConfigurationContextService 
cfgCtxService) {
-        
LoadBalancerContext.getInstance().setAxisConfiguration(cfgCtxService.getServerConfigContext().getAxisConfiguration());
-        
LoadBalancerContext.getInstance().setConfigCtxt(cfgCtxService.getServerConfigContext());
+        
ServiceReferenceHolder.getInstance().setAxisConfiguration(cfgCtxService.getServerConfigContext().getAxisConfiguration());
+        
ServiceReferenceHolder.getInstance().setConfigCtxt(cfgCtxService.getServerConfigContext());
     }
 
     protected void 
unsetConfigurationContextService(ConfigurationContextService cfgCtxService) {
-        LoadBalancerContext.getInstance().setAxisConfiguration(null);
-        LoadBalancerContext.getInstance().setConfigCtxt(null);
+        ServiceReferenceHolder.getInstance().setAxisConfiguration(null);
+        ServiceReferenceHolder.getInstance().setConfigCtxt(null);
     }
 
     protected void setSynapseConfigurationService(SynapseConfigurationService 
synapseConfigurationService) {
-        
LoadBalancerContext.getInstance().setSynapseConfiguration(synapseConfigurationService.getSynapseConfiguration());
+        
ServiceReferenceHolder.getInstance().setSynapseConfiguration(synapseConfigurationService.getSynapseConfiguration());
     }
 
     protected void 
unsetSynapseConfigurationService(SynapseConfigurationService 
synapseConfigurationService) {
-        LoadBalancerContext.getInstance().setSynapseConfiguration(null);
+        ServiceReferenceHolder.getInstance().setSynapseConfiguration(null);
     }
 
     /**
@@ -327,9 +349,9 @@ public class LoadBalancerServiceComponent {
             log.debug("RegistryService bound to the endpoint component");
         }
         try {
-            LoadBalancerContext.getInstance().setConfigRegistry(
+            ServiceReferenceHolder.getInstance().setConfigRegistry(
                     regService.getConfigSystemRegistry());
-            LoadBalancerContext.getInstance().setGovernanceRegistry(
+            ServiceReferenceHolder.getInstance().setGovernanceRegistry(
                     regService.getGovernanceSystemRegistry());
         } catch (RegistryException e) {
             log.error("Couldn't retrieve the registry from the registry 
service");
@@ -340,7 +362,7 @@ public class LoadBalancerServiceComponent {
         if (log.isDebugEnabled()) {
             log.debug("RegistryService unbound from the endpoint component");
         }
-        LoadBalancerContext.getInstance().setConfigRegistry(null);
+        ServiceReferenceHolder.getInstance().setConfigRegistry(null);
     }
 
     protected void setDependencyManager(
@@ -348,7 +370,7 @@ public class LoadBalancerServiceComponent {
         if (log.isDebugEnabled()) {
             log.debug("Dependency management service bound to the endpoint 
component");
         }
-        LoadBalancerContext.getInstance().setDependencyManager(dependencyMgr);
+        
ServiceReferenceHolder.getInstance().setDependencyManager(dependencyMgr);
     }
 
     protected void unsetDependencyManager(
@@ -356,12 +378,11 @@ public class LoadBalancerServiceComponent {
         if (log.isDebugEnabled()) {
             log.debug("Dependency management service unbound from the endpoint 
component");
         }
-        LoadBalancerContext.getInstance().setDependencyManager(null);
+        ServiceReferenceHolder.getInstance().setDependencyManager(null);
     }
 
     protected void setSynapseRegistrationsService(
             SynapseRegistrationsService synapseRegistrationsService) {
-
     }
 
     protected void unsetSynapseRegistrationsService(
@@ -391,18 +412,26 @@ public class LoadBalancerServiceComponent {
     }
 
     protected void setRealmService(RealmService realmService) {
-        LoadBalancerContext.getInstance().setRealmService(realmService);
+        ServiceReferenceHolder.getInstance().setRealmService(realmService);
     }
 
     protected void unsetRealmService(RealmService realmService) {
-        LoadBalancerContext.getInstance().setRealmService(null);
+        ServiceReferenceHolder.getInstance().setRealmService(null);
+    }
+
+    public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
+        
ServiceReferenceHolder.getInstance().setHazelcastInstance(hazelcastInstance);
+    }
+
+    public void unsetHazelcastInstance(HazelcastInstance hazelcastInstance) {
+        ServiceReferenceHolder.getInstance().setHazelcastInstance(null);
     }
 
     protected void setDistributedMapProvider(DistributedMapProvider 
mapProvider) {
-        
LoadBalancerContext.getInstance().setDistributedMapProvider(mapProvider);
+        
ServiceReferenceHolder.getInstance().setDistributedMapProvider(mapProvider);
     }
 
     protected void unsetDistributedMapProvider(DistributedMapProvider 
mapProvider) {
-        LoadBalancerContext.getInstance().setDistributedMapProvider(null);
+        ServiceReferenceHolder.getInstance().setDistributedMapProvider(null);
     }
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/17091107/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/ServiceReferenceHolder.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/ServiceReferenceHolder.java
 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/ServiceReferenceHolder.java
new file mode 100644
index 0000000..719eb10
--- /dev/null
+++ 
b/components/org.apache.stratos.load.balancer/src/main/java/org/apache/stratos/load/balancer/internal/ServiceReferenceHolder.java
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.load.balancer.internal;
+
+import com.hazelcast.core.HazelcastInstance;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import 
org.apache.stratos.load.balancer.exception.TenantAwareLoadBalanceEndpointException;
+import org.apache.synapse.config.SynapseConfiguration;
+import org.wso2.carbon.caching.impl.DistributedMapProvider;
+import 
org.wso2.carbon.mediation.dependency.mgt.services.DependencyManagementService;
+import org.wso2.carbon.registry.core.session.UserRegistry;
+import org.wso2.carbon.user.core.service.RealmService;
+
+/**
+ * Service reference holder keeps references of bind OSGi services.
+ */
+public class ServiceReferenceHolder {
+
+    private static final Log log = 
LogFactory.getLog(ServiceReferenceHolder.class);
+    private static volatile ServiceReferenceHolder instance;
+
+    private SynapseConfiguration synapseConfiguration;
+    private ConfigurationContext configCtxt;
+    private AxisConfiguration axisConfiguration;
+    private UserRegistry configRegistry;
+    private UserRegistry governanceRegistry;
+    private DependencyManagementService dependencyManager;
+    private DistributedMapProvider distributedMapProvider;
+    private HazelcastInstance hazelcastInstance;
+    private RealmService realmService;
+
+    private ServiceReferenceHolder() {
+    }
+
+    public static ServiceReferenceHolder getInstance() {
+        if (instance == null) {
+            synchronized (ServiceReferenceHolder.class) {
+                if (instance == null) {
+                    instance = new ServiceReferenceHolder();
+                }
+            }
+        }
+        return instance;
+    }
+
+    public RealmService getRealmService() {
+        return realmService;
+    }
+
+    public void setRealmService(RealmService realmService) {
+        this.realmService = realmService;
+    }
+
+    public SynapseConfiguration getSynapseConfiguration() throws 
TenantAwareLoadBalanceEndpointException {
+        assertNull("SynapseConfiguration", synapseConfiguration);
+        return synapseConfiguration;
+    }
+
+    public void setSynapseConfiguration(SynapseConfiguration 
synapseConfiguration) {
+        this.synapseConfiguration = synapseConfiguration;
+    }
+
+    public AxisConfiguration getAxisConfiguration() throws 
TenantAwareLoadBalanceEndpointException {
+        assertNull("AxisConfiguration", axisConfiguration);
+        return axisConfiguration;
+    }
+
+    public void setAxisConfiguration(AxisConfiguration axisConfiguration) {
+        this.axisConfiguration = axisConfiguration;
+    }
+
+    public UserRegistry getConfigRegistry() throws 
TenantAwareLoadBalanceEndpointException {
+        assertNull("Registry", configRegistry);
+        return configRegistry;
+    }
+
+    public void setConfigRegistry(UserRegistry configRegistry) {
+        this.configRegistry = configRegistry;
+    }
+
+    public DependencyManagementService getDependencyManager() {
+        return dependencyManager;
+    }
+
+    public void setDependencyManager(DependencyManagementService 
dependencyManager) {
+        this.dependencyManager = dependencyManager;
+    }
+
+    private void assertNull(String name, Object object) throws 
TenantAwareLoadBalanceEndpointException {
+        if (object == null) {
+            String message = name + " reference in service reference holder is 
null";
+            log.error(message);
+            throw new TenantAwareLoadBalanceEndpointException(message);
+        }
+    }
+
+    public UserRegistry getGovernanceRegistry() {
+        return governanceRegistry;
+    }
+
+    public void setGovernanceRegistry(UserRegistry governanceRegistry) {
+        this.governanceRegistry = governanceRegistry;
+    }
+
+    public ConfigurationContext getConfigCtxt() {
+        return configCtxt;
+    }
+
+    public void setConfigCtxt(ConfigurationContext configCtxt) {
+        this.configCtxt = configCtxt;
+    }
+
+    public void setDistributedMapProvider(DistributedMapProvider 
distributedMapProvider) {
+        this.distributedMapProvider = distributedMapProvider;
+    }
+
+    public DistributedMapProvider getDistributedMapProvider() {
+        return distributedMapProvider;
+    }
+
+    public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
+        this.hazelcastInstance = hazelcastInstance;
+    }
+
+    public HazelcastInstance getHazelcastInstance() {
+        return hazelcastInstance;
+    }
+}

Reply via email to