Avatar billede martinhrj Nybegynder
16. august 2002 - 17:37 Der er 1 løsning

Ingen exceptions?

Normalt er det godt ikke at få nogen exceptions... men her ville jeg nu gerne have nogen. Ellers er det jo lidt svært at fejlfinde.

Men det kan jo være at koden bare mangler et par nye øjne! Er der nogen der kan hjælpe mig?

Jeg får ingen "results" i min NamingEnumeration. Og jeg VED at der er nogen. Når jeg prøver med et lille program i VB-script, er de der!

/*
* Copyright (c) 1997.  Sun Microsystems. All rights reserved.
*
* Search the directory for all people whose surname (last name) is
* "Jensen".  Since the "sn" attribute is a caseignorestring (cis), case
* is not significant when searching.
*
* [equivalent to search.c in Netscape's SDK.]
*
*/

import java.util.Hashtable;
import java.util.Enumeration;

import javax.naming.*;
import javax.naming.directory.*;

class Search {

public static void main(String[] args) {

    Hashtable env = new Hashtable(5, 0.75f);
    /*
    * Specify the initial context implementation to use.
    * This could also be set by using the -D option to the java program.
    * For example,
    *  java -Djava.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory \
    *    Search
    */
    env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);

    /* Specify host and port to use for directory service */
    env.put(Context.PROVIDER_URL, Env.MY_SERVICE);

    try {
        /* get a handle to an Initial DirContext */
        DirContext ctx = new InitialDirContext(env);

        /* specify search constraints to search subtree */
        SearchControls constraints = new SearchControls();
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

        /* search for all entries with surname of Jensen */
        NamingEnumeration results = ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, constraints);
        /* for each entry print out name + all attrs and values */

        while (results != null && results.hasMore()) {
            System.out.println("We have results!");
            SearchResult si = (SearchResult)results.next();

            /* print its name */
            System.out.println("name: " + si.getName());

            Attributes attrs = si.getAttributes();
            if (attrs == null) {
                System.out.println("No attributes");
            } else {
                /* print each attribute */
                for (NamingEnumeration ae = attrs.getAll();
                    ae.hasMoreElements();) {
                    Attribute attr = (Attribute)ae.next();
                    String attrId = attr.getID();

                    /* print each value */
                    for (Enumeration vals = attr.getAll();
                        vals.hasMoreElements();
                        System.out.println(attrId + ": " + vals.nextElement()))
                            ;
                }
            }
            System.out.println();
        }
    } catch (NamingException e) {
        System.err.println("Search example failed.");
        e.printStackTrace();
    }
}
}
Avatar billede martinhrj Nybegynder
20. august 2002 - 13:49 #1
Så lykkedes det mig selv at finde ud af hvorfor :)

Jeg skulle bare lige tilføje 3 linjer til min kode:

env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "valid.user@mydomain.com");
env.put(Context.SECURITY_CREDENTIALS, "password");
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester