/*
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 quarks.runtime.appservice;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonObject;

import quarks.execution.DirectSubmitter;
import quarks.execution.Job;
import quarks.execution.services.ControlService;
import quarks.function.BiConsumer;
import quarks.topology.Topology;
import quarks.topology.TopologyProvider;
import quarks.topology.mbeans.ApplicationServiceMXBean;
import quarks.topology.services.ApplicationService;

public class AppService implements ApplicationService {
    
    public static ApplicationService createAndRegister(TopologyProvider provider, DirectSubmitter submitter) {
        
        AppService service = new AppService(provider, submitter, ALIAS);
        
        submitter.getServices().addService(ApplicationService.class, service);
        
        return service;
        
    }
    
    private final Map<String,BiConsumer<Topology, JsonObject>> applications =
            Collections.synchronizedMap(new HashMap<>());
    
    private final TopologyProvider provider;
    private final DirectSubmitter<Topology, Job> submitter;
    
    public AppService(TopologyProvider provider,
            DirectSubmitter<Topology, Job> submitter, String alias) {
        this.provider = provider;
        this.submitter = submitter;
        
        ControlService cs = submitter.getServices().getService(ControlService.class);
        if (cs != null)
            cs.registerControl(ApplicationServiceMXBean.TYPE,
                    ALIAS+System.currentTimeMillis(), alias,
                    ApplicationServiceMXBean.class,
                    new AppServiceControl(this));
    }

    @Override
    public void registerTopology(String applicationName, BiConsumer<Topology, JsonObject> builder) {
        applications.put(applicationName, builder);
    }
    
    BiConsumer<Topology, JsonObject> getBuilder(String applicationName) {
        return applications.get(applicationName);
    }
    
    TopologyProvider getProvider() {
        return provider;
    }
    
    
    DirectSubmitter<Topology, Job> getSubmitter() {
        return submitter;
    }   
}

@@ -1246,68 +1246,805 @@
 e;%0A%0A
-public class AppService implements ApplicationService %7B%0A    
+/**%0A * Application service for a %7B@code TopologyProvider%7D.%0A * %3CBR%3E%0A * Applications %7B@link #registerTopology(String, BiConsumer) registered%7D%0A * can be submitted through the control %7B@link ApplicationServiceMXBean%7D%0A * registered with the %7B@link ControlService%7D for the topology provider.%0A * %3CBR%3E%0A * If a control service is not available then no control MBean is registered%0A * and the application service is effectively inactive.%0A *%0A */%0Apublic class AppService implements ApplicationService %7B%0A    %0A%09/**%0A%09 * Create an register an application service using the default alias %7B@link ApplicationService#ALIAS%7D.%0A%09 * @param provider Provider to create topology instances for registered applications.%0A%09 * @param submitter Submitter for registered applications.%0A%09 * @return Application service instance.%0A%09 */
 %0A   
@@ -2137,16 +2137,31 @@
 ubmitter
+%3CTopology, Job%3E
  submitt
@@ -2636,24 +2636,309 @@
 itter;%0A    %0A
+    /**%0A     * Create an %7B@code ApplicationService%7D instance.%0A     * @param provider Provider to create topology instances for registered applications.%0A     * @param submitter Submitter for registered applications.%0A     * @param alias Alias used to register the control MBean.%0A     */%0A
     public A

Fix warnings in AppService.java and add javadoc