if ( (username == null) || (password == null) || (username.length() == 0) || (password.length() == 0) ) return null;   String keyStore = System.getProperty("no.feide.moria.backend.ldap.keystore"); if (keyStore != null) { log.config("Key store is "+keyStore); System.setProperty("javax.net.ssl.keyStore", keyStore); } String keyStorePassword = System.getProperty("no.feide.moria.backend.ldap.keystorePassword"); if (keyStorePassword != null) System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword); String trustStore = System.getProperty("no.feide.moria.backend.ldap.trustStore"); if (trustStore != null) { log.config("Trust store is "+trustStore); System.setProperty("javax.net.ssl.trustStore", trustStore); } String trustStorePassword = System.getProperty("no.feide.moria.backend.ldap.trustStorePassword"); if (trustStorePassword != null) System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword); String usernameAttribute = System.getProperty("no.feide.moria.backend.ldap.usernameAttribute"); if (usernameAttribute == null) throw new BackendException("Required property no.feide.moria.backend.ldap.usernameAttribute not set"); log.config("User name attribute is "+usernameAttribute); String ldapURL = System.getProperty("no.feide.moria.backend.ldap.url"); if (ldapURL == null) throw new BackendException("Required property no.feide.moria.backend.ldap.url not set"); log.config("LDAP URL is "+ldapURL);  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    public static User authenticate(Credentials c)    throws BackendException {        log.finer("authenticate(Credentials c)");                // Validate credentials.        if (c.getType() != Credentials.PASSWORD) {            log.severe("Unsupported credentials");            throw new BackendException("Unsupported credentials");        }        String username = (String)c.getIdentifier();        String password = (String)c.getCredentials();                // Sanity check.        if ( (username == null) || (password == null) ||             (username.length() == 0) || (password.length() == 0) )            return null;                // Get and verify some properties.        String keyStore = System.getProperty("no.feide.moria.backend.ldap.keystore");        if (keyStore != null) {            log.config("Key store is "+keyStore);            System.setProperty("javax.net.ssl.keyStore", keyStore);        }        String keyStorePassword = System.getProperty("no.feide.moria.backend.ldap.keystorePassword");        if (keyStorePassword != null)                System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);         String trustStore = System.getProperty("no.feide.moria.backend.ldap.trustStore");        if (trustStore != null) {            log.config("Trust store is "+trustStore);            System.setProperty("javax.net.ssl.trustStore", trustStore);        }        String trustStorePassword = System.getProperty("no.feide.moria.backend.ldap.trustStorePassword");        if (trustStorePassword != null)                System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);        String usernameAttribute = System.getProperty("no.feide.moria.backend.ldap.usernameAttribute");        if (usernameAttribute == null)            throw new BackendException("Required property no.feide.moria.backend.ldap.usernameAttribute not set");        log.config("User name attribute is "+usernameAttribute);	String ldapURL = System.getProperty("no.feide.moria.backend.ldap.url");	if (ldapURL == null)	    throw new BackendException("Required property no.feide.moria.backend.ldap.url not set");        log.config("LDAP URL is "+ldapURL);                       Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());        try {                        // Prepare LDAP context and look up user element.            Hashtable env = new Hashtable();            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");            env.put(Context.PROVIDER_URL, ldapURL);            env.put(Context.REFERRAL, "throw");  // To catch referrals.            InitialLdapContext ldap = new InitialLdapContext(env, null);	    log.config("Connected to "+env.get(Context.PROVIDER_URL));            ldap.addToEnvironment(Context.SECURITY_PRINCIPAL, "");            ldap.addToEnvironment(Context.SECURITY_CREDENTIALS, "");            String rdn = ldapSearch(ldap, usernameAttribute+'='+username);            if (rdn == null) {                // No user element found.                log.fine("No subtree match for "+usernameAttribute+'='+username+" on "+ldap.getEnvironment().get(Context.PROVIDER_URL));                return null;            }            log.fine("Found element at "+rdn+" on "+ldap.getEnvironment().get(Context.PROVIDER_URL));            // Authenticate.            ldap.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");            ldap.addToEnvironment(Context.SECURITY_PRINCIPAL, rdn+','+ldap.getNameInNamespace());            ldap.addToEnvironment(Context.SECURITY_CREDENTIALS, password);            try {                ldap.reconnect(null);                log.fine("Authenticated as "+rdn+" on "+ldap.getEnvironment().get(Context.PROVIDER_URL));                return new User(ldap, rdn);  // Success.            } catch (AuthenticationException e) {                log.fine("Failed to authenticate as "+rdn+" on "+ldap.getEnvironment().get(Context.PROVIDER_URL));                return null;  // Failure.            }                    } catch (NamingException e) {            log.severe("NamingException caught and re-thrown as BackendException");            throw new BackendException(e);        }    }