/*
 * Copyright (c) 2016, WSO2 Inc. (http://wso2.com) All Rights Reserved.
 * Licensed 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.wso2.andes.server.cluster.coordination;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.andes.configuration.AndesConfigurationManager;
import org.wso2.andes.configuration.enums.AndesConfiguration;
import org.wso2.andes.kernel.AndesContext;
import org.wso2.andes.kernel.AndesContextStore;
import org.wso2.andes.kernel.AndesException;
import org.wso2.andes.server.ClusterResourceHolder;
import org.wso2.andes.server.cluster.coordination.rdbms.RDBMSBasedNotificationAgentImpl;
import org.wso2.andes.server.cluster.coordination.rdbms.RDBMSClusterNotificationListenerImpl;

/**
 * This i the factory class for creating event publishers and event listeners
 * for cluster communication across nodes
 */
public class CoordinationComponentFactory {

    private Log log = LogFactory.getLog(CoordinationComponentFactory.class);

    private boolean isRDBMSBasedCoordinationEnabled;

    private boolean isClusteringEnabled;

    /**
     * Create a {@link ClusterNotificationListenerManager} implementation based on the
     * configurations
     *
     * @return ClusterNotificationListenerManager instance
     */
    public ClusterNotificationListenerManager createClusterNotificationListener() throws AndesException {
        initConfiguration();
        ClusterNotificationListenerManager clusterNotificationListenerManager = null;
        if (ClusterResourceHolder.getInstance().getClusterManager().isClusteringEnabled()) {
            if (isRDBMSBasedCoordinationEnabled) {
                log.info("Broker is initialized with RDBMS based cluster event synchronization.");
                AndesContextStore contextStore = AndesContext.getInstance().getAndesContextStore();
                clusterNotificationListenerManager = new RDBMSClusterNotificationListenerImpl(contextStore);
            }
        }
        return clusterNotificationListenerManager;

    }

    /**
     * Create a {@link org.wso2.andes.server.cluster.coordination.ClusterNotificationAgent}
     * implementation based on the configurations.
     *
     * @return ClusterNotificationAgent instance
     * @throws AndesException in case of Hazelcast based ClusterNotificationAgent you need to call
     *                        {@link CoordinationComponentFactory#createClusterNotificationListener()} first.
     *                        Otherwise exception is thrown
     */
    public ClusterNotificationAgent createClusterNotificationAgent() throws AndesException {
        initConfiguration();
        AndesContextStore contextStore = AndesContext.getInstance().getAndesContextStore();
        ClusterNotificationAgent clusterNotificationAgent = new StandaloneMockNotificationAgent();
        if (isClusteringEnabled) {
            if (isRDBMSBasedCoordinationEnabled) {
                clusterNotificationAgent = new RDBMSBasedNotificationAgentImpl(contextStore);
            }
        }
        return clusterNotificationAgent;
    }

    /**
     * Init the configs needed
     */
    private void initConfiguration() {
        isRDBMSBasedCoordinationEnabled =
                AndesConfigurationManager.readValue(AndesConfiguration.CLUSTER_EVENT_SYNC_MODE_RDBMS_ENABLED);
        isClusteringEnabled = ClusterResourceHolder.getInstance().getClusterManager().isClusteringEnabled();
    }

}


@@ -2107,19 +2107,16 @@
 nabled()
-) %7B
 %0A       
@@ -2112,36 +2112,39 @@
 d()%0A            
-if (
+    && 
 isRDBMSBasedCoor
@@ -2154,36 +2154,32 @@
 ationEnabled) %7B%0A
-    
             log.
@@ -2261,36 +2261,32 @@
 %22);%0A            
-   
- 
 AndesContextStor
@@ -2349,28 +2349,24 @@
 extStore();%0A
-    
             
@@ -2454,38 +2454,24 @@
 textStore);%0A
-            %7D%0A
         %7D%0A  
@@ -3359,28 +3359,12 @@
 bled
-) %7B%0A            if (
+ && 
 isRD
@@ -3386,36 +3386,32 @@
 ationEnabled) %7B%0A
-    
             clus
@@ -3488,22 +3488,8 @@
 e);%0A
-            %7D%0A
     

Address review comments