Example usage for java.util Hashtable Hashtable

List of usage examples for java.util Hashtable Hashtable

Introduction

In this page you can find the example usage for java.util Hashtable Hashtable.

Prototype

Hashtable(Void dummy) 

Source Link

Document

A constructor chained from Properties keeps Hashtable fields uninitialized since they are not used.

Usage

From source file:SortedResults.java

public static void main(String[] args) throws IOException {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/ou=People,o=JNDITutorial");

    try {//from  w  w  w.  j a v  a 2 s . c  o m
        // Create initial context with no connection request controls
        LdapContext ctx = new InitialLdapContext(env, null);

        // Create a sort control that sorts based on CN
        String sortKey = "cn";
        ctx.setRequestControls(new Control[] { new SortControl(sortKey, Control.CRITICAL) });

        // Perform a search
        NamingEnumeration results = ctx.search("", "(objectclass=*)", new SearchControls());

        // Iterate over search results
        System.out.println("---->sort by cn");
        while (results != null && results.hasMore()) {
            // Display an entry
            SearchResult entry = (SearchResult) results.next();
            System.out.println(entry.getName());

            // Handle the entry's response controls (if any)
            if (entry instanceof HasControls) {
                // ((HasControls)entry).getControls();
            }
        }
        // Examine the sort control response
        Control[] controls = ctx.getResponseControls();
        if (controls != null) {
            for (int i = 0; i < controls.length; i++) {
                if (controls[i] instanceof SortResponseControl) {
                    SortResponseControl src = (SortResponseControl) controls[i];
                    if (!src.isSorted()) {
                        throw src.getException();
                    }
                } else {
                    // Handle other response controls (if any)
                }
            }
        }

        // Close when no longer needed
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:FullName.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from   w  ww  .j  a v a  2 s .  co m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration answer = ctx.search("ou=People", null);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:RegUnsol.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDItutorial");

    try {//  w w w . ja v  a  2  s.c  om
        // Get event context for registering listener
        EventContext ctx = (EventContext) (new InitialContext(env).lookup("ou=People"));

        // Create listener
        NamingListener listener = new UnsolListener();

        // Register listener with context (all targets equivalent)
        ctx.addNamingListener("", EventContext.ONELEVEL_SCOPE, listener);

        // Wait 1 minutes for listener to receive events
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            System.out.println("sleep interrupted");
        }

        // Not strictly necessary if we're going to close context anyhow
        ctx.removeNamingListener(listener);

        // Close context when we're done
        ctx.close();

    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:SerObjWithCodebase.java

public static void main(String[] args) {

    if (args.length != 1) {
        System.err.println("usage: java SerObjWithCodebase <codebase URL>");
        System.exit(-1);/*from w  w  w .  j a v a  2  s . c  o m*/
    }

    String codebase = args[0];

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Create object to be bound
        Flower f = new Flower("rose", "pink");

        // Perform bind and specify codebase
        ctx.bind("cn=Flower", f, new BasicAttributes("javaCodebase", codebase));

        // Check that it is bound
        Flower f2 = (Flower) ctx.lookup("cn=Flower");
        System.out.println(f2);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:ModAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from  www. j  a v a  2 s.c  o  m
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);
        String name = "cn=Ted Geisel, ou=People";

        // Save original attributes
        Attributes orig = ctx.getAttributes(name, new String[] { "mail", "telephonenumber", "jpegphoto" });

        // Specify the changes to make
        ModificationItem[] mods = new ModificationItem[3];

        // Replace the "mail" attribute with a new value
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("mail", "geisel@wizards.com"));

        // Add additional value to "telephonenumber"
        mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute("telephonenumber", "+1 555 555 5555"));

        // Remove the "jpegphoto" attribute
        mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("jpegphoto"));

        // Perform the requested modifications on the named object
        ctx.modifyAttributes(name, mods);

        // Check attributes
        System.out.println("**** new attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Revert changes
        ctx.modifyAttributes(name, DirContext.REPLACE_ATTRIBUTE, orig);

        // Check that the attributes got restored
        System.out.println("**** reverted to original attributes *****");
        printAttrs(ctx.getAttributes(name));

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:GetAllAttrs.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*from   ww w .  ja  v a 2 s . co m*/
        // Create the initial context
        DirContext ctx = new InitialDirContext(env);

        // Get all the attributes of named object
        Attributes answer = ctx.getAttributes("cn=Ted Geisel, ou=People");

        // Print the answer
        printAttrs(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:SearchTimeLimit.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*  w  ww  .  j  a v  a2  s  .  c  o m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Set search controls to limit count to 'timeout'
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ctls.setTimeLimit(timeout); //

        // Search for objects with those matching attributes
        NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (TimeLimitExceededException e) {
        System.out.println("time limit exceeded");
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:SearchCountLimit.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/* ww w  . j a v  a2  s.c o m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Set search controls to limit count to 'expected'
        SearchControls ctls = new SearchControls();
        ctls.setCountLimit(expected);

        // Search for objects with those matching attributes
        NamingEnumeration answer = ctx.search("ou=People", "(sn=M*)", ctls);

        // Print the answer
        printSearchEnumeration(answer);

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static Hashtable asHashtable(java.util.Map map) {
    return map instanceof Hashtable ? (Hashtable) map : new Hashtable(map);
}

From source file:Main.java

/**
 * Method getHashtableFromVector.//from www .  j a  v a  2s  .co  m
 * @param vector Vector
 * @return Hashtable
 */
public static Hashtable getHashtableFromVector(Vector vector) {
    Hashtable hashtable = new Hashtable(vector.size());

    for (int i = 0; i < vector.size(); i++) {
        hashtable.put(vector.elementAt(i), vector.elementAt(i));
    }

    return hashtable;
}