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);
        for( AuthMetaData authMetaData: allMetaData ) {
            if ( authMetaData != null ) {
                Collection<Config> authConfigs = authMetaData.getConfigurations();
                for ( Config config : authConfigs ) {
                    installAuthenticator(unit, phaseContext, config);
                }
            }
        }
	}

	@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" );
}

@@ -1336,32 +1336,71 @@
 TTACHMENT_KEY);%0A
+        if (allMetaData != null) %7B%0A    
         for( Aut
@@ -1447,24 +1447,28 @@
             
+    
 if ( authMet
@@ -1501,16 +1501,20 @@
         
+ 
+   
 Collecti
@@ -1588,16 +1588,20 @@
         
+    
 for ( Co
@@ -1626,24 +1626,28 @@
 Configs ) %7B%0A
+    
             
@@ -1712,32 +1712,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -1730,33 +1730,149 @@
   %7D%0A            
-%7D
+    %7D%0A            %7D%0A        %7D%0A        else %7B%0A        %09log.info(%22No authentication configured. Skipping deployment.%22);
 %0A        %7D%0A%09%7D%0A%0A%09

Fix NPE