package org.jboss.shamrock.agroal;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.function.Function;

import javax.inject.Inject;

import org.jboss.shamrock.agroal.runtime.DataSourceProducer;
import org.jboss.shamrock.agroal.runtime.DataSourceTemplate;
import org.jboss.shamrock.deployment.ArchiveContext;
import org.jboss.shamrock.deployment.BeanDeployment;
import org.jboss.shamrock.deployment.ProcessorContext;
import org.jboss.shamrock.deployment.ResourceProcessor;
import org.jboss.shamrock.deployment.RuntimePriority;
import org.jboss.shamrock.deployment.buildconfig.BuildConfig;
import org.jboss.shamrock.deployment.codegen.BytecodeRecorder;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import io.agroal.pool.ConnectionHandler;

class AgroalProcessor implements ResourceProcessor {

    @Inject
    private BeanDeployment beanDeployment;

    @Override
    public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
        BuildConfig config = archiveContext.getBuildConfig();
        BuildConfig.ConfigNode ds = config.getApplicationConfig().get("datasource");
        if (ds.isNull()) {
            return;
        }
        String driver = ds.get("driver").asString();
        String url = ds.get("url").asString();
        if (driver == null) {
            throw new RuntimeException("Driver is required");
        }
        if (url == null) {
            throw new RuntimeException("Driver is required");
        }
        String userName = ds.get("username").asString();
        String password = ds.get("password").asString();

        processorContext.addReflectiveClass(false, false, driver);
        beanDeployment.addAdditionalBean(DataSourceProducer.class);
        try (BytecodeRecorder bc = processorContext.addDeploymentTask(RuntimePriority.DATASOURCE_DEPLOYMENT)) {
            DataSourceTemplate template = bc.getRecordingProxy(DataSourceTemplate.class);
            template.addDatasource(null, url, bc.classProxy(driver), userName, password);
        }
    }

    @Override
    public int getPriority() {
        return 1;
    }
}

@@ -33,99 +33,8 @@
 l;%0A%0A
-import java.sql.ResultSet;%0Aimport java.sql.Statement;%0Aimport java.util.function.Function;%0A%0A
 impo
@@ -579,163 +579,8 @@
 der;
-%0Aimport org.objectweb.asm.ClassVisitor;%0Aimport org.objectweb.asm.MethodVisitor;%0Aimport org.objectweb.asm.Opcodes;%0A%0Aimport io.agroal.pool.ConnectionHandler;
 %0A%0Acl
@@ -1194,32 +1194,71 @@
 iver is required
+ (property 'driver' under 'datasource')
 %22);%0A        %7D%0A  
@@ -1314,38 +1314,40 @@
 ntimeException(%22
-Driver
+JDBC URL
  is required%22);%0A
@@ -1342,16 +1342,52 @@
 required
+ (property 'url' under 'datasource')
 %22);%0A    

Improve error messages of AgroalProcessor