/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.wso2.carbon.uis.internal;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.uis.api.App;
import org.wso2.carbon.uis.api.http.HttpConnector;
import org.wso2.carbon.uis.internal.deployment.AppDeploymentEventListener;
import org.wso2.carbon.uis.internal.http.RequestDispatcher;
import org.wso2.carbon.uis.spi.Server;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Carbon UI server service component.
 *
 * @since 0.8.0
 */
@Component(service = {Server.class, AppDeploymentEventListener.class},
           immediate = true)
public class CarbonUiServer implements Server, AppDeploymentEventListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarbonUiServer.class);

    private HttpConnector httpConnector;
    /**
     * Contains deployed apps. Here key is the app name and value is the deployed app.
     */
    private final ConcurrentMap<String, App> deployedApps = new ConcurrentHashMap<>();

    @Reference(service = HttpConnector.class,
               cardinality = ReferenceCardinality.MANDATORY,
               policy = ReferencePolicy.DYNAMIC,
               unbind = "unsetHttpConnector")
    protected void setHttpConnector(HttpConnector httpConnector) {
        this.httpConnector = httpConnector;
        LOGGER.debug("An instance of class '{}' registered as a HTTP connector to Carbon UI server.",
                     httpConnector.getClass().getName());
    }

    protected void unsetHttpConnector(HttpConnector httpConnector) {
        this.httpConnector = null;
        LOGGER.debug("An instance of class '{}' unregistered as a HTTP connector from Carbon UI server.",
                     httpConnector.getClass().getName());
    }

    @Activate
    protected void activate(BundleContext bundleContext) {
        LOGGER.debug("Carbon UI Server activated.");
    }

    @Deactivate
    protected void deactivate(BundleContext bundleContext) {
        deployedApps.clear();
        httpConnector.unregisterAllApps();
        LOGGER.debug("Carbon UI Server deactivated.");
    }

    @Override
    public Optional<App> getApp(String appName) {
        return Optional.ofNullable(deployedApps.get(appName));
    }

    @Override
    public void appDeploymentEvent(App app) {
        deployedApps.put(app.getName(), app);
        RequestDispatcher requestDispatcher = new RequestDispatcher(app);
        httpConnector.registerApp(app, requestDispatcher::serve);
    }

    @Override
    public void appUndeploymentEvent(String appName) {
        deployedApps.remove(appName);
        httpConnector.unregisterApp(appName);
    }
}

@@ -721,539 +721,31 @@
 org.
-osgi.framework.BundleContext;%0Aimport org.osgi.service.component.annotations.Activate;%0Aimport org.osgi.service.component.annotations.Component;%0Aimport org.osgi.service.component.annotations.Deactivate;%0Aimport org.osgi.service.component.annotations.Reference;%0Aimport org.osgi.service.component.annotations.ReferenceCardinality;%0Aimport org.osgi.service.component.annotations.ReferencePolicy;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.wso2.carbon.uis.api.App;%0Aimport org.wso2.carbon.uis.api.http.HttpConnector
+wso2.carbon.uis.api.App
 ;%0Aim
@@ -821,68 +821,8 @@
 er;%0A
-import org.wso2.carbon.uis.internal.http.RequestDispatcher;%0A
 impo
@@ -1045,1604 +1045,274 @@
  */%0A
-@Component(service = %7BServer.class, AppDeploymentEventListener.class%7D,%0A           immediate = true)%0Apublic class CarbonUiServer implements Server, AppDeploymentEventListener %7B%0A%0A    private static final Logger LOGGER = LoggerFactory.getLogger(CarbonUiServer.class);%0A%0A    private HttpConnector httpConnector;%0A    /**%0A     * Contains deployed apps. Here key is the app name and value is the deployed app.%0A     */%0A    private final ConcurrentMap%3CString, App%3E deployedApps = new ConcurrentHashMap%3C%3E();%0A%0A    @Reference(service = HttpConnector.class,%0A               cardinality = ReferenceCardinality.MANDATORY,%0A               policy = ReferencePolicy.DYNAMIC,%0A               unbind = %22unsetHttpConnector%22)%0A    protected void setHttpConnector(HttpConnector httpConnector) %7B%0A        this.httpConnector = httpConnector;%0A        LOGGER.debug(%22An instance of class '%7B%7D' registered as a HTTP connector to Carbon UI server.%22,%0A                     httpConnector.getClass().getName());%0A    %7D%0A%0A    protected void unsetHttpConnector(HttpConnector httpConnector) %7B%0A        this.httpConnector = null;%0A        LOGGER.debug(%22An instance of class '%7B%7D' unregistered as a HTTP connector from Carbon UI server.%22,%0A                     httpConnector.getClass().getName());%0A    %7D%0A%0A    @Activate%0A    protected void activate(BundleContext bundleContext) %7B%0A        LOGGER.debug(%22Carbon UI Server activated.%22);%0A    %7D%0A%0A    @Deactivate%0A    protected void deactivate(BundleContext bundleContext) %7B%0A        deployedApps.clear();%0A        httpConnector.unregisterAllApps();%0A        LOGGER.debug(%22Carbon UI Server deactivated.%22);%0A    %7D
+public class CarbonUiServer implements Server, AppDeploymentEventListener %7B%0A%0A    /**%0A     * Contains deployed apps. Here key is the app name and value is the deployed app.%0A     */%0A    private final ConcurrentMap%3CString, App%3E deployedApps = new ConcurrentHashMap%3C%3E();
 %0A%0A  
@@ -1553,148 +1553,8 @@
 p);%0A
-        RequestDispatcher requestDispatcher = new RequestDispatcher(app);%0A        httpConnector.registerApp(app, requestDispatcher::serve);%0A
     
@@ -1671,47 +1671,64 @@
     
+%7D%0A%0A
     
-httpConnector.unregisterApp(appName
+public void close() %7B%0A        deployedApps.clear(
 );%0A 

remove OSGi annotations from CarbonUiServer class