package org.torquebox.auth;

import java.util.Collection;

import org.jboss.as.server.deployment.AttachmentList;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.logging.Logger;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.ServiceController.Mode;
import org.torquebox.auth.AuthMetaData.Config;
import org.torquebox.auth.as.AuthServices;
import org.torquebox.core.app.RubyApplicationMetaData;

public class AuthDeployer implements DeploymentUnitProcessor {

	@Override
	public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
		DeploymentUnit unit = phaseContext.getDeploymentUnit();
		
		// We need the application name to name our bean with
        RubyApplicationMetaData appMetaData = unit.getAttachment(RubyApplicationMetaData.ATTACHMENT_KEY);
        String applicationName = appMetaData.getApplicationName();
        this.setApplicationName(applicationName);

        // Install authenticators for every domain
        AttachmentList<AuthMetaData> allMetaData = unit.getAttachment(AuthMetaData.ATTACHMENT_KEY);
        if (allMetaData != null) {
            for( AuthMetaData authMetaData: allMetaData ) {
                if ( authMetaData != null ) {
                    Collection<Config> authConfigs = authMetaData.getConfigurations();
                    for ( Config config : authConfigs ) {
                        installAuthenticator(unit, phaseContext, config);
                    }
                }
            }
        }
        else {
        	log.info("No authentication configured. Skipping deployment.");
        }
	}

	@Override
	public void undeploy(DeploymentUnit unit) {
		// TODO Clean up?
	}
	
    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }

    public String getApplicationName() {
        return this.applicationName;
    }

    private void installAuthenticator(DeploymentUnit unit, DeploymentPhaseContext phaseContext, Config config) {
        String name     = config.getName();
        String domain   = config.getDomain();
        if (name != null && domain != null) {
            ServiceName serviceName = AuthServices.authenticationService( unit, name );
            log.info( "Deploying Authenticator: " + serviceName );
            Authenticator authenticator = new Authenticator();
            authenticator.setAuthDomain(domain);
            ServiceBuilder<Authenticator> builder = phaseContext.getServiceTarget().addService( serviceName, authenticator );
            builder.setInitialMode( Mode.PASSIVE );
            builder.install();
        }
    }
    
    private String applicationName;
    private static final Logger log = Logger.getLogger( "org.torquebox.auth" );
}

@@ -55,63 +55,32 @@
 on;%0A
-%0A
 import 
-org.jboss.as.server.deployment.Attachment
+java.util.
 List;%0A
+%0A
 impo
@@ -1036,32 +1036,71 @@
 TTACHMENT_KEY);%0A
+        if (appMetaData != null) %7B%0A    
         String a
@@ -1154,24 +1154,28 @@
 ();%0A        
+    
 this.setAppl
@@ -1213,16 +1213,20 @@
         
+ 
+   
 // Insta
@@ -1268,26 +1268,20 @@
         
-Attachment
+    
 List%3CAut
@@ -1323,16 +1323,20 @@
 tachment
+List
 (AuthMet
@@ -1362,43 +1362,8 @@
 Y);%0A
-        if (allMetaData != null) %7B%0A
     
@@ -1751,106 +1751,8 @@
   %7D%0A
-        else %7B%0A        %09log.info(%22No authentication configured. Skipping deployment.%22);%0A        %7D%0A
 %09%7D%0A%0A

Check for null. Again.