简体   繁体   中英

Replace obsolete `Hashtable` collection

I have this simple ldap client which uses obsolete Hashtable collection.

class SAuth {

    public static void main(String[] args) {

        Hashtable env = new Hashtable(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://xx.xx.xx.xx:yyyy/");

        // Authenticate as S. User and password "mysecret"
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=orcladmin");
        env.put(Context.SECURITY_CREDENTIALS, "password");

        try {

            DirContext ctx = new InitialDirContext(env);
            System.out.println(" i guess the connection is sucessfull :)");

        // Do something useful with ctx 
            // Close the context when we're done
            ctx.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }

    }
}

Is there any modern collection which I can use without breaking the code instead of Hashtable?

Update:

class tSAuth {

    public static void main(String[] args) {

        Map<String, String> env = new HashMap<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://xx.xx.xx.xx:yyyy/");

        // Authenticate as S. User and password "mysecret"
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=orcladmin");
        env.put(Context.SECURITY_CREDENTIALS, "password");

        try {

            DirContext ctx = new InitialDirContext((Hashtable<?, ?>) env);
            System.out.println(" i guess the connection is sucessfull :)");

        // Do something useful with ctx 
            // Close the context when we're done
            ctx.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }

    }
}

Use HashMap instead of HashTable like this:

Map env = new HashMap();

I'm not sure the exact type of Context.* , however, if it's String , then you could write the code like this:

Map<String, String> env = new HashMap<String, String>();

EDIT:

The InitialDirContext constructor's parameter type is Hashtable<?,?> . So you should Hashtable in this case. Perhaps you can code like this:

Hashtable<String, String> env = new Hashtable<String, String>();

You have to use a Hashtable according to the java documentation for InitialDirContext. http://docs.oracle.com/javase/7/docs/api/javax/naming/directory/InitialDirContext.html#InitialDirContext%28java.util.Hashtable%29

你应该可以在这里使用java.util.Properties ,因为它扩展了java.util.Hashtable<Object,Object>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM