targets = findCertificates(certSelect, pkixParams.getCertStores());
try { targets = findCertificates(certSelect, pkixParams.getCertStores()); } catch (CertStoreException e) { throw new CertPathBuilderException(e); }
    public CertPathBuilderResult engineBuild(        CertPathParameters params)        throws CertPathBuilderException, InvalidAlgorithmParameterException     {        if (!(params instanceof PKIXBuilderParameters))        {            throw new InvalidAlgorithmParameterException("params must be a PKIXBuilderParameters instance");        }        PKIXBuilderParameters pkixParams = (PKIXBuilderParameters)params;        Collection targets;        Iterator targetIter;        List certPathList = new ArrayList();        X509Certificate cert;        Collection      certs;        CertPath        certPath = null;        Exception       certPathException = null;        // search target certificates        CertSelector certSelect = pkixParams.getTargetCertConstraints();        if (certSelect == null)        {            throw new CertPathBuilderException("targetCertConstraints must be non-null for CertPath building");        }        targets = findCertificates(certSelect, pkixParams.getCertStores());        if (targets.isEmpty())        {            throw new CertPathBuilderException("no certificate found matching targetCertContraints");        }        CertificateFactory  cFact;        CertPathValidator   validator;        try        {            cFact = CertificateFactory.getInstance("X.509", "BC");            validator = CertPathValidator.getInstance("PKIX", "BC");        }        catch (Exception e)        {            throw new CertPathBuilderException("exception creating support classes: " + e);        }        //        // check all potential target certificates        targetIter = targets.iterator();        while (targetIter.hasNext())        {            cert = (X509Certificate)targetIter.next();            certPathList.clear();            while (cert != null)            {                // add cert to the certpath                certPathList.add(cert);                // check wether the issuer of <cert> is a TrustAnchor                 if (findTrustAnchor(cert, pkixParams.getTrustAnchors()) != null)                {                    try                    {                        certPath = cFact.generateCertPath(certPathList);                        PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult)validator.validate(certPath, pkixParams);                        return new PKIXCertPathBuilderResult(certPath,                                     result.getTrustAnchor(),                                     result.getPolicyTree(),                                     result.getPublicKey());                    }                    catch (CertificateException ex)                    {                        certPathException = ex;                    }                    catch (CertPathValidatorException ex)                    {                        certPathException = ex;                    }                    // if validation failed go to next certificate                    cert = null;                }                else                {                    // try to get the issuer certificate from one                    // of the CertStores                    try                    {                        X509Certificate issuer = findIssuer(cert, pkixParams.getCertStores());                        if (issuer.equals(cert))                        {                            cert = null;                        }                        else                        {                            cert = issuer;                        }                    }                    catch (CertPathValidatorException ex)                    {                        certPathException = ex;                        cert = null;                    }                }            }        }        if (certPath != null)        {            throw new CertPathBuilderException("found certificate chain, but could not be validated", certPathException);        }        throw new CertPathBuilderException("unable to find certificate chain");    }