/*
 * Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) 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.carbon.caching.invalidator.amqp;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.caching.invalidator.internal.CacheInvalidationDataHolder;
import org.wso2.carbon.context.PrivilegedCarbonContext;

import javax.cache.CacheManager;
import javax.cache.Caching;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.Timer;
import java.util.TimerTask;

public class CacheInvalidationSubscriber {
    private static final Log log = LogFactory.getLog(CacheInvalidationSubscriber.class);
    private QueueingConsumer consumer = null;

    public void init(){
        if(ConfigurationManager.init()) {
            Timer timer = new Timer(true);
            timer.scheduleAtFixedRate(timerTask, 0, ConfigurationManager.getCoordinatorCheckInterval()*1000);
        }
    }

    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            boolean isCoordinator = CacheInvalidationDataHolder.getConfigContext().getAxisConfiguration().getClusteringAgent().isCoordinator();
            if(isCoordinator){
                subscribe();
            }
        }
    };

    private void subscribe() {
        log.debug("Global cache invalidation: initializing the subscription");
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(ConfigurationManager.getProviderUrl());
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(ConfigurationManager.getTopicName(), "topic");
            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, ConfigurationManager.getTopicName(), "#");
            consumer = new QueueingConsumer(channel);
            channel.basicConsume(queueName, true, consumer);
            Thread reciever = new Thread(messageReciever);
            reciever.start();
            log.info("Global cache invalidation is online");
        } catch (Exception e) {
            log.error("Global cache invalidation: Error message broker initialization", e);
        }
    }

    public void onMessage(byte[] data) {
        log.debug("Cache invalidation message received: " + new String(data));
        boolean isCoordinator = CacheInvalidationDataHolder.getConfigContext().getAxisConfiguration().getClusteringAgent().isCoordinator();
        if(isCoordinator) {
            PrivilegedCarbonContext.startTenantFlow();
            try {
                log.debug("Global cache invalidation: deserializing data to object");
                GlobalCacheInvalidationEvent event = (GlobalCacheInvalidationEvent) deserialize(data);
                log.debug("Global cache invalidation: deserializing complete");
                if (ConfigurationManager.getSentMsgBuffer().contains(event.getUuid().trim())) { // Ignore own messages
                    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(event.getTenantId(), true);
                    CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(event.getCacheManagerName());
                    if (cacheManager != null) {
                        if (cacheManager.getCache(event.getCacheName()) != null) {
                            cacheManager.getCache(event.getCacheName()).remove(event.getCacheKey());
                            log.debug("Global cache invalidated: " + event.getCacheKey());
                        } else {
                            log.error("Global cache invalidation: error cache is null");
                        }
                    } else {
                        log.error("Global cache invalidation: error cache manager is null");
                    }
                } else {
                    ConfigurationManager.getSentMsgBuffer().remove(event.getUuid().trim()); // To resolve future performance issues
                    log.debug("Global cache invalidation: own message ignored");
                }
            } catch (Exception e) {
                log.error("Global cache invalidation: error local cache update", e);
            } finally {
                PrivilegedCarbonContext.endTenantFlow();
            }
        }
    }

    Runnable messageReciever = new Runnable() {
        @Override
        public void run() {
            while(true) {
                try {
                    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                    onMessage(delivery.getBody());
                } catch (Exception e) {
                    log.error("Global cache invalidation: error message recieve.", e);
                }
            }
        }
    };

    private Object deserialize(byte[] bytes) throws Exception {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        return objectInputStream.readObject();
    }
}

@@ -5122,16 +5122,24 @@
  %7D%0A%0A    
+private 
 Runnable
@@ -5238,20 +5238,32 @@
   while(
-true
+consumer != null
 ) %7B%0A    

modify variable from public to private