package se.su.it.cognos.cognosshibauth.visa;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.cognos.CAM_AAA.authentication.IAccount;
import com.cognos.CAM_AAA.authentication.IBiBusHeader;
import com.cognos.CAM_AAA.authentication.ICredential;
import com.cognos.CAM_AAA.authentication.IGroup;
import com.cognos.CAM_AAA.authentication.IRole;
import com.cognos.CAM_AAA.authentication.ITrustedCredential;
import com.cognos.CAM_AAA.authentication.IVisa;
import com.cognos.CAM_AAA.authentication.SystemRecoverableException;
import com.cognos.CAM_AAA.authentication.UnrecoverableException;
import com.cognos.CAM_AAA.authentication.UserRecoverableException;
import se.su.it.cognos.cognosshibauth.adapters.Credential;
import se.su.it.cognos.cognosshibauth.adapters.TrustedCredential;
import se.su.it.cognos.cognosshibauth.config.ConfigHandler;


public class Visa implements IVisa {

  private final Logger LOG = Logger.getLogger(Visa.class.getName());

  private List<IRole> roles;
  private List<IGroup> groups;
  private IAccount account;

  private VisaValidator visaValidator = null;

  private ConfigHandler configHandler = null;

  public Visa(ConfigHandler configHandler) {
    LOG.log(Level.FINEST, "Creating a Visa.");
    roles = null;
    groups = null;
    this.configHandler = configHandler;
  }

  public void init(IAccount theAccount) throws UnrecoverableException {
    account = theAccount;
    
    LOG.log(Level.FINEST, "Initing new account for '" + account.getUserName() + "'");

    String visaValidatorClassName = configHandler.getVisaValidatorClass();
    ClassLoader classLoader = Visa.class.getClassLoader();

    try {
      visaValidator = (VisaValidator) classLoader.loadClass(visaValidatorClassName).newInstance();
    } catch (Exception e) {
      LOG.log(Level.SEVERE, "Failed to load visa validator: " + e.getMessage() + " " + e.toString());
      for(StackTraceElement ste : e.getStackTrace())
        LOG.log(Level.SEVERE, "Failed to load visa validator: " + ste.getClassName() + " " + ste.getFileName() + " " + ste.getMethodName());
      e.printStackTrace();
      throw new UnrecoverableException("Failed to load visa validator.", e.getMessage());
    }

    visaValidator.init(account);
  }

  public void destroy() {
    LOG.log(Level.FINEST, "Destroying Visa for '" + account.getUserName() + "'.");
    roles = null;
    groups = null;
    account = null;
    visaValidator.destroy();
  }

  public ITrustedCredential generateTrustedCredential(IBiBusHeader theAuthRequest)
          throws UnrecoverableException {
    //TODO: Implement something smart
    LOG.log(Level.FINEST, "Generating trusted credentials.");
    return null;
  }

  public ICredential generateCredential(IBiBusHeader theAuthRequest)
          throws UnrecoverableException {
    //TODO: Implement something smart
    LOG.log(Level.FINEST, "Generating credentials.");
    return null;
  }

  public boolean isValid() {
    LOG.log(Level.FINEST, "Checking isValid.");
    return visaValidator.isValid();
  }

  public IAccount getAccount() {
    LOG.log(Level.FINEST, "Getting account for '" + account.getUserName() + "'.");
    return account;
  }


  public void addGroup(IGroup theGroup) {
    LOG.log(Level.FINEST, "Adding group to Visa for '" + account.getUserName() + "'.");
    if (groups == null)
      groups = new ArrayList<IGroup>();
    groups.add(theGroup);
  }


  public IGroup[] getGroups() {
    LOG.log(Level.FINEST, "Getting groups from Visa for '" + account.getUserName() + "'.");
    if (groups != null)
      return groups.toArray(new IGroup[groups.size()]);
    return null;
  }


  public void addRole(IRole theRole) {
    LOG.log(Level.FINEST, "Adding role to Visa for '" + account.getUserName() + "'.");
    if (roles == null)
      roles = new ArrayList<IRole>();
    roles.add(theRole);
  }



  public IRole[] getRoles() {
    LOG.log(Level.FINEST, "Getting roles from Visa for '" + account.getUserName() + "'.");
    if (roles != null)
      return roles.toArray(new IRole[roles.size()]);
    return null;
  }

}

@@ -37,24 +37,132 @@
 th.visa;%0D%0A%0D%0A
+import com.cognos.CAM_AAA.authentication.*;%0D%0Aimport se.su.it.cognos.cognosshibauth.config.ConfigHandler;%0D%0A%0D%0A
 import java.
@@ -275,777 +275,8 @@
 %0D%0A%0D%0A
-import com.cognos.CAM_AAA.authentication.IAccount;%0D%0Aimport com.cognos.CAM_AAA.authentication.IBiBusHeader;%0D%0Aimport com.cognos.CAM_AAA.authentication.ICredential;%0D%0Aimport com.cognos.CAM_AAA.authentication.IGroup;%0D%0Aimport com.cognos.CAM_AAA.authentication.IRole;%0D%0Aimport com.cognos.CAM_AAA.authentication.ITrustedCredential;%0D%0Aimport com.cognos.CAM_AAA.authentication.IVisa;%0D%0Aimport com.cognos.CAM_AAA.authentication.SystemRecoverableException;%0D%0Aimport com.cognos.CAM_AAA.authentication.UnrecoverableException;%0D%0Aimport com.cognos.CAM_AAA.authentication.UserRecoverableException;%0D%0Aimport se.su.it.cognos.cognosshibauth.adapters.Credential;%0D%0Aimport se.su.it.cognos.cognosshibauth.adapters.TrustedCredential;%0D%0Aimport se.su.it.cognos.cognosshibauth.config.ConfigHandler;%0D%0A%0D%0A%0D%0A
 publ
@@ -2651,34 +2651,32 @@
 ccount;%0D%0A  %7D%0D%0A%0D%0A
-%0D%0A
   public void ad
@@ -2883,34 +2883,32 @@
 Group);%0D%0A  %7D%0D%0A%0D%0A
-%0D%0A
   public IGroup%5B
@@ -3120,26 +3120,24 @@
 ll;%0D%0A  %7D%0D%0A%0D%0A
-%0D%0A
   public voi
@@ -3344,19 +3344,15 @@
 );%0D%0A
+
   %7D%0D%0A%0D%0A
-%0D%0A%0D%0A
   pu

Optimize imports