/*
 * 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.cloud.controller.application.status.receiver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.cloud.controller.topology.TopologyBuilder;
import org.apache.stratos.messaging.event.Event;
import org.apache.stratos.messaging.event.application.status.ClusterActivatedEvent;
import org.apache.stratos.messaging.event.application.status.GroupActivatedEvent;
import org.apache.stratos.messaging.listener.application.status.ApplicationActivatedEventListener;
import org.apache.stratos.messaging.listener.topology.ClusterActivatedEventListener;
import org.apache.stratos.messaging.listener.topology.GroupActivatedEventListener;
import org.apache.stratos.messaging.message.receiver.application.status.ApplicationStatusEventReceiver;

public class ApplicationStatusTopicReceiver implements Runnable {
    private static final Log log = LogFactory.getLog(ApplicationStatusTopicReceiver.class);

    private ApplicationStatusEventReceiver statusEventReceiver;
    private boolean terminated;

    public ApplicationStatusTopicReceiver() {
        this.statusEventReceiver = new ApplicationStatusEventReceiver();
        addEventListeners();
    }

    @Override
    public void run() {
        //FIXME this activated before autoscaler deployer activated.
        try {
            Thread.sleep(15000);
        } catch (InterruptedException ignore) {
        }
        Thread thread = new Thread(statusEventReceiver);
        thread.start();
        if (log.isInfoEnabled()) {
            log.info("Cloud controller application status thread started");
        }

        // Keep the thread live until terminated
        while (!terminated) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {
            }
        }
        if (log.isInfoEnabled()) {
            log.info("Cloud controller application status thread terminated");
        }

    }

    private void addEventListeners() {
        // Listen to topology events that affect clusters
        statusEventReceiver.addEventListener(new ClusterActivatedEventListener() {
            @Override
            protected void onEvent(Event event) {
                TopologyBuilder.handleClusterActivatedEvent((ClusterActivatedEvent) event);
            }
        });

        statusEventReceiver.addEventListener(new GroupActivatedEventListener() {
            @Override
            protected void onEvent(Event event) {
                TopologyBuilder.handleGroupActivatedEvent((GroupActivatedEvent) event);

            }
        });

        statusEventReceiver.addEventListener(new ApplicationActivatedEventListener() {

            @Override
            protected void onEvent(Event event) {

            }
        });


    }

}

@@ -1073,24 +1073,112 @@
 vent.Event;%0A
+import org.apache.stratos.messaging.event.application.status.ApplicationActivatedEvent;%0A
 import org.a
@@ -3674,42 +3674,238 @@
 ) %7B%0A
-%0A            %7D%0A        %7D);%0A%0A%0A    %7D%0A
+                TopologyBuilder.handleApplicationActivatedEvent((ApplicationActivatedEvent) event);%0A%0A            %7D%0A        %7D);%0A%0A%0A    %7D%0A%0A    public void setTerminated(boolean terminated) %7B%0A        this.terminated = terminated;%0A    %7D
 %0A%7D%0A

update cc to publish app activation to Topology