package org.wildfly.swarm.ejb.runtime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ValueExpression;
import org.wildfly.apigen.invocation.Marshaller;
import org.wildfly.swarm.config.ejb3.subsystem.cache.Cache;
import org.wildfly.swarm.config.ejb3.subsystem.service.TimerService;
import org.wildfly.swarm.config.ejb3.subsystem.service.fileDataStore.FileDataStore;
import org.wildfly.swarm.config.ejb3.subsystem.strictMaxBeanInstancePool.StrictMaxBeanInstancePool;
import org.wildfly.swarm.config.ejb3.subsystem.threadPool.ThreadPool;
import org.wildfly.swarm.container.runtime.AbstractServerConfiguration;
import org.wildfly.swarm.ejb.EJBFraction;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.EXTENSION;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;

/**
 * @author Ken Finnigan
 */
public class EJBConfiguration extends AbstractServerConfiguration<EJBFraction> {

    private PathAddress address = PathAddress.pathAddress(PathElement.pathElement(SUBSYSTEM, "ejb3"));

    public EJBConfiguration() {
        super(EJBFraction.class);
    }

    @Override
    public EJBFraction defaultFraction() {

        Map threadPoolSettings = new HashMap<>();
        threadPoolSettings.put("time", "100");
        threadPoolSettings.put("unit", "MILLISECONDS");

        EJBFraction fraction = new EJBFraction();
        fraction.defaultStatefulBeanAccessTimeout(5000L)
                .defaultSingletonBeanAccessTimeout(5000L)
                .defaultSfsbCache("simple")
                .defaultSecurityDomain("other")
                .defaultMissingMethodPermissionsDenyAccess(true)
                .logSystemExceptions(true)
                .defaultResourceAdapterName(
                        new ValueExpression("${ejb.resource-adapter-name:activemq-ra.rar}").resolveString())
                .strictMaxBeanInstancePool(new StrictMaxBeanInstancePool("slsb-strict-max-pool")
                        .maxPoolSize(20)
                        .timeout(5L)
                        .timeoutUnit("MINUTES"))
                .strictMaxBeanInstancePool(new StrictMaxBeanInstancePool("mdb-strict-max-pool")
                        .maxPoolSize(20)
                        .timeout(5L)
                        .timeoutUnit("MINUTES"))
                .cache(new Cache("simple"))
                .timerService(new TimerService()
                        .threadPoolName("default")
                        .defaultDataStore("default-file-store")
                        .fileDataStore(new FileDataStore("default-file-store")
                            .path("timer-service-data")
                            .relativeTo("jboss.server.data.dir")))
                .threadPool(new ThreadPool("default")
                        .maxThreads(10)
                        .keepaliveTime(threadPoolSettings));

        return fraction;
    }

    @Override
    public List<ModelNode> getList(EJBFraction fraction) {
        List<ModelNode> list = new ArrayList<>();

        ModelNode node = new ModelNode();
        node.get(OP_ADDR).set(EXTENSION, "org.jboss.as.ejb3");
        node.get(OP).set(ADD);
        list.add(node);

        try {
            list.addAll(Marshaller.marshal(fraction));
        } catch (Exception e) {
            System.err.println("Cannot configure EJB subsystem. " + e);
        }

        return list;
    }
}

@@ -400,16 +400,78 @@
 .Cache;%0A
+import org.wildfly.swarm.config.ejb3.subsystem.service.Async;%0A
 import o
@@ -2835,24 +2835,94 @@
 (%22simple%22))%0A
+                .async(new Async().threadPoolName(%22default%22))%0A        
             
@@ -2958,16 +2958,16 @@
 rvice()%0A
-
         
@@ -3180,16 +3180,20 @@
         
+    
 .path(%22t
@@ -3212,16 +3212,20 @@
 -data%22)%0A
+    
         
@@ -3283,16 +3283,24 @@
 dir%22)))%0A
+        
         
@@ -3365,24 +3365,32 @@
             
+        
 .maxThreads(
@@ -3393,16 +3393,24 @@
 ads(10)%0A
+        
         
@@ -3959,25 +3959,24 @@
 ;%0A        %7D%0A
-%0A
         retu

Add EJB Async service property from config-lib