Index: branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java
===================================================================
--- branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java	(revision 4123)
+++ branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/DigitalCertificate.java	(revision 4198)
@@ -2,4 +2,5 @@
 
 import br.gov.serpro.setup.Setup;
+import br.gov.serpro.cert.Token;
 import java.awt.Frame;
 import java.io.ByteArrayInputStream;
@@ -58,4 +59,5 @@
 import java.io.OutputStreamWriter;
 import java.security.AlgorithmParameters;
+import java.security.NoSuchProviderException;
 import java.security.cert.CertificateEncodingException;
 import java.util.regex.Matcher;
@@ -256,5 +258,13 @@
 	// TODO: Usar dentro de um "loop" para testar outros modelos de tokens.
 	this.tokens = new TokenCollection(setup);
-	int interfaceType = DigitalCertificate.KEYSTORE_DETECTED;
+
+        Provider[] providers = Security.getProviders();
+        if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
+            for (Provider provider : providers) {
+                System.out.println(provider.getInfo());
+            }
+        }
+
+        int interfaceType = DigitalCertificate.KEYSTORE_DETECTED;
 
 	try {
@@ -264,10 +274,5 @@
 	} catch (Exception e1) {
 
-	    Provider[] providers = Security.getProviders();
 	    if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
-		for (Provider provider : providers) {
-		    System.out.println(provider.getInfo());
-		}
-
 		// Não conseguiu abrir o token (ePass2000).
 		System.out.println("Erro ao ler o token: " + e1.getMessage());
@@ -746,11 +751,24 @@
      * 							 ou Smartcard não foi encontrada, ou o usuário não inseriu o Token.
      */
-    private void loadKeyStore() throws KeyStoreException {
-
-	//Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(new ByteArrayInputStream(pkcs11Config.getBytes()));
-	//Security.addProvider(pkcs11Provider);
-	this.keyStore = KeyStore.getInstance("PKCS11");
-	this.providerName = keyStore.getProvider().getName();
-
+    private void loadKeyStore() throws GeneralSecurityException {
+
+        try{
+            if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
+                System.out.println("Carregando provider: PKCS11");
+            }
+            this.keyStore = KeyStore.getInstance("PKCS11");
+            this.providerName = keyStore.getProvider().getName();
+        }
+        catch (GeneralSecurityException kex){
+            if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
+                System.out.println("Erro ao carregar provider: PKCS11");
+                Throwable cause = kex.getCause();
+                kex.printStackTrace();
+                if (cause != null){
+                    cause.printStackTrace();
+                }
+            }
+            throw kex;
+        }
     }
 
@@ -779,6 +797,6 @@
                 this.cert = this.keyStore.getCertificate(this.selectedCertificateAlias);
             
-                System.out.println("Aliases (" + this.keyStore.size() + "): ");
                 if (this.setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    System.out.println("Aliases (" + this.keyStore.size() + "): ");
                     for (Enumeration alias = this.keyStore.aliases(); alias.hasMoreElements();) {
                         System.out.println(alias.nextElement());
@@ -808,5 +826,9 @@
      */
     public String getPEMCertificate() throws CertificateEncodingException {
-	return Base64Utils.der2pem(this.cert.getEncoded());
+        if (this.cert != null){
+            return Base64Utils.der2pem(this.cert.getEncoded());
+        }
+        return null;
+
     }
 
Index: branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/Token.java
===================================================================
--- branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/Token.java	(revision 1174)
+++ branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/Token.java	(revision 4198)
@@ -53,8 +53,13 @@
     }
 
-    protected void registerToken(){
-        String tokenConfiguration = new String("name = " + name + "\n" +
-            "library = " + libraryPath + "\ndisabledMechanisms = {\n" +
-            "CKM_SHA1_RSA_PKCS\n}");
+    public String getProviderName(){
+        return this.tokenProvider.getName();
+    }
+
+    protected void registerToken(long slot){
+
+        String tokenConfiguration = new String("name = " + name + "_" + slot + "\n" +
+            "library = " + libraryPath + "\nslot = " + slot +
+            "\ndisabledMechanisms = {\n" + "CKM_SHA1_RSA_PKCS\n}");
 
         try{
@@ -63,5 +68,14 @@
                 Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(new ByteArrayInputStream(tokenConfiguration.getBytes()));
                 this.tokenProvider = pkcs11Provider;
+                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    System.out.println("Adding provider: "+pkcs11Provider.getName());
+                    System.out.println("Provider info: " + pkcs11Provider.getInfo());
+                    System.out.println("Provider services:");
+                    for (Provider.Service service : pkcs11Provider.getServices()){
+                        System.out.println("\t"+service.toString());
+                    }
+                }
                 Security.addProvider(pkcs11Provider);
+                this.setName(this.tokenProvider.getName());
                 this.registered = true;
             }
@@ -79,10 +93,4 @@
     }
 
-/*    public KeyStore getKeystore() throws KeyStoreException {
-
-        return (this.keyStore = KeyStore.getInstance("PKCS11"));
-
-    }
-*/
     public boolean libraryExists(){
 
Index: branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/TokenCollection.java
===================================================================
--- branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/TokenCollection.java	(revision 1174)
+++ branches/2.2.0.1/security/ExpressoCert/src/br/gov/serpro/cert/TokenCollection.java	(revision 4198)
@@ -7,5 +7,13 @@
 
 import br.gov.serpro.setup.Setup;
+import java.io.IOException;
 import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import sun.security.pkcs11.wrapper.CK_C_INITIALIZE_ARGS;
+import sun.security.pkcs11.wrapper.CK_TOKEN_INFO;
+import sun.security.pkcs11.wrapper.PKCS11;
+import sun.security.pkcs11.wrapper.PKCS11Exception;
+import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
 
 /**
@@ -36,11 +44,25 @@
         
         String[] tokensArray = tokens.split(",");
-        for (String tokenString : tokensArray){
-            if (tokenString != null && tokenString.length() > 0){
-                String[] tokenArray = tokenString.split(";");
+        for (int i = 0; i < tokensArray.length; i++){
+            if (tokensArray[i] != null && tokensArray[i].length() > 0){
+                String[] tokenArray = tokensArray[i].split(";");
                 Token token = new Token(tokenArray[0], tokenArray[1], this.setup);
-                token.registerToken();
-                if (token.isRegistered()){
-                    this.put(token.getName(), token);
+
+                // Aqui testar se existe token inserido em algum slot para determinado driver.
+                // Pega sempre o primeiro slot registrado com a lib tokenArray[1]
+                // TODO: Deixar o usuário escolher o token que vai usar;
+                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    System.out.println("Getting slots from " + tokenArray[1]);
+                }
+                try {
+                    long[] slots = getSlotsWithTokens(tokenArray[1]);
+                    if (slots.length > 0){
+                        token.registerToken(slots[0]);
+                        if (token.isRegistered()){
+                            this.put(token.getName(), token);
+                        }
+                    }
+                } catch (IOException iex){
+                    continue;
                 }
             }
@@ -48,3 +70,58 @@
     }
 
+    public long[] getSlotsWithTokens(String libraryPath) throws IOException{
+        CK_C_INITIALIZE_ARGS initArgs = new CK_C_INITIALIZE_ARGS();
+        String functionList = "C_GetFunctionList";
+
+        initArgs.flags = CKF_OS_LOCKING_OK;
+        PKCS11 tmpPKCS11 = null;
+        long[] slotList = null;
+        try {
+            try {
+                tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, false);
+            } catch (IOException ex) {
+                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex);
+                }
+                throw ex;
+            }
+        } catch (PKCS11Exception e) {
+            try {
+                initArgs = null;
+                tmpPKCS11 = PKCS11.getInstance(libraryPath, functionList, initArgs, true);
+            } catch (IOException ex) {
+                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex);
+                }
+            } catch (PKCS11Exception ex) {
+                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex);
+                }
+            }
+        }
+
+        try {
+            slotList = tmpPKCS11.C_GetSlotList(true);
+
+            for (long slot : slotList){
+                CK_TOKEN_INFO tokenInfo = tmpPKCS11.C_GetTokenInfo(slot);
+                if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                    System.out.println("slot: "+slot+"\nmanufacturerID: "
+                            + String.valueOf(tokenInfo.manufacturerID) + "\nmodel: "
+                            + String.valueOf(tokenInfo.model));
+                }
+            }
+        } catch (PKCS11Exception ex) {
+            if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, ex);
+            }
+        } catch (Throwable t) {
+            if (setup.getParameter("debug").equalsIgnoreCase("true")) {
+                Logger.getLogger(TokenCollection.class.getName()).log(Level.SEVERE, null, t);
+            }
+        }
+
+        return slotList;
+
+    }
 }
